| 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" | |
| 15 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 17 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
| 18 #include "base/platform_file.h" | 17 #include "base/platform_file.h" |
| 19 #include "base/rand_util.h" | 18 #include "base/rand_util.h" |
| 20 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 21 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| 22 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 24 #include "content/public/test/async_file_test_helper.h" | 23 #include "content/public/test/async_file_test_helper.h" |
| 25 #include "content/public/test/test_file_system_backend.h" | |
| 26 #include "content/public/test/test_file_system_context.h" | 24 #include "content/public/test/test_file_system_context.h" |
| 27 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
| 28 #include "net/base/mime_util.h" | 26 #include "net/base/mime_util.h" |
| 29 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 30 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
| 31 #include "net/base/request_priority.h" | 29 #include "net/base/request_priority.h" |
| 32 #include "net/http/http_byte_range.h" | 30 #include "net/http/http_byte_range.h" |
| 33 #include "net/http/http_request_headers.h" | 31 #include "net/http/http_request_headers.h" |
| 34 #include "net/url_request/url_request.h" | 32 #include "net/url_request/url_request.h" |
| 35 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 48 namespace { | 46 namespace { |
| 49 | 47 |
| 50 // We always use the TEMPORARY FileSystem in this test. | 48 // We always use the TEMPORARY FileSystem in this test. |
| 51 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; | 49 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; |
| 52 const char kTestFileData[] = "0123456789"; | 50 const char kTestFileData[] = "0123456789"; |
| 53 | 51 |
| 54 void FillBuffer(char* buffer, size_t len) { | 52 void FillBuffer(char* buffer, size_t len) { |
| 55 base::RandBytes(buffer, len); | 53 base::RandBytes(buffer, len); |
| 56 } | 54 } |
| 57 | 55 |
| 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 | |
| 84 } // namespace | 56 } // namespace |
| 85 | 57 |
| 86 class FileSystemURLRequestJobTest : public testing::Test { | 58 class FileSystemURLRequestJobTest : public testing::Test { |
| 87 protected: | 59 protected: |
| 88 FileSystemURLRequestJobTest() : weak_factory_(this) { | 60 FileSystemURLRequestJobTest() : weak_factory_(this) { |
| 89 } | 61 } |
| 90 | 62 |
| 91 virtual void SetUp() OVERRIDE { | 63 virtual void SetUp() OVERRIDE { |
| 92 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 93 | 65 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 111 net::URLRequest::Deprecated::RegisterProtocolFactory("filesystem", NULL); | 83 net::URLRequest::Deprecated::RegisterProtocolFactory("filesystem", NULL); |
| 112 ClearUnusedJob(); | 84 ClearUnusedJob(); |
| 113 if (pending_job_.get()) { | 85 if (pending_job_.get()) { |
| 114 pending_job_->Kill(); | 86 pending_job_->Kill(); |
| 115 pending_job_ = NULL; | 87 pending_job_ = NULL; |
| 116 } | 88 } |
| 117 // FileReader posts a task to close the file in destructor. | 89 // FileReader posts a task to close the file in destructor. |
| 118 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
| 119 } | 91 } |
| 120 | 92 |
| 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 | |
| 140 void OnOpenFileSystem(const GURL& root_url, | 93 void OnOpenFileSystem(const GURL& root_url, |
| 141 const std::string& name, | 94 const std::string& name, |
| 142 base::File::Error result) { | 95 base::File::Error result) { |
| 143 ASSERT_EQ(base::File::FILE_OK, result); | 96 ASSERT_EQ(base::File::FILE_OK, result); |
| 144 } | 97 } |
| 145 | 98 |
| 146 void TestRequestHelper(const GURL& url, | 99 void TestRequestHelper(const GURL& url, |
| 147 const net::HttpRequestHeaders* headers, | 100 const net::HttpRequestHeaders* headers, |
| 148 bool run_to_completion, | 101 bool run_to_completion, |
| 149 FileSystemContext* file_system_context) { | 102 FileSystemContext* file_system_context) { |
| 150 delegate_.reset(new net::TestDelegate()); | 103 delegate_.reset(new net::TestDelegate()); |
| 151 // Make delegate_ exit the MessageLoop when the request is done. | 104 // Make delegate_ exit the MessageLoop when the request is done. |
| 152 delegate_->set_quit_on_complete(true); | 105 delegate_->set_quit_on_complete(true); |
| 153 delegate_->set_quit_on_redirect(true); | 106 delegate_->set_quit_on_redirect(true); |
| 154 request_ = empty_context_.CreateRequest( | 107 request_ = empty_context_.CreateRequest( |
| 155 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); | 108 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); |
| 156 if (headers) | 109 if (headers) |
| 157 request_->SetExtraRequestHeaders(*headers); | 110 request_->SetExtraRequestHeaders(*headers); |
| 158 ASSERT_TRUE(!job_); | 111 ASSERT_TRUE(!job_); |
| 159 job_ = new FileSystemURLRequestJob( | 112 job_ = new FileSystemURLRequestJob( |
| 160 request_.get(), NULL, url.GetOrigin().host(), file_system_context); | 113 request_.get(), NULL, file_system_context); |
| 161 pending_job_ = job_; | 114 pending_job_ = job_; |
| 162 | 115 |
| 163 request_->Start(); | 116 request_->Start(); |
| 164 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 117 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
| 165 if (run_to_completion) | 118 if (run_to_completion) |
| 166 base::MessageLoop::current()->Run(); | 119 base::MessageLoop::current()->Run(); |
| 167 } | 120 } |
| 168 | 121 |
| 169 void TestRequest(const GURL& url) { | 122 void TestRequest(const GURL& url) { |
| 170 TestRequestHelper(url, NULL, true, file_system_context_.get()); | 123 TestRequestHelper(url, NULL, true, file_system_context_.get()); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 net::HttpRequestHeaders::kRange, | 253 net::HttpRequestHeaders::kRange, |
| 301 net::HttpByteRange::RightUnbounded(first_byte_position).GetHeaderValue()); | 254 net::HttpByteRange::RightUnbounded(first_byte_position).GetHeaderValue()); |
| 302 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers); | 255 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers); |
| 303 ASSERT_FALSE(request_->is_pending()); | 256 ASSERT_FALSE(request_->is_pending()); |
| 304 EXPECT_EQ(1, delegate_->response_started_count()); | 257 EXPECT_EQ(1, delegate_->response_started_count()); |
| 305 EXPECT_FALSE(delegate_->received_data_before_response()); | 258 EXPECT_FALSE(delegate_->received_data_before_response()); |
| 306 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. | 259 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 307 EXPECT_TRUE(partial_buffer_string == delegate_->data_received()); | 260 EXPECT_TRUE(partial_buffer_string == delegate_->data_received()); |
| 308 } | 261 } |
| 309 | 262 |
| 263 |
| 310 TEST_F(FileSystemURLRequestJobTest, FileTestMultipleRangesNotSupported) { | 264 TEST_F(FileSystemURLRequestJobTest, FileTestMultipleRangesNotSupported) { |
| 311 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); | 265 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); |
| 312 net::HttpRequestHeaders headers; | 266 net::HttpRequestHeaders headers; |
| 313 headers.SetHeader(net::HttpRequestHeaders::kRange, | 267 headers.SetHeader(net::HttpRequestHeaders::kRange, |
| 314 "bytes=0-5,10-200,200-300"); | 268 "bytes=0-5,10-200,200-300"); |
| 315 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers); | 269 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers); |
| 316 EXPECT_TRUE(delegate_->request_failed()); | 270 EXPECT_TRUE(delegate_->request_failed()); |
| 317 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, | 271 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, |
| 318 request_->status().error()); | 272 request_->status().error()); |
| 319 } | 273 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 EXPECT_TRUE(delegate_->request_failed()); | 362 EXPECT_TRUE(delegate_->request_failed()); |
| 409 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 363 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 410 | 364 |
| 411 // Make sure it returns success with regular (non-incognito) context. | 365 // Make sure it returns success with regular (non-incognito) context. |
| 412 TestRequest(CreateFileSystemURL("file")); | 366 TestRequest(CreateFileSystemURL("file")); |
| 413 ASSERT_FALSE(request_->is_pending()); | 367 ASSERT_FALSE(request_->is_pending()); |
| 414 EXPECT_EQ(kTestFileData, delegate_->data_received()); | 368 EXPECT_EQ(kTestFileData, delegate_->data_received()); |
| 415 EXPECT_EQ(200, request_->GetResponseCode()); | 369 EXPECT_EQ(200, request_->GetResponseCode()); |
| 416 } | 370 } |
| 417 | 371 |
| 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 | |
| 462 } // namespace | 372 } // namespace |
| 463 } // namespace content | 373 } // namespace content |
| OLD | NEW |