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/macros.h" | 10 #include "base/macros.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 class MockProtocolHandler : | 108 class MockProtocolHandler : |
109 public net::URLRequestJobFactory::ProtocolHandler { | 109 public net::URLRequestJobFactory::ProtocolHandler { |
110 public: | 110 public: |
111 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} | 111 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} |
112 | 112 |
113 // net::URLRequestJobFactory::ProtocolHandler override. | 113 // net::URLRequestJobFactory::ProtocolHandler override. |
114 net::URLRequestJob* MaybeCreateJob( | 114 net::URLRequestJob* MaybeCreateJob( |
115 net::URLRequest* request, | 115 net::URLRequest* request, |
116 net::NetworkDelegate* network_delegate) const override { | 116 net::NetworkDelegate* network_delegate) const override { |
117 return new BlobURLRequestJob(request, network_delegate, | 117 return new BlobURLRequestJob(request, network_delegate, |
118 test_->GetSnapshotFromBuilder(), | 118 test_->GetHandleFromBuilder(), |
119 test_->file_system_context_.get(), | 119 test_->file_system_context_.get(), |
120 base::ThreadTaskRunnerHandle::Get().get()); | 120 base::ThreadTaskRunnerHandle::Get().get()); |
121 } | 121 } |
122 | 122 |
123 private: | 123 private: |
124 BlobURLRequestJobTest* test_; | 124 BlobURLRequestJobTest* test_; |
125 }; | 125 }; |
126 | 126 |
127 BlobURLRequestJobTest() | 127 BlobURLRequestJobTest() |
128 : blob_data_(new BlobDataBuilder("uuid")), expected_status_code_(0) {} | 128 : blob_data_(new BlobDataBuilder("uuid")), expected_status_code_(0) {} |
(...skipping 21 matching lines...) Expand all Loading... |
150 disk_cache_entry_ = CreateDiskCacheEntry( | 150 disk_cache_entry_ = CreateDiskCacheEntry( |
151 disk_cache_backend_.get(), kTestDiskCacheKey, kTestDiskCacheData); | 151 disk_cache_backend_.get(), kTestDiskCacheKey, kTestDiskCacheData); |
152 | 152 |
153 url_request_job_factory_.SetProtocolHandler( | 153 url_request_job_factory_.SetProtocolHandler( |
154 "blob", make_scoped_ptr(new MockProtocolHandler(this))); | 154 "blob", make_scoped_ptr(new MockProtocolHandler(this))); |
155 url_request_context_.set_job_factory(&url_request_job_factory_); | 155 url_request_context_.set_job_factory(&url_request_job_factory_); |
156 } | 156 } |
157 | 157 |
158 void TearDown() override { | 158 void TearDown() override { |
159 blob_handle_.reset(); | 159 blob_handle_.reset(); |
| 160 request_.reset(); |
160 // Clean up for ASAN | 161 // Clean up for ASAN |
161 base::RunLoop run_loop; | 162 base::RunLoop run_loop; |
162 run_loop.RunUntilIdle(); | 163 run_loop.RunUntilIdle(); |
163 } | 164 } |
164 | 165 |
165 void SetUpFileSystem() { | 166 void SetUpFileSystem() { |
166 // Prepare file system. | 167 // Prepare file system. |
167 file_system_context_ = CreateFileSystemContextForTesting( | 168 file_system_context_ = CreateFileSystemContextForTesting( |
168 NULL, temp_dir_.path()); | 169 NULL, temp_dir_.path()); |
169 | 170 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 *expected_result += std::string(kTestData2 + 4, 5); | 276 *expected_result += std::string(kTestData2 + 4, 5); |
276 | 277 |
277 blob_data_->AppendFile(temp_file2_, 5, 6, temp_file_modification_time2_); | 278 blob_data_->AppendFile(temp_file2_, 5, 6, temp_file_modification_time2_); |
278 *expected_result += std::string(kTestFileData2 + 5, 6); | 279 *expected_result += std::string(kTestFileData2 + 5, 6); |
279 | 280 |
280 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, | 281 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, |
281 temp_file_system_file_modification_time2_); | 282 temp_file_system_file_modification_time2_); |
282 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); | 283 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); |
283 } | 284 } |
284 | 285 |
285 scoped_ptr<BlobDataSnapshot> GetSnapshotFromBuilder() { | 286 storage::BlobDataHandle* GetHandleFromBuilder() { |
286 if (!blob_handle_) { | 287 if (!blob_handle_) { |
287 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass(); | 288 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass(); |
288 } | 289 } |
289 return blob_handle_->CreateSnapshot().Pass(); | 290 return blob_handle_.get(); |
290 } | 291 } |
291 | 292 |
292 // This only works if all the Blob items have a definite pre-computed length. | 293 // This only works if all the Blob items have a definite pre-computed length. |
293 // Otherwise, this will fail a CHECK. | 294 // Otherwise, this will fail a CHECK. |
294 int64 GetTotalBlobLength() { | 295 int64 GetTotalBlobLength() { |
295 int64 total = 0; | 296 int64 total = 0; |
296 scoped_ptr<BlobDataSnapshot> data = GetSnapshotFromBuilder(); | 297 scoped_ptr<BlobDataSnapshot> data = |
| 298 GetHandleFromBuilder()->CreateSnapshot(); |
297 const auto& items = data->items(); | 299 const auto& items = data->items(); |
298 for (const auto& item : items) { | 300 for (const auto& item : items) { |
299 int64 length = base::checked_cast<int64>(item->length()); | 301 int64 length = base::checked_cast<int64>(item->length()); |
300 CHECK(length <= kint64max - total); | 302 CHECK(length <= kint64max - total); |
301 total += length; | 303 total += length; |
302 } | 304 } |
303 return total; | 305 return total; |
304 } | 306 } |
305 | 307 |
306 protected: | 308 protected: |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 | 486 |
485 int64 total = GetTotalBlobLength(); | 487 int64 total = GetTotalBlobLength(); |
486 int64 first = 0, last = 0, length = 0; | 488 int64 first = 0, last = 0, length = 0; |
487 EXPECT_TRUE( | 489 EXPECT_TRUE( |
488 request_->response_headers()->GetContentRange(&first, &last, &length)); | 490 request_->response_headers()->GetContentRange(&first, &last, &length)); |
489 EXPECT_EQ(total - 10, first); | 491 EXPECT_EQ(total - 10, first); |
490 EXPECT_EQ(total - 1, last); | 492 EXPECT_EQ(total - 1, last); |
491 EXPECT_EQ(total, length); | 493 EXPECT_EQ(total, length); |
492 } | 494 } |
493 | 495 |
| 496 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest3) { |
| 497 SetUpFileSystem(); |
| 498 std::string result; |
| 499 BuildComplicatedData(&result); |
| 500 net::HttpRequestHeaders extra_headers; |
| 501 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, |
| 502 net::HttpByteRange::Bounded(0, 2).GetHeaderValue()); |
| 503 expected_status_code_ = 206; |
| 504 expected_response_ = result.substr(0, 3); |
| 505 TestRequest("GET", extra_headers); |
| 506 |
| 507 EXPECT_EQ(3, request_->response_headers()->GetContentLength()); |
| 508 |
| 509 int64 first = 0, last = 0, length = 0; |
| 510 EXPECT_TRUE( |
| 511 request_->response_headers()->GetContentRange(&first, &last, &length)); |
| 512 EXPECT_EQ(0, first); |
| 513 EXPECT_EQ(2, last); |
| 514 EXPECT_EQ(GetTotalBlobLength(), length); |
| 515 } |
| 516 |
494 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { | 517 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { |
495 blob_data_->set_content_type(kTestContentType); | 518 blob_data_->set_content_type(kTestContentType); |
496 blob_data_->set_content_disposition(kTestContentDisposition); | 519 blob_data_->set_content_disposition(kTestContentDisposition); |
497 blob_data_->AppendData(kTestData1); | 520 blob_data_->AppendData(kTestData1); |
498 expected_status_code_ = 200; | 521 expected_status_code_ = 200; |
499 expected_response_ = kTestData1; | 522 expected_response_ = kTestData1; |
500 TestRequest("GET", net::HttpRequestHeaders()); | 523 TestRequest("GET", net::HttpRequestHeaders()); |
501 | 524 |
502 std::string content_type; | 525 std::string content_type; |
503 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); | 526 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
504 EXPECT_EQ(kTestContentType, content_type); | 527 EXPECT_EQ(kTestContentType, content_type); |
505 void* iter = NULL; | 528 void* iter = NULL; |
506 std::string content_disposition; | 529 std::string content_disposition; |
507 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( | 530 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
508 &iter, "Content-Disposition", &content_disposition)); | 531 &iter, "Content-Disposition", &content_disposition)); |
509 EXPECT_EQ(kTestContentDisposition, content_disposition); | 532 EXPECT_EQ(kTestContentDisposition, content_disposition); |
510 } | 533 } |
511 | 534 |
512 } // namespace content | 535 } // namespace content |
OLD | NEW |