| 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 <string> |
| 8 |
| 9 #include "base/test/simple_test_tick_clock.h" |
| 10 #include "base/time/time.h" |
| 11 #include "net/base/backoff_entry.h" |
| 12 #include "net/reporting/reporting_cache.h" |
| 13 #include "net/reporting/reporting_client.h" |
| 14 #include "net/reporting/reporting_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" |
| 17 #include "url/origin.h" |
| 18 |
| 19 namespace net { |
| 20 namespace { |
| 21 |
| 22 class ReportingEndpointManagerTest : public ::testing::Test { |
| 23 protected: |
| 24 ReportingEndpointManagerTest() |
| 25 : manager_(&clock_, &cache_, &backoff_policy_) { |
| 26 backoff_policy_.num_errors_to_ignore = 0; |
| 27 backoff_policy_.initial_delay_ms = 60000; |
| 28 backoff_policy_.multiply_factor = 2.0; |
| 29 backoff_policy_.jitter_factor = 0.0; |
| 30 backoff_policy_.maximum_backoff_ms = -1; |
| 31 backoff_policy_.entry_lifetime_ms = 0; |
| 32 backoff_policy_.always_use_initial_delay = false; |
| 33 } |
| 34 |
| 35 base::TimeTicks yesterday() { |
| 36 return clock_.NowTicks() - base::TimeDelta::FromDays(1); |
| 37 } |
| 38 |
| 39 base::TimeTicks tomorrow() { |
| 40 return clock_.NowTicks() + base::TimeDelta::FromDays(1); |
| 41 } |
| 42 |
| 43 const url::Origin kOrigin_ = url::Origin(GURL("https://origin/")); |
| 44 const GURL kEndpoint_ = GURL("https://endpoint/"); |
| 45 const std::string kGroup_ = "group"; |
| 46 |
| 47 base::SimpleTestTickClock clock_; |
| 48 ReportingCache cache_; |
| 49 BackoffEntry::Policy backoff_policy_; |
| 50 ReportingEndpointManager manager_; |
| 51 }; |
| 52 |
| 53 TEST_F(ReportingEndpointManagerTest, NoEndpoint) { |
| 54 GURL endpoint_url; |
| 55 bool found_endpoint = |
| 56 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 57 EXPECT_FALSE(found_endpoint); |
| 58 } |
| 59 |
| 60 TEST_F(ReportingEndpointManagerTest, Endpoint) { |
| 61 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 62 kGroup_, tomorrow()); |
| 63 |
| 64 GURL endpoint_url; |
| 65 bool found_endpoint = |
| 66 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 67 EXPECT_TRUE(found_endpoint); |
| 68 EXPECT_EQ(kEndpoint_, endpoint_url); |
| 69 } |
| 70 |
| 71 TEST_F(ReportingEndpointManagerTest, ExpiredEndpoint) { |
| 72 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 73 kGroup_, yesterday()); |
| 74 |
| 75 GURL endpoint_url; |
| 76 bool found_endpoint = |
| 77 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 78 EXPECT_FALSE(found_endpoint); |
| 79 } |
| 80 |
| 81 TEST_F(ReportingEndpointManagerTest, PendingEndpoint) { |
| 82 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 83 kGroup_, tomorrow()); |
| 84 |
| 85 manager_.SetEndpointPending(kEndpoint_); |
| 86 |
| 87 GURL endpoint_url; |
| 88 bool found_endpoint = |
| 89 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 90 EXPECT_FALSE(found_endpoint); |
| 91 |
| 92 manager_.ClearEndpointPending(kEndpoint_); |
| 93 |
| 94 found_endpoint = |
| 95 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 96 EXPECT_TRUE(found_endpoint); |
| 97 EXPECT_EQ(kEndpoint_, endpoint_url); |
| 98 } |
| 99 |
| 100 TEST_F(ReportingEndpointManagerTest, BackedOffEndpoint) { |
| 101 ASSERT_EQ(2.0, backoff_policy_.multiply_factor); |
| 102 |
| 103 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 104 kGroup_, tomorrow()); |
| 105 |
| 106 manager_.InformOfEndpointRequest(kEndpoint_, false); |
| 107 |
| 108 // After one failure, endpoint is in exponential backoff. |
| 109 GURL endpoint_url; |
| 110 bool found_endpoint = |
| 111 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 112 EXPECT_FALSE(found_endpoint); |
| 113 |
| 114 // After initial delay, endpoint is usable again. |
| 115 clock_.Advance( |
| 116 base::TimeDelta::FromMilliseconds(backoff_policy_.initial_delay_ms)); |
| 117 |
| 118 found_endpoint = |
| 119 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 120 EXPECT_TRUE(found_endpoint); |
| 121 EXPECT_EQ(kEndpoint_, endpoint_url); |
| 122 |
| 123 manager_.InformOfEndpointRequest(kEndpoint_, false); |
| 124 |
| 125 // After a second failure, endpoint is backed off again. |
| 126 found_endpoint = |
| 127 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 128 EXPECT_FALSE(found_endpoint); |
| 129 |
| 130 clock_.Advance( |
| 131 base::TimeDelta::FromMilliseconds(backoff_policy_.initial_delay_ms)); |
| 132 |
| 133 // Next backoff is longer -- 2x the first -- so endpoint isn't usable yet. |
| 134 found_endpoint = |
| 135 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 136 EXPECT_FALSE(found_endpoint); |
| 137 |
| 138 clock_.Advance( |
| 139 base::TimeDelta::FromMilliseconds(backoff_policy_.initial_delay_ms)); |
| 140 |
| 141 // After 2x the initial delay, the endpoint is usable again. |
| 142 found_endpoint = |
| 143 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 144 EXPECT_TRUE(found_endpoint); |
| 145 EXPECT_EQ(kEndpoint_, endpoint_url); |
| 146 |
| 147 manager_.InformOfEndpointRequest(kEndpoint_, true); |
| 148 manager_.InformOfEndpointRequest(kEndpoint_, true); |
| 149 |
| 150 // Two more successful requests should reset the backoff to the initial delay |
| 151 // again. |
| 152 manager_.InformOfEndpointRequest(kEndpoint_, false); |
| 153 |
| 154 found_endpoint = |
| 155 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 156 EXPECT_FALSE(found_endpoint); |
| 157 |
| 158 clock_.Advance( |
| 159 base::TimeDelta::FromMilliseconds(backoff_policy_.initial_delay_ms)); |
| 160 |
| 161 found_endpoint = |
| 162 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); |
| 163 EXPECT_TRUE(found_endpoint); |
| 164 } |
| 165 |
| 166 // Make sure that multiple endpoints will all be returned at some point, to |
| 167 // avoid accidentally or intentionally implementing any priority ordering. |
| 168 TEST_F(ReportingEndpointManagerTest, RandomEndpoint) { |
| 169 static const GURL kEndpoint1("https://endpoint1/"); |
| 170 static const GURL kEndpoint2("https://endpoint2/"); |
| 171 static const int kMaxAttempts = 20; |
| 172 |
| 173 cache_.SetClient(kOrigin_, kEndpoint1, ReportingClient::Subdomains::EXCLUDE, |
| 174 kGroup_, tomorrow()); |
| 175 cache_.SetClient(kOrigin_, kEndpoint2, ReportingClient::Subdomains::EXCLUDE, |
| 176 kGroup_, tomorrow()); |
| 177 |
| 178 bool endpoint1_seen = false; |
| 179 bool endpoint2_seen = false; |
| 180 |
| 181 for (int i = 0; i < kMaxAttempts; i++) { |
| 182 GURL endpoint_url; |
| 183 bool found_endpoint = manager_.FindEndpointForOriginAndGroup( |
| 184 kOrigin_, kGroup_, &endpoint_url); |
| 185 ASSERT_TRUE(found_endpoint); |
| 186 ASSERT_TRUE(endpoint_url == kEndpoint1 || endpoint_url == kEndpoint2); |
| 187 |
| 188 if (endpoint_url == kEndpoint1) |
| 189 endpoint1_seen = true; |
| 190 else if (endpoint_url == kEndpoint2) |
| 191 endpoint2_seen = true; |
| 192 |
| 193 if (endpoint1_seen && endpoint2_seen) |
| 194 break; |
| 195 } |
| 196 |
| 197 EXPECT_TRUE(endpoint1_seen); |
| 198 EXPECT_TRUE(endpoint2_seen); |
| 199 } |
| 200 |
| 201 } // namespace |
| 202 } // namespace net |
| OLD | NEW |