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 bool IsHandledProtocol(const std::string
& scheme
) const override
{
95 bool IsHandledURL(const GURL
& url
) const override
{ return true; }
97 bool IsSafeRedirectTarget(const GURL
& location
) const override
{
102 std::string storage_domain_
;
103 FileSystemContext
* file_system_context_
;
109 class FileSystemDirURLRequestJobTest
: public testing::Test
{
111 FileSystemDirURLRequestJobTest()
112 : weak_factory_(this) {
115 virtual void SetUp() override
{
116 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
118 special_storage_policy_
= new MockSpecialStoragePolicy
;
119 file_system_context_
= CreateFileSystemContextForTesting(
120 NULL
, temp_dir_
.path());
122 file_system_context_
->OpenFileSystem(
123 GURL("http://remote/"),
124 storage::kFileSystemTypeTemporary
,
125 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT
,
126 base::Bind(&FileSystemDirURLRequestJobTest::OnOpenFileSystem
,
127 weak_factory_
.GetWeakPtr()));
128 base::RunLoop().RunUntilIdle();
131 virtual void TearDown() override
{
132 // NOTE: order matters, request must die before delegate
133 request_
.reset(NULL
);
134 delegate_
.reset(NULL
);
137 void SetUpAutoMountContext(base::FilePath
* mnt_point
) {
138 *mnt_point
= temp_dir_
.path().AppendASCII("auto_mount_dir");
139 ASSERT_TRUE(base::CreateDirectory(*mnt_point
));
141 ScopedVector
<storage::FileSystemBackend
> additional_providers
;
142 additional_providers
.push_back(new TestFileSystemBackend(
143 base::MessageLoopProxy::current().get(), *mnt_point
));
145 std::vector
<storage::URLRequestAutoMountHandler
> handlers
;
146 handlers
.push_back(base::Bind(&TestAutoMountForURLRequest
));
148 file_system_context_
= CreateFileSystemContextWithAutoMountersForTesting(
149 NULL
, additional_providers
.Pass(), handlers
, temp_dir_
.path());
152 void OnOpenFileSystem(const GURL
& root_url
,
153 const std::string
& name
,
154 base::File::Error result
) {
155 ASSERT_EQ(base::File::FILE_OK
, result
);
158 void TestRequestHelper(const GURL
& url
, bool run_to_completion
,
159 FileSystemContext
* file_system_context
) {
160 delegate_
.reset(new net::TestDelegate());
161 delegate_
->set_quit_on_redirect(true);
162 job_factory_
.reset(new FileSystemDirURLRequestJobFactory(
163 url
.GetOrigin().host(), file_system_context
));
164 empty_context_
.set_job_factory(job_factory_
.get());
166 request_
= empty_context_
.CreateRequest(
167 url
, net::DEFAULT_PRIORITY
, delegate_
.get(), NULL
);
169 ASSERT_TRUE(request_
->is_pending()); // verify that we're starting async
170 if (run_to_completion
)
171 base::MessageLoop::current()->Run();
174 void TestRequest(const GURL
& url
) {
175 TestRequestHelper(url
, true, file_system_context_
.get());
178 void TestRequestWithContext(const GURL
& url
,
179 FileSystemContext
* file_system_context
) {
180 TestRequestHelper(url
, true, file_system_context
);
183 void TestRequestNoRun(const GURL
& url
) {
184 TestRequestHelper(url
, false, file_system_context_
.get());
187 FileSystemURL
CreateURL(const base::FilePath
& file_path
) {
188 return file_system_context_
->CreateCrackedFileSystemURL(
189 GURL("http://remote"), storage::kFileSystemTypeTemporary
, file_path
);
192 FileSystemOperationContext
* NewOperationContext() {
193 FileSystemOperationContext
* context(
194 new FileSystemOperationContext(file_system_context_
.get()));
195 context
->set_allowed_bytes_growth(1024);
199 void CreateDirectory(const base::StringPiece
& dir_name
) {
200 base::FilePath path
= base::FilePath().AppendASCII(dir_name
);
201 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
202 ASSERT_EQ(base::File::FILE_OK
, file_util()->CreateDirectory(
205 false /* exclusive */,
206 false /* recursive */));
209 void EnsureFileExists(const base::StringPiece file_name
) {
210 base::FilePath path
= base::FilePath().AppendASCII(file_name
);
211 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
212 ASSERT_EQ(base::File::FILE_OK
, file_util()->EnsureFileExists(
213 context
.get(), CreateURL(path
), NULL
));
216 void TruncateFile(const base::StringPiece file_name
, int64 length
) {
217 base::FilePath path
= base::FilePath().AppendASCII(file_name
);
218 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
219 ASSERT_EQ(base::File::FILE_OK
, file_util()->Truncate(
220 context
.get(), CreateURL(path
), length
));
223 base::File::Error
GetFileInfo(const base::FilePath
& path
,
224 base::File::Info
* file_info
,
225 base::FilePath
* platform_file_path
) {
226 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
227 return file_util()->GetFileInfo(context
.get(),
229 file_info
, platform_file_path
);
232 // If |size| is negative, the reported size is ignored.
233 void VerifyListingEntry(const std::string
& entry_line
,
234 const std::string
& name
,
235 const std::string
& url
,
238 #define STR "([^\"]*)"
239 icu::UnicodeString
pattern("^<script>addRow\\(\"" STR
"\",\"" STR
240 "\",(0|1),\"" STR
"\",\"" STR
"\"\\);</script>");
242 icu::UnicodeString
input(entry_line
.c_str());
244 UErrorCode status
= U_ZERO_ERROR
;
245 icu::RegexMatcher
match(pattern
, input
, 0, status
);
247 EXPECT_TRUE(match
.find());
248 EXPECT_EQ(5, match
.groupCount());
249 EXPECT_EQ(icu::UnicodeString(name
.c_str()), match
.group(1, status
));
250 EXPECT_EQ(icu::UnicodeString(url
.c_str()), match
.group(2, status
));
251 EXPECT_EQ(icu::UnicodeString(is_directory
? "1" : "0"),
252 match
.group(3, status
));
254 icu::UnicodeString
size_string(FormatBytesUnlocalized(size
).c_str());
255 EXPECT_EQ(size_string
, match
.group(4, status
));
259 icu::UnicodeString
date_ustr(match
.group(5, status
));
260 std::string date_str
;
261 base::UTF16ToUTF8(date_ustr
.getBuffer(), date_ustr
.length(), &date_str
);
262 EXPECT_TRUE(base::Time::FromString(date_str
.c_str(), &date
));
263 EXPECT_FALSE(date
.is_null());
266 GURL
CreateFileSystemURL(const std::string path
) {
267 return GURL(kFileSystemURLPrefix
+ path
);
270 storage::FileSystemFileUtil
* file_util() {
271 return file_system_context_
->sandbox_delegate()->sync_file_util();
274 // Put the message loop at the top, so that it's the last thing deleted.
275 // Delete all MessageLoopProxy objects before the MessageLoop, to help prevent
276 // leaks caused by tasks posted during shutdown.
277 base::MessageLoopForIO message_loop_
;
279 base::ScopedTempDir temp_dir_
;
280 net::URLRequestContext empty_context_
;
281 scoped_ptr
<net::TestDelegate
> delegate_
;
282 scoped_ptr
<net::URLRequest
> request_
;
283 scoped_ptr
<FileSystemDirURLRequestJobFactory
> job_factory_
;
284 scoped_refptr
<MockSpecialStoragePolicy
> special_storage_policy_
;
285 scoped_refptr
<FileSystemContext
> file_system_context_
;
286 base::WeakPtrFactory
<FileSystemDirURLRequestJobTest
> weak_factory_
;
291 TEST_F(FileSystemDirURLRequestJobTest
, DirectoryListing
) {
292 CreateDirectory("foo");
293 CreateDirectory("foo/bar");
294 CreateDirectory("foo/bar/baz");
296 EnsureFileExists("foo/bar/hoge");
297 TruncateFile("foo/bar/hoge", 10);
299 TestRequest(CreateFileSystemURL("foo/bar/"));
301 ASSERT_FALSE(request_
->is_pending());
302 EXPECT_EQ(1, delegate_
->response_started_count());
303 EXPECT_FALSE(delegate_
->received_data_before_response());
304 EXPECT_GT(delegate_
->bytes_received(), 0);
306 std::istringstream
in(delegate_
->data_received());
308 EXPECT_TRUE(!!std::getline(in
, line
));
311 EXPECT_EQ("<script>start(\"foo\\\\bar\");</script>", line
);
312 #elif defined(OS_POSIX)
313 EXPECT_EQ("<script>start(\"/foo/bar\");</script>", line
);
316 EXPECT_TRUE(!!std::getline(in
, line
));
317 VerifyListingEntry(line
, "hoge", "hoge", false, 10);
319 EXPECT_TRUE(!!std::getline(in
, line
));
320 VerifyListingEntry(line
, "baz", "baz", true, 0);
321 EXPECT_FALSE(!!std::getline(in
, line
));
324 TEST_F(FileSystemDirURLRequestJobTest
, InvalidURL
) {
325 TestRequest(GURL("filesystem:/foo/bar/baz"));
326 ASSERT_FALSE(request_
->is_pending());
327 EXPECT_TRUE(delegate_
->request_failed());
328 ASSERT_FALSE(request_
->status().is_success());
329 EXPECT_EQ(net::ERR_INVALID_URL
, request_
->status().error());
332 TEST_F(FileSystemDirURLRequestJobTest
, NoSuchRoot
) {
333 TestRequest(GURL("filesystem:http://remote/persistent/somedir/"));
334 ASSERT_FALSE(request_
->is_pending());
335 ASSERT_FALSE(request_
->status().is_success());
336 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
339 TEST_F(FileSystemDirURLRequestJobTest
, NoSuchDirectory
) {
340 TestRequest(CreateFileSystemURL("somedir/"));
341 ASSERT_FALSE(request_
->is_pending());
342 ASSERT_FALSE(request_
->status().is_success());
343 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
346 TEST_F(FileSystemDirURLRequestJobTest
, Cancel
) {
347 CreateDirectory("foo");
348 TestRequestNoRun(CreateFileSystemURL("foo/"));
349 // Run StartAsync() and only StartAsync().
350 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, request_
.release());
351 base::RunLoop().RunUntilIdle();
352 // If we get here, success! we didn't crash!
355 TEST_F(FileSystemDirURLRequestJobTest
, Incognito
) {
356 CreateDirectory("foo");
358 scoped_refptr
<FileSystemContext
> file_system_context
=
359 CreateIncognitoFileSystemContextForTesting(NULL
, temp_dir_
.path());
361 TestRequestWithContext(CreateFileSystemURL("/"),
362 file_system_context
.get());
363 ASSERT_FALSE(request_
->is_pending());
364 ASSERT_TRUE(request_
->status().is_success());
366 std::istringstream
in(delegate_
->data_received());
368 EXPECT_TRUE(!!std::getline(in
, line
));
369 EXPECT_FALSE(!!std::getline(in
, line
));
371 TestRequestWithContext(CreateFileSystemURL("foo"),
372 file_system_context
.get());
373 ASSERT_FALSE(request_
->is_pending());
374 ASSERT_FALSE(request_
->status().is_success());
375 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
378 TEST_F(FileSystemDirURLRequestJobTest
, AutoMountDirectoryListing
) {
379 base::FilePath mnt_point
;
380 SetUpAutoMountContext(&mnt_point
);
381 ASSERT_TRUE(base::CreateDirectory(mnt_point
));
382 ASSERT_TRUE(base::CreateDirectory(mnt_point
.AppendASCII("foo")));
384 base::WriteFile(mnt_point
.AppendASCII("bar"), "1234567890", 10));
386 TestRequest(GURL("filesystem:http://automount/external/mnt_name"));
388 ASSERT_FALSE(request_
->is_pending());
389 EXPECT_EQ(1, delegate_
->response_started_count());
390 EXPECT_FALSE(delegate_
->received_data_before_response());
391 EXPECT_GT(delegate_
->bytes_received(), 0);
393 std::istringstream
in(delegate_
->data_received());
395 EXPECT_TRUE(!!std::getline(in
, line
)); // |line| contains the temp dir path.
397 // Result order is not guaranteed, so sort the results.
398 std::vector
<std::string
> listing_entries
;
399 while (!!std::getline(in
, line
))
400 listing_entries
.push_back(line
);
402 ASSERT_EQ(2U, listing_entries
.size());
403 std::sort(listing_entries
.begin(), listing_entries
.end());
404 VerifyListingEntry(listing_entries
[0], "bar", "bar", false, 10);
405 VerifyListingEntry(listing_entries
[1], "foo", "foo", true, -1);
408 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
409 kValidExternalMountPoint
));
412 TEST_F(FileSystemDirURLRequestJobTest
, AutoMountInvalidRoot
) {
413 base::FilePath mnt_point
;
414 SetUpAutoMountContext(&mnt_point
);
415 TestRequest(GURL("filesystem:http://automount/external/invalid"));
417 ASSERT_FALSE(request_
->is_pending());
418 ASSERT_FALSE(request_
->status().is_success());
419 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
422 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
426 TEST_F(FileSystemDirURLRequestJobTest
, AutoMountNoHandler
) {
427 base::FilePath mnt_point
;
428 SetUpAutoMountContext(&mnt_point
);
429 TestRequest(GURL("filesystem:http://noauto/external/mnt_name"));
431 ASSERT_FALSE(request_
->is_pending());
432 ASSERT_FALSE(request_
->status().is_success());
433 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
436 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
437 kValidExternalMountPoint
));
440 } // namespace (anonymous)
441 } // namespace content