| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 | 501 |
| 502 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); | 502 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); |
| 503 int len = static_cast<int>(base::strlcpy(buf->data(), | 503 int len = static_cast<int>(base::strlcpy(buf->data(), |
| 504 "rg: 00-09 rg: 10-19 ", 100)); | 504 "rg: 00-09 rg: 10-19 ", 100)); |
| 505 TestCompletionCallback cb; | 505 TestCompletionCallback cb; |
| 506 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); | 506 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); |
| 507 EXPECT_EQ(len, cb.GetResult(rv)); | 507 EXPECT_EQ(len, cb.GetResult(rv)); |
| 508 entry->Close(); | 508 entry->Close(); |
| 509 } | 509 } |
| 510 | 510 |
| 511 // Verifies that there's an entry with this |key| with the truncated flag set to |
| 512 // |flag_value|, and with an optional |data_size| (if not zero). |
| 513 void VerifyTruncatedFlag(MockHttpCache* cache, |
| 514 const std::string& key, |
| 515 bool flag_value, |
| 516 int data_size) { |
| 517 disk_cache::Entry* entry; |
| 518 ASSERT_TRUE(cache->OpenBackendEntry(key, &entry)); |
| 519 disk_cache::ScopedEntryPtr closer(entry); |
| 520 |
| 521 HttpResponseInfo response; |
| 522 bool truncated = !flag_value; |
| 523 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 524 EXPECT_EQ(flag_value, truncated); |
| 525 if (data_size) |
| 526 EXPECT_EQ(data_size, entry->GetDataSize(1)); |
| 527 } |
| 528 |
| 511 // Helper to represent a network HTTP response. | 529 // Helper to represent a network HTTP response. |
| 512 struct Response { | 530 struct Response { |
| 513 // Set this response into |trans|. | 531 // Set this response into |trans|. |
| 514 void AssignTo(MockTransaction* trans) const { | 532 void AssignTo(MockTransaction* trans) const { |
| 515 trans->status = status; | 533 trans->status = status; |
| 516 trans->response_headers = headers; | 534 trans->response_headers = headers; |
| 517 trans->data = body; | 535 trans->data = body; |
| 518 } | 536 } |
| 519 | 537 |
| 520 std::string status_and_headers() const { | 538 std::string status_and_headers() const { |
| (...skipping 5111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5632 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5650 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5633 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 5651 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5634 | 5652 |
| 5635 RemoveMockTransaction(&transaction); | 5653 RemoveMockTransaction(&transaction); |
| 5636 } | 5654 } |
| 5637 | 5655 |
| 5638 // Tests that we mark an entry as incomplete when the request is cancelled. | 5656 // Tests that we mark an entry as incomplete when the request is cancelled. |
| 5639 TEST(HttpCache, SetTruncatedFlag) { | 5657 TEST(HttpCache, SetTruncatedFlag) { |
| 5640 MockHttpCache cache; | 5658 MockHttpCache cache; |
| 5641 | 5659 |
| 5642 MockTransaction transaction(kSimpleGET_Transaction); | 5660 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5643 transaction.response_headers = | 5661 transaction.response_headers = |
| 5644 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5662 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5645 "Content-Length: 22\n" | 5663 "Content-Length: 22\n" |
| 5646 "Etag: \"foopy\"\n"; | 5664 "Etag: \"foopy\"\n"; |
| 5647 AddMockTransaction(&transaction); | |
| 5648 MockHttpRequest request(transaction); | 5665 MockHttpRequest request(transaction); |
| 5649 | 5666 |
| 5650 scoped_ptr<Context> c(new Context()); | 5667 scoped_ptr<Context> c(new Context()); |
| 5651 | 5668 |
| 5652 int rv = cache.CreateTransaction(&c->trans); | 5669 int rv = cache.CreateTransaction(&c->trans); |
| 5653 ASSERT_EQ(OK, rv); | 5670 ASSERT_EQ(OK, rv); |
| 5654 | 5671 |
| 5655 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); | 5672 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5656 if (rv == ERR_IO_PENDING) | 5673 if (rv == ERR_IO_PENDING) |
| 5657 rv = c->callback.WaitForResult(); | 5674 rv = c->callback.WaitForResult(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 5679 MockHttpCache::SetTestMode(0); | 5696 MockHttpCache::SetTestMode(0); |
| 5680 | 5697 |
| 5681 | 5698 |
| 5682 // Make sure that we don't invoke the callback. We may have an issue if the | 5699 // Make sure that we don't invoke the callback. We may have an issue if the |
| 5683 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we | 5700 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 5684 // could end up with the transaction being deleted twice if we send any | 5701 // could end up with the transaction being deleted twice if we send any |
| 5685 // notification from the transaction destructor (see http://crbug.com/31723). | 5702 // notification from the transaction destructor (see http://crbug.com/31723). |
| 5686 EXPECT_FALSE(c->callback.have_result()); | 5703 EXPECT_FALSE(c->callback.have_result()); |
| 5687 | 5704 |
| 5688 // Verify that the entry is marked as incomplete. | 5705 // Verify that the entry is marked as incomplete. |
| 5689 disk_cache::Entry* entry; | 5706 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 5690 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 5691 HttpResponseInfo response; | |
| 5692 bool truncated = false; | |
| 5693 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 5694 EXPECT_TRUE(truncated); | |
| 5695 entry->Close(); | |
| 5696 | |
| 5697 RemoveMockTransaction(&transaction); | |
| 5698 } | 5707 } |
| 5699 | 5708 |
| 5700 // Tests that we don't mark an entry as truncated when we read everything. | 5709 // Tests that we don't mark an entry as truncated when we read everything. |
| 5701 TEST(HttpCache, DontSetTruncatedFlag) { | 5710 TEST(HttpCache, DontSetTruncatedFlag) { |
| 5702 MockHttpCache cache; | 5711 MockHttpCache cache; |
| 5703 | 5712 |
| 5704 MockTransaction transaction(kSimpleGET_Transaction); | 5713 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5705 transaction.response_headers = | 5714 transaction.response_headers = |
| 5706 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5715 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5707 "Content-Length: 22\n" | 5716 "Content-Length: 22\n" |
| 5708 "Etag: \"foopy\"\n"; | 5717 "Etag: \"foopy\"\n"; |
| 5709 AddMockTransaction(&transaction); | |
| 5710 MockHttpRequest request(transaction); | 5718 MockHttpRequest request(transaction); |
| 5711 | 5719 |
| 5712 scoped_ptr<Context> c(new Context()); | 5720 scoped_ptr<Context> c(new Context()); |
| 5713 int rv = cache.CreateTransaction(&c->trans); | 5721 int rv = cache.CreateTransaction(&c->trans); |
| 5714 ASSERT_EQ(OK, rv); | 5722 ASSERT_EQ(OK, rv); |
| 5715 | 5723 |
| 5716 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); | 5724 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5717 EXPECT_EQ(OK, c->callback.GetResult(rv)); | 5725 EXPECT_EQ(OK, c->callback.GetResult(rv)); |
| 5718 | 5726 |
| 5719 // Read everything. | 5727 // Read everything. |
| 5720 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); | 5728 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); |
| 5721 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5729 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5722 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); | 5730 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); |
| 5723 | 5731 |
| 5724 // Destroy the transaction. | 5732 // Destroy the transaction. |
| 5725 c->trans.reset(); | 5733 c->trans.reset(); |
| 5726 | 5734 |
| 5727 // Verify that the entry is not marked as truncated. | 5735 // Verify that the entry is not marked as truncated. |
| 5728 disk_cache::Entry* entry; | 5736 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); |
| 5729 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | 5737 } |
| 5730 HttpResponseInfo response; | |
| 5731 bool truncated = true; | |
| 5732 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 5733 EXPECT_FALSE(truncated); | |
| 5734 entry->Close(); | |
| 5735 | 5738 |
| 5736 RemoveMockTransaction(&transaction); | 5739 // Tests that sparse entries don't set the truncate flag. |
| 5740 TEST(HttpCache, RangeGET_DontTruncate) { |
| 5741 MockHttpCache cache; |
| 5742 |
| 5743 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5744 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; |
| 5745 |
| 5746 scoped_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5747 scoped_ptr<HttpTransaction> trans; |
| 5748 |
| 5749 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5750 EXPECT_EQ(OK, rv); |
| 5751 |
| 5752 TestCompletionCallback cb; |
| 5753 rv = trans->Start(request.get(), cb.callback(), BoundNetLog()); |
| 5754 EXPECT_EQ(0, cb.GetResult(rv)); |
| 5755 |
| 5756 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5757 rv = trans->Read(buf.get(), 10, cb.callback()); |
| 5758 EXPECT_EQ(10, cb.GetResult(rv)); |
| 5759 |
| 5760 // Should not trigger any DCHECK. |
| 5761 trans.reset(); |
| 5762 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5763 } |
| 5764 |
| 5765 // Tests that sparse entries don't set the truncate flag (when the byte range |
| 5766 // starts after 0). |
| 5767 TEST(HttpCache, RangeGET_DontTruncate2) { |
| 5768 MockHttpCache cache; |
| 5769 |
| 5770 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5771 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; |
| 5772 |
| 5773 scoped_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5774 scoped_ptr<HttpTransaction> trans; |
| 5775 |
| 5776 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5777 EXPECT_EQ(OK, rv); |
| 5778 |
| 5779 TestCompletionCallback cb; |
| 5780 rv = trans->Start(request.get(), cb.callback(), BoundNetLog()); |
| 5781 EXPECT_EQ(0, cb.GetResult(rv)); |
| 5782 |
| 5783 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5784 rv = trans->Read(buf.get(), 10, cb.callback()); |
| 5785 EXPECT_EQ(10, cb.GetResult(rv)); |
| 5786 |
| 5787 // Should not trigger any DCHECK. |
| 5788 trans.reset(); |
| 5789 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5737 } | 5790 } |
| 5738 | 5791 |
| 5739 // Tests that we can continue with a request that was interrupted. | 5792 // Tests that we can continue with a request that was interrupted. |
| 5740 TEST(HttpCache, GET_IncompleteResource) { | 5793 TEST(HttpCache, GET_IncompleteResource) { |
| 5741 MockHttpCache cache; | 5794 MockHttpCache cache; |
| 5742 AddMockTransaction(&kRangeGET_TransactionOK); | 5795 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5743 | 5796 |
| 5744 std::string raw_headers("HTTP/1.1 200 OK\n" | 5797 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5745 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5798 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5746 "ETag: \"foo\"\n" | 5799 "ETag: \"foo\"\n" |
| 5747 "Accept-Ranges: bytes\n" | 5800 "Accept-Ranges: bytes\n" |
| 5748 "Content-Length: 80\n"); | 5801 "Content-Length: 80\n"); |
| 5749 CreateTruncatedEntry(raw_headers, &cache); | 5802 CreateTruncatedEntry(raw_headers, &cache); |
| 5750 | 5803 |
| 5751 // Now make a regular request. | 5804 // Now make a regular request. |
| 5752 std::string headers; | 5805 std::string headers; |
| 5753 MockTransaction transaction(kRangeGET_TransactionOK); | |
| 5754 transaction.request_headers = EXTRA_HEADER; | 5806 transaction.request_headers = EXTRA_HEADER; |
| 5755 transaction.data = kFullRangeData; | 5807 transaction.data = kFullRangeData; |
| 5756 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 5808 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5757 | 5809 |
| 5758 // We update the headers with the ones received while revalidating. | 5810 // We update the headers with the ones received while revalidating. |
| 5759 std::string expected_headers( | 5811 std::string expected_headers( |
| 5760 "HTTP/1.1 200 OK\n" | 5812 "HTTP/1.1 200 OK\n" |
| 5761 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5813 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5762 "Accept-Ranges: bytes\n" | 5814 "Accept-Ranges: bytes\n" |
| 5763 "ETag: \"foo\"\n" | 5815 "ETag: \"foo\"\n" |
| 5764 "Content-Length: 80\n"); | 5816 "Content-Length: 80\n"); |
| 5765 | 5817 |
| 5766 EXPECT_EQ(expected_headers, headers); | 5818 EXPECT_EQ(expected_headers, headers); |
| 5767 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5819 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5768 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5820 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5769 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5821 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5770 | 5822 |
| 5771 // Verify that the disk entry was updated. | 5823 // Verify that the disk entry was updated. |
| 5772 disk_cache::Entry* entry; | 5824 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
| 5773 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 5774 EXPECT_EQ(80, entry->GetDataSize(1)); | |
| 5775 bool truncated = true; | |
| 5776 HttpResponseInfo response; | |
| 5777 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 5778 EXPECT_FALSE(truncated); | |
| 5779 entry->Close(); | |
| 5780 | |
| 5781 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
| 5782 } | 5825 } |
| 5783 | 5826 |
| 5784 // Tests the handling of no-store when revalidating a truncated entry. | 5827 // Tests the handling of no-store when revalidating a truncated entry. |
| 5785 TEST(HttpCache, GET_IncompleteResource_NoStore) { | 5828 TEST(HttpCache, GET_IncompleteResource_NoStore) { |
| 5786 MockHttpCache cache; | 5829 MockHttpCache cache; |
| 5787 AddMockTransaction(&kRangeGET_TransactionOK); | 5830 AddMockTransaction(&kRangeGET_TransactionOK); |
| 5788 | 5831 |
| 5789 std::string raw_headers("HTTP/1.1 200 OK\n" | 5832 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5790 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5833 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5791 "ETag: \"foo\"\n" | 5834 "ETag: \"foo\"\n" |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6037 if (rv == ERR_IO_PENDING) | 6080 if (rv == ERR_IO_PENDING) |
| 6038 rv = c->callback.WaitForResult(); | 6081 rv = c->callback.WaitForResult(); |
| 6039 std::string content; | 6082 std::string content; |
| 6040 rv = ReadTransaction(c->trans.get(), &content); | 6083 rv = ReadTransaction(c->trans.get(), &content); |
| 6041 EXPECT_EQ(ERR_CACHE_AUTH_FAILURE_AFTER_READ, rv); | 6084 EXPECT_EQ(ERR_CACHE_AUTH_FAILURE_AFTER_READ, rv); |
| 6042 } | 6085 } |
| 6043 | 6086 |
| 6044 // Tests that we cache a 200 response to the validation request. | 6087 // Tests that we cache a 200 response to the validation request. |
| 6045 TEST(HttpCache, GET_IncompleteResource4) { | 6088 TEST(HttpCache, GET_IncompleteResource4) { |
| 6046 MockHttpCache cache; | 6089 MockHttpCache cache; |
| 6047 AddMockTransaction(&kRangeGET_TransactionOK); | 6090 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 6048 | 6091 |
| 6049 std::string raw_headers("HTTP/1.1 200 OK\n" | 6092 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6050 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | 6093 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 6051 "ETag: \"foo\"\n" | 6094 "ETag: \"foo\"\n" |
| 6052 "Accept-Ranges: bytes\n" | 6095 "Accept-Ranges: bytes\n" |
| 6053 "Content-Length: 80\n"); | 6096 "Content-Length: 80\n"); |
| 6054 CreateTruncatedEntry(raw_headers, &cache); | 6097 CreateTruncatedEntry(raw_headers, &cache); |
| 6055 | 6098 |
| 6056 // Now make a regular request. | 6099 // Now make a regular request. |
| 6057 std::string headers; | 6100 std::string headers; |
| 6058 MockTransaction transaction(kRangeGET_TransactionOK); | |
| 6059 transaction.request_headers = EXTRA_HEADER; | 6101 transaction.request_headers = EXTRA_HEADER; |
| 6060 transaction.data = "Not a range"; | 6102 transaction.data = "Not a range"; |
| 6061 RangeTransactionServer handler; | 6103 RangeTransactionServer handler; |
| 6062 handler.set_bad_200(true); | 6104 handler.set_bad_200(true); |
| 6063 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 6105 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 6064 | 6106 |
| 6065 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6107 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6066 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6108 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6067 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6109 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6068 | 6110 |
| 6069 // Verify that the disk entry was updated. | 6111 // Verify that the disk entry was updated. |
| 6070 disk_cache::Entry* entry; | 6112 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 11); |
| 6071 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 6072 EXPECT_EQ(11, entry->GetDataSize(1)); | |
| 6073 bool truncated = true; | |
| 6074 HttpResponseInfo response; | |
| 6075 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6076 EXPECT_FALSE(truncated); | |
| 6077 entry->Close(); | |
| 6078 | |
| 6079 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
| 6080 } | 6113 } |
| 6081 | 6114 |
| 6082 // Tests that when we cancel a request that was interrupted, we mark it again | 6115 // Tests that when we cancel a request that was interrupted, we mark it again |
| 6083 // as truncated. | 6116 // as truncated. |
| 6084 TEST(HttpCache, GET_CancelIncompleteResource) { | 6117 TEST(HttpCache, GET_CancelIncompleteResource) { |
| 6085 MockHttpCache cache; | 6118 MockHttpCache cache; |
| 6086 AddMockTransaction(&kRangeGET_TransactionOK); | 6119 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 6087 | 6120 |
| 6088 std::string raw_headers("HTTP/1.1 200 OK\n" | 6121 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6089 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | 6122 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 6090 "ETag: \"foo\"\n" | 6123 "ETag: \"foo\"\n" |
| 6091 "Accept-Ranges: bytes\n" | 6124 "Accept-Ranges: bytes\n" |
| 6092 "Content-Length: 80\n"); | 6125 "Content-Length: 80\n"); |
| 6093 CreateTruncatedEntry(raw_headers, &cache); | 6126 CreateTruncatedEntry(raw_headers, &cache); |
| 6094 | 6127 |
| 6095 // Now make a regular request. | 6128 // Now make a regular request. |
| 6096 MockTransaction transaction(kRangeGET_TransactionOK); | |
| 6097 transaction.request_headers = EXTRA_HEADER; | 6129 transaction.request_headers = EXTRA_HEADER; |
| 6098 | 6130 |
| 6099 MockHttpRequest request(transaction); | 6131 MockHttpRequest request(transaction); |
| 6100 Context* c = new Context(); | 6132 Context* c = new Context(); |
| 6101 int rv = cache.CreateTransaction(&c->trans); | 6133 int rv = cache.CreateTransaction(&c->trans); |
| 6102 ASSERT_EQ(OK, rv); | 6134 ASSERT_EQ(OK, rv); |
| 6103 | 6135 |
| 6104 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); | 6136 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 6105 EXPECT_EQ(OK, c->callback.GetResult(rv)); | 6137 EXPECT_EQ(OK, c->callback.GetResult(rv)); |
| 6106 | 6138 |
| 6107 // Read 20 bytes from the cache, and 10 from the net. | 6139 // Read 20 bytes from the cache, and 10 from the net. |
| 6108 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); | 6140 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); |
| 6109 rv = c->trans->Read(buf.get(), 20, c->callback.callback()); | 6141 rv = c->trans->Read(buf.get(), 20, c->callback.callback()); |
| 6110 EXPECT_EQ(20, c->callback.GetResult(rv)); | 6142 EXPECT_EQ(20, c->callback.GetResult(rv)); |
| 6111 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); | 6143 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
| 6112 EXPECT_EQ(10, c->callback.GetResult(rv)); | 6144 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 6113 | 6145 |
| 6114 // At this point, we are already reading so canceling the request should leave | 6146 // At this point, we are already reading so canceling the request should leave |
| 6115 // a truncated one. | 6147 // a truncated one. |
| 6116 delete c; | 6148 delete c; |
| 6117 | 6149 |
| 6118 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 6150 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6119 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6151 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6120 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6152 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6121 | 6153 |
| 6122 // Verify that the disk entry was updated: now we have 30 bytes. | 6154 // Verify that the disk entry was updated: now we have 30 bytes. |
| 6123 disk_cache::Entry* entry; | 6155 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30); |
| 6124 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 6125 EXPECT_EQ(30, entry->GetDataSize(1)); | |
| 6126 bool truncated = false; | |
| 6127 HttpResponseInfo response; | |
| 6128 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6129 EXPECT_TRUE(truncated); | |
| 6130 entry->Close(); | |
| 6131 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
| 6132 } | 6156 } |
| 6133 | 6157 |
| 6134 // Tests that we can handle range requests when we have a truncated entry. | 6158 // Tests that we can handle range requests when we have a truncated entry. |
| 6135 TEST(HttpCache, RangeGET_IncompleteResource) { | 6159 TEST(HttpCache, RangeGET_IncompleteResource) { |
| 6136 MockHttpCache cache; | 6160 MockHttpCache cache; |
| 6137 AddMockTransaction(&kRangeGET_TransactionOK); | 6161 AddMockTransaction(&kRangeGET_TransactionOK); |
| 6138 | 6162 |
| 6139 // Content-length will be intentionally bogus. | 6163 // Content-length will be intentionally bogus. |
| 6140 std::string raw_headers("HTTP/1.1 200 OK\n" | 6164 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6141 "Last-Modified: something\n" | 6165 "Last-Modified: something\n" |
| (...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6868 TEST(HttpCache, StopCachingSavesEntry) { | 6892 TEST(HttpCache, StopCachingSavesEntry) { |
| 6869 MockHttpCache cache; | 6893 MockHttpCache cache; |
| 6870 TestCompletionCallback callback; | 6894 TestCompletionCallback callback; |
| 6871 MockHttpRequest request(kSimpleGET_Transaction); | 6895 MockHttpRequest request(kSimpleGET_Transaction); |
| 6872 | 6896 |
| 6873 { | 6897 { |
| 6874 scoped_ptr<HttpTransaction> trans; | 6898 scoped_ptr<HttpTransaction> trans; |
| 6875 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); | 6899 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
| 6876 | 6900 |
| 6877 // Force a response that can be resumed. | 6901 // Force a response that can be resumed. |
| 6878 MockTransaction mock_transaction(kSimpleGET_Transaction); | 6902 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); |
| 6879 AddMockTransaction(&mock_transaction); | 6903 AddMockTransaction(&mock_transaction); |
| 6880 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" | 6904 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 6881 "Content-Length: 42\n" | 6905 "Content-Length: 42\n" |
| 6882 "Etag: \"foo\"\n"; | 6906 "Etag: \"foo\"\n"; |
| 6883 | 6907 |
| 6884 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 6908 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6885 EXPECT_EQ(OK, callback.GetResult(rv)); | 6909 EXPECT_EQ(OK, callback.GetResult(rv)); |
| 6886 | 6910 |
| 6887 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 6911 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 6888 rv = trans->Read(buf.get(), 10, callback.callback()); | 6912 rv = trans->Read(buf.get(), 10, callback.callback()); |
| 6889 EXPECT_EQ(callback.GetResult(rv), 10); | 6913 EXPECT_EQ(callback.GetResult(rv), 10); |
| 6890 | 6914 |
| 6891 trans->StopCaching(); | 6915 trans->StopCaching(); |
| 6892 | 6916 |
| 6893 // We should be able to keep reading. | 6917 // We should be able to keep reading. |
| 6894 rv = trans->Read(buf.get(), 256, callback.callback()); | 6918 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6895 EXPECT_GT(callback.GetResult(rv), 0); | 6919 EXPECT_GT(callback.GetResult(rv), 0); |
| 6896 rv = trans->Read(buf.get(), 256, callback.callback()); | 6920 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6897 EXPECT_EQ(callback.GetResult(rv), 0); | 6921 EXPECT_EQ(callback.GetResult(rv), 0); |
| 6898 | |
| 6899 RemoveMockTransaction(&mock_transaction); | |
| 6900 } | 6922 } |
| 6901 | 6923 |
| 6902 // Verify that the entry is marked as incomplete. | 6924 // Verify that the entry is marked as incomplete. |
| 6903 disk_cache::Entry* entry; | 6925 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 6904 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 6905 HttpResponseInfo response; | |
| 6906 bool truncated = false; | |
| 6907 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6908 EXPECT_TRUE(truncated); | |
| 6909 entry->Close(); | |
| 6910 } | 6926 } |
| 6911 | 6927 |
| 6912 // Tests that we handle truncated enries when StopCaching is called. | 6928 // Tests that we handle truncated enries when StopCaching is called. |
| 6913 TEST(HttpCache, StopCachingTruncatedEntry) { | 6929 TEST(HttpCache, StopCachingTruncatedEntry) { |
| 6914 MockHttpCache cache; | 6930 MockHttpCache cache; |
| 6915 TestCompletionCallback callback; | 6931 TestCompletionCallback callback; |
| 6916 MockHttpRequest request(kRangeGET_TransactionOK); | 6932 MockHttpRequest request(kRangeGET_TransactionOK); |
| 6917 request.extra_headers.Clear(); | 6933 request.extra_headers.Clear(); |
| 6918 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); | 6934 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); |
| 6919 AddMockTransaction(&kRangeGET_TransactionOK); | 6935 AddMockTransaction(&kRangeGET_TransactionOK); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 6943 // We should be able to keep reading. | 6959 // We should be able to keep reading. |
| 6944 rv = trans->Read(buf.get(), 256, callback.callback()); | 6960 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6945 EXPECT_GT(callback.GetResult(rv), 0); | 6961 EXPECT_GT(callback.GetResult(rv), 0); |
| 6946 rv = trans->Read(buf.get(), 256, callback.callback()); | 6962 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6947 EXPECT_GT(callback.GetResult(rv), 0); | 6963 EXPECT_GT(callback.GetResult(rv), 0); |
| 6948 rv = trans->Read(buf.get(), 256, callback.callback()); | 6964 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6949 EXPECT_EQ(callback.GetResult(rv), 0); | 6965 EXPECT_EQ(callback.GetResult(rv), 0); |
| 6950 } | 6966 } |
| 6951 | 6967 |
| 6952 // Verify that the disk entry was updated. | 6968 // Verify that the disk entry was updated. |
| 6953 disk_cache::Entry* entry; | 6969 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
| 6954 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 6955 EXPECT_EQ(80, entry->GetDataSize(1)); | |
| 6956 bool truncated = true; | |
| 6957 HttpResponseInfo response; | |
| 6958 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6959 EXPECT_FALSE(truncated); | |
| 6960 entry->Close(); | |
| 6961 | |
| 6962 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6970 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6963 } | 6971 } |
| 6964 | 6972 |
| 6965 // Tests that we detect truncated resources from the net when there is | 6973 // Tests that we detect truncated resources from the net when there is |
| 6966 // a Content-Length header. | 6974 // a Content-Length header. |
| 6967 TEST(HttpCache, TruncatedByContentLength) { | 6975 TEST(HttpCache, TruncatedByContentLength) { |
| 6968 MockHttpCache cache; | 6976 MockHttpCache cache; |
| 6969 TestCompletionCallback callback; | 6977 TestCompletionCallback callback; |
| 6970 | 6978 |
| 6971 MockTransaction transaction(kSimpleGET_Transaction); | 6979 MockTransaction transaction(kSimpleGET_Transaction); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 6991 | 6999 |
| 6992 MockTransaction transaction(kSimpleGET_Transaction); | 7000 MockTransaction transaction(kSimpleGET_Transaction); |
| 6993 AddMockTransaction(&transaction); | 7001 AddMockTransaction(&transaction); |
| 6994 transaction.response_headers = "Cache-Control: max-age=10000\n" | 7002 transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 6995 "Content-Length: 100\n" | 7003 "Content-Length: 100\n" |
| 6996 "Etag: \"foo\"\n"; | 7004 "Etag: \"foo\"\n"; |
| 6997 RunTransactionTest(cache.http_cache(), transaction); | 7005 RunTransactionTest(cache.http_cache(), transaction); |
| 6998 RemoveMockTransaction(&transaction); | 7006 RemoveMockTransaction(&transaction); |
| 6999 | 7007 |
| 7000 // Verify that the entry is marked as incomplete. | 7008 // Verify that the entry is marked as incomplete. |
| 7001 disk_cache::Entry* entry; | 7009 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 7002 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 7003 HttpResponseInfo response; | |
| 7004 bool truncated = false; | |
| 7005 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 7006 EXPECT_TRUE(truncated); | |
| 7007 entry->Close(); | |
| 7008 } | 7010 } |
| 7009 | 7011 |
| 7010 // Make sure that calling SetPriority on a cache transaction passes on | 7012 // Make sure that calling SetPriority on a cache transaction passes on |
| 7011 // its priority updates to its underlying network transaction. | 7013 // its priority updates to its underlying network transaction. |
| 7012 TEST(HttpCache, SetPriority) { | 7014 TEST(HttpCache, SetPriority) { |
| 7013 MockHttpCache cache; | 7015 MockHttpCache cache; |
| 7014 | 7016 |
| 7015 scoped_ptr<HttpTransaction> trans; | 7017 scoped_ptr<HttpTransaction> trans; |
| 7016 ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans)); | 7018 ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans)); |
| 7017 | 7019 |
| (...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7647 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 7649 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7648 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7650 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7649 EXPECT_TRUE(response_info.was_cached); | 7651 EXPECT_TRUE(response_info.was_cached); |
| 7650 | 7652 |
| 7651 // The new SSL state is reported. | 7653 // The new SSL state is reported. |
| 7652 EXPECT_EQ(status2, response_info.ssl_info.connection_status); | 7654 EXPECT_EQ(status2, response_info.ssl_info.connection_status); |
| 7653 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); | 7655 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); |
| 7654 } | 7656 } |
| 7655 | 7657 |
| 7656 } // namespace net | 7658 } // namespace net |
| OLD | NEW |