Chromium Code Reviews| 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" |
| 38 #include "net/websockets/websocket_handshake_stream_base.h" | 40 #include "net/websockets/websocket_handshake_stream_base.h" |
| 39 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 40 | 42 |
| 41 using base::Time; | 43 using base::Time; |
| 42 | 44 |
| 45 namespace net { | |
| 46 | |
| 43 namespace { | 47 namespace { |
| 44 | 48 |
| 45 // Tests the load timing values of a request that goes through a | 49 // Tests the load timing values of a request that goes through a |
| 46 // MockNetworkTransaction. | 50 // MockNetworkTransaction. |
| 47 void TestLoadTimingNetworkRequest(const net::LoadTimingInfo& load_timing_info) { | 51 void TestLoadTimingNetworkRequest(const net::LoadTimingInfo& load_timing_info) { |
| 48 EXPECT_FALSE(load_timing_info.socket_reused); | 52 EXPECT_FALSE(load_timing_info.socket_reused); |
| 49 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | 53 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 50 | 54 |
| 51 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | 55 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 52 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | 56 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| (...skipping 6713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6766 | 6770 |
| 6767 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | 6771 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
| 6768 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | 6772 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
| 6769 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | 6773 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
| 6770 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 6774 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); |
| 6771 EXPECT_EQ(range_response_size * 2, received_bytes); | 6775 EXPECT_EQ(range_response_size * 2, received_bytes); |
| 6772 | 6776 |
| 6773 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6777 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6774 } | 6778 } |
| 6775 | 6779 |
| 6780 class HttpCachePrefetchValidationTest : public ::testing::Test { | |
| 6781 protected: | |
| 6782 static const int kMaxAge = 100; | |
| 6783 static const int kRequireValidation = kMaxAge + 1; | |
| 6784 | |
| 6785 HttpCachePrefetchValidationTest() | |
| 6786 : transaction_(new ScopedMockTransaction(kSimpleGET_Transaction)) { | |
|
rvargas (doing something else)
2014/12/12 02:20:50
This class is borderline to require separation bet
jkarlin
2014/12/15 17:19:25
I've removed the 206 tests as they're unnecessary,
| |
| 6787 DCHECK(kMaxAge < prefetch_reuse_mins() * kNumSecondsPerMinute) | |
|
rvargas (doing something else)
2014/12/12 02:20:50
_GT
jkarlin
2014/12/15 17:19:25
Done, but _LT.
| |
| 6788 << "Test requirement"; | |
|
rvargas (doing something else)
2014/12/12 02:20:50
Does this string really provide any info?
jkarlin
2014/12/15 17:19:25
Done.
| |
| 6789 clock_ = new base::SimpleTestClock(); | |
| 6790 cache_.http_cache()->clock_.reset(clock_); | |
|
rvargas (doing something else)
2014/12/12 02:20:50
If the accessor is public (for tests), this class
jkarlin
2014/12/15 17:19:25
I somewhat agree on making the accessors public (a
rvargas (doing something else)
2014/12/24 02:15:24
If the name of the method clearly specify that it
| |
| 6791 cache_.network_layer()->SetClock(clock_); | |
| 6792 | |
| 6793 response_headers_ = base::StringPrintf( | |
|
rvargas (doing something else)
2014/12/12 02:20:50
it is slightly weird to use StringPrintf with all
jkarlin
2014/12/15 17:19:25
Done. I only need this string once now so it's jus
| |
| 6794 "HTTP/1.1 200 OK\n" | |
| 6795 "Cache-Control: max-age=%d\n", | |
| 6796 kMaxAge); | |
| 6797 transaction_->response_headers = response_headers_.c_str(); | |
| 6798 | |
| 6799 if (ShouldUseRangeTransaction()) | |
| 6800 SetUpRangeTransaction(); | |
| 6801 } | |
| 6802 | |
| 6803 bool TransactionRequiresNetwork(int load_flags) { | |
| 6804 int pre_transaction_count = transaction_count(); | |
| 6805 | |
| 6806 transaction_->load_flags = load_flags; | |
| 6807 | |
| 6808 int64 received_bytes = | |
| 6809 RunTransactionAndGetReceivedBytes(cache_, *transaction_); | |
|
rvargas (doing something else)
2014/12/12 02:20:50
you mean some form of get() ?
jkarlin
2014/12/15 17:19:25
No longer pertinent as transaction_ is no longer o
| |
| 6810 | |
| 6811 bool network_used = pre_transaction_count != transaction_count(); | |
| 6812 EXPECT_EQ(network_used ? TransactionSize(*transaction_) : 0, | |
| 6813 received_bytes); | |
| 6814 | |
| 6815 return network_used; | |
| 6816 } | |
| 6817 | |
| 6818 void SetUpRangeTransaction() { | |
| 6819 transaction_.reset(new ScopedMockTransaction(kRangeGET_TransactionOK)); | |
| 6820 response_headers_ = std::string(transaction_->response_headers); | |
| 6821 response_headers_ += | |
| 6822 base::StringPrintf("Cache-Control: max-age=%d\n", kMaxAge); | |
| 6823 | |
| 6824 transaction_->response_headers = response_headers_.c_str(); | |
| 6825 } | |
| 6826 | |
| 6827 virtual bool ShouldUseRangeTransaction() const { return false; } | |
|
rvargas (doing something else)
2014/12/12 02:20:50
wait until this is actually used?
jkarlin
2014/12/15 17:19:25
I've deleted the range request parameterized test
| |
| 6828 | |
| 6829 void AdvanceTime(int seconds) { | |
| 6830 clock_->Advance(base::TimeDelta::FromSeconds(seconds)); | |
| 6831 } | |
| 6832 | |
| 6833 int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; } | |
| 6834 | |
| 6835 // How many times this test has sent requests to the (fake) origin | |
| 6836 // server. Every test case needs to make at least one request to initialise | |
| 6837 // the cache. | |
| 6838 int transaction_count() { | |
| 6839 return cache_.network_layer()->transaction_count(); | |
| 6840 } | |
| 6841 | |
| 6842 // How many times an existing cache entry was opened during the test case. | |
| 6843 int open_count() { return cache_.disk_cache()->open_count(); } | |
| 6844 | |
| 6845 MockHttpCache cache_; | |
| 6846 scoped_ptr<ScopedMockTransaction> transaction_; | |
|
rvargas (doing something else)
2014/12/12 02:20:50
why a pointer?
jkarlin
2014/12/15 17:19:25
So that the original transaction can be swapped at
| |
| 6847 std::string response_headers_; | |
| 6848 base::SimpleTestClock* clock_; | |
| 6849 }; | |
| 6850 | |
| 6851 class HttpCachePrefetchValidationTestP | |
| 6852 : public HttpCachePrefetchValidationTest, | |
| 6853 public testing::WithParamInterface<bool> { | |
| 6854 bool ShouldUseRangeTransaction() const override { return GetParam(); } | |
| 6855 }; | |
| 6856 | |
| 6857 TEST_P(HttpCachePrefetchValidationTestP, SkipValidationShortlyAfterPrefetch) { | |
| 6858 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
| 6859 AdvanceTime(kRequireValidation); | |
| 6860 EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
| 6861 } | |
| 6862 | |
| 6863 TEST_P(HttpCachePrefetchValidationTestP, ValidateLongAfterPrefetch) { | |
| 6864 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
| 6865 AdvanceTime(prefetch_reuse_mins() * kNumSecondsPerMinute); | |
| 6866 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
| 6867 } | |
| 6868 | |
| 6869 TEST_P(HttpCachePrefetchValidationTestP, SkipValidationOnceOnly) { | |
| 6870 EXPECT_TRUE(TransactionRequiresNetwork(LOAD_PREFETCH)); | |
| 6871 AdvanceTime(kRequireValidation); | |
| 6872 EXPECT_FALSE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6873 EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6874 } | |
| 6875 | |
| 6876 TEST_P(HttpCachePrefetchValidationTestP, | |
| 6877 SkipValidationOnExistingEntryThatNeedsValidation) { | |
| 6878 EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6879 AdvanceTime(kRequireValidation); | |
| 6880 EXPECT_TRUE(TransactionRequiresNetwork(LOAD_PREFETCH)); | |
| 6881 AdvanceTime(kRequireValidation); | |
| 6882 EXPECT_FALSE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6883 EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6884 } | |
| 6885 | |
| 6886 TEST_P(HttpCachePrefetchValidationTestP, | |
| 6887 SkipValidationOnExistingEntryThatDoesNotNeedValidation) { | |
| 6888 EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6889 EXPECT_FALSE(TransactionRequiresNetwork(LOAD_PREFETCH)); | |
| 6890 AdvanceTime(kRequireValidation); | |
| 6891 EXPECT_FALSE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6892 EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); | |
| 6893 } | |
| 6894 | |
| 6895 INSTANTIATE_TEST_CASE_P(HttpCachePrefetchValidationTests, | |
| 6896 HttpCachePrefetchValidationTestP, | |
| 6897 ::testing::Values(false, true)); | |
| 6898 | |
| 6776 // Framework for tests of stale-while-revalidate related functionality. With | 6899 // Framework for tests of stale-while-revalidate related functionality. With |
| 6777 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it | 6900 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it |
| 6778 // will trigger the stale-while-revalidate asynchronous revalidation. Setting | 6901 // will trigger the stale-while-revalidate asynchronous revalidation. Setting |
| 6779 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause | 6902 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause |
| 6780 // synchronous revalidation. | 6903 // synchronous revalidation. |
| 6781 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { | 6904 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { |
| 6782 protected: | 6905 protected: |
| 6783 HttpCacheStaleWhileRevalidateTest() | 6906 HttpCacheStaleWhileRevalidateTest() |
| 6784 : transaction_(kSimpleGET_Transaction), | 6907 : transaction_(kSimpleGET_Transaction), |
| 6785 age_(3601), | 6908 age_(3601), |
| (...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7341 | 7464 |
| 7342 // Here the second transaction proceeds without reading the first body. | 7465 // Here the second transaction proceeds without reading the first body. |
| 7343 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7466 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
| 7344 base::MessageLoop::current()->RunUntilIdle(); | 7467 base::MessageLoop::current()->RunUntilIdle(); |
| 7345 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7468 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); |
| 7346 ASSERT_TRUE(second->trans->GetResponseInfo()); | 7469 ASSERT_TRUE(second->trans->GetResponseInfo()); |
| 7347 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | 7470 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
| 7348 "Cache-Control", "no-store")); | 7471 "Cache-Control", "no-store")); |
| 7349 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | 7472 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
| 7350 } | 7473 } |
| 7474 | |
| 7475 } // namespace net | |
| OLD | NEW |