| OLD | NEW |
| (Empty) | |
| 1 // Copyright 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/reporting/reporting_endpoint_manager.h" |
| 6 |
| 7 #include "base/test/simple_test_tick_clock.h" |
| 8 #include "base/time/time.h" |
| 9 #include "net/base/backoff_entry.h" |
| 10 #include "net/reporting/reporting_cache.h" |
| 11 #include "net/reporting/reporting_test_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "url/gurl.h" |
| 14 #include "url/origin.h" |
| 15 |
| 16 namespace net { |
| 17 namespace { |
| 18 |
| 19 const url::Origin kOrigin(GURL("https://origin/")); |
| 20 const GURL kEndpoint("https://endpoint/"); |
| 21 const std::string kGroup("group"); |
| 22 |
| 23 class ReportingEndpointManagerTest : public ::testing::Test { |
| 24 protected: |
| 25 ReportingEndpointManagerTest() |
| 26 : manager_(&clock_, &cache_, &backoff_policy_) { |
| 27 backoff_policy_.num_errors_to_ignore = 0; |
| 28 backoff_policy_.initial_delay_ms = 60000; |
| 29 backoff_policy_.multiply_factor = 2.0; |
| 30 backoff_policy_.jitter_factor = 0.0; |
| 31 backoff_policy_.maximum_backoff_ms = -1; |
| 32 backoff_policy_.entry_lifetime_ms = 0; |
| 33 backoff_policy_.always_use_initial_delay = false; |
| 34 } |
| 35 |
| 36 base::TimeTicks yesterday() { |
| 37 return clock_.NowTicks() - base::TimeDelta::FromDays(1); |
| 38 } |
| 39 |
| 40 base::TimeTicks tomorrow() { |
| 41 return clock_.NowTicks() + base::TimeDelta::FromDays(1); |
| 42 } |
| 43 |
| 44 base::SimpleTestTickClock clock_; |
| 45 ReportingCache cache_; |
| 46 BackoffEntry::Policy backoff_policy_; |
| 47 ReportingEndpointManager manager_; |
| 48 }; |
| 49 |
| 50 TEST_F(ReportingEndpointManagerTest, NoEndpoint) { |
| 51 GURL endpoint_url; |
| 52 bool found_endpoint = |
| 53 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 54 EXPECT_FALSE(found_endpoint); |
| 55 } |
| 56 |
| 57 TEST_F(ReportingEndpointManagerTest, Endpoint) { |
| 58 cache_.SetClient(kOrigin, kEndpoint, false, kGroup, tomorrow()); |
| 59 |
| 60 GURL endpoint_url; |
| 61 bool found_endpoint = |
| 62 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 63 EXPECT_TRUE(found_endpoint); |
| 64 EXPECT_EQ(kEndpoint, endpoint_url); |
| 65 } |
| 66 |
| 67 TEST_F(ReportingEndpointManagerTest, ExpiredEndpoint) { |
| 68 cache_.SetClient(kOrigin, kEndpoint, false, kGroup, yesterday()); |
| 69 |
| 70 GURL endpoint_url; |
| 71 bool found_endpoint = |
| 72 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 73 EXPECT_FALSE(found_endpoint); |
| 74 } |
| 75 |
| 76 TEST_F(ReportingEndpointManagerTest, PendingEndpoint) { |
| 77 cache_.SetClient(kOrigin, kEndpoint, false, kGroup, tomorrow()); |
| 78 |
| 79 manager_.SetEndpointPending(kEndpoint); |
| 80 |
| 81 GURL endpoint_url; |
| 82 bool found_endpoint = |
| 83 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 84 EXPECT_FALSE(found_endpoint); |
| 85 |
| 86 manager_.ClearEndpointPending(kEndpoint); |
| 87 |
| 88 found_endpoint = |
| 89 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 90 EXPECT_TRUE(found_endpoint); |
| 91 EXPECT_EQ(kEndpoint, endpoint_url); |
| 92 } |
| 93 |
| 94 TEST_F(ReportingEndpointManagerTest, BackedOffEndpoint) { |
| 95 cache_.SetClient(kOrigin, kEndpoint, false, kGroup, tomorrow()); |
| 96 |
| 97 manager_.InformOfEndpointRequest(kEndpoint, false); |
| 98 |
| 99 GURL endpoint_url; |
| 100 bool found_endpoint = |
| 101 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 102 EXPECT_FALSE(found_endpoint); |
| 103 |
| 104 clock_.Advance( |
| 105 2 * base::TimeDelta::FromMilliseconds(backoff_policy_.initial_delay_ms)); |
| 106 |
| 107 found_endpoint = |
| 108 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 109 EXPECT_TRUE(found_endpoint); |
| 110 EXPECT_EQ(kEndpoint, endpoint_url); |
| 111 } |
| 112 |
| 113 // Make sure that multiple endpoints will all be returned at some point, to |
| 114 // avoid accidentally or intentionally implementing any priority ordering. |
| 115 TEST_F(ReportingEndpointManagerTest, RandomEndpoint) { |
| 116 static const GURL kEndpoint1("https://endpoint1/"); |
| 117 static const GURL kEndpoint2("https://endpoint2/"); |
| 118 static const int kMaxAttempts = 20; |
| 119 |
| 120 cache_.SetClient(kOrigin, kEndpoint1, false, kGroup, tomorrow()); |
| 121 cache_.SetClient(kOrigin, kEndpoint2, false, kGroup, tomorrow()); |
| 122 |
| 123 bool endpoint1_seen = false; |
| 124 bool endpoint2_seen = false; |
| 125 |
| 126 for (int i = 0; i < kMaxAttempts; i++) { |
| 127 GURL endpoint_url; |
| 128 bool found_endpoint = |
| 129 manager_.FindEndpointForOriginAndGroup(kOrigin, kGroup, &endpoint_url); |
| 130 ASSERT_TRUE(found_endpoint); |
| 131 ASSERT_TRUE(endpoint_url == kEndpoint1 || endpoint_url == kEndpoint2); |
| 132 |
| 133 if (endpoint_url == kEndpoint1) |
| 134 endpoint1_seen = true; |
| 135 else if (endpoint_url == kEndpoint2) |
| 136 endpoint2_seen = true; |
| 137 |
| 138 if (endpoint1_seen && endpoint2_seen) |
| 139 break; |
| 140 } |
| 141 |
| 142 EXPECT_TRUE(endpoint1_seen); |
| 143 EXPECT_TRUE(endpoint2_seen); |
| 144 } |
| 145 |
| 146 } // namespace |
| 147 } // namespace net |
| OLD | NEW |