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> |
| 8 |
7 #include <algorithm> | 9 #include <algorithm> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
11 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
16 #include "base/test/simple_test_clock.h" | 18 #include "base/test/simple_test_clock.h" |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 std::string expected(trans_info.data); | 131 std::string expected(trans_info.data); |
130 EXPECT_EQ(expected, content); | 132 EXPECT_EQ(expected, content); |
131 } | 133 } |
132 | 134 |
133 void RunTransactionTestBase(HttpCache* cache, | 135 void RunTransactionTestBase(HttpCache* cache, |
134 const MockTransaction& trans_info, | 136 const MockTransaction& trans_info, |
135 const MockHttpRequest& request, | 137 const MockHttpRequest& request, |
136 HttpResponseInfo* response_info, | 138 HttpResponseInfo* response_info, |
137 const BoundNetLog& net_log, | 139 const BoundNetLog& net_log, |
138 LoadTimingInfo* load_timing_info, | 140 LoadTimingInfo* load_timing_info, |
139 int64* received_bytes) { | 141 int64_t* sent_bytes, |
| 142 int64_t* received_bytes) { |
140 TestCompletionCallback callback; | 143 TestCompletionCallback callback; |
141 | 144 |
142 // write to the cache | 145 // write to the cache |
143 | 146 |
144 scoped_ptr<HttpTransaction> trans; | 147 scoped_ptr<HttpTransaction> trans; |
145 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); | 148 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); |
146 EXPECT_EQ(OK, rv); | 149 EXPECT_EQ(OK, rv); |
147 ASSERT_TRUE(trans.get()); | 150 ASSERT_TRUE(trans.get()); |
148 | 151 |
149 rv = trans->Start(&request, callback.callback(), net_log); | 152 rv = trans->Start(&request, callback.callback(), net_log); |
(...skipping 13 matching lines...) Expand all Loading... |
163 if (load_timing_info) { | 166 if (load_timing_info) { |
164 // If a fake network connection is used, need a NetLog to get a fake socket | 167 // If a fake network connection is used, need a NetLog to get a fake socket |
165 // ID. | 168 // ID. |
166 EXPECT_TRUE(net_log.net_log()); | 169 EXPECT_TRUE(net_log.net_log()); |
167 *load_timing_info = LoadTimingInfo(); | 170 *load_timing_info = LoadTimingInfo(); |
168 trans->GetLoadTimingInfo(load_timing_info); | 171 trans->GetLoadTimingInfo(load_timing_info); |
169 } | 172 } |
170 | 173 |
171 ReadAndVerifyTransaction(trans.get(), trans_info); | 174 ReadAndVerifyTransaction(trans.get(), trans_info); |
172 | 175 |
| 176 if (sent_bytes) |
| 177 *sent_bytes = trans->GetTotalSentBytes(); |
173 if (received_bytes) | 178 if (received_bytes) |
174 *received_bytes = trans->GetTotalReceivedBytes(); | 179 *received_bytes = trans->GetTotalReceivedBytes(); |
175 } | 180 } |
176 | 181 |
177 void RunTransactionTestWithRequest(HttpCache* cache, | 182 void RunTransactionTestWithRequest(HttpCache* cache, |
178 const MockTransaction& trans_info, | 183 const MockTransaction& trans_info, |
179 const MockHttpRequest& request, | 184 const MockHttpRequest& request, |
180 HttpResponseInfo* response_info) { | 185 HttpResponseInfo* response_info) { |
181 RunTransactionTestBase(cache, trans_info, request, response_info, | 186 RunTransactionTestBase(cache, trans_info, request, response_info, |
182 BoundNetLog(), NULL, NULL); | 187 BoundNetLog(), nullptr, nullptr, nullptr); |
183 } | 188 } |
184 | 189 |
185 void RunTransactionTestAndGetTiming(HttpCache* cache, | 190 void RunTransactionTestAndGetTiming(HttpCache* cache, |
186 const MockTransaction& trans_info, | 191 const MockTransaction& trans_info, |
187 const BoundNetLog& log, | 192 const BoundNetLog& log, |
188 LoadTimingInfo* load_timing_info) { | 193 LoadTimingInfo* load_timing_info) { |
189 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | 194 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
190 NULL, log, load_timing_info, NULL); | 195 nullptr, log, load_timing_info, nullptr, nullptr); |
191 } | 196 } |
192 | 197 |
193 void RunTransactionTest(HttpCache* cache, const MockTransaction& trans_info) { | 198 void RunTransactionTest(HttpCache* cache, const MockTransaction& trans_info) { |
194 RunTransactionTestAndGetTiming(cache, trans_info, BoundNetLog(), NULL); | 199 RunTransactionTestAndGetTiming(cache, trans_info, BoundNetLog(), nullptr); |
195 } | 200 } |
196 | 201 |
197 void RunTransactionTestWithLog(HttpCache* cache, | 202 void RunTransactionTestWithLog(HttpCache* cache, |
198 const MockTransaction& trans_info, | 203 const MockTransaction& trans_info, |
199 const BoundNetLog& log) { | 204 const BoundNetLog& log) { |
200 RunTransactionTestAndGetTiming(cache, trans_info, log, NULL); | 205 RunTransactionTestAndGetTiming(cache, trans_info, log, nullptr); |
201 } | 206 } |
202 | 207 |
203 void RunTransactionTestWithResponseInfo(HttpCache* cache, | 208 void RunTransactionTestWithResponseInfo(HttpCache* cache, |
204 const MockTransaction& trans_info, | 209 const MockTransaction& trans_info, |
205 HttpResponseInfo* response) { | 210 HttpResponseInfo* response) { |
206 RunTransactionTestWithRequest(cache, trans_info, MockHttpRequest(trans_info), | 211 RunTransactionTestWithRequest(cache, trans_info, MockHttpRequest(trans_info), |
207 response); | 212 response); |
208 } | 213 } |
209 | 214 |
210 void RunTransactionTestWithResponseInfoAndGetTiming( | 215 void RunTransactionTestWithResponseInfoAndGetTiming( |
211 HttpCache* cache, | 216 HttpCache* cache, |
212 const MockTransaction& trans_info, | 217 const MockTransaction& trans_info, |
213 HttpResponseInfo* response, | 218 HttpResponseInfo* response, |
214 const BoundNetLog& log, | 219 const BoundNetLog& log, |
215 LoadTimingInfo* load_timing_info) { | 220 LoadTimingInfo* load_timing_info) { |
216 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | 221 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
217 response, log, load_timing_info, NULL); | 222 response, log, load_timing_info, nullptr, nullptr); |
218 } | 223 } |
219 | 224 |
220 void RunTransactionTestWithResponse(HttpCache* cache, | 225 void RunTransactionTestWithResponse(HttpCache* cache, |
221 const MockTransaction& trans_info, | 226 const MockTransaction& trans_info, |
222 std::string* response_headers) { | 227 std::string* response_headers) { |
223 HttpResponseInfo response; | 228 HttpResponseInfo response; |
224 RunTransactionTestWithResponseInfo(cache, trans_info, &response); | 229 RunTransactionTestWithResponseInfo(cache, trans_info, &response); |
225 response.headers->GetNormalizedHeaders(response_headers); | 230 response.headers->GetNormalizedHeaders(response_headers); |
226 } | 231 } |
227 | 232 |
228 void RunTransactionTestWithResponseAndGetTiming( | 233 void RunTransactionTestWithResponseAndGetTiming( |
229 HttpCache* cache, | 234 HttpCache* cache, |
230 const MockTransaction& trans_info, | 235 const MockTransaction& trans_info, |
231 std::string* response_headers, | 236 std::string* response_headers, |
232 const BoundNetLog& log, | 237 const BoundNetLog& log, |
233 LoadTimingInfo* load_timing_info) { | 238 LoadTimingInfo* load_timing_info) { |
234 HttpResponseInfo response; | 239 HttpResponseInfo response; |
235 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | 240 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
236 &response, log, load_timing_info, NULL); | 241 &response, log, load_timing_info, nullptr, nullptr); |
237 response.headers->GetNormalizedHeaders(response_headers); | 242 response.headers->GetNormalizedHeaders(response_headers); |
238 } | 243 } |
239 | 244 |
240 // This class provides a handler for kFastNoStoreGET_Transaction so that the | 245 // This class provides a handler for kFastNoStoreGET_Transaction so that the |
241 // no-store header can be included on demand. | 246 // no-store header can be included on demand. |
242 class FastTransactionServer { | 247 class FastTransactionServer { |
243 public: | 248 public: |
244 FastTransactionServer() { | 249 FastTransactionServer() { |
245 no_store = false; | 250 no_store = false; |
246 } | 251 } |
(...skipping 6854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7101 trans->SetPriority(HIGHEST); | 7106 trans->SetPriority(HIGHEST); |
7102 // Should trigger a new network transaction and pick up the new | 7107 // Should trigger a new network transaction and pick up the new |
7103 // priority. | 7108 // priority. |
7104 ReadAndVerifyTransaction(trans.get(), transaction); | 7109 ReadAndVerifyTransaction(trans.get(), transaction); |
7105 | 7110 |
7106 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); | 7111 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); |
7107 | 7112 |
7108 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7113 RemoveMockTransaction(&kRangeGET_TransactionOK); |
7109 } | 7114 } |
7110 | 7115 |
7111 int64 RunTransactionAndGetReceivedBytes( | 7116 namespace { |
7112 MockHttpCache& cache, | 7117 |
7113 const MockTransaction& trans_info) { | 7118 void RunTransactionAndGetNetworkBytes(MockHttpCache& cache, |
7114 int64 received_bytes = -1; | 7119 const MockTransaction& trans_info, |
| 7120 int64_t* sent_bytes, |
| 7121 int64_t* received_bytes) { |
7115 RunTransactionTestBase(cache.http_cache(), trans_info, | 7122 RunTransactionTestBase(cache.http_cache(), trans_info, |
7116 MockHttpRequest(trans_info), NULL, BoundNetLog(), NULL, | 7123 MockHttpRequest(trans_info), nullptr, BoundNetLog(), |
7117 &received_bytes); | 7124 nullptr, sent_bytes, received_bytes); |
7118 return received_bytes; | |
7119 } | 7125 } |
7120 | 7126 |
7121 int64 TransactionSize(const MockTransaction& transaction) { | 7127 } // namespace |
7122 return strlen(transaction.status) + strlen(transaction.response_headers) + | |
7123 strlen(transaction.data); | |
7124 } | |
7125 | 7128 |
7126 TEST(HttpCache, ReceivedBytesCacheMissAndThenHit) { | 7129 TEST(HttpCache, NetworkBytesCacheMissAndThenHit) { |
7127 MockHttpCache cache; | 7130 MockHttpCache cache; |
7128 | 7131 |
7129 MockTransaction transaction(kSimpleGET_Transaction); | 7132 MockTransaction transaction(kSimpleGET_Transaction); |
7130 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7133 int64_t sent, received; |
7131 EXPECT_EQ(TransactionSize(transaction), received_bytes); | 7134 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7135 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7136 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
7132 | 7137 |
7133 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7138 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
7134 EXPECT_EQ(0, received_bytes); | 7139 EXPECT_EQ(0, sent); |
| 7140 EXPECT_EQ(0, received); |
7135 } | 7141 } |
7136 | 7142 |
7137 TEST(HttpCache, ReceivedBytesConditionalRequest304) { | 7143 TEST(HttpCache, NetworkBytesConditionalRequest304) { |
7138 MockHttpCache cache; | 7144 MockHttpCache cache; |
7139 | 7145 |
7140 ScopedMockTransaction transaction(kETagGET_Transaction); | 7146 ScopedMockTransaction transaction(kETagGET_Transaction); |
7141 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7147 int64_t sent, received; |
7142 EXPECT_EQ(TransactionSize(transaction), received_bytes); | 7148 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7149 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7150 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
7143 | 7151 |
7144 transaction.load_flags = LOAD_VALIDATE_CACHE; | 7152 transaction.load_flags = LOAD_VALIDATE_CACHE; |
7145 transaction.handler = ETagGet_ConditionalRequest_Handler; | 7153 transaction.handler = ETagGet_ConditionalRequest_Handler; |
7146 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7154 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
7147 EXPECT_EQ(TransactionSize(transaction), received_bytes); | 7155 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7156 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
7148 } | 7157 } |
7149 | 7158 |
7150 TEST(HttpCache, ReceivedBytesConditionalRequest200) { | 7159 TEST(HttpCache, NetworkBytesConditionalRequest200) { |
7151 MockHttpCache cache; | 7160 MockHttpCache cache; |
7152 | 7161 |
7153 MockTransaction transaction(kTypicalGET_Transaction); | 7162 MockTransaction transaction(kTypicalGET_Transaction); |
7154 transaction.request_headers = "Foo: bar\r\n"; | 7163 transaction.request_headers = "Foo: bar\r\n"; |
7155 transaction.response_headers = | 7164 transaction.response_headers = |
7156 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | 7165 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
7157 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 7166 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
7158 "Etag: \"foopy\"\n" | 7167 "Etag: \"foopy\"\n" |
7159 "Cache-Control: max-age=0\n" | 7168 "Cache-Control: max-age=0\n" |
7160 "Vary: Foo\n"; | 7169 "Vary: Foo\n"; |
7161 AddMockTransaction(&transaction); | 7170 AddMockTransaction(&transaction); |
7162 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7171 int64_t sent, received; |
7163 EXPECT_EQ(TransactionSize(transaction), received_bytes); | 7172 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7173 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7174 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
7164 | 7175 |
7165 RevalidationServer server; | 7176 RevalidationServer server; |
7166 transaction.handler = server.Handler; | 7177 transaction.handler = server.Handler; |
7167 transaction.request_headers = "Foo: none\r\n"; | 7178 transaction.request_headers = "Foo: none\r\n"; |
7168 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7179 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
7169 EXPECT_EQ(TransactionSize(transaction), received_bytes); | 7180 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7181 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
7170 | 7182 |
7171 RemoveMockTransaction(&transaction); | 7183 RemoveMockTransaction(&transaction); |
7172 } | 7184 } |
7173 | 7185 |
7174 TEST(HttpCache, ReceivedBytesRange) { | 7186 TEST(HttpCache, NetworkBytesRange) { |
7175 MockHttpCache cache; | 7187 MockHttpCache cache; |
7176 AddMockTransaction(&kRangeGET_TransactionOK); | 7188 AddMockTransaction(&kRangeGET_TransactionOK); |
7177 MockTransaction transaction(kRangeGET_TransactionOK); | 7189 MockTransaction transaction(kRangeGET_TransactionOK); |
7178 | 7190 |
7179 // Read bytes 40-49 from the network. | 7191 // Read bytes 40-49 from the network. |
7180 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7192 int64_t sent, received; |
7181 int64 range_response_size = TransactionSize(transaction); | 7193 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
7182 EXPECT_EQ(range_response_size, received_bytes); | 7194 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7195 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
7183 | 7196 |
7184 // Read bytes 40-49 from the cache. | 7197 // Read bytes 40-49 from the cache. |
7185 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7198 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
7186 EXPECT_EQ(0, received_bytes); | 7199 EXPECT_EQ(0, sent); |
| 7200 EXPECT_EQ(0, received); |
7187 base::MessageLoop::current()->RunUntilIdle(); | 7201 base::MessageLoop::current()->RunUntilIdle(); |
7188 | 7202 |
7189 // Read bytes 30-39 from the network. | 7203 // Read bytes 30-39 from the network. |
7190 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; | 7204 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
7191 transaction.data = "rg: 30-39 "; | 7205 transaction.data = "rg: 30-39 "; |
7192 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7206 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
7193 EXPECT_EQ(range_response_size, received_bytes); | 7207 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7208 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
7194 base::MessageLoop::current()->RunUntilIdle(); | 7209 base::MessageLoop::current()->RunUntilIdle(); |
7195 | 7210 |
7196 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | 7211 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
7197 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | 7212 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
7198 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | 7213 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
7199 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7214 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
7200 EXPECT_EQ(range_response_size * 2, received_bytes); | 7215 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes * 2, sent); |
| 7216 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes * 2, received); |
7201 | 7217 |
7202 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7218 RemoveMockTransaction(&kRangeGET_TransactionOK); |
7203 } | 7219 } |
7204 | 7220 |
7205 class HttpCachePrefetchValidationTest : public ::testing::Test { | 7221 class HttpCachePrefetchValidationTest : public ::testing::Test { |
7206 protected: | 7222 protected: |
7207 static const int kMaxAgeSecs = 100; | 7223 static const int kMaxAgeSecs = 100; |
7208 static const int kRequireValidationSecs = kMaxAgeSecs + 1; | 7224 static const int kRequireValidationSecs = kMaxAgeSecs + 1; |
7209 | 7225 |
7210 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { | 7226 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7631 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 7647 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
7632 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7648 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
7633 EXPECT_TRUE(response_info.was_cached); | 7649 EXPECT_TRUE(response_info.was_cached); |
7634 | 7650 |
7635 // The new SSL state is reported. | 7651 // The new SSL state is reported. |
7636 EXPECT_EQ(status2, response_info.ssl_info.connection_status); | 7652 EXPECT_EQ(status2, response_info.ssl_info.connection_status); |
7637 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); | 7653 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); |
7638 } | 7654 } |
7639 | 7655 |
7640 } // namespace net | 7656 } // namespace net |
OLD | NEW |