Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/http/broken_alternative_services.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/test/test_mock_time_task_runner.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const base::TimeDelta BROKEN_ALT_SVC_EXPIRE_DELAYS[10] = { | |
| 19 base::TimeDelta::FromSeconds(300), base::TimeDelta::FromSeconds(600), | |
| 20 base::TimeDelta::FromSeconds(1200), base::TimeDelta::FromSeconds(2400), | |
| 21 base::TimeDelta::FromSeconds(4800), base::TimeDelta::FromSeconds(9600), | |
| 22 base::TimeDelta::FromSeconds(19200), base::TimeDelta::FromSeconds(38400), | |
| 23 base::TimeDelta::FromSeconds(76800), base::TimeDelta::FromSeconds(153600), | |
| 24 }; | |
| 25 | |
| 26 class TestClock : public BrokenAlternativeServices::Clock { | |
| 27 public: | |
| 28 TestClock() : now_(base::TimeTicks::Now()) {} | |
| 29 ~TestClock() override {} | |
| 30 | |
| 31 base::TimeTicks Now() const override { return now_; } | |
| 32 | |
| 33 void Set(base::TimeTicks now) { now_ = now; } | |
| 34 void Advance(base::TimeDelta delta) { now_ += delta; } | |
| 35 | |
| 36 private: | |
| 37 base::TimeTicks now_; | |
| 38 }; | |
| 39 | |
| 40 class BrokenAlternativeServicesTest | |
| 41 : public BrokenAlternativeServices::Delegate, | |
| 42 public ::testing::Test { | |
| 43 public: | |
| 44 BrokenAlternativeServicesTest() | |
| 45 : services_(this, &test_clock_), | |
| 46 test_task_runner_(new base::TestMockTimeTaskRunner()) {} | |
| 47 | |
| 48 // BrokenAlternativeServices::Delegate implementation | |
| 49 void OnExpireBrokenAlternativeService( | |
| 50 const AlternativeService& expired_alternative_service) override { | |
| 51 expired_alt_svcs.push_back(expired_alternative_service); | |
| 52 } | |
| 53 | |
| 54 ::testing::AssertionResult MarkAlternativeServiceBrokenAndLetExpireNTimes( | |
|
Ryan Hamilton
2017/05/26 03:52:09
This method is quite complicated. It seems like it
wangyix1
2017/05/27 01:20:11
Done.
| |
| 55 const AlternativeService& alternative_service, | |
| 56 int num_times) { | |
| 57 DCHECK(num_times < 4); | |
| 58 | |
| 59 for (int i = 0; i < num_times; ++i) { | |
| 60 { | |
| 61 base::TestMockTimeTaskRunner::ScopedContext scoped_context( | |
| 62 test_task_runner_); | |
|
Ryan Hamilton
2017/05/26 03:52:09
Is this required? From reading the class, I don't
wangyix1
2017/05/27 01:20:11
Done.
| |
| 63 services_.MarkAlternativeServiceBroken(alternative_service); | |
| 64 } | |
| 65 if (!services_.IsAlternativeServiceBroken(alternative_service)) { | |
| 66 return ::testing::AssertionFailure() << "Alt svc not showing as broken " | |
| 67 << "after being marked broken."; | |
| 68 } | |
| 69 | |
| 70 // |services_| should have posted task to expire the brokenness of | |
| 71 // |alternative_service|. | |
| 72 int pending_task_count = test_task_runner_->GetPendingTaskCount(); | |
| 73 if (pending_task_count != 1u) { | |
| 74 return ::testing::AssertionFailure() | |
| 75 << "Test task runner has " << pending_task_count | |
| 76 << " pending tasks instead of 1 after marking " | |
| 77 << "alternative service broken."; | |
| 78 } | |
| 79 | |
| 80 // Advance time by just enough so that |alternative_service|'s brokenness | |
| 81 // expires. | |
| 82 base::TimeDelta delta = BROKEN_ALT_SVC_EXPIRE_DELAYS[std::min(i, 9)]; | |
| 83 test_clock_.Advance(delta); | |
| 84 test_task_runner_->FastForwardBy(delta); | |
| 85 | |
| 86 // Ensure |alternative_service| brokenness has expired | |
| 87 if (services_.IsAlternativeServiceBroken(alternative_service)) { | |
| 88 return ::testing::AssertionFailure() << "Alt svc showing as broken " | |
| 89 << " after being confirmed."; | |
| 90 } | |
| 91 } | |
| 92 return ::testing::AssertionSuccess(); | |
| 93 } | |
| 94 | |
| 95 TestClock test_clock_; | |
| 96 std::vector<AlternativeService> expired_alt_svcs; | |
| 97 BrokenAlternativeServices services_; | |
|
Ryan Hamilton
2017/05/26 03:52:09
nit: how 'bout "broken_services_"
wangyix1
2017/05/27 01:20:11
Done.
| |
| 98 | |
| 99 scoped_refptr<base::TestMockTimeTaskRunner> test_task_runner_; | |
| 100 }; | |
| 101 | |
| 102 TEST_F(BrokenAlternativeServicesTest, MarkBroken) { | |
| 103 const AlternativeService alternative_service1(kProtoHTTP2, "foo", 443); | |
| 104 const AlternativeService alternative_service2(kProtoHTTP2, "foo", 1234); | |
| 105 | |
| 106 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 107 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 108 | |
| 109 services_.MarkAlternativeServiceBroken(alternative_service1); | |
| 110 | |
| 111 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 112 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 113 | |
| 114 services_.MarkAlternativeServiceBroken(alternative_service2); | |
| 115 | |
| 116 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 117 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 118 | |
| 119 services_.ConfirmAlternativeService(alternative_service1); | |
| 120 | |
| 121 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 122 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 123 | |
| 124 services_.ConfirmAlternativeService(alternative_service2); | |
| 125 | |
| 126 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 127 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 128 } | |
| 129 | |
| 130 TEST_F(BrokenAlternativeServicesTest, MarkRecentlyBroken) { | |
| 131 const AlternativeService alternative_service(kProtoHTTP2, "foo", 443); | |
| 132 | |
| 133 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service)); | |
| 134 EXPECT_FALSE( | |
| 135 services_.WasAlternativeServiceRecentlyBroken(alternative_service)); | |
| 136 | |
| 137 services_.MarkAlternativeServiceRecentlyBroken(alternative_service); | |
| 138 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service)); | |
| 139 EXPECT_TRUE( | |
| 140 services_.WasAlternativeServiceRecentlyBroken(alternative_service)); | |
| 141 | |
| 142 services_.ConfirmAlternativeService(alternative_service); | |
| 143 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service)); | |
| 144 EXPECT_FALSE( | |
| 145 services_.WasAlternativeServiceRecentlyBroken(alternative_service)); | |
| 146 } | |
| 147 | |
| 148 TEST_F(BrokenAlternativeServicesTest, ExpireBrokenAlternateProtoclMappings) { | |
| 149 AlternativeService alternative_service(kProtoQUIC, "foo", 443); | |
| 150 | |
| 151 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service)); | |
| 152 EXPECT_FALSE( | |
| 153 services_.WasAlternativeServiceRecentlyBroken(alternative_service)); | |
| 154 | |
| 155 EXPECT_TRUE( | |
| 156 MarkAlternativeServiceBrokenAndLetExpireNTimes(alternative_service, 1)); | |
| 157 } | |
| 158 | |
| 159 TEST_F(BrokenAlternativeServicesTest, RemoveExpiredBrokenAltSvc) { | |
| 160 // This test will mark broken an alternative service A that has already been | |
| 161 // marked broken many times, then immediately mark another alternative service | |
| 162 // B as broken for the first time. Because A's been marked broken many times | |
| 163 // already, its brokenness will be scheduled to expire much further in the | |
| 164 // future than B, even though it was marked broken before B. This test makes | |
| 165 // sure that even though A was marked broken before B, B's brokenness should | |
| 166 // expire before A. | |
| 167 | |
| 168 AlternativeService alternative_service1(kProtoQUIC, "foo", 443); | |
| 169 AlternativeService alternative_service2(kProtoQUIC, "bar", 443); | |
| 170 | |
| 171 EXPECT_TRUE( | |
| 172 MarkAlternativeServiceBrokenAndLetExpireNTimes(alternative_service1, 3)); | |
| 173 | |
| 174 { | |
| 175 base::TestMockTimeTaskRunner::ScopedContext scoped_context( | |
| 176 test_task_runner_); | |
| 177 | |
| 178 services_.MarkAlternativeServiceBroken(alternative_service1); | |
| 179 services_.MarkAlternativeServiceBroken(alternative_service2); | |
| 180 } | |
| 181 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 182 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 183 | |
| 184 // Advance time until one time quantum before |alternative_service2|'s | |
| 185 // brokenness expires. | |
| 186 base::TimeDelta delta = | |
| 187 BROKEN_ALT_SVC_EXPIRE_DELAYS[0] - base::TimeDelta::FromInternalValue(1); | |
| 188 test_clock_.Advance(delta); | |
| 189 test_task_runner_->FastForwardBy(delta); | |
| 190 | |
| 191 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 192 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 193 | |
| 194 // Advance time by one time quantum. |alternative_service2| should no longer | |
| 195 // be broken. | |
| 196 delta = base::TimeDelta::FromInternalValue(1); | |
| 197 test_clock_.Advance(delta); | |
| 198 test_task_runner_->FastForwardBy(delta); | |
| 199 | |
| 200 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 201 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 202 | |
| 203 // Advance time until one time quantum before |alternative_service1|'s | |
| 204 // brokenness expires | |
| 205 delta = BROKEN_ALT_SVC_EXPIRE_DELAYS[3] - BROKEN_ALT_SVC_EXPIRE_DELAYS[0] - | |
| 206 base::TimeDelta::FromInternalValue(1); | |
| 207 test_clock_.Advance(delta); | |
| 208 test_task_runner_->FastForwardBy(delta); | |
| 209 | |
| 210 EXPECT_TRUE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 211 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 212 | |
| 213 // Advance time by one time quantum. |alternative_service1| should no longer | |
| 214 // be broken. | |
| 215 delta = base::TimeDelta::FromInternalValue(1); | |
| 216 test_clock_.Advance(delta); | |
| 217 test_task_runner_->FastForwardBy(delta); | |
| 218 | |
| 219 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service1)); | |
| 220 EXPECT_FALSE(services_.IsAlternativeServiceBroken(alternative_service2)); | |
| 221 } | |
| 222 | |
| 223 } // namespace | |
| 224 | |
| 225 } // namespace net | |
| OLD | NEW |