Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / browser / extension_throttle_unittest.cc
blob480700dc17d82741aae94e2102f121eab8775530
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
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/pickle.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "extensions/browser/extension_throttle_entry.h"
13 #include "extensions/browser/extension_throttle_manager.h"
14 #include "extensions/browser/extension_throttle_test_support.h"
15 #include "net/base/load_flags.h"
16 #include "net/base/request_priority.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/url_request/url_request.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using base::TimeDelta;
24 using base::TimeTicks;
25 using net::BackoffEntry;
26 using net::NetworkChangeNotifier;
27 using net::TestNetworkDelegate;
28 using net::TestURLRequestContext;
29 using net::URLRequest;
30 using net::URLRequestContext;
32 namespace extensions {
34 namespace {
36 class MockExtensionThrottleEntry : public ExtensionThrottleEntry {
37 public:
38 explicit MockExtensionThrottleEntry(ExtensionThrottleManager* manager)
39 : ExtensionThrottleEntry(manager, std::string()),
40 backoff_entry_(&backoff_policy_, &fake_clock_) {
41 InitPolicy();
43 MockExtensionThrottleEntry(ExtensionThrottleManager* manager,
44 const TimeTicks& exponential_backoff_release_time,
45 const TimeTicks& sliding_window_release_time,
46 const TimeTicks& fake_now)
47 : ExtensionThrottleEntry(manager, std::string()),
48 fake_clock_(fake_now),
49 backoff_entry_(&backoff_policy_, &fake_clock_) {
50 InitPolicy();
52 set_exponential_backoff_release_time(exponential_backoff_release_time);
53 set_sliding_window_release_time(sliding_window_release_time);
56 void InitPolicy() {
57 // Some tests become flaky if we have jitter.
58 backoff_policy_.jitter_factor = 0.0;
60 // This lets us avoid having to make multiple failures initially (this
61 // logic is already tested in the BackoffEntry unit tests).
62 backoff_policy_.num_errors_to_ignore = 0;
65 const BackoffEntry* GetBackoffEntry() const override {
66 return &backoff_entry_;
69 BackoffEntry* GetBackoffEntry() override { return &backoff_entry_; }
71 static bool ExplicitUserRequest(int load_flags) {
72 return ExtensionThrottleEntry::ExplicitUserRequest(load_flags);
75 void ResetToBlank(const TimeTicks& time_now) {
76 fake_clock_.set_now(time_now);
78 GetBackoffEntry()->Reset();
79 set_sliding_window_release_time(time_now);
82 // Overridden for tests.
83 TimeTicks ImplGetTimeNow() const override { return fake_clock_.NowTicks(); }
85 void set_fake_now(const TimeTicks& now) { fake_clock_.set_now(now); }
87 void set_exponential_backoff_release_time(const TimeTicks& release_time) {
88 GetBackoffEntry()->SetCustomReleaseTime(release_time);
91 TimeTicks sliding_window_release_time() const {
92 return ExtensionThrottleEntry::sliding_window_release_time();
95 void set_sliding_window_release_time(const TimeTicks& release_time) {
96 ExtensionThrottleEntry::set_sliding_window_release_time(release_time);
99 protected:
100 ~MockExtensionThrottleEntry() override {}
102 private:
103 mutable TestTickClock fake_clock_;
104 BackoffEntry backoff_entry_;
107 class MockExtensionThrottleManager : public ExtensionThrottleManager {
108 public:
109 MockExtensionThrottleManager() : create_entry_index_(0) {}
111 // Method to process the URL using ExtensionThrottleManager protected
112 // method.
113 std::string DoGetUrlIdFromUrl(const GURL& url) { return GetIdFromUrl(url); }
115 // Method to use the garbage collecting method of ExtensionThrottleManager.
116 void DoGarbageCollectEntries() { GarbageCollectEntries(); }
118 // Returns the number of entries in the map.
119 int GetNumberOfEntries() const { return GetNumberOfEntriesForTests(); }
121 void CreateEntry(bool is_outdated) {
122 TimeTicks time = TimeTicks::Now();
123 if (is_outdated) {
124 time -= TimeDelta::FromMilliseconds(
125 MockExtensionThrottleEntry::kDefaultEntryLifetimeMs + 1000);
127 std::string fake_url_string("http://www.fakeurl.com/");
128 fake_url_string.append(base::IntToString(create_entry_index_++));
129 GURL fake_url(fake_url_string);
130 OverrideEntryForTests(
131 fake_url, new MockExtensionThrottleEntry(this, time, TimeTicks::Now(),
132 TimeTicks::Now()));
135 private:
136 int create_entry_index_;
139 struct TimeAndBool {
140 TimeAndBool(const TimeTicks& time_value, bool expected, int line_num) {
141 time = time_value;
142 result = expected;
143 line = line_num;
145 TimeTicks time;
146 bool result;
147 int line;
150 struct GurlAndString {
151 GurlAndString(const GURL& url_value,
152 const std::string& expected,
153 int line_num) {
154 url = url_value;
155 result = expected;
156 line = line_num;
158 GURL url;
159 std::string result;
160 int line;
163 } // namespace
165 class ExtensionThrottleEntryTest : public testing::Test {
166 protected:
167 ExtensionThrottleEntryTest()
168 : request_(context_.CreateRequest(GURL(), net::DEFAULT_PRIORITY, NULL)) {}
170 void SetUp() override;
172 TimeTicks now_;
173 MockExtensionThrottleManager manager_; // Dummy object, not used.
174 scoped_refptr<MockExtensionThrottleEntry> entry_;
175 base::MessageLoopForIO message_loop_;
177 TestURLRequestContext context_;
178 scoped_ptr<URLRequest> request_;
181 void ExtensionThrottleEntryTest::SetUp() {
182 request_->SetLoadFlags(0);
184 now_ = TimeTicks::Now();
185 entry_ = new MockExtensionThrottleEntry(&manager_);
186 entry_->ResetToBlank(now_);
189 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) {
190 return out << time.ToInternalValue();
193 TEST_F(ExtensionThrottleEntryTest, CanThrottleRequest) {
194 entry_->set_exponential_backoff_release_time(entry_->ImplGetTimeNow() +
195 TimeDelta::FromMilliseconds(1));
197 EXPECT_TRUE(entry_->ShouldRejectRequest(*request_));
200 TEST_F(ExtensionThrottleEntryTest, InterfaceDuringExponentialBackoff) {
201 entry_->set_exponential_backoff_release_time(entry_->ImplGetTimeNow() +
202 TimeDelta::FromMilliseconds(1));
203 EXPECT_TRUE(entry_->ShouldRejectRequest(*request_));
205 // Also end-to-end test the load flags exceptions.
206 request_->SetLoadFlags(net::LOAD_MAYBE_USER_GESTURE);
207 EXPECT_FALSE(entry_->ShouldRejectRequest(*request_));
210 TEST_F(ExtensionThrottleEntryTest, InterfaceNotDuringExponentialBackoff) {
211 entry_->set_exponential_backoff_release_time(entry_->ImplGetTimeNow());
212 EXPECT_FALSE(entry_->ShouldRejectRequest(*request_));
213 entry_->set_exponential_backoff_release_time(entry_->ImplGetTimeNow() -
214 TimeDelta::FromMilliseconds(1));
215 EXPECT_FALSE(entry_->ShouldRejectRequest(*request_));
218 TEST_F(ExtensionThrottleEntryTest, InterfaceUpdateFailure) {
219 entry_->UpdateWithResponse(503);
220 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(),
221 entry_->ImplGetTimeNow())
222 << "A failure should increase the release_time";
225 TEST_F(ExtensionThrottleEntryTest, InterfaceUpdateSuccess) {
226 entry_->UpdateWithResponse(200);
227 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(),
228 entry_->ImplGetTimeNow())
229 << "A success should not add any delay";
232 TEST_F(ExtensionThrottleEntryTest, InterfaceUpdateSuccessThenFailure) {
233 entry_->UpdateWithResponse(200);
234 entry_->UpdateWithResponse(503);
235 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(),
236 entry_->ImplGetTimeNow())
237 << "This scenario should add delay";
238 entry_->UpdateWithResponse(200);
241 TEST_F(ExtensionThrottleEntryTest, IsEntryReallyOutdated) {
242 TimeDelta lifetime = TimeDelta::FromMilliseconds(
243 MockExtensionThrottleEntry::kDefaultEntryLifetimeMs);
244 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5);
246 TimeAndBool test_values[] = {
247 TimeAndBool(now_, false, __LINE__),
248 TimeAndBool(now_ - kFiveMs, false, __LINE__),
249 TimeAndBool(now_ + kFiveMs, false, __LINE__),
250 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__),
251 TimeAndBool(now_ - lifetime, true, __LINE__),
252 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)};
254 for (unsigned int i = 0; i < arraysize(test_values); ++i) {
255 entry_->set_exponential_backoff_release_time(test_values[i].time);
256 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result)
257 << "Test case #" << i << " line " << test_values[i].line << " failed";
261 TEST_F(ExtensionThrottleEntryTest, MaxAllowedBackoff) {
262 for (int i = 0; i < 30; ++i) {
263 entry_->UpdateWithResponse(503);
266 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_;
267 EXPECT_EQ(delay.InMilliseconds(),
268 MockExtensionThrottleEntry::kDefaultMaximumBackoffMs);
271 TEST_F(ExtensionThrottleEntryTest, MalformedContent) {
272 for (int i = 0; i < 5; ++i)
273 entry_->UpdateWithResponse(503);
275 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime();
277 // Inform the entry that a response body was malformed, which is supposed to
278 // increase the back-off time. Note that we also submit a successful
279 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that
280 // is what happens in practice (if a body is received, then a non-500
281 // response must also have been received).
282 entry_->ReceivedContentWasMalformed(200);
283 entry_->UpdateWithResponse(200);
284 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures);
287 TEST_F(ExtensionThrottleEntryTest, SlidingWindow) {
288 int max_send = ExtensionThrottleEntry::kDefaultMaxSendThreshold;
289 int sliding_window = ExtensionThrottleEntry::kDefaultSlidingWindowPeriodMs;
291 TimeTicks time_1 = entry_->ImplGetTimeNow() +
292 TimeDelta::FromMilliseconds(sliding_window / 3);
293 TimeTicks time_2 = entry_->ImplGetTimeNow() +
294 TimeDelta::FromMilliseconds(2 * sliding_window / 3);
295 TimeTicks time_3 =
296 entry_->ImplGetTimeNow() + TimeDelta::FromMilliseconds(sliding_window);
297 TimeTicks time_4 =
298 entry_->ImplGetTimeNow() +
299 TimeDelta::FromMilliseconds(sliding_window + 2 * sliding_window / 3);
301 entry_->set_exponential_backoff_release_time(time_1);
303 for (int i = 0; i < max_send / 2; ++i) {
304 EXPECT_EQ(2 * sliding_window / 3,
305 entry_->ReserveSendingTimeForNextRequest(time_2));
307 EXPECT_EQ(time_2, entry_->sliding_window_release_time());
309 entry_->set_fake_now(time_3);
311 for (int i = 0; i < (max_send + 1) / 2; ++i)
312 EXPECT_EQ(0, entry_->ReserveSendingTimeForNextRequest(TimeTicks()));
314 EXPECT_EQ(time_4, entry_->sliding_window_release_time());
317 TEST_F(ExtensionThrottleEntryTest, ExplicitUserRequest) {
318 ASSERT_FALSE(MockExtensionThrottleEntry::ExplicitUserRequest(0));
319 ASSERT_TRUE(MockExtensionThrottleEntry::ExplicitUserRequest(
320 net::LOAD_MAYBE_USER_GESTURE));
321 ASSERT_FALSE(MockExtensionThrottleEntry::ExplicitUserRequest(
322 ~net::LOAD_MAYBE_USER_GESTURE));
325 class ExtensionThrottleManagerTest : public testing::Test {
326 protected:
327 ExtensionThrottleManagerTest()
328 : request_(context_.CreateRequest(GURL(), net::DEFAULT_PRIORITY, NULL)) {}
330 void SetUp() override { request_->SetLoadFlags(0); }
332 void ExpectEntryAllowsAllOnErrorIfOptedOut(
333 ExtensionThrottleEntryInterface* entry,
334 bool opted_out,
335 const URLRequest& request) {
336 EXPECT_FALSE(entry->ShouldRejectRequest(request));
337 for (int i = 0; i < 10; ++i) {
338 entry->UpdateWithResponse(503);
340 EXPECT_NE(opted_out, entry->ShouldRejectRequest(request));
342 if (opted_out) {
343 // We're not mocking out GetTimeNow() in this scenario
344 // so add a 100 ms buffer to avoid flakiness (that should always
345 // give enough time to get from the TimeTicks::Now() call here
346 // to the TimeTicks::Now() call in the entry class).
347 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
348 entry->GetExponentialBackoffReleaseTime());
349 } else {
350 // As above, add 100 ms.
351 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
352 entry->GetExponentialBackoffReleaseTime());
356 base::MessageLoopForIO message_loop_;
357 // context_ must be declared before request_.
358 TestURLRequestContext context_;
359 scoped_ptr<URLRequest> request_;
362 TEST_F(ExtensionThrottleManagerTest, IsUrlStandardised) {
363 MockExtensionThrottleManager manager;
364 GurlAndString test_values[] = {
365 GurlAndString(GURL("http://www.example.com"),
366 std::string("http://www.example.com/"), __LINE__),
367 GurlAndString(GURL("http://www.Example.com"),
368 std::string("http://www.example.com/"), __LINE__),
369 GurlAndString(GURL("http://www.ex4mple.com/Pr4c71c41"),
370 std::string("http://www.ex4mple.com/pr4c71c41"), __LINE__),
371 GurlAndString(GURL("http://www.example.com/0/token/false"),
372 std::string("http://www.example.com/0/token/false"),
373 __LINE__),
374 GurlAndString(GURL("http://www.example.com/index.php?code=javascript"),
375 std::string("http://www.example.com/index.php"), __LINE__),
376 GurlAndString(GURL("http://www.example.com/index.php?code=1#superEntry"),
377 std::string("http://www.example.com/index.php"), __LINE__),
378 GurlAndString(GURL("http://www.example.com/index.php#superEntry"),
379 std::string("http://www.example.com/index.php"), __LINE__),
380 GurlAndString(GURL("http://www.example.com:1234/"),
381 std::string("http://www.example.com:1234/"), __LINE__)};
383 for (unsigned int i = 0; i < arraysize(test_values); ++i) {
384 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url);
385 EXPECT_EQ(temp, test_values[i].result) << "Test case #" << i << " line "
386 << test_values[i].line << " failed";
390 TEST_F(ExtensionThrottleManagerTest, AreEntriesBeingCollected) {
391 MockExtensionThrottleManager manager;
393 manager.CreateEntry(true); // true = Entry is outdated.
394 manager.CreateEntry(true);
395 manager.CreateEntry(true);
396 manager.DoGarbageCollectEntries();
397 EXPECT_EQ(0, manager.GetNumberOfEntries());
399 manager.CreateEntry(false);
400 manager.CreateEntry(false);
401 manager.CreateEntry(false);
402 manager.CreateEntry(true);
403 manager.DoGarbageCollectEntries();
404 EXPECT_EQ(3, manager.GetNumberOfEntries());
407 TEST_F(ExtensionThrottleManagerTest, IsHostBeingRegistered) {
408 MockExtensionThrottleManager manager;
410 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
411 manager.RegisterRequestUrl(GURL("http://www.google.com/"));
412 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0"));
413 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1"));
414 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure"));
416 EXPECT_EQ(3, manager.GetNumberOfEntries());
419 TEST_F(ExtensionThrottleManagerTest, LocalHostOptedOut) {
420 MockExtensionThrottleManager manager;
421 // A localhost entry should always be opted out.
422 scoped_refptr<ExtensionThrottleEntryInterface> localhost_entry =
423 manager.RegisterRequestUrl(GURL("http://localhost/hello"));
424 EXPECT_FALSE(localhost_entry->ShouldRejectRequest((*request_)));
425 for (int i = 0; i < 10; ++i) {
426 localhost_entry->UpdateWithResponse(503);
428 EXPECT_FALSE(localhost_entry->ShouldRejectRequest((*request_)));
430 // We're not mocking out GetTimeNow() in this scenario
431 // so add a 100 ms buffer to avoid flakiness (that should always
432 // give enough time to get from the TimeTicks::Now() call here
433 // to the TimeTicks::Now() call in the entry class).
434 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
435 localhost_entry->GetExponentialBackoffReleaseTime());
438 TEST_F(ExtensionThrottleManagerTest, ClearOnNetworkChange) {
439 for (int i = 0; i < 3; ++i) {
440 MockExtensionThrottleManager manager;
441 scoped_refptr<ExtensionThrottleEntryInterface> entry_before =
442 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
443 for (int j = 0; j < 10; ++j) {
444 entry_before->UpdateWithResponse(503);
446 EXPECT_TRUE(entry_before->ShouldRejectRequest(*request_));
448 switch (i) {
449 case 0:
450 manager.OnIPAddressChanged();
451 break;
452 case 1:
453 manager.OnConnectionTypeChanged(
454 NetworkChangeNotifier::CONNECTION_UNKNOWN);
455 break;
456 case 2:
457 manager.OnConnectionTypeChanged(NetworkChangeNotifier::CONNECTION_NONE);
458 break;
459 default:
460 FAIL();
463 scoped_refptr<ExtensionThrottleEntryInterface> entry_after =
464 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
465 EXPECT_FALSE(entry_after->ShouldRejectRequest(*request_));
469 } // namespace extensions