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 6980 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7018 | 7020 |
7019 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | 7021 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
7020 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | 7022 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
7021 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | 7023 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
7022 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7024 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); |
7023 EXPECT_EQ(range_response_size * 2, received_bytes); | 7025 EXPECT_EQ(range_response_size * 2, received_bytes); |
7024 | 7026 |
7025 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7027 RemoveMockTransaction(&kRangeGET_TransactionOK); |
7026 } | 7028 } |
7027 | 7029 |
| 7030 class HttpCachePrefetchValidationTest : public ::testing::Test { |
| 7031 protected: |
| 7032 static const int kMaxAgeSecs = 100; |
| 7033 static const int kRequireValidationSecs = kMaxAgeSecs + 1; |
| 7034 |
| 7035 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { |
| 7036 DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * net::kNumSecondsPerMinute); |
| 7037 |
| 7038 clock_ = new base::SimpleTestClock(); |
| 7039 cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_)); |
| 7040 cache_.network_layer()->SetClock(clock_); |
| 7041 |
| 7042 transaction_.response_headers = "Cache-Control: max-age=100\n"; |
| 7043 } |
| 7044 |
| 7045 bool TransactionRequiredNetwork(int load_flags) { |
| 7046 int pre_transaction_count = transaction_count(); |
| 7047 transaction_.load_flags = load_flags; |
| 7048 RunTransactionTest(cache_.http_cache(), transaction_); |
| 7049 return pre_transaction_count != transaction_count(); |
| 7050 } |
| 7051 |
| 7052 void AdvanceTime(int seconds) { |
| 7053 clock_->Advance(base::TimeDelta::FromSeconds(seconds)); |
| 7054 } |
| 7055 |
| 7056 int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; } |
| 7057 |
| 7058 // How many times this test has sent requests to the (fake) origin |
| 7059 // server. Every test case needs to make at least one request to initialise |
| 7060 // the cache. |
| 7061 int transaction_count() { |
| 7062 return cache_.network_layer()->transaction_count(); |
| 7063 } |
| 7064 |
| 7065 MockHttpCache cache_; |
| 7066 ScopedMockTransaction transaction_; |
| 7067 std::string response_headers_; |
| 7068 base::SimpleTestClock* clock_; |
| 7069 }; |
| 7070 |
| 7071 TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) { |
| 7072 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7073 AdvanceTime(kRequireValidationSecs); |
| 7074 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7075 } |
| 7076 |
| 7077 TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { |
| 7078 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7079 AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute); |
| 7080 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7081 } |
| 7082 |
| 7083 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { |
| 7084 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7085 AdvanceTime(kRequireValidationSecs); |
| 7086 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7087 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7088 } |
| 7089 |
| 7090 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { |
| 7091 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7092 AdvanceTime(kRequireValidationSecs); |
| 7093 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_ONLY_FROM_CACHE)); |
| 7094 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7095 } |
| 7096 |
| 7097 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { |
| 7098 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7099 AdvanceTime(kRequireValidationSecs); |
| 7100 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_BYPASS_CACHE)); |
| 7101 AdvanceTime(kRequireValidationSecs); |
| 7102 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7103 } |
| 7104 |
| 7105 TEST_F(HttpCachePrefetchValidationTest, |
| 7106 SkipValidationOnExistingEntryThatNeedsValidation) { |
| 7107 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7108 AdvanceTime(kRequireValidationSecs); |
| 7109 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7110 AdvanceTime(kRequireValidationSecs); |
| 7111 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7112 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7113 } |
| 7114 |
| 7115 TEST_F(HttpCachePrefetchValidationTest, |
| 7116 SkipValidationOnExistingEntryThatDoesNotNeedValidation) { |
| 7117 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7118 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7119 AdvanceTime(kRequireValidationSecs); |
| 7120 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7121 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7122 } |
| 7123 |
| 7124 TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) { |
| 7125 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7126 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7127 AdvanceTime(kRequireValidationSecs); |
| 7128 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7129 } |
| 7130 |
| 7131 TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { |
| 7132 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7133 AdvanceTime(kRequireValidationSecs); |
| 7134 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
| 7135 AdvanceTime(kRequireValidationSecs); |
| 7136 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
| 7137 } |
| 7138 |
7028 // Framework for tests of stale-while-revalidate related functionality. With | 7139 // Framework for tests of stale-while-revalidate related functionality. With |
7029 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it | 7140 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it |
7030 // will trigger the stale-while-revalidate asynchronous revalidation. Setting | 7141 // will trigger the stale-while-revalidate asynchronous revalidation. Setting |
7031 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause | 7142 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause |
7032 // synchronous revalidation. | 7143 // synchronous revalidation. |
7033 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { | 7144 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { |
7034 protected: | 7145 protected: |
7035 HttpCacheStaleWhileRevalidateTest() | 7146 HttpCacheStaleWhileRevalidateTest() |
7036 : transaction_(kSimpleGET_Transaction), | 7147 : transaction_(kSimpleGET_Transaction), |
7037 age_(3601), | 7148 age_(3601), |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7593 | 7704 |
7594 // Here the second transaction proceeds without reading the first body. | 7705 // Here the second transaction proceeds without reading the first body. |
7595 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7706 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
7596 base::MessageLoop::current()->RunUntilIdle(); | 7707 base::MessageLoop::current()->RunUntilIdle(); |
7597 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7708 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); |
7598 ASSERT_TRUE(second->trans->GetResponseInfo()); | 7709 ASSERT_TRUE(second->trans->GetResponseInfo()); |
7599 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | 7710 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
7600 "Cache-Control", "no-store")); | 7711 "Cache-Control", "no-store")); |
7601 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | 7712 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
7602 } | 7713 } |
OLD | NEW |