Chromium Code Reviews| 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 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 102 kGroup_, tomorrow()); | |
| 103 | |
| 104 manager_.InformOfEndpointRequest(kEndpoint_, false); | |
|
Randy Smith (Not in Mondays)
2017/03/31 15:14:09
This seems like the only call to InformOfEnpointRe
Julia Tuttle
2017/03/31 15:40:27
Done, grumpily. This now tests fail, fail, success
| |
| 105 | |
| 106 GURL endpoint_url; | |
| 107 bool found_endpoint = | |
| 108 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); | |
| 109 EXPECT_FALSE(found_endpoint); | |
| 110 | |
| 111 clock_.Advance( | |
| 112 2 * base::TimeDelta::FromMilliseconds(backoff_policy_.initial_delay_ms)); | |
| 113 | |
| 114 found_endpoint = | |
| 115 manager_.FindEndpointForOriginAndGroup(kOrigin_, kGroup_, &endpoint_url); | |
| 116 EXPECT_TRUE(found_endpoint); | |
| 117 EXPECT_EQ(kEndpoint_, endpoint_url); | |
| 118 } | |
| 119 | |
| 120 // Make sure that multiple endpoints will all be returned at some point, to | |
| 121 // avoid accidentally or intentionally implementing any priority ordering. | |
| 122 TEST_F(ReportingEndpointManagerTest, RandomEndpoint) { | |
| 123 static const GURL kEndpoint1("https://endpoint1/"); | |
| 124 static const GURL kEndpoint2("https://endpoint2/"); | |
| 125 static const int kMaxAttempts = 20; | |
| 126 | |
| 127 cache_.SetClient(kOrigin_, kEndpoint1, ReportingClient::Subdomains::EXCLUDE, | |
| 128 kGroup_, tomorrow()); | |
| 129 cache_.SetClient(kOrigin_, kEndpoint2, ReportingClient::Subdomains::EXCLUDE, | |
| 130 kGroup_, tomorrow()); | |
| 131 | |
| 132 bool endpoint1_seen = false; | |
| 133 bool endpoint2_seen = false; | |
| 134 | |
| 135 for (int i = 0; i < kMaxAttempts; i++) { | |
| 136 GURL endpoint_url; | |
| 137 bool found_endpoint = manager_.FindEndpointForOriginAndGroup( | |
| 138 kOrigin_, kGroup_, &endpoint_url); | |
| 139 ASSERT_TRUE(found_endpoint); | |
| 140 ASSERT_TRUE(endpoint_url == kEndpoint1 || endpoint_url == kEndpoint2); | |
| 141 | |
| 142 if (endpoint_url == kEndpoint1) | |
| 143 endpoint1_seen = true; | |
| 144 else if (endpoint_url == kEndpoint2) | |
| 145 endpoint2_seen = true; | |
| 146 | |
| 147 if (endpoint1_seen && endpoint2_seen) | |
| 148 break; | |
| 149 } | |
| 150 | |
| 151 EXPECT_TRUE(endpoint1_seen); | |
| 152 EXPECT_TRUE(endpoint2_seen); | |
| 153 } | |
| 154 | |
| 155 } // namespace | |
| 156 } // namespace net | |
| OLD | NEW |