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_->GetHandleFromBuilder(), | 118 test_->GetSnapshotFromBuilder(), |
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(); | |
161 // Clean up for ASAN | 160 // Clean up for ASAN |
162 base::RunLoop run_loop; | 161 base::RunLoop run_loop; |
163 run_loop.RunUntilIdle(); | 162 run_loop.RunUntilIdle(); |
164 } | 163 } |
165 | 164 |
166 void SetUpFileSystem() { | 165 void SetUpFileSystem() { |
167 // Prepare file system. | 166 // Prepare file system. |
168 file_system_context_ = CreateFileSystemContextForTesting( | 167 file_system_context_ = CreateFileSystemContextForTesting( |
169 NULL, temp_dir_.path()); | 168 NULL, temp_dir_.path()); |
170 | 169 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 *expected_result += std::string(kTestData2 + 4, 5); | 275 *expected_result += std::string(kTestData2 + 4, 5); |
277 | 276 |
278 blob_data_->AppendFile(temp_file2_, 5, 6, temp_file_modification_time2_); | 277 blob_data_->AppendFile(temp_file2_, 5, 6, temp_file_modification_time2_); |
279 *expected_result += std::string(kTestFileData2 + 5, 6); | 278 *expected_result += std::string(kTestFileData2 + 5, 6); |
280 | 279 |
281 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, | 280 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, |
282 temp_file_system_file_modification_time2_); | 281 temp_file_system_file_modification_time2_); |
283 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); | 282 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); |
284 } | 283 } |
285 | 284 |
286 storage::BlobDataHandle* GetHandleFromBuilder() { | 285 scoped_ptr<BlobDataSnapshot> GetSnapshotFromBuilder() { |
287 if (!blob_handle_) { | 286 if (!blob_handle_) { |
288 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass(); | 287 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass(); |
289 } | 288 } |
290 return blob_handle_.get(); | 289 return blob_handle_->CreateSnapshot().Pass(); |
291 } | 290 } |
292 | 291 |
293 // This only works if all the Blob items have a definite pre-computed length. | 292 // This only works if all the Blob items have a definite pre-computed length. |
294 // Otherwise, this will fail a CHECK. | 293 // Otherwise, this will fail a CHECK. |
295 int64 GetTotalBlobLength() { | 294 int64 GetTotalBlobLength() { |
296 int64 total = 0; | 295 int64 total = 0; |
297 scoped_ptr<BlobDataSnapshot> data = | 296 scoped_ptr<BlobDataSnapshot> data = GetSnapshotFromBuilder(); |
298 GetHandleFromBuilder()->CreateSnapshot(); | |
299 const auto& items = data->items(); | 297 const auto& items = data->items(); |
300 for (const auto& item : items) { | 298 for (const auto& item : items) { |
301 int64 length = base::checked_cast<int64>(item->length()); | 299 int64 length = base::checked_cast<int64>(item->length()); |
302 CHECK(length <= kint64max - total); | 300 CHECK(length <= kint64max - total); |
303 total += length; | 301 total += length; |
304 } | 302 } |
305 return total; | 303 return total; |
306 } | 304 } |
307 | 305 |
308 protected: | 306 protected: |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 | 484 |
487 int64 total = GetTotalBlobLength(); | 485 int64 total = GetTotalBlobLength(); |
488 int64 first = 0, last = 0, length = 0; | 486 int64 first = 0, last = 0, length = 0; |
489 EXPECT_TRUE( | 487 EXPECT_TRUE( |
490 request_->response_headers()->GetContentRange(&first, &last, &length)); | 488 request_->response_headers()->GetContentRange(&first, &last, &length)); |
491 EXPECT_EQ(total - 10, first); | 489 EXPECT_EQ(total - 10, first); |
492 EXPECT_EQ(total - 1, last); | 490 EXPECT_EQ(total - 1, last); |
493 EXPECT_EQ(total, length); | 491 EXPECT_EQ(total, length); |
494 } | 492 } |
495 | 493 |
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 | |
517 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { | 494 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { |
518 blob_data_->set_content_type(kTestContentType); | 495 blob_data_->set_content_type(kTestContentType); |
519 blob_data_->set_content_disposition(kTestContentDisposition); | 496 blob_data_->set_content_disposition(kTestContentDisposition); |
520 blob_data_->AppendData(kTestData1); | 497 blob_data_->AppendData(kTestData1); |
521 expected_status_code_ = 200; | 498 expected_status_code_ = 200; |
522 expected_response_ = kTestData1; | 499 expected_response_ = kTestData1; |
523 TestRequest("GET", net::HttpRequestHeaders()); | 500 TestRequest("GET", net::HttpRequestHeaders()); |
524 | 501 |
525 std::string content_type; | 502 std::string content_type; |
526 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); | 503 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
527 EXPECT_EQ(kTestContentType, content_type); | 504 EXPECT_EQ(kTestContentType, content_type); |
528 void* iter = NULL; | 505 void* iter = NULL; |
529 std::string content_disposition; | 506 std::string content_disposition; |
530 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( | 507 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
531 &iter, "Content-Disposition", &content_disposition)); | 508 &iter, "Content-Disposition", &content_disposition)); |
532 EXPECT_EQ(kTestContentDisposition, content_disposition); | 509 EXPECT_EQ(kTestContentDisposition, content_disposition); |
533 } | 510 } |
534 | 511 |
535 } // namespace content | 512 } // namespace content |
OLD | NEW |