Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(511)

Side by Side Diff: net/http/http_server_properties_impl_unittest.cc

Issue 2898983006: Fix and refactor HttpServerPropertiesImpl's alternative services brokenness expiration behavior (Closed)
Patch Set: Added checks for contents of expired_alt_svcs_ in BrokenAlternativeServicesTest Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
51 class HttpServerPropertiesImplTest : public testing::Test { 64 class HttpServerPropertiesImplTest : public testing::Test {
52 protected: 65 protected:
66 HttpServerPropertiesImplTest()
67 : test_task_runner_(new base::TestMockTimeTaskRunner()),
68 broken_services_clock_(test_task_runner_->GetMockTickClock()),
69 impl_(broken_services_clock_.get()) {}
70
53 bool HasAlternativeService(const url::SchemeHostPort& origin) { 71 bool HasAlternativeService(const url::SchemeHostPort& origin) {
54 const AlternativeServiceVector alternative_service_vector = 72 const AlternativeServiceVector alternative_service_vector =
55 impl_.GetAlternativeServices(origin); 73 impl_.GetAlternativeServices(origin);
56 return !alternative_service_vector.empty(); 74 return !alternative_service_vector.empty();
57 } 75 }
58 76
59 bool SetAlternativeService(const url::SchemeHostPort& origin, 77 bool SetAlternativeService(const url::SchemeHostPort& origin,
60 const AlternativeService& alternative_service) { 78 const AlternativeService& alternative_service) {
61 const base::Time expiration = 79 const base::Time expiration =
62 base::Time::Now() + base::TimeDelta::FromDays(1); 80 base::Time::Now() + base::TimeDelta::FromDays(1);
63 return impl_.SetAlternativeService(origin, alternative_service, expiration); 81 return impl_.SetAlternativeService(origin, alternative_service, expiration);
64 } 82 }
65 83
84 void MarkBrokenAndLetExpireAlternativeServiceNTimes(
85 const AlternativeService& alternative_service,
86 int num_times) {}
87
88 scoped_refptr<base::TestMockTimeTaskRunner> test_task_runner_;
89
90 std::unique_ptr<base::TickClock> broken_services_clock_;
66 HttpServerPropertiesImpl impl_; 91 HttpServerPropertiesImpl impl_;
67 }; 92 };
68 93
69 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest; 94 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest;
70 95
71 TEST_F(SpdyServerPropertiesTest, SetWithSchemeHostPort) { 96 TEST_F(SpdyServerPropertiesTest, SetWithSchemeHostPort) {
72 // Check spdy servers are correctly set with SchemeHostPort key. 97 // Check spdy servers are correctly set with SchemeHostPort key.
73 url::SchemeHostPort https_www_server("https", "www.google.com", 443); 98 url::SchemeHostPort https_www_server("https", "www.google.com", 443);
74 url::SchemeHostPort http_photo_server("http", "photos.google.com", 80); 99 url::SchemeHostPort http_photo_server("http", "photos.google.com", 80);
75 // Servers with port equal to default port in scheme will drop port components 100 // Servers with port equal to default port in scheme will drop port components
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 TEST_F(AlternateProtocolServerPropertiesTest, 951 TEST_F(AlternateProtocolServerPropertiesTest,
927 ExpireBrokenAlternateProtocolMappings) { 952 ExpireBrokenAlternateProtocolMappings) {
928 url::SchemeHostPort server("https", "foo", 443); 953 url::SchemeHostPort server("https", "foo", 443);
929 AlternativeService alternative_service(kProtoQUIC, "foo", 443); 954 AlternativeService alternative_service(kProtoQUIC, "foo", 443);
930 SetAlternativeService(server, alternative_service); 955 SetAlternativeService(server, alternative_service);
931 EXPECT_TRUE(HasAlternativeService(server)); 956 EXPECT_TRUE(HasAlternativeService(server));
932 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service)); 957 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service));
933 EXPECT_FALSE(impl_.WasAlternativeServiceRecentlyBroken(alternative_service)); 958 EXPECT_FALSE(impl_.WasAlternativeServiceRecentlyBroken(alternative_service));
934 959
935 base::TimeTicks past = 960 base::TimeTicks past =
936 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(42); 961 broken_services_clock_->NowTicks() - base::TimeDelta::FromSeconds(42);
937 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime( 962 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime(
938 impl_, alternative_service, past); 963 impl_, alternative_service, past);
939 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service)); 964 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service));
940 EXPECT_TRUE(impl_.WasAlternativeServiceRecentlyBroken(alternative_service)); 965 EXPECT_TRUE(impl_.WasAlternativeServiceRecentlyBroken(alternative_service));
941 966
942 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_); 967 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_);
943 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service)); 968 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service));
944 EXPECT_TRUE(impl_.WasAlternativeServiceRecentlyBroken(alternative_service)); 969 EXPECT_TRUE(impl_.WasAlternativeServiceRecentlyBroken(alternative_service));
945 } 970 }
946 971
947 // Regression test for https://crbug.com/505413. 972 // Regression test for https://crbug.com/505413.
948 TEST_F(AlternateProtocolServerPropertiesTest, RemoveExpiredBrokenAltSvc) { 973 TEST_F(AlternateProtocolServerPropertiesTest, RemoveExpiredBrokenAltSvc) {
949 url::SchemeHostPort foo_server("https", "foo", 443); 974 url::SchemeHostPort foo_server("https", "foo", 443);
950 AlternativeService bar_alternative_service(kProtoQUIC, "bar", 443); 975 AlternativeService bar_alternative_service(kProtoQUIC, "bar", 443);
951 SetAlternativeService(foo_server, bar_alternative_service); 976 SetAlternativeService(foo_server, bar_alternative_service);
952 EXPECT_TRUE(HasAlternativeService(foo_server)); 977 EXPECT_TRUE(HasAlternativeService(foo_server));
953 978
954 url::SchemeHostPort bar_server1("http", "bar", 80); 979 url::SchemeHostPort bar_server1("http", "bar", 80);
955 AlternativeService nohost_alternative_service(kProtoQUIC, "", 443); 980 AlternativeService nohost_alternative_service(kProtoQUIC, "", 443);
956 SetAlternativeService(bar_server1, nohost_alternative_service); 981 SetAlternativeService(bar_server1, nohost_alternative_service);
957 EXPECT_TRUE(HasAlternativeService(bar_server1)); 982 EXPECT_TRUE(HasAlternativeService(bar_server1));
958 983
959 url::SchemeHostPort bar_server2("https", "bar", 443); 984 url::SchemeHostPort bar_server2("https", "bar", 443);
960 AlternativeService baz_alternative_service(kProtoQUIC, "baz", 1234); 985 AlternativeService baz_alternative_service(kProtoQUIC, "baz", 1234);
961 SetAlternativeService(bar_server2, baz_alternative_service); 986 SetAlternativeService(bar_server2, baz_alternative_service);
962 EXPECT_TRUE(HasAlternativeService(bar_server2)); 987 EXPECT_TRUE(HasAlternativeService(bar_server2));
963 988
964 // Mark "bar:443" as broken. 989 // Mark "bar:443" as broken.
965 base::TimeTicks past = 990 base::TimeTicks past =
966 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(42); 991 broken_services_clock_->NowTicks() - base::TimeDelta::FromSeconds(42);
967 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime( 992 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime(
968 impl_, bar_alternative_service, past); 993 impl_, bar_alternative_service, past);
969 994
970 // Expire brokenness of "bar:443". 995 // Expire brokenness of "bar:443".
971 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_); 996 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_);
972 997
973 // "foo:443" should have no alternative service now. 998 // "foo:443" should have no alternative service now.
974 EXPECT_FALSE(HasAlternativeService(foo_server)); 999 EXPECT_FALSE(HasAlternativeService(foo_server));
975 // "bar:80" should have no alternative service now. 1000 // "bar:80" should have no alternative service now.
976 EXPECT_FALSE(HasAlternativeService(bar_server1)); 1001 EXPECT_FALSE(HasAlternativeService(bar_server1));
977 // The alternative service of "bar:443" should be unaffected. 1002 // The alternative service of "bar:443" should be unaffected.
978 EXPECT_TRUE(HasAlternativeService(bar_server2)); 1003 EXPECT_TRUE(HasAlternativeService(bar_server2));
979 1004
980 EXPECT_TRUE( 1005 EXPECT_TRUE(
981 impl_.WasAlternativeServiceRecentlyBroken(bar_alternative_service)); 1006 impl_.WasAlternativeServiceRecentlyBroken(bar_alternative_service));
982 EXPECT_FALSE( 1007 EXPECT_FALSE(
983 impl_.WasAlternativeServiceRecentlyBroken(baz_alternative_service)); 1008 impl_.WasAlternativeServiceRecentlyBroken(baz_alternative_service));
984 } 1009 }
985 1010
1011 // Regression test for https://crbug.com/724302
1012 TEST_F(AlternateProtocolServerPropertiesTest, RemoveExpiredBrokenAltSvc2) {
1013 // This test will mark an alternative service A that has already been marked
1014 // broken many times, then immediately mark another alternative service B as
1015 // broken for the first time. Because A's been marked broken many times
1016 // already, its brokenness will be scheduled to expire much further in the
1017 // future than B, even though it was marked broken before B. This test makes
1018 // sure that even though A was marked broken before B, B's brokenness should
1019 // expire before A.
1020
1021 url::SchemeHostPort server1("https", "foo", 443);
1022 AlternativeService alternative_service1(kProtoQUIC, "foo", 443);
1023 SetAlternativeService(server1, alternative_service1);
1024
1025 url::SchemeHostPort server2("https", "bar", 443);
1026 AlternativeService alternative_service2(kProtoQUIC, "bar", 443);
1027 SetAlternativeService(server2, alternative_service2);
1028
1029 // Repeatedly mark alt svc 1 broken and wait for its brokenness to expire.
1030 // This will increase its time until expiration.
1031 for (int i = 0; i < 3; ++i) {
1032 {
1033 base::TestMockTimeTaskRunner::ScopedContext scoped_context(
1034 test_task_runner_);
1035 impl_.MarkAlternativeServiceBroken(alternative_service1);
1036 }
1037 // |impl_| should have posted task to expire the brokenness of
1038 // |alternative_service1|
1039 EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount());
1040 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service1));
1041
1042 // Advance time by just enough so that |alternative_service1|'s brokenness
1043 // expires.
1044 test_task_runner_->FastForwardBy(BROKEN_ALT_SVC_EXPIRE_DELAYS[i]);
1045
1046 // Ensure brokenness of |alternative_service1| has expired.
1047 EXPECT_FALSE(test_task_runner_->HasPendingTask());
1048 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service1));
1049 }
1050
1051 {
1052 base::TestMockTimeTaskRunner::ScopedContext scoped_context(
1053 test_task_runner_);
1054 impl_.MarkAlternativeServiceBroken(alternative_service1);
1055 impl_.MarkAlternativeServiceBroken(alternative_service2);
1056 }
1057
1058 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service2));
1059
1060 // Advance time by just enough so that |alternative_service2|'s brokennness
1061 // expires.
1062 test_task_runner_->FastForwardBy(BROKEN_ALT_SVC_EXPIRE_DELAYS[0]);
1063
1064 EXPECT_TRUE(impl_.IsAlternativeServiceBroken(alternative_service1));
1065 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service2));
1066
1067 // Advance time by enough so that |alternative_service1|'s brokenness expires.
1068 test_task_runner_->FastForwardBy(BROKEN_ALT_SVC_EXPIRE_DELAYS[3] -
1069 BROKEN_ALT_SVC_EXPIRE_DELAYS[0]);
1070
1071 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service1));
1072 EXPECT_FALSE(impl_.IsAlternativeServiceBroken(alternative_service2));
1073 }
1074
986 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest; 1075 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest;
987 1076
988 TEST_F(SupportsQuicServerPropertiesTest, Set) { 1077 TEST_F(SupportsQuicServerPropertiesTest, Set) {
989 HostPortPair quic_server_google("www.google.com", 443); 1078 HostPortPair quic_server_google("www.google.com", 443);
990 1079
991 // Check by initializing empty address. 1080 // Check by initializing empty address.
992 IPAddress initial_address; 1081 IPAddress initial_address;
993 impl_.SetSupportsQuic(&initial_address); 1082 impl_.SetSupportsQuic(&initial_address);
994 1083
995 IPAddress address; 1084 IPAddress address;
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 EXPECT_EQ(quic_server_info1, *(impl_.GetQuicServerInfo(quic_server_id))); 1313 EXPECT_EQ(quic_server_info1, *(impl_.GetQuicServerInfo(quic_server_id)));
1225 1314
1226 impl_.Clear(); 1315 impl_.Clear();
1227 EXPECT_EQ(0u, impl_.quic_server_info_map().size()); 1316 EXPECT_EQ(0u, impl_.quic_server_info_map().size());
1228 EXPECT_EQ(nullptr, impl_.GetQuicServerInfo(quic_server_id)); 1317 EXPECT_EQ(nullptr, impl_.GetQuicServerInfo(quic_server_id));
1229 } 1318 }
1230 1319
1231 } // namespace 1320 } // namespace
1232 1321
1233 } // namespace net 1322 } // namespace net
OLDNEW
« net/http/broken_alternative_services_unittest.cc ('K') | « net/http/http_server_properties_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698