OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/browser/fileapi/file_system_url_request_job.h" | 5 #include "webkit/browser/fileapi/file_system_url_request_job.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/memory/scoped_vector.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
16 #include "base/message_loop/message_loop_proxy.h" | 17 #include "base/message_loop/message_loop_proxy.h" |
17 #include "base/platform_file.h" | 18 #include "base/platform_file.h" |
18 #include "base/rand_util.h" | 19 #include "base/rand_util.h" |
19 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
20 #include "base/strings/string_piece.h" | 21 #include "base/strings/string_piece.h" |
21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
22 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
23 #include "content/public/test/async_file_test_helper.h" | 24 #include "content/public/test/async_file_test_helper.h" |
| 25 #include "content/public/test/test_file_system_backend.h" |
24 #include "content/public/test/test_file_system_context.h" | 26 #include "content/public/test/test_file_system_context.h" |
25 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
26 #include "net/base/mime_util.h" | 28 #include "net/base/mime_util.h" |
27 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
28 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
29 #include "net/base/request_priority.h" | 31 #include "net/base/request_priority.h" |
30 #include "net/http/http_byte_range.h" | 32 #include "net/http/http_byte_range.h" |
31 #include "net/http/http_request_headers.h" | 33 #include "net/http/http_request_headers.h" |
32 #include "net/url_request/url_request.h" | 34 #include "net/url_request/url_request.h" |
33 #include "net/url_request/url_request_context.h" | 35 #include "net/url_request/url_request_context.h" |
(...skipping 12 matching lines...) Expand all Loading... |
46 namespace { | 48 namespace { |
47 | 49 |
48 // We always use the TEMPORARY FileSystem in this test. | 50 // We always use the TEMPORARY FileSystem in this test. |
49 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; | 51 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; |
50 const char kTestFileData[] = "0123456789"; | 52 const char kTestFileData[] = "0123456789"; |
51 | 53 |
52 void FillBuffer(char* buffer, size_t len) { | 54 void FillBuffer(char* buffer, size_t len) { |
53 base::RandBytes(buffer, len); | 55 base::RandBytes(buffer, len); |
54 } | 56 } |
55 | 57 |
| 58 const char kValidExternalMountPoint[] = "mnt_name"; |
| 59 |
| 60 // An auto mounter that will try to mount anything for |storage_domain| = |
| 61 // "automount", but will only succeed for the mount point "mnt_name". |
| 62 bool TestAutoMountForURLRequest( |
| 63 const net::URLRequest* /*url_request*/, |
| 64 const fileapi::FileSystemURL& filesystem_url, |
| 65 const std::string& storage_domain, |
| 66 const base::Callback<void(base::File::Error result)>& callback) { |
| 67 if (storage_domain != "automount") |
| 68 return false; |
| 69 std::vector<base::FilePath::StringType> components; |
| 70 filesystem_url.path().GetComponents(&components); |
| 71 std::string mount_point = base::FilePath(components[0]).AsUTF8Unsafe(); |
| 72 |
| 73 if (mount_point == kValidExternalMountPoint) { |
| 74 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
| 75 kValidExternalMountPoint, fileapi::kFileSystemTypeTest, |
| 76 fileapi::FileSystemMountOption(), base::FilePath()); |
| 77 callback.Run(base::File::FILE_OK); |
| 78 } else { |
| 79 callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
| 80 } |
| 81 return true; |
| 82 } |
| 83 |
56 } // namespace | 84 } // namespace |
57 | 85 |
58 class FileSystemURLRequestJobTest : public testing::Test { | 86 class FileSystemURLRequestJobTest : public testing::Test { |
59 protected: | 87 protected: |
60 FileSystemURLRequestJobTest() : weak_factory_(this) { | 88 FileSystemURLRequestJobTest() : weak_factory_(this) { |
61 } | 89 } |
62 | 90 |
63 virtual void SetUp() OVERRIDE { | 91 virtual void SetUp() OVERRIDE { |
64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 92 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
65 | 93 |
(...skipping 17 matching lines...) Expand all Loading... |
83 net::URLRequest::Deprecated::RegisterProtocolFactory("filesystem", NULL); | 111 net::URLRequest::Deprecated::RegisterProtocolFactory("filesystem", NULL); |
84 ClearUnusedJob(); | 112 ClearUnusedJob(); |
85 if (pending_job_.get()) { | 113 if (pending_job_.get()) { |
86 pending_job_->Kill(); | 114 pending_job_->Kill(); |
87 pending_job_ = NULL; | 115 pending_job_ = NULL; |
88 } | 116 } |
89 // FileReader posts a task to close the file in destructor. | 117 // FileReader posts a task to close the file in destructor. |
90 base::RunLoop().RunUntilIdle(); | 118 base::RunLoop().RunUntilIdle(); |
91 } | 119 } |
92 | 120 |
| 121 void SetUpAutoMountContext() { |
| 122 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); |
| 123 ASSERT_TRUE(base::CreateDirectory(mnt_point)); |
| 124 |
| 125 ScopedVector<fileapi::FileSystemBackend> additional_providers; |
| 126 additional_providers.push_back(new TestFileSystemBackend( |
| 127 base::MessageLoopProxy::current().get(), mnt_point)); |
| 128 |
| 129 std::vector<fileapi::URLRequestAutoMountHandler> handlers; |
| 130 handlers.push_back(base::Bind(&TestAutoMountForURLRequest)); |
| 131 |
| 132 file_system_context_ = CreateFileSystemContextWithAutoMountersForTesting( |
| 133 NULL, additional_providers.Pass(), handlers, temp_dir_.path()); |
| 134 |
| 135 ASSERT_EQ(static_cast<int>(sizeof(kTestFileData)) - 1, |
| 136 base::WriteFile(mnt_point.AppendASCII("foo"), kTestFileData, |
| 137 sizeof(kTestFileData) - 1)); |
| 138 } |
| 139 |
93 void OnOpenFileSystem(const GURL& root_url, | 140 void OnOpenFileSystem(const GURL& root_url, |
94 const std::string& name, | 141 const std::string& name, |
95 base::File::Error result) { | 142 base::File::Error result) { |
96 ASSERT_EQ(base::File::FILE_OK, result); | 143 ASSERT_EQ(base::File::FILE_OK, result); |
97 } | 144 } |
98 | 145 |
99 void TestRequestHelper(const GURL& url, | 146 void TestRequestHelper(const GURL& url, |
100 const net::HttpRequestHeaders* headers, | 147 const net::HttpRequestHeaders* headers, |
101 bool run_to_completion, | 148 bool run_to_completion, |
102 FileSystemContext* file_system_context) { | 149 FileSystemContext* file_system_context) { |
103 delegate_.reset(new net::TestDelegate()); | 150 delegate_.reset(new net::TestDelegate()); |
104 // Make delegate_ exit the MessageLoop when the request is done. | 151 // Make delegate_ exit the MessageLoop when the request is done. |
105 delegate_->set_quit_on_complete(true); | 152 delegate_->set_quit_on_complete(true); |
106 delegate_->set_quit_on_redirect(true); | 153 delegate_->set_quit_on_redirect(true); |
107 request_ = empty_context_.CreateRequest( | 154 request_ = empty_context_.CreateRequest( |
108 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); | 155 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); |
109 if (headers) | 156 if (headers) |
110 request_->SetExtraRequestHeaders(*headers); | 157 request_->SetExtraRequestHeaders(*headers); |
111 ASSERT_TRUE(!job_); | 158 ASSERT_TRUE(!job_); |
112 job_ = new FileSystemURLRequestJob( | 159 job_ = new FileSystemURLRequestJob( |
113 request_.get(), NULL, file_system_context); | 160 request_.get(), NULL, url.GetOrigin().host(), file_system_context); |
114 pending_job_ = job_; | 161 pending_job_ = job_; |
115 | 162 |
116 request_->Start(); | 163 request_->Start(); |
117 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 164 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
118 if (run_to_completion) | 165 if (run_to_completion) |
119 base::MessageLoop::current()->Run(); | 166 base::MessageLoop::current()->Run(); |
120 } | 167 } |
121 | 168 |
122 void TestRequest(const GURL& url) { | 169 void TestRequest(const GURL& url) { |
123 TestRequestHelper(url, NULL, true, file_system_context_.get()); | 170 TestRequestHelper(url, NULL, true, file_system_context_.get()); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 net::HttpRequestHeaders::kRange, | 300 net::HttpRequestHeaders::kRange, |
254 net::HttpByteRange::RightUnbounded(first_byte_position).GetHeaderValue()); | 301 net::HttpByteRange::RightUnbounded(first_byte_position).GetHeaderValue()); |
255 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers); | 302 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers); |
256 ASSERT_FALSE(request_->is_pending()); | 303 ASSERT_FALSE(request_->is_pending()); |
257 EXPECT_EQ(1, delegate_->response_started_count()); | 304 EXPECT_EQ(1, delegate_->response_started_count()); |
258 EXPECT_FALSE(delegate_->received_data_before_response()); | 305 EXPECT_FALSE(delegate_->received_data_before_response()); |
259 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. | 306 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
260 EXPECT_TRUE(partial_buffer_string == delegate_->data_received()); | 307 EXPECT_TRUE(partial_buffer_string == delegate_->data_received()); |
261 } | 308 } |
262 | 309 |
263 | |
264 TEST_F(FileSystemURLRequestJobTest, FileTestMultipleRangesNotSupported) { | 310 TEST_F(FileSystemURLRequestJobTest, FileTestMultipleRangesNotSupported) { |
265 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); | 311 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); |
266 net::HttpRequestHeaders headers; | 312 net::HttpRequestHeaders headers; |
267 headers.SetHeader(net::HttpRequestHeaders::kRange, | 313 headers.SetHeader(net::HttpRequestHeaders::kRange, |
268 "bytes=0-5,10-200,200-300"); | 314 "bytes=0-5,10-200,200-300"); |
269 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers); | 315 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers); |
270 EXPECT_TRUE(delegate_->request_failed()); | 316 EXPECT_TRUE(delegate_->request_failed()); |
271 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, | 317 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, |
272 request_->status().error()); | 318 request_->status().error()); |
273 } | 319 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 EXPECT_TRUE(delegate_->request_failed()); | 408 EXPECT_TRUE(delegate_->request_failed()); |
363 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 409 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
364 | 410 |
365 // Make sure it returns success with regular (non-incognito) context. | 411 // Make sure it returns success with regular (non-incognito) context. |
366 TestRequest(CreateFileSystemURL("file")); | 412 TestRequest(CreateFileSystemURL("file")); |
367 ASSERT_FALSE(request_->is_pending()); | 413 ASSERT_FALSE(request_->is_pending()); |
368 EXPECT_EQ(kTestFileData, delegate_->data_received()); | 414 EXPECT_EQ(kTestFileData, delegate_->data_received()); |
369 EXPECT_EQ(200, request_->GetResponseCode()); | 415 EXPECT_EQ(200, request_->GetResponseCode()); |
370 } | 416 } |
371 | 417 |
| 418 TEST_F(FileSystemURLRequestJobTest, AutoMountFileTest) { |
| 419 SetUpAutoMountContext(); |
| 420 TestRequest(GURL("filesystem:http://automount/external/mnt_name/foo")); |
| 421 |
| 422 ASSERT_FALSE(request_->is_pending()); |
| 423 EXPECT_EQ(1, delegate_->response_started_count()); |
| 424 EXPECT_FALSE(delegate_->received_data_before_response()); |
| 425 EXPECT_EQ(kTestFileData, delegate_->data_received()); |
| 426 EXPECT_EQ(200, request_->GetResponseCode()); |
| 427 std::string cache_control; |
| 428 request_->GetResponseHeaderByName("cache-control", &cache_control); |
| 429 EXPECT_EQ("no-cache", cache_control); |
| 430 |
| 431 ASSERT_TRUE( |
| 432 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 433 kValidExternalMountPoint)); |
| 434 } |
| 435 |
| 436 TEST_F(FileSystemURLRequestJobTest, AutoMountInvalidRoot) { |
| 437 SetUpAutoMountContext(); |
| 438 TestRequest(GURL("filesystem:http://automount/external/invalid/foo")); |
| 439 |
| 440 ASSERT_FALSE(request_->is_pending()); |
| 441 EXPECT_TRUE(delegate_->request_failed()); |
| 442 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 443 |
| 444 ASSERT_FALSE( |
| 445 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 446 "invalid")); |
| 447 } |
| 448 |
| 449 TEST_F(FileSystemURLRequestJobTest, AutoMountNoHandler) { |
| 450 SetUpAutoMountContext(); |
| 451 TestRequest(GURL("filesystem:http://noauto/external/mnt_name/foo")); |
| 452 |
| 453 ASSERT_FALSE(request_->is_pending()); |
| 454 EXPECT_TRUE(delegate_->request_failed()); |
| 455 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 456 |
| 457 ASSERT_FALSE( |
| 458 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 459 kValidExternalMountPoint)); |
| 460 } |
| 461 |
372 } // namespace | 462 } // namespace |
373 } // namespace content | 463 } // namespace content |
OLD | NEW |