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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/numerics/safe_conversions.h" | 17 #include "base/numerics/safe_conversions.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "content/browser/fileapi/mock_url_request_delegate.h" | |
| 22 #include "content/public/test/async_file_test_helper.h" | 21 #include "content/public/test/async_file_test_helper.h" |
| 23 #include "content/public/test/test_file_system_context.h" | 22 #include "content/public/test/test_file_system_context.h" |
| 24 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 25 #include "net/base/request_priority.h" | 24 #include "net/base/request_priority.h" |
| 26 #include "net/base/test_completion_callback.h" | 25 #include "net/base/test_completion_callback.h" |
| 27 #include "net/disk_cache/disk_cache.h" | 26 #include "net/disk_cache/disk_cache.h" |
| 28 #include "net/http/http_byte_range.h" | 27 #include "net/http/http_byte_range.h" |
| 29 #include "net/http/http_request_headers.h" | 28 #include "net/http/http_request_headers.h" |
| 30 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
| 31 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" |
| 32 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
| 33 #include "net/url_request/url_request_job_factory_impl.h" | 32 #include "net/url_request/url_request_job_factory_impl.h" |
| 33 #include "net/url_request/url_request_test_util.h" | |
| 34 #include "storage/browser/blob/blob_data_builder.h" | 34 #include "storage/browser/blob/blob_data_builder.h" |
| 35 #include "storage/browser/blob/blob_data_handle.h" | 35 #include "storage/browser/blob/blob_data_handle.h" |
| 36 #include "storage/browser/blob/blob_data_snapshot.h" | 36 #include "storage/browser/blob/blob_data_snapshot.h" |
| 37 #include "storage/browser/blob/blob_storage_context.h" | 37 #include "storage/browser/blob/blob_storage_context.h" |
| 38 #include "storage/browser/blob/blob_url_request_job.h" | 38 #include "storage/browser/blob/blob_url_request_job.h" |
| 39 #include "storage/browser/fileapi/file_system_context.h" | 39 #include "storage/browser/fileapi/file_system_context.h" |
| 40 #include "storage/browser/fileapi/file_system_operation_context.h" | 40 #include "storage/browser/fileapi/file_system_operation_context.h" |
| 41 #include "storage/browser/fileapi/file_system_url.h" | 41 #include "storage/browser/fileapi/file_system_url.h" |
| 42 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 43 | 43 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 expected_response_ = expected_response; | 252 expected_response_ = expected_response; |
| 253 TestRequest("GET", net::HttpRequestHeaders()); | 253 TestRequest("GET", net::HttpRequestHeaders()); |
| 254 EXPECT_EQ(expected_content_length, | 254 EXPECT_EQ(expected_content_length, |
| 255 request_->response_headers()->GetContentLength()); | 255 request_->response_headers()->GetContentLength()); |
| 256 } | 256 } |
| 257 | 257 |
| 258 void TestErrorRequest(int expected_status_code) { | 258 void TestErrorRequest(int expected_status_code) { |
| 259 expected_status_code_ = expected_status_code; | 259 expected_status_code_ = expected_status_code; |
| 260 expected_response_ = ""; | 260 expected_response_ = ""; |
| 261 TestRequest("GET", net::HttpRequestHeaders()); | 261 TestRequest("GET", net::HttpRequestHeaders()); |
| 262 EXPECT_FALSE(url_request_delegate_.metadata()); | 262 EXPECT_FALSE(request_->response_info().metadata); |
| 263 } | 263 } |
| 264 | 264 |
| 265 void TestRequest(const std::string& method, | 265 void TestRequest(const std::string& method, |
| 266 const net::HttpRequestHeaders& extra_headers) { | 266 const net::HttpRequestHeaders& extra_headers) { |
| 267 request_ = url_request_context_.CreateRequest( | 267 request_ = url_request_context_.CreateRequest( |
| 268 GURL("blob:blah"), net::DEFAULT_PRIORITY, &url_request_delegate_); | 268 GURL("blob:blah"), net::DEFAULT_PRIORITY, &url_request_delegate_); |
| 269 request_->set_method(method); | 269 request_->set_method(method); |
| 270 if (!extra_headers.IsEmpty()) | 270 if (!extra_headers.IsEmpty()) |
| 271 request_->SetExtraRequestHeaders(extra_headers); | 271 request_->SetExtraRequestHeaders(extra_headers); |
| 272 request_->Start(); | 272 request_->Start(); |
| 273 | 273 |
| 274 base::RunLoop().Run(); | 274 base::RunLoop().Run(); |
| 275 | 275 |
| 276 // Verify response. | 276 // Verify response. |
| 277 EXPECT_EQ(net::OK, url_request_delegate_.request_status()); | 277 EXPECT_EQ(net::OK, url_request_delegate_.request_status()); |
| 278 EXPECT_EQ(expected_status_code_, | 278 EXPECT_EQ(expected_status_code_, |
| 279 request_->response_headers()->response_code()); | 279 request_->response_headers()->response_code()); |
| 280 EXPECT_EQ(expected_response_, url_request_delegate_.response_data()); | 280 EXPECT_EQ(expected_response_, url_request_delegate_.data_received()); |
| 281 } | 281 } |
| 282 | 282 |
| 283 void BuildComplicatedData(std::string* expected_result) { | 283 void BuildComplicatedData(std::string* expected_result) { |
| 284 blob_data_->AppendData(kTestData1 + 1, 2); | 284 blob_data_->AppendData(kTestData1 + 1, 2); |
| 285 *expected_result = std::string(kTestData1 + 1, 2); | 285 *expected_result = std::string(kTestData1 + 1, 2); |
| 286 | 286 |
| 287 blob_data_->AppendFile(temp_file1_, 2, 3, temp_file_modification_time1_); | 287 blob_data_->AppendFile(temp_file1_, 2, 3, temp_file_modification_time1_); |
| 288 *expected_result += std::string(kTestFileData1 + 2, 3); | 288 *expected_result += std::string(kTestFileData1 + 2, 3); |
| 289 | 289 |
| 290 blob_data_->AppendDiskCacheEntry(new EmptyDataHandle(), | 290 blob_data_->AppendDiskCacheEntry(new EmptyDataHandle(), |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 | 346 |
| 347 base::MessageLoopForIO message_loop_; | 347 base::MessageLoopForIO message_loop_; |
| 348 scoped_refptr<storage::FileSystemContext> file_system_context_; | 348 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 349 | 349 |
| 350 storage::BlobStorageContext blob_context_; | 350 storage::BlobStorageContext blob_context_; |
| 351 std::unique_ptr<storage::BlobDataHandle> blob_handle_; | 351 std::unique_ptr<storage::BlobDataHandle> blob_handle_; |
| 352 std::unique_ptr<BlobDataBuilder> blob_data_; | 352 std::unique_ptr<BlobDataBuilder> blob_data_; |
| 353 std::unique_ptr<BlobDataSnapshot> blob_data_snapshot_; | 353 std::unique_ptr<BlobDataSnapshot> blob_data_snapshot_; |
| 354 net::URLRequestJobFactoryImpl url_request_job_factory_; | 354 net::URLRequestJobFactoryImpl url_request_job_factory_; |
| 355 net::URLRequestContext url_request_context_; | 355 net::URLRequestContext url_request_context_; |
| 356 MockURLRequestDelegate url_request_delegate_; | 356 net::TestDelegate url_request_delegate_; |
| 357 std::unique_ptr<net::URLRequest> request_; | 357 std::unique_ptr<net::URLRequest> request_; |
| 358 | 358 |
| 359 int expected_status_code_; | 359 int expected_status_code_; |
| 360 std::string expected_response_; | 360 std::string expected_response_; |
| 361 }; | 361 }; |
| 362 | 362 |
| 363 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { | 363 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { |
| 364 blob_data_->AppendData(kTestData1); | 364 blob_data_->AppendData(kTestData1); |
| 365 TestSuccessNonrangeRequest(kTestData1, arraysize(kTestData1) - 1); | 365 TestSuccessNonrangeRequest(kTestData1, arraysize(kTestData1) - 1); |
| 366 } | 366 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 std::string result; | 485 std::string result; |
| 486 BuildComplicatedData(&result); | 486 BuildComplicatedData(&result); |
| 487 net::HttpRequestHeaders extra_headers; | 487 net::HttpRequestHeaders extra_headers; |
| 488 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, | 488 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, |
| 489 net::HttpByteRange::Bounded(5, 10).GetHeaderValue()); | 489 net::HttpByteRange::Bounded(5, 10).GetHeaderValue()); |
| 490 expected_status_code_ = 206; | 490 expected_status_code_ = 206; |
| 491 expected_response_ = result.substr(5, 10 - 5 + 1); | 491 expected_response_ = result.substr(5, 10 - 5 + 1); |
| 492 TestRequest("GET", extra_headers); | 492 TestRequest("GET", extra_headers); |
| 493 | 493 |
| 494 EXPECT_EQ(6, request_->response_headers()->GetContentLength()); | 494 EXPECT_EQ(6, request_->response_headers()->GetContentLength()); |
| 495 EXPECT_FALSE(url_request_delegate_.metadata()); | 495 EXPECT_FALSE(request_->response_info().metadata); |
| 496 | 496 |
| 497 int64_t first = 0, last = 0, length = 0; | 497 int64_t first = 0, last = 0, length = 0; |
| 498 EXPECT_TRUE(request_->response_headers()->GetContentRangeFor206(&first, &last, | 498 EXPECT_TRUE(request_->response_headers()->GetContentRangeFor206(&first, &last, |
| 499 &length)); | 499 &length)); |
| 500 EXPECT_EQ(5, first); | 500 EXPECT_EQ(5, first); |
| 501 EXPECT_EQ(10, last); | 501 EXPECT_EQ(10, last); |
| 502 EXPECT_EQ(GetTotalBlobLength(), length); | 502 EXPECT_EQ(GetTotalBlobLength(), length); |
| 503 } | 503 } |
| 504 | 504 |
| 505 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest2) { | 505 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest2) { |
| 506 SetUpFileSystem(); | 506 SetUpFileSystem(); |
| 507 std::string result; | 507 std::string result; |
| 508 BuildComplicatedData(&result); | 508 BuildComplicatedData(&result); |
| 509 net::HttpRequestHeaders extra_headers; | 509 net::HttpRequestHeaders extra_headers; |
| 510 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, | 510 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, |
| 511 net::HttpByteRange::Suffix(10).GetHeaderValue()); | 511 net::HttpByteRange::Suffix(10).GetHeaderValue()); |
| 512 expected_status_code_ = 206; | 512 expected_status_code_ = 206; |
| 513 expected_response_ = result.substr(result.length() - 10); | 513 expected_response_ = result.substr(result.length() - 10); |
| 514 TestRequest("GET", extra_headers); | 514 TestRequest("GET", extra_headers); |
| 515 | 515 |
| 516 EXPECT_EQ(10, request_->response_headers()->GetContentLength()); | 516 EXPECT_EQ(10, request_->response_headers()->GetContentLength()); |
| 517 EXPECT_FALSE(url_request_delegate_.metadata()); | 517 EXPECT_FALSE(request_->response_info().metadata); |
| 518 | 518 |
| 519 int64_t total = GetTotalBlobLength(); | 519 int64_t total = GetTotalBlobLength(); |
| 520 int64_t first = 0, last = 0, length = 0; | 520 int64_t first = 0, last = 0, length = 0; |
| 521 EXPECT_TRUE(request_->response_headers()->GetContentRangeFor206(&first, &last, | 521 EXPECT_TRUE(request_->response_headers()->GetContentRangeFor206(&first, &last, |
| 522 &length)); | 522 &length)); |
| 523 EXPECT_EQ(total - 10, first); | 523 EXPECT_EQ(total - 10, first); |
| 524 EXPECT_EQ(total - 1, last); | 524 EXPECT_EQ(total - 1, last); |
| 525 EXPECT_EQ(total, length); | 525 EXPECT_EQ(total, length); |
| 526 } | 526 } |
| 527 | 527 |
| 528 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest3) { | 528 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest3) { |
| 529 SetUpFileSystem(); | 529 SetUpFileSystem(); |
| 530 std::string result; | 530 std::string result; |
| 531 BuildComplicatedData(&result); | 531 BuildComplicatedData(&result); |
| 532 net::HttpRequestHeaders extra_headers; | 532 net::HttpRequestHeaders extra_headers; |
| 533 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, | 533 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, |
| 534 net::HttpByteRange::Bounded(0, 2).GetHeaderValue()); | 534 net::HttpByteRange::Bounded(0, 2).GetHeaderValue()); |
| 535 expected_status_code_ = 206; | 535 expected_status_code_ = 206; |
| 536 expected_response_ = result.substr(0, 3); | 536 expected_response_ = result.substr(0, 3); |
| 537 TestRequest("GET", extra_headers); | 537 TestRequest("GET", extra_headers); |
| 538 | 538 |
| 539 EXPECT_EQ(3, request_->response_headers()->GetContentLength()); | 539 EXPECT_EQ(3, request_->response_headers()->GetContentLength()); |
| 540 EXPECT_FALSE(url_request_delegate_.metadata()); | 540 EXPECT_FALSE(request_->response_info().metadata); |
| 541 | 541 |
| 542 int64_t first = 0, last = 0, length = 0; | 542 int64_t first = 0, last = 0, length = 0; |
| 543 EXPECT_TRUE(request_->response_headers()->GetContentRangeFor206(&first, &last, | 543 EXPECT_TRUE(request_->response_headers()->GetContentRangeFor206(&first, &last, |
| 544 &length)); | 544 &length)); |
| 545 EXPECT_EQ(0, first); | 545 EXPECT_EQ(0, first); |
| 546 EXPECT_EQ(2, last); | 546 EXPECT_EQ(2, last); |
| 547 EXPECT_EQ(GetTotalBlobLength(), length); | 547 EXPECT_EQ(GetTotalBlobLength(), length); |
| 548 } | 548 } |
| 549 | 549 |
| 550 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { | 550 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { |
| 551 blob_data_->set_content_type(kTestContentType); | 551 blob_data_->set_content_type(kTestContentType); |
| 552 blob_data_->set_content_disposition(kTestContentDisposition); | 552 blob_data_->set_content_disposition(kTestContentDisposition); |
| 553 blob_data_->AppendData(kTestData1); | 553 blob_data_->AppendData(kTestData1); |
| 554 expected_status_code_ = 200; | 554 expected_status_code_ = 200; |
| 555 expected_response_ = kTestData1; | 555 expected_response_ = kTestData1; |
| 556 TestRequest("GET", net::HttpRequestHeaders()); | 556 TestRequest("GET", net::HttpRequestHeaders()); |
| 557 | 557 |
| 558 std::string content_type; | 558 std::string content_type; |
| 559 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); | 559 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
| 560 EXPECT_EQ(kTestContentType, content_type); | 560 EXPECT_EQ(kTestContentType, content_type); |
| 561 EXPECT_FALSE(url_request_delegate_.metadata()); | 561 EXPECT_FALSE(request_->response_info().metadata); |
| 562 size_t iter = 0; | 562 size_t iter = 0; |
| 563 std::string content_disposition; | 563 std::string content_disposition; |
| 564 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( | 564 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
| 565 &iter, "Content-Disposition", &content_disposition)); | 565 &iter, "Content-Disposition", &content_disposition)); |
| 566 EXPECT_EQ(kTestContentDisposition, content_disposition); | 566 EXPECT_EQ(kTestContentDisposition, content_disposition); |
| 567 } | 567 } |
| 568 | 568 |
| 569 TEST_F(BlobURLRequestJobTest, TestSideData) { | 569 TEST_F(BlobURLRequestJobTest, TestSideData) { |
| 570 disk_cache::ScopedEntryPtr disk_cache_entry_with_side_data = | 570 disk_cache::ScopedEntryPtr disk_cache_entry_with_side_data = |
| 571 CreateDiskCacheEntryWithSideData(disk_cache_backend_.get(), | 571 CreateDiskCacheEntryWithSideData(disk_cache_backend_.get(), |
| 572 kTestDiskCacheKey2, kTestDiskCacheData2, | 572 kTestDiskCacheKey2, kTestDiskCacheData2, |
| 573 kTestDiskCacheSideData); | 573 kTestDiskCacheSideData); |
| 574 blob_data_->AppendDiskCacheEntryWithSideData( | 574 blob_data_->AppendDiskCacheEntryWithSideData( |
| 575 new EmptyDataHandle(), disk_cache_entry_with_side_data.get(), | 575 new EmptyDataHandle(), disk_cache_entry_with_side_data.get(), |
| 576 kTestDiskCacheStreamIndex, kTestDiskCacheSideStreamIndex); | 576 kTestDiskCacheStreamIndex, kTestDiskCacheSideStreamIndex); |
| 577 expected_status_code_ = 200; | 577 expected_status_code_ = 200; |
| 578 expected_response_ = kTestDiskCacheData2; | 578 expected_response_ = kTestDiskCacheData2; |
| 579 TestRequest("GET", net::HttpRequestHeaders()); | 579 TestRequest("GET", net::HttpRequestHeaders()); |
| 580 EXPECT_EQ(static_cast<int>(arraysize(kTestDiskCacheData2) - 1), | 580 EXPECT_EQ(static_cast<int>(arraysize(kTestDiskCacheData2) - 1), |
| 581 request_->response_headers()->GetContentLength()); | 581 request_->response_headers()->GetContentLength()); |
| 582 | 582 |
| 583 EXPECT_TRUE(url_request_delegate_.metadata()); | 583 EXPECT_TRUE(request_->response_info().metadata); |
|
mattm
2017/04/04 22:06:34
While you're here, could change this to ASSERT_TRU
pwnall
2017/04/04 22:29:20
Done.
Good catch, thank you very much!
| |
| 584 std::string metadata(url_request_delegate_.metadata()->data(), | 584 std::string metadata(request_->response_info().metadata->data(), |
| 585 url_request_delegate_.metadata()->size()); | 585 request_->response_info().metadata->size()); |
| 586 EXPECT_EQ(std::string(kTestDiskCacheSideData), metadata); | 586 EXPECT_EQ(std::string(kTestDiskCacheSideData), metadata); |
| 587 } | 587 } |
| 588 | 588 |
| 589 TEST_F(BlobURLRequestJobTest, TestZeroSizeSideData) { | 589 TEST_F(BlobURLRequestJobTest, TestZeroSizeSideData) { |
| 590 disk_cache::ScopedEntryPtr disk_cache_entry_with_side_data = | 590 disk_cache::ScopedEntryPtr disk_cache_entry_with_side_data = |
| 591 CreateDiskCacheEntryWithSideData(disk_cache_backend_.get(), | 591 CreateDiskCacheEntryWithSideData(disk_cache_backend_.get(), |
| 592 kTestDiskCacheKey2, kTestDiskCacheData2, | 592 kTestDiskCacheKey2, kTestDiskCacheData2, |
| 593 ""); | 593 ""); |
| 594 blob_data_->AppendDiskCacheEntryWithSideData( | 594 blob_data_->AppendDiskCacheEntryWithSideData( |
| 595 new EmptyDataHandle(), disk_cache_entry_with_side_data.get(), | 595 new EmptyDataHandle(), disk_cache_entry_with_side_data.get(), |
| 596 kTestDiskCacheStreamIndex, kTestDiskCacheSideStreamIndex); | 596 kTestDiskCacheStreamIndex, kTestDiskCacheSideStreamIndex); |
| 597 expected_status_code_ = 200; | 597 expected_status_code_ = 200; |
| 598 expected_response_ = kTestDiskCacheData2; | 598 expected_response_ = kTestDiskCacheData2; |
| 599 TestRequest("GET", net::HttpRequestHeaders()); | 599 TestRequest("GET", net::HttpRequestHeaders()); |
| 600 EXPECT_EQ(static_cast<int>(arraysize(kTestDiskCacheData2) - 1), | 600 EXPECT_EQ(static_cast<int>(arraysize(kTestDiskCacheData2) - 1), |
| 601 request_->response_headers()->GetContentLength()); | 601 request_->response_headers()->GetContentLength()); |
| 602 | 602 |
| 603 EXPECT_FALSE(url_request_delegate_.metadata()); | 603 EXPECT_FALSE(request_->response_info().metadata); |
| 604 } | 604 } |
| 605 | 605 |
| 606 TEST_F(BlobURLRequestJobTest, BrokenBlob) { | 606 TEST_F(BlobURLRequestJobTest, BrokenBlob) { |
| 607 blob_handle_ = blob_context_.AddBrokenBlob( | 607 blob_handle_ = blob_context_.AddBrokenBlob( |
| 608 "uuid", "", "", storage::BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS); | 608 "uuid", "", "", storage::BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS); |
| 609 TestErrorRequest(500); | 609 TestErrorRequest(500); |
| 610 } | 610 } |
| 611 | 611 |
| 612 } // namespace content | 612 } // namespace content |
| OLD | NEW |