Chromium Code Reviews| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "content/browser/fileapi/mock_url_request_delegate.h" | 15 #include "content/browser/fileapi/mock_url_request_delegate.h" |
| 16 #include "content/public/test/async_file_test_helper.h" | 16 #include "content/public/test/async_file_test_helper.h" |
| 17 #include "content/public/test/test_file_system_context.h" | 17 #include "content/public/test/test_file_system_context.h" |
| 18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
| 19 #include "net/http/http_byte_range.h" | 19 #include "net/http/http_byte_range.h" |
| 20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
| 21 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
| 22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 24 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
| 25 #include "storage/browser/blob/blob_data_builder.h" | 25 #include "storage/browser/blob/blob_data_builder.h" |
| 26 #include "storage/browser/blob/blob_data_handle.h" | |
| 26 #include "storage/browser/blob/blob_data_snapshot.h" | 27 #include "storage/browser/blob/blob_data_snapshot.h" |
| 28 #include "storage/browser/blob/blob_storage_context.h" | |
| 27 #include "storage/browser/blob/blob_url_request_job.h" | 29 #include "storage/browser/blob/blob_url_request_job.h" |
| 28 #include "storage/browser/fileapi/file_system_context.h" | 30 #include "storage/browser/fileapi/file_system_context.h" |
| 29 #include "storage/browser/fileapi/file_system_operation_context.h" | 31 #include "storage/browser/fileapi/file_system_operation_context.h" |
| 30 #include "storage/browser/fileapi/file_system_url.h" | 32 #include "storage/browser/fileapi/file_system_url.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 34 |
| 33 using storage::BlobDataSnapshot; | 35 using storage::BlobDataSnapshot; |
| 34 using storage::BlobDataBuilder; | 36 using storage::BlobDataBuilder; |
| 35 using storage::BlobURLRequestJob; | 37 using storage::BlobURLRequestJob; |
| 36 | 38 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 60 class MockProtocolHandler : | 62 class MockProtocolHandler : |
| 61 public net::URLRequestJobFactory::ProtocolHandler { | 63 public net::URLRequestJobFactory::ProtocolHandler { |
| 62 public: | 64 public: |
| 63 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} | 65 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} |
| 64 | 66 |
| 65 // net::URLRequestJobFactory::ProtocolHandler override. | 67 // net::URLRequestJobFactory::ProtocolHandler override. |
| 66 net::URLRequestJob* MaybeCreateJob( | 68 net::URLRequestJob* MaybeCreateJob( |
| 67 net::URLRequest* request, | 69 net::URLRequest* request, |
| 68 net::NetworkDelegate* network_delegate) const override { | 70 net::NetworkDelegate* network_delegate) const override { |
| 69 return new BlobURLRequestJob(request, network_delegate, | 71 return new BlobURLRequestJob(request, network_delegate, |
| 70 test_->blob_data_->BuildSnapshot().Pass(), | 72 test_->GetSnapshotFromBuilder(), |
| 71 test_->file_system_context_.get(), | 73 test_->file_system_context_.get(), |
| 72 base::MessageLoopProxy::current().get()); | 74 base::MessageLoopProxy::current().get()); |
| 73 } | 75 } |
| 74 | 76 |
| 75 private: | 77 private: |
| 76 BlobURLRequestJobTest* test_; | 78 BlobURLRequestJobTest* test_; |
| 77 }; | 79 }; |
| 78 | 80 |
| 79 BlobURLRequestJobTest() | 81 BlobURLRequestJobTest() |
| 80 : blob_data_(new BlobDataBuilder("uuid")), expected_status_code_(0) {} | 82 : blob_data_(new BlobDataBuilder("uuid")), expected_status_code_(0) {} |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 96 arraysize(kTestFileData2) - 1)); | 98 arraysize(kTestFileData2) - 1)); |
| 97 base::File::Info file_info2; | 99 base::File::Info file_info2; |
| 98 base::GetFileInfo(temp_file2_, &file_info2); | 100 base::GetFileInfo(temp_file2_, &file_info2); |
| 99 temp_file_modification_time2_ = file_info2.last_modified; | 101 temp_file_modification_time2_ = file_info2.last_modified; |
| 100 | 102 |
| 101 url_request_job_factory_.SetProtocolHandler("blob", | 103 url_request_job_factory_.SetProtocolHandler("blob", |
| 102 new MockProtocolHandler(this)); | 104 new MockProtocolHandler(this)); |
| 103 url_request_context_.set_job_factory(&url_request_job_factory_); | 105 url_request_context_.set_job_factory(&url_request_job_factory_); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void TearDown() override {} | 108 void TearDown() override { |
| 109 blob_handle_.reset(); | |
| 110 { // Clean up for ASAN | |
|
Alexei Svitkine (slow)
2015/02/10 16:05:05
Nit: Inner scope is not necessary here.
dmurph
2015/02/10 22:17:47
Done.
| |
| 111 base::RunLoop run_loop; | |
| 112 run_loop.RunUntilIdle(); | |
| 113 } | |
| 114 } | |
| 107 | 115 |
| 108 void SetUpFileSystem() { | 116 void SetUpFileSystem() { |
| 109 // Prepare file system. | 117 // Prepare file system. |
| 110 file_system_context_ = CreateFileSystemContextForTesting( | 118 file_system_context_ = CreateFileSystemContextForTesting( |
| 111 NULL, temp_dir_.path()); | 119 NULL, temp_dir_.path()); |
| 112 | 120 |
| 113 file_system_context_->OpenFileSystem( | 121 file_system_context_->OpenFileSystem( |
| 114 GURL(kFileSystemURLOrigin), | 122 GURL(kFileSystemURLOrigin), |
| 115 kFileSystemType, | 123 kFileSystemType, |
| 116 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 124 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, | 216 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, |
| 209 temp_file_system_file_modification_time2_); | 217 temp_file_system_file_modification_time2_); |
| 210 *expected_result = std::string(kTestData1 + 1, 2); | 218 *expected_result = std::string(kTestData1 + 1, 2); |
| 211 *expected_result += std::string(kTestFileData1 + 2, 3); | 219 *expected_result += std::string(kTestFileData1 + 2, 3); |
| 212 *expected_result += std::string(kTestFileSystemFileData1 + 3, 4); | 220 *expected_result += std::string(kTestFileSystemFileData1 + 3, 4); |
| 213 *expected_result += std::string(kTestData2 + 4, 5); | 221 *expected_result += std::string(kTestData2 + 4, 5); |
| 214 *expected_result += std::string(kTestFileData2 + 5, 6); | 222 *expected_result += std::string(kTestFileData2 + 5, 6); |
| 215 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); | 223 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); |
| 216 } | 224 } |
| 217 | 225 |
| 226 scoped_ptr<BlobDataSnapshot> GetSnapshotFromBuilder() { | |
| 227 if (!blob_handle_) { | |
| 228 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass(); | |
| 229 } | |
| 230 return blob_handle_->CreateSnapshot().Pass(); | |
| 231 } | |
| 232 | |
| 218 // This only works if all the Blob items have a definite pre-computed length. | 233 // This only works if all the Blob items have a definite pre-computed length. |
| 219 // Otherwise, this will fail a CHECK. | 234 // Otherwise, this will fail a CHECK. |
| 220 int64 GetTotalBlobLength() const { | 235 int64 GetTotalBlobLength() { |
| 221 int64 total = 0; | 236 int64 total = 0; |
| 222 scoped_ptr<BlobDataSnapshot> data = blob_data_->BuildSnapshot(); | 237 scoped_ptr<BlobDataSnapshot> data = GetSnapshotFromBuilder(); |
| 223 const auto& items = data->items(); | 238 const auto& items = data->items(); |
| 224 for (const auto& item : items) { | 239 for (const auto& item : items) { |
| 225 int64 length = base::checked_cast<int64>(item->length()); | 240 int64 length = base::checked_cast<int64>(item->length()); |
| 226 CHECK(length <= kint64max - total); | 241 CHECK(length <= kint64max - total); |
| 227 total += length; | 242 total += length; |
| 228 } | 243 } |
| 229 return total; | 244 return total; |
| 230 } | 245 } |
| 231 | 246 |
| 232 protected: | 247 protected: |
| 233 base::ScopedTempDir temp_dir_; | 248 base::ScopedTempDir temp_dir_; |
| 234 base::FilePath temp_file1_; | 249 base::FilePath temp_file1_; |
| 235 base::FilePath temp_file2_; | 250 base::FilePath temp_file2_; |
| 236 base::Time temp_file_modification_time1_; | 251 base::Time temp_file_modification_time1_; |
| 237 base::Time temp_file_modification_time2_; | 252 base::Time temp_file_modification_time2_; |
| 238 GURL file_system_root_url_; | 253 GURL file_system_root_url_; |
| 239 GURL temp_file_system_file1_; | 254 GURL temp_file_system_file1_; |
| 240 GURL temp_file_system_file2_; | 255 GURL temp_file_system_file2_; |
| 241 base::Time temp_file_system_file_modification_time1_; | 256 base::Time temp_file_system_file_modification_time1_; |
| 242 base::Time temp_file_system_file_modification_time2_; | 257 base::Time temp_file_system_file_modification_time2_; |
| 243 | 258 |
| 244 base::MessageLoopForIO message_loop_; | 259 base::MessageLoopForIO message_loop_; |
| 245 scoped_refptr<storage::FileSystemContext> file_system_context_; | 260 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 261 | |
| 262 storage::BlobStorageContext blob_context_; | |
| 263 scoped_ptr<storage::BlobDataHandle> blob_handle_; | |
| 246 scoped_ptr<BlobDataBuilder> blob_data_; | 264 scoped_ptr<BlobDataBuilder> blob_data_; |
| 247 scoped_ptr<BlobDataSnapshot> blob_data_snapshot_; | 265 scoped_ptr<BlobDataSnapshot> blob_data_snapshot_; |
| 248 net::URLRequestJobFactoryImpl url_request_job_factory_; | 266 net::URLRequestJobFactoryImpl url_request_job_factory_; |
| 249 net::URLRequestContext url_request_context_; | 267 net::URLRequestContext url_request_context_; |
| 250 MockURLRequestDelegate url_request_delegate_; | 268 MockURLRequestDelegate url_request_delegate_; |
| 251 scoped_ptr<net::URLRequest> request_; | 269 scoped_ptr<net::URLRequest> request_; |
| 252 | 270 |
| 253 int expected_status_code_; | 271 int expected_status_code_; |
| 254 std::string expected_response_; | 272 std::string expected_response_; |
| 255 }; | 273 }; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); | 426 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
| 409 EXPECT_EQ(kTestContentType, content_type); | 427 EXPECT_EQ(kTestContentType, content_type); |
| 410 void* iter = NULL; | 428 void* iter = NULL; |
| 411 std::string content_disposition; | 429 std::string content_disposition; |
| 412 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( | 430 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
| 413 &iter, "Content-Disposition", &content_disposition)); | 431 &iter, "Content-Disposition", &content_disposition)); |
| 414 EXPECT_EQ(kTestContentDisposition, content_disposition); | 432 EXPECT_EQ(kTestContentDisposition, content_disposition); |
| 415 } | 433 } |
| 416 | 434 |
| 417 } // namespace content | 435 } // namespace content |
| OLD | NEW |