OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/hash_tables.h" | 8 #include "base/hash_tables.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 5121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5132 MessageLoop::current()->RunAllPending(); | 5132 MessageLoop::current()->RunAllPending(); |
5133 | 5133 |
5134 // Read from the cache. | 5134 // Read from the cache. |
5135 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 5135 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
5136 | 5136 |
5137 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5137 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
5138 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5138 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
5139 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5139 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
5140 } | 5140 } |
5141 | 5141 |
| 5142 // Tests that we stop cachining when told. |
| 5143 TEST(HttpCache, StopCachingDeletesEntry) { |
| 5144 MockHttpCache cache; |
| 5145 TestOldCompletionCallback callback; |
| 5146 MockHttpRequest request(kSimpleGET_Transaction); |
| 5147 |
| 5148 { |
| 5149 scoped_ptr<net::HttpTransaction> trans; |
| 5150 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 5151 EXPECT_EQ(net::OK, rv); |
| 5152 |
| 5153 rv = trans->Start(&request, &callback, net::BoundNetLog()); |
| 5154 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 5155 |
| 5156 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
| 5157 rv = trans->Read(buf, 10, &callback); |
| 5158 EXPECT_EQ(callback.GetResult(rv), 10); |
| 5159 |
| 5160 trans->StopCaching(); |
| 5161 |
| 5162 // We should be able to keep reading. |
| 5163 rv = trans->Read(buf, 256, &callback); |
| 5164 EXPECT_GT(callback.GetResult(rv), 0); |
| 5165 rv = trans->Read(buf, 256, &callback); |
| 5166 EXPECT_EQ(callback.GetResult(rv), 0); |
| 5167 } |
| 5168 |
| 5169 // Make sure that the ActiveEntry is gone. |
| 5170 MessageLoop::current()->RunAllPending(); |
| 5171 |
| 5172 // Verify that the entry is gone. |
| 5173 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5174 |
| 5175 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5176 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5177 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5178 } |
| 5179 |
| 5180 // Tests that when we are told to stop caching we don't throw away valid data. |
| 5181 TEST(HttpCache, StopCachingSavesEntry) { |
| 5182 MockHttpCache cache; |
| 5183 TestOldCompletionCallback callback; |
| 5184 MockHttpRequest request(kSimpleGET_Transaction); |
| 5185 |
| 5186 { |
| 5187 scoped_ptr<net::HttpTransaction> trans; |
| 5188 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 5189 EXPECT_EQ(net::OK, rv); |
| 5190 |
| 5191 // Force a response that can be resumed. |
| 5192 MockTransaction mock_transaction(kSimpleGET_Transaction); |
| 5193 AddMockTransaction(&mock_transaction); |
| 5194 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 5195 "Content-Length: 42\n" |
| 5196 "Etag: foo\n"; |
| 5197 |
| 5198 rv = trans->Start(&request, &callback, net::BoundNetLog()); |
| 5199 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 5200 |
| 5201 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
| 5202 rv = trans->Read(buf, 10, &callback); |
| 5203 EXPECT_EQ(callback.GetResult(rv), 10); |
| 5204 |
| 5205 trans->StopCaching(); |
| 5206 |
| 5207 // We should be able to keep reading. |
| 5208 rv = trans->Read(buf, 256, &callback); |
| 5209 EXPECT_GT(callback.GetResult(rv), 0); |
| 5210 rv = trans->Read(buf, 256, &callback); |
| 5211 EXPECT_EQ(callback.GetResult(rv), 0); |
| 5212 |
| 5213 RemoveMockTransaction(&mock_transaction); |
| 5214 } |
| 5215 |
| 5216 // Verify that the entry is marked as incomplete. |
| 5217 disk_cache::Entry* entry; |
| 5218 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 5219 net::HttpResponseInfo response; |
| 5220 bool truncated = false; |
| 5221 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 5222 EXPECT_TRUE(truncated); |
| 5223 entry->Close(); |
| 5224 } |
| 5225 |
5142 // Tests that we detect truncated rersources from the net when there is | 5226 // Tests that we detect truncated rersources from the net when there is |
5143 // a Content-Length header. | 5227 // a Content-Length header. |
5144 TEST(HttpCache, TruncatedByContentLength) { | 5228 TEST(HttpCache, TruncatedByContentLength) { |
5145 MockHttpCache cache; | 5229 MockHttpCache cache; |
5146 TestOldCompletionCallback callback; | 5230 TestOldCompletionCallback callback; |
5147 | 5231 |
5148 MockTransaction transaction(kSimpleGET_Transaction); | 5232 MockTransaction transaction(kSimpleGET_Transaction); |
5149 AddMockTransaction(&transaction); | 5233 AddMockTransaction(&transaction); |
5150 transaction.response_headers = "Cache-Control: max-age=10000\n" | 5234 transaction.response_headers = "Cache-Control: max-age=10000\n" |
5151 "Content-Length: 100\n"; | 5235 "Content-Length: 100\n"; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5214 new net::DiskCacheBasedSSLHostInfo("https://www.verisign.com", ssl_config, | 5298 new net::DiskCacheBasedSSLHostInfo("https://www.verisign.com", ssl_config, |
5215 &cert_verifier, cache.http_cache()); | 5299 &cert_verifier, cache.http_cache()); |
5216 ssl_host_info->Start(); | 5300 ssl_host_info->Start(); |
5217 DeleteSSLHostInfoOldCompletionCallback callback(ssl_host_info); | 5301 DeleteSSLHostInfoOldCompletionCallback callback(ssl_host_info); |
5218 int rv = ssl_host_info->WaitForDataReady(&callback); | 5302 int rv = ssl_host_info->WaitForDataReady(&callback); |
5219 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 5303 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
5220 // Now complete the backend creation and let the callback run. | 5304 // Now complete the backend creation and let the callback run. |
5221 factory->FinishCreation(); | 5305 factory->FinishCreation(); |
5222 EXPECT_EQ(net::OK, callback.GetResult(rv)); | 5306 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
5223 } | 5307 } |
OLD | NEW |