| 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_server_properties_impl.h" | 5 #include "net/http/http_server_properties_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/test/test_mock_time_task_runner.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "net/base/host_port_pair.h" | 14 #include "net/base/host_port_pair.h" |
| 14 #include "net/base/ip_address.h" | 15 #include "net/base/ip_address.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class ListValue; | 20 class ListValue; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace net { | 23 namespace net { |
| 23 | 24 |
| 25 const base::TimeDelta BROKEN_ALT_SVC_EXPIRE_DELAYS[10] = { |
| 26 base::TimeDelta::FromSeconds(300), base::TimeDelta::FromSeconds(600), |
| 27 base::TimeDelta::FromSeconds(1200), base::TimeDelta::FromSeconds(2400), |
| 28 base::TimeDelta::FromSeconds(4800), base::TimeDelta::FromSeconds(9600), |
| 29 base::TimeDelta::FromSeconds(19200), base::TimeDelta::FromSeconds(38400), |
| 30 base::TimeDelta::FromSeconds(76800), base::TimeDelta::FromSeconds(153600), |
| 31 }; |
| 32 |
| 24 class HttpServerPropertiesImplPeer { | 33 class HttpServerPropertiesImplPeer { |
| 25 public: | 34 public: |
| 26 static void AddBrokenAlternativeServiceWithExpirationTime( | 35 static void AddBrokenAlternativeServiceWithExpirationTime( |
| 27 HttpServerPropertiesImpl& impl, | 36 HttpServerPropertiesImpl& impl, |
| 28 AlternativeService alternative_service, | 37 const AlternativeService& alternative_service, |
| 29 base::TimeTicks when) { | 38 base::TimeTicks when) { |
| 30 impl.broken_alternative_services_.insert( | 39 BrokenAlternativeServices::BrokenAlternativeServiceList::iterator unused_it; |
| 31 std::make_pair(alternative_service, when)); | 40 impl.broken_alternative_services_.AddToBrokenAlternativeServiceListAndMap( |
| 41 alternative_service, when, &unused_it); |
| 32 auto it = | 42 auto it = |
| 33 impl.recently_broken_alternative_services_.Get(alternative_service); | 43 impl.broken_alternative_services_.recently_broken_alternative_services_ |
| 34 if (it == impl.recently_broken_alternative_services_.end()) { | 44 .Get(alternative_service); |
| 35 impl.recently_broken_alternative_services_.Put(alternative_service, 1); | 45 if (it == impl.broken_alternative_services_ |
| 46 .recently_broken_alternative_services_.end()) { |
| 47 impl.broken_alternative_services_.recently_broken_alternative_services_ |
| 48 .Put(alternative_service, 1); |
| 36 } else { | 49 } else { |
| 37 it->second++; | 50 it->second++; |
| 38 } | 51 } |
| 39 } | 52 } |
| 40 | 53 |
| 41 static void ExpireBrokenAlternateProtocolMappings( | 54 static void ExpireBrokenAlternateProtocolMappings( |
| 42 HttpServerPropertiesImpl& impl) { | 55 HttpServerPropertiesImpl& impl) { |
| 43 impl.ExpireBrokenAlternateProtocolMappings(); | 56 impl.broken_alternative_services_.ExpireBrokenAlternateProtocolMappings(); |
| 44 } | 57 } |
| 45 }; | 58 }; |
| 46 | 59 |
| 47 namespace { | 60 namespace { |
| 48 | 61 |
| 49 const int kMaxSupportsSpdyServerHosts = 500; | 62 const int kMaxSupportsSpdyServerHosts = 500; |
| 50 | 63 |
| 64 class TestClock : public BrokenAlternativeServices::Clock { |
| 65 public: |
| 66 TestClock() : now_(base::TimeTicks::Now()) {} |
| 67 ~TestClock() override {} |
| 68 |
| 69 base::TimeTicks Now() const override { return now_; } |
| 70 |
| 71 void SetNow(base::TimeTicks now) { now_ = now; } |
| 72 void AdvanceNow(base::TimeDelta delta) { now_ += delta; } |
| 73 |
| 74 private: |
| 75 base::TimeTicks now_; |
| 76 }; |
| 77 |
| 51 class HttpServerPropertiesImplTest : public testing::Test { | 78 class HttpServerPropertiesImplTest : public testing::Test { |
| 52 protected: | 79 protected: |
| 80 HttpServerPropertiesImplTest() : impl_(&test_clock_) {} |
| 81 |
| 53 bool HasAlternativeService(const url::SchemeHostPort& origin) { | 82 bool HasAlternativeService(const url::SchemeHostPort& origin) { |
| 54 const AlternativeServiceVector alternative_service_vector = | 83 const AlternativeServiceVector alternative_service_vector = |
| 55 impl_.GetAlternativeServices(origin); | 84 impl_.GetAlternativeServices(origin); |
| 56 return !alternative_service_vector.empty(); | 85 return !alternative_service_vector.empty(); |
| 57 } | 86 } |
| 58 | 87 |
| 59 bool SetAlternativeService(const url::SchemeHostPort& origin, | 88 bool SetAlternativeService(const url::SchemeHostPort& origin, |
| 60 const AlternativeService& alternative_service) { | 89 const AlternativeService& alternative_service) { |
| 61 const base::Time expiration = | 90 const base::Time expiration = |
| 62 base::Time::Now() + base::TimeDelta::FromDays(1); | 91 base::Time::Now() + base::TimeDelta::FromDays(1); |
| 63 return impl_.SetAlternativeService(origin, alternative_service, expiration); | 92 return impl_.SetAlternativeService(origin, alternative_service, expiration); |
| 64 } | 93 } |
| 65 | 94 |
| 95 void MarkBrokenAndLetExpireAlternativeServiceNTimes( |
| 96 const AlternativeService& alternative_service, |
| 97 int num_times) {} |
| 98 |
| 99 TestClock test_clock_; |
| 66 HttpServerPropertiesImpl impl_; | 100 HttpServerPropertiesImpl impl_; |
| 67 }; | 101 }; |
| 68 | 102 |
| 69 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest; | 103 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest; |
| 70 | 104 |
| 71 TEST_F(SpdyServerPropertiesTest, SetWithSchemeHostPort) { | 105 TEST_F(SpdyServerPropertiesTest, SetWithSchemeHostPort) { |
| 72 // Check spdy servers are correctly set with SchemeHostPort key. | 106 // Check spdy servers are correctly set with SchemeHostPort key. |
| 73 url::SchemeHostPort https_www_server("https", "www.google.com", 443); | 107 url::SchemeHostPort https_www_server("https", "www.google.com", 443); |
| 74 url::SchemeHostPort http_photo_server("http", "photos.google.com", 80); | 108 url::SchemeHostPort http_photo_server("http", "photos.google.com", 80); |
| 75 // Servers with port equal to default port in scheme will drop port components | 109 // Servers with port equal to default port in scheme will drop port components |
| (...skipping 900 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 EXPECT_FALSE(HasAlternativeService(bar_server1)); | 1010 EXPECT_FALSE(HasAlternativeService(bar_server1)); |
| 977 // The alternative service of "bar:443" should be unaffected. | 1011 // The alternative service of "bar:443" should be unaffected. |
| 978 EXPECT_TRUE(HasAlternativeService(bar_server2)); | 1012 EXPECT_TRUE(HasAlternativeService(bar_server2)); |
| 979 | 1013 |
| 980 EXPECT_TRUE( | 1014 EXPECT_TRUE( |
| 981 impl_.WasAlternativeServiceRecentlyBroken(bar_alternative_service)); | 1015 impl_.WasAlternativeServiceRecentlyBroken(bar_alternative_service)); |
| 982 EXPECT_FALSE( | 1016 EXPECT_FALSE( |
| 983 impl_.WasAlternativeServiceRecentlyBroken(baz_alternative_service)); | 1017 impl_.WasAlternativeServiceRecentlyBroken(baz_alternative_service)); |
| 984 } | 1018 } |
| 985 | 1019 |
| 1020 // Regression test for https://crbug.com/724302 |
| 1021 TEST_F(AlternateProtocolServerPropertiesTest, RemoveExpiredBrokenAltSvc2) { |
| 1022 scoped_refptr<base::TestMockTimeTaskRunner> test_task_runner = |
| 1023 new base::TestMockTimeTaskRunner; |
| 1024 |
| 1025 // This test will mark an alternative service A that has already been marked |
| 1026 // broken many times, then immediately mark another alternative service B as |
| 1027 // broken for the first time. Because A's been marked broken many times |
| 1028 // already, its brokenness will be scheduled to expire much further in the |
| 1029 // future than B, even though it was marked broken before B. This test makes |
| 1030 // sure that even though A was marked broken before B, B's brokenness should |
| 1031 // expire before A. |
| 1032 |
| 1033 url::SchemeHostPort server1("https", "foo", 443); |
| 1034 AlternativeService alternative_service1(kProtoQUIC, "foo", 443); |
| 1035 SetAlternativeService(server1, alternative_service1); |
| 1036 |
| 1037 url::SchemeHostPort server2("https", "bar", 443); |
| 1038 AlternativeService alternative_service2(kProtoQUIC, "bar", 443); |
| 1039 SetAlternativeService(server2, alternative_service2); |
| 1040 |
| 1041 // Repeatedly mark alt svc 1 broken and wait for its brokenness to expire. |
| 1042 // This will increase its time until expiration. |
| 1043 for (int i = 0; i < 3; ++i) { |
| 1044 { |
| 1045 base::TestMockTimeTaskRunner::ScopedContext scoped_context( |
| 1046 test_task_runner); |
| 1047 impl_.MarkAlternativeServiceBroken(alternative_service1); |
| 1048 } |
| 1049 // |impl_| should have posted task to expire the brokenness of |
| 1050 // |alternative_service1| |
| 1051 EXPECT_EQ(1u, test_task_runner->GetPendingTaskCount()); |
| 1052 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service1)); |
| 1053 |
| 1054 // Advance time by just enough so that |alternative_service1|'s brokenness |
| 1055 // expires. |
| 1056 base::TimeDelta delta = BROKEN_ALT_SVC_EXPIRE_DELAYS[i]; |
| 1057 test_clock_.AdvanceNow(delta); |
| 1058 test_task_runner->FastForwardBy(delta); |
| 1059 |
| 1060 // Ensure brokenness of |alternative_service1| has expired. |
| 1061 EXPECT_FALSE(test_task_runner->HasPendingTask()); |
| 1062 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service1)); |
| 1063 } |
| 1064 |
| 1065 { |
| 1066 base::TestMockTimeTaskRunner::ScopedContext scoped_context( |
| 1067 test_task_runner); |
| 1068 impl_.MarkAlternativeServiceBroken(alternative_service1); |
| 1069 impl_.MarkAlternativeServiceBroken(alternative_service2); |
| 1070 } |
| 1071 |
| 1072 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service2)); |
| 1073 |
| 1074 // Advance time by just enough so that |alternative_service2|'s brokennness |
| 1075 // expires. |
| 1076 base::TimeDelta delta = BROKEN_ALT_SVC_EXPIRE_DELAYS[0]; |
| 1077 test_clock_.AdvanceNow(delta); |
| 1078 test_task_runner->FastForwardBy(delta); |
| 1079 |
| 1080 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service1)); |
| 1081 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service2)); |
| 1082 |
| 1083 // Advance time by enough so that |alternative_service1|'s brokenness expires. |
| 1084 delta = BROKEN_ALT_SVC_EXPIRE_DELAYS[3] - BROKEN_ALT_SVC_EXPIRE_DELAYS[0]; |
| 1085 test_clock_.AdvanceNow(delta); |
| 1086 test_task_runner->FastForwardBy(delta); |
| 1087 |
| 1088 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service1)); |
| 1089 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service2)); |
| 1090 } |
| 1091 |
| 986 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest; | 1092 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest; |
| 987 | 1093 |
| 988 TEST_F(SupportsQuicServerPropertiesTest, Set) { | 1094 TEST_F(SupportsQuicServerPropertiesTest, Set) { |
| 989 HostPortPair quic_server_google("www.google.com", 443); | 1095 HostPortPair quic_server_google("www.google.com", 443); |
| 990 | 1096 |
| 991 // Check by initializing empty address. | 1097 // Check by initializing empty address. |
| 992 IPAddress initial_address; | 1098 IPAddress initial_address; |
| 993 impl_.SetSupportsQuic(&initial_address); | 1099 impl_.SetSupportsQuic(&initial_address); |
| 994 | 1100 |
| 995 IPAddress address; | 1101 IPAddress address; |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1224 EXPECT_EQ(quic_server_info1, *(impl_.GetQuicServerInfo(quic_server_id))); | 1330 EXPECT_EQ(quic_server_info1, *(impl_.GetQuicServerInfo(quic_server_id))); |
| 1225 | 1331 |
| 1226 impl_.Clear(); | 1332 impl_.Clear(); |
| 1227 EXPECT_EQ(0u, impl_.quic_server_info_map().size()); | 1333 EXPECT_EQ(0u, impl_.quic_server_info_map().size()); |
| 1228 EXPECT_EQ(nullptr, impl_.GetQuicServerInfo(quic_server_id)); | 1334 EXPECT_EQ(nullptr, impl_.GetQuicServerInfo(quic_server_id)); |
| 1229 } | 1335 } |
| 1230 | 1336 |
| 1231 } // namespace | 1337 } // namespace |
| 1232 | 1338 |
| 1233 } // namespace net | 1339 } // namespace net |
| OLD | NEW |