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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/test/simple_test_clock.h" |
16 #include "net/base/cache_type.h" | 17 #include "net/base/cache_type.h" |
17 #include "net/base/elements_upload_data_stream.h" | 18 #include "net/base/elements_upload_data_stream.h" |
18 #include "net/base/host_port_pair.h" | 19 #include "net/base/host_port_pair.h" |
19 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
20 #include "net/base/load_timing_info.h" | 21 #include "net/base/load_timing_info.h" |
21 #include "net/base/load_timing_info_test_util.h" | 22 #include "net/base/load_timing_info_test_util.h" |
22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
23 #include "net/base/net_log_unittest.h" | 24 #include "net/base/net_log_unittest.h" |
24 #include "net/base/upload_bytes_element_reader.h" | 25 #include "net/base/upload_bytes_element_reader.h" |
25 #include "net/cert/cert_status_flags.h" | 26 #include "net/cert/cert_status_flags.h" |
26 #include "net/disk_cache/disk_cache.h" | 27 #include "net/disk_cache/disk_cache.h" |
27 #include "net/http/http_byte_range.h" | 28 #include "net/http/http_byte_range.h" |
| 29 #include "net/http/http_cache_transaction.h" |
28 #include "net/http/http_request_headers.h" | 30 #include "net/http/http_request_headers.h" |
29 #include "net/http/http_request_info.h" | 31 #include "net/http/http_request_info.h" |
30 #include "net/http/http_response_headers.h" | 32 #include "net/http/http_response_headers.h" |
31 #include "net/http/http_response_info.h" | 33 #include "net/http/http_response_info.h" |
32 #include "net/http/http_transaction.h" | 34 #include "net/http/http_transaction.h" |
33 #include "net/http/http_transaction_test_util.h" | 35 #include "net/http/http_transaction_test_util.h" |
34 #include "net/http/http_util.h" | 36 #include "net/http/http_util.h" |
35 #include "net/http/mock_http_cache.h" | 37 #include "net/http/mock_http_cache.h" |
36 #include "net/socket/client_socket_handle.h" | 38 #include "net/socket/client_socket_handle.h" |
37 #include "net/ssl/ssl_cert_request_info.h" | 39 #include "net/ssl/ssl_cert_request_info.h" |
(...skipping 7051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7089 | 7091 |
7090 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | 7092 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
7091 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | 7093 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
7092 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | 7094 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
7093 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7095 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); |
7094 EXPECT_EQ(range_response_size * 2, received_bytes); | 7096 EXPECT_EQ(range_response_size * 2, received_bytes); |
7095 | 7097 |
7096 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7098 RemoveMockTransaction(&kRangeGET_TransactionOK); |
7097 } | 7099 } |
7098 | 7100 |
| 7101 class HttpCachePrefetchValidationTest : public ::testing::Test { |
| 7102 protected: |
| 7103 static const int kMaxAgeSecs = 100; |
| 7104 static const int kRequireValidationSecs = kMaxAgeSecs + 1; |
| 7105 |
| 7106 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { |
| 7107 DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * net::kNumSecondsPerMinute); |
| 7108 |
| 7109 clock_ = new base::SimpleTestClock(); |
| 7110 cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_)); |
| 7111 cache_.network_layer()->SetClock(clock_); |
| 7112 |
| 7113 transaction_.response_headers = "Cache-Control: max-age=100\n"; |
| 7114 } |
| 7115 |
| 7116 bool TransactionRequiredNetwork(int load_flags) { |
| 7117 int pre_transaction_count = transaction_count(); |
| 7118 transaction_.load_flags = load_flags; |
| 7119 RunTransactionTest(cache_.http_cache(), transaction_); |
| 7120 return pre_transaction_count != transaction_count(); |
| 7121 } |
| 7122 |
| 7123 void AdvanceTime(int seconds) { |
| 7124 clock_->Advance(base::TimeDelta::FromSeconds(seconds)); |
| 7125 } |
| 7126 |
| 7127 int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; } |
| 7128 |
| 7129 // How many times this test has sent requests to the (fake) origin |
| 7130 // server. Every test case needs to make at least one request to initialise |
| 7131 // the cache. |
| 7132 int transaction_count() { |
| 7133 return cache_.network_layer()->transaction_count(); |
| 7134 } |
| 7135 |
| 7136 MockHttpCache cache_; |
| 7137 ScopedMockTransaction transaction_; |
| 7138 std::string response_headers_; |
| 7139 base::SimpleTestClock* clock_; |
| 7140 }; |
| 7141 |
| 7142 TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) { |
| 7143 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7144 AdvanceTime(kRequireValidationSecs); |
| 7145 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7146 } |
| 7147 |
| 7148 TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { |
| 7149 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7150 AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute); |
| 7151 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7152 } |
| 7153 |
| 7154 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { |
| 7155 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7156 AdvanceTime(kRequireValidationSecs); |
| 7157 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7158 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7159 } |
| 7160 |
| 7161 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { |
| 7162 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7163 AdvanceTime(kRequireValidationSecs); |
| 7164 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_ONLY_FROM_CACHE)); |
| 7165 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7166 } |
| 7167 |
| 7168 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { |
| 7169 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7170 AdvanceTime(kRequireValidationSecs); |
| 7171 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_BYPASS_CACHE)); |
| 7172 AdvanceTime(kRequireValidationSecs); |
| 7173 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7174 } |
| 7175 |
| 7176 TEST_F(HttpCachePrefetchValidationTest, |
| 7177 SkipValidationOnExistingEntryThatNeedsValidation) { |
| 7178 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7179 AdvanceTime(kRequireValidationSecs); |
| 7180 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7181 AdvanceTime(kRequireValidationSecs); |
| 7182 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7183 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7184 } |
| 7185 |
| 7186 TEST_F(HttpCachePrefetchValidationTest, |
| 7187 SkipValidationOnExistingEntryThatDoesNotNeedValidation) { |
| 7188 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7189 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7190 AdvanceTime(kRequireValidationSecs); |
| 7191 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7192 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7193 } |
| 7194 |
| 7195 TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) { |
| 7196 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7197 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7198 AdvanceTime(kRequireValidationSecs); |
| 7199 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7200 } |
| 7201 |
| 7202 TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { |
| 7203 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7204 AdvanceTime(kRequireValidationSecs); |
| 7205 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7206 AdvanceTime(kRequireValidationSecs); |
| 7207 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7208 } |
| 7209 |
7099 // Framework for tests of stale-while-revalidate related functionality. With | 7210 // Framework for tests of stale-while-revalidate related functionality. With |
7100 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it | 7211 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it |
7101 // will trigger the stale-while-revalidate asynchronous revalidation. Setting | 7212 // will trigger the stale-while-revalidate asynchronous revalidation. Setting |
7102 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause | 7213 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause |
7103 // synchronous revalidation. | 7214 // synchronous revalidation. |
7104 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { | 7215 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { |
7105 protected: | 7216 protected: |
7106 HttpCacheStaleWhileRevalidateTest() | 7217 HttpCacheStaleWhileRevalidateTest() |
7107 : transaction_(kSimpleGET_Transaction), | 7218 : transaction_(kSimpleGET_Transaction), |
7108 age_(3601), | 7219 age_(3601), |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7664 | 7775 |
7665 // Here the second transaction proceeds without reading the first body. | 7776 // Here the second transaction proceeds without reading the first body. |
7666 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7777 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
7667 base::MessageLoop::current()->RunUntilIdle(); | 7778 base::MessageLoop::current()->RunUntilIdle(); |
7668 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7779 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); |
7669 ASSERT_TRUE(second->trans->GetResponseInfo()); | 7780 ASSERT_TRUE(second->trans->GetResponseInfo()); |
7670 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | 7781 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
7671 "Cache-Control", "no-store")); | 7782 "Cache-Control", "no-store")); |
7672 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | 7783 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
7673 } | 7784 } |
OLD | NEW |