OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/domain_reliability/monitor.h" | 5 #include "components/domain_reliability/monitor.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
14 #include "components/domain_reliability/baked_in_configs.h" | 14 #include "components/domain_reliability/baked_in_configs.h" |
15 #include "components/domain_reliability/beacon.h" | 15 #include "components/domain_reliability/beacon.h" |
16 #include "components/domain_reliability/config.h" | 16 #include "components/domain_reliability/config.h" |
17 #include "components/domain_reliability/test_util.h" | 17 #include "components/domain_reliability/test_util.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "net/base/host_port_pair.h" |
19 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
| 21 #include "net/http/http_response_headers.h" |
| 22 #include "net/http/http_util.h" |
20 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
21 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
22 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
24 | 27 |
25 namespace domain_reliability { | 28 namespace domain_reliability { |
26 | 29 |
| 30 namespace { |
| 31 |
27 typedef std::vector<DomainReliabilityBeacon> BeaconVector; | 32 typedef std::vector<DomainReliabilityBeacon> BeaconVector; |
28 | 33 |
| 34 static const size_t kAlwaysReportIndex = 0u; |
| 35 static const size_t kNeverReportIndex = 1u; |
| 36 |
| 37 scoped_refptr<net::HttpResponseHeaders> MakeHttpResponseHeaders( |
| 38 const std::string& headers) { |
| 39 return scoped_refptr<net::HttpResponseHeaders>( |
| 40 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 41 headers.c_str(), headers.length()))); |
| 42 } |
| 43 |
| 44 static scoped_ptr<const DomainReliabilityConfig> MakeConfig() { |
| 45 DomainReliabilityConfig* config = new DomainReliabilityConfig(); |
| 46 |
| 47 DomainReliabilityConfig::Resource* resource; |
| 48 |
| 49 resource = new DomainReliabilityConfig::Resource(); |
| 50 resource->name = "always_report"; |
| 51 resource->url_patterns.push_back( |
| 52 new std::string("http://example/always_report")); |
| 53 resource->success_sample_rate = 1.0; |
| 54 resource->failure_sample_rate = 1.0; |
| 55 EXPECT_TRUE(resource->IsValid()); |
| 56 config->resources.push_back(resource); |
| 57 |
| 58 resource = new DomainReliabilityConfig::Resource(); |
| 59 resource->name = "never_report"; |
| 60 resource->url_patterns.push_back( |
| 61 new std::string("http://example/never_report")); |
| 62 resource->success_sample_rate = 0.0; |
| 63 resource->failure_sample_rate = 0.0; |
| 64 EXPECT_TRUE(resource->IsValid()); |
| 65 config->resources.push_back(resource); |
| 66 |
| 67 DomainReliabilityConfig::Collector* collector; |
| 68 collector = new DomainReliabilityConfig::Collector(); |
| 69 collector->upload_url = GURL("https://example/upload"); |
| 70 EXPECT_TRUE(collector->IsValid()); |
| 71 config->collectors.push_back(collector); |
| 72 |
| 73 config->version = "1"; |
| 74 config->valid_until = 1234567890.0; |
| 75 config->domain = "example"; |
| 76 EXPECT_TRUE(config->IsValid()); |
| 77 |
| 78 return scoped_ptr<const DomainReliabilityConfig>(config); |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
29 class DomainReliabilityMonitorTest : public testing::Test { | 83 class DomainReliabilityMonitorTest : public testing::Test { |
30 protected: | 84 protected: |
31 typedef DomainReliabilityMonitor::RequestInfo RequestInfo; | 85 typedef DomainReliabilityMonitor::RequestInfo RequestInfo; |
32 | 86 |
33 DomainReliabilityMonitorTest() | 87 DomainReliabilityMonitorTest() |
34 : bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 88 : bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
35 url_request_context_getter_(new net::TestURLRequestContextGetter( | 89 url_request_context_getter_(new net::TestURLRequestContextGetter( |
36 base::MessageLoopProxy::current())), | 90 base::MessageLoopProxy::current())), |
37 time_(new MockTime()), | 91 time_(new MockTime()), |
38 monitor_(url_request_context_getter_->GetURLRequestContext(), | 92 monitor_(url_request_context_getter_->GetURLRequestContext(), |
39 "test-reporter", | 93 "test-reporter", |
40 scoped_ptr<MockableTime>(time_)), | 94 scoped_ptr<MockableTime>(time_)), |
41 context_(monitor_.AddContextForTesting(CreateConfig())) {} | 95 context_(monitor_.AddContextForTesting(MakeConfig())) {} |
42 | 96 |
43 static scoped_ptr<const DomainReliabilityConfig> CreateConfig() { | 97 static RequestInfo MakeRequestInfo() { |
44 DomainReliabilityConfig* config = new DomainReliabilityConfig(); | |
45 | |
46 DomainReliabilityConfig::Resource* resource; | |
47 | |
48 resource = new DomainReliabilityConfig::Resource(); | |
49 resource->name = "always_report"; | |
50 resource->url_patterns.push_back( | |
51 new std::string("http://example/always_report")); | |
52 resource->success_sample_rate = 1.0; | |
53 resource->failure_sample_rate = 1.0; | |
54 EXPECT_TRUE(resource->IsValid()); | |
55 config->resources.push_back(resource); | |
56 | |
57 resource = new DomainReliabilityConfig::Resource(); | |
58 resource->name = "never_report"; | |
59 resource->url_patterns.push_back( | |
60 new std::string("http://example/never_report")); | |
61 resource->success_sample_rate = 0.0; | |
62 resource->failure_sample_rate = 0.0; | |
63 EXPECT_TRUE(resource->IsValid()); | |
64 config->resources.push_back(resource); | |
65 | |
66 DomainReliabilityConfig::Collector* collector; | |
67 collector = new DomainReliabilityConfig::Collector(); | |
68 collector->upload_url = GURL("https://example/upload"); | |
69 EXPECT_TRUE(collector->IsValid()); | |
70 config->collectors.push_back(collector); | |
71 | |
72 config->version = "1"; | |
73 config->valid_until = 1234567890.0; | |
74 config->domain = "example"; | |
75 EXPECT_TRUE(config->IsValid()); | |
76 | |
77 return scoped_ptr<const DomainReliabilityConfig>(config); | |
78 } | |
79 | |
80 RequestInfo MakeRequestInfo() { | |
81 RequestInfo request; | 98 RequestInfo request; |
82 request.status = net::URLRequestStatus(); | 99 request.status = net::URLRequestStatus(); |
83 request.response_code = 200; | 100 request.status.set_status(net::URLRequestStatus::SUCCESS); |
84 request.was_cached = false; | 101 request.status.set_error(net::OK); |
| 102 request.response_info.socket_address = |
| 103 net::HostPortPair::FromString("12.34.56.78:80"); |
| 104 request.response_info.headers = MakeHttpResponseHeaders( |
| 105 "HTTP/1.1 200 OK\n\n"); |
| 106 request.response_info.network_accessed = true; |
| 107 request.response_info.was_fetched_via_proxy = false; |
85 request.load_flags = 0; | 108 request.load_flags = 0; |
86 request.is_upload = false; | 109 request.is_upload = false; |
87 return request; | 110 return request; |
88 } | 111 } |
89 | 112 |
90 bool CheckNoBeacons(size_t index) { | |
91 BeaconVector beacons; | |
92 unsigned successful, failed; | |
93 context_->GetQueuedDataForTesting(index, &beacons, &successful, &failed); | |
94 return beacons.empty() && successful == 0 && failed == 0; | |
95 } | |
96 | |
97 void OnRequestLegComplete(const RequestInfo& info) { | 113 void OnRequestLegComplete(const RequestInfo& info) { |
98 monitor_.OnRequestLegComplete(info); | 114 monitor_.OnRequestLegComplete(info); |
99 } | 115 } |
100 | 116 |
| 117 size_t CountPendingBeacons(size_t index) { |
| 118 BeaconVector beacons; |
| 119 context_->GetQueuedDataForTesting(index, &beacons, NULL, NULL); |
| 120 return beacons.size(); |
| 121 } |
| 122 |
| 123 bool CheckRequestCounts(size_t index, |
| 124 uint32 expected_successful, |
| 125 uint32 expected_failed) { |
| 126 uint32 successful, failed; |
| 127 context_->GetQueuedDataForTesting(index, NULL, &successful, &failed); |
| 128 EXPECT_EQ(expected_successful, successful); |
| 129 EXPECT_EQ(expected_failed, failed); |
| 130 return expected_successful == successful && expected_failed == failed; |
| 131 } |
| 132 |
101 content::TestBrowserThreadBundle bundle_; | 133 content::TestBrowserThreadBundle bundle_; |
102 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 134 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
103 MockTime* time_; | 135 MockTime* time_; |
104 DomainReliabilityMonitor monitor_; | 136 DomainReliabilityMonitor monitor_; |
105 DomainReliabilityContext* context_; | 137 DomainReliabilityContext* context_; |
106 DomainReliabilityMonitor::RequestInfo request_; | 138 DomainReliabilityMonitor::RequestInfo request_; |
107 }; | 139 }; |
108 | 140 |
| 141 namespace { |
| 142 |
109 TEST_F(DomainReliabilityMonitorTest, Create) { | 143 TEST_F(DomainReliabilityMonitorTest, Create) { |
110 EXPECT_TRUE(CheckNoBeacons(0)); | 144 EXPECT_EQ(0u, CountPendingBeacons(kAlwaysReportIndex)); |
111 EXPECT_TRUE(CheckNoBeacons(1)); | 145 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 0u)); |
| 146 EXPECT_EQ(0u, CountPendingBeacons(kNeverReportIndex)); |
| 147 EXPECT_TRUE(CheckRequestCounts(kNeverReportIndex, 0u, 0u)); |
112 } | 148 } |
113 | 149 |
114 TEST_F(DomainReliabilityMonitorTest, NoContextRequest) { | 150 TEST_F(DomainReliabilityMonitorTest, NoContext) { |
115 RequestInfo request = MakeRequestInfo(); | 151 RequestInfo request = MakeRequestInfo(); |
116 request.url = GURL("http://no-context/"); | 152 request.url = GURL("http://no-context/"); |
117 OnRequestLegComplete(request); | 153 OnRequestLegComplete(request); |
118 | 154 |
119 EXPECT_TRUE(CheckNoBeacons(0)); | 155 EXPECT_EQ(0u, CountPendingBeacons(kAlwaysReportIndex)); |
120 EXPECT_TRUE(CheckNoBeacons(1)); | 156 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 0u)); |
| 157 EXPECT_EQ(0u, CountPendingBeacons(kNeverReportIndex)); |
| 158 EXPECT_TRUE(CheckRequestCounts(kNeverReportIndex, 0u, 0u)); |
121 } | 159 } |
122 | 160 |
123 TEST_F(DomainReliabilityMonitorTest, ContextRequest) { | 161 TEST_F(DomainReliabilityMonitorTest, NotReported) { |
| 162 RequestInfo request = MakeRequestInfo(); |
| 163 request.url = GURL("http://example/never_report"); |
| 164 OnRequestLegComplete(request); |
| 165 |
| 166 EXPECT_EQ(0u, CountPendingBeacons(kNeverReportIndex)); |
| 167 EXPECT_TRUE(CheckRequestCounts(kNeverReportIndex, 1u, 0u)); |
| 168 } |
| 169 |
| 170 TEST_F(DomainReliabilityMonitorTest, NetworkFailure) { |
| 171 RequestInfo request = MakeRequestInfo(); |
| 172 request.url = GURL("http://example/always_report"); |
| 173 request.status.set_status(net::URLRequestStatus::FAILED); |
| 174 request.status.set_error(net::ERR_CONNECTION_RESET); |
| 175 request.response_info.headers = NULL; |
| 176 OnRequestLegComplete(request); |
| 177 |
| 178 EXPECT_EQ(1u, CountPendingBeacons(kAlwaysReportIndex)); |
| 179 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 1u)); |
| 180 } |
| 181 |
| 182 TEST_F(DomainReliabilityMonitorTest, ServerFailure) { |
| 183 RequestInfo request = MakeRequestInfo(); |
| 184 request.url = GURL("http://example/always_report"); |
| 185 request.response_info.headers = |
| 186 MakeHttpResponseHeaders("HTTP/1.1 500 :(\n\n"); |
| 187 OnRequestLegComplete(request); |
| 188 |
| 189 EXPECT_EQ(1u, CountPendingBeacons(kAlwaysReportIndex)); |
| 190 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 1u)); |
| 191 } |
| 192 |
| 193 TEST_F(DomainReliabilityMonitorTest, NotReportedFailure) { |
| 194 RequestInfo request = MakeRequestInfo(); |
| 195 request.url = GURL("http://example/never_report"); |
| 196 request.status.set_status(net::URLRequestStatus::FAILED); |
| 197 request.status.set_error(net::ERR_CONNECTION_RESET); |
| 198 OnRequestLegComplete(request); |
| 199 |
| 200 EXPECT_EQ(0u, CountPendingBeacons(kNeverReportIndex)); |
| 201 EXPECT_TRUE(CheckRequestCounts(kNeverReportIndex, 0u, 1u)); |
| 202 } |
| 203 |
| 204 TEST_F(DomainReliabilityMonitorTest, Request) { |
124 RequestInfo request = MakeRequestInfo(); | 205 RequestInfo request = MakeRequestInfo(); |
125 request.url = GURL("http://example/always_report"); | 206 request.url = GURL("http://example/always_report"); |
126 OnRequestLegComplete(request); | 207 OnRequestLegComplete(request); |
127 | 208 |
128 BeaconVector beacons; | 209 EXPECT_EQ(1u, CountPendingBeacons(kAlwaysReportIndex)); |
129 context_->GetQueuedDataForTesting(0, &beacons, NULL, NULL); | 210 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 1u, 0u)); |
130 EXPECT_EQ(1u, beacons.size()); | |
131 EXPECT_TRUE(CheckNoBeacons(1)); | |
132 } | 211 } |
133 | 212 |
134 TEST_F(DomainReliabilityMonitorTest, ContextRequestWithDoNotSendCookies) { | 213 // Make sure the monitor does not log requests that did not access the network. |
| 214 TEST_F(DomainReliabilityMonitorTest, DidNotAccessNetwork) { |
| 215 RequestInfo request = MakeRequestInfo(); |
| 216 request.url = GURL("http://example/always_report"); |
| 217 request.response_info.network_accessed = false; |
| 218 OnRequestLegComplete(request); |
| 219 |
| 220 EXPECT_EQ(0u, CountPendingBeacons(kAlwaysReportIndex)); |
| 221 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 0u)); |
| 222 } |
| 223 |
| 224 // Make sure the monitor does not log requests that don't send cookies. |
| 225 TEST_F(DomainReliabilityMonitorTest, DoNotSendCookies) { |
135 RequestInfo request = MakeRequestInfo(); | 226 RequestInfo request = MakeRequestInfo(); |
136 request.url = GURL("http://example/always_report"); | 227 request.url = GURL("http://example/always_report"); |
137 request.load_flags = net::LOAD_DO_NOT_SEND_COOKIES; | 228 request.load_flags = net::LOAD_DO_NOT_SEND_COOKIES; |
138 OnRequestLegComplete(request); | 229 OnRequestLegComplete(request); |
139 | 230 |
140 EXPECT_TRUE(CheckNoBeacons(0)); | 231 EXPECT_EQ(0u, CountPendingBeacons(kAlwaysReportIndex)); |
141 EXPECT_TRUE(CheckNoBeacons(1)); | 232 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 0u)); |
142 } | 233 } |
143 | 234 |
144 TEST_F(DomainReliabilityMonitorTest, ContextRequestThatIsUpload) { | 235 // Make sure the monitor does not log upload requests. |
| 236 TEST_F(DomainReliabilityMonitorTest, IsUpload) { |
145 RequestInfo request = MakeRequestInfo(); | 237 RequestInfo request = MakeRequestInfo(); |
146 request.url = GURL("http://example/always_report"); | 238 request.url = GURL("http://example/always_report"); |
147 request.is_upload = true; | 239 request.is_upload = true; |
148 OnRequestLegComplete(request); | 240 OnRequestLegComplete(request); |
149 | 241 |
150 EXPECT_TRUE(CheckNoBeacons(0)); | 242 EXPECT_EQ(0u, CountPendingBeacons(kAlwaysReportIndex)); |
151 EXPECT_TRUE(CheckNoBeacons(1)); | 243 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 0u)); |
| 244 } |
| 245 |
| 246 // Make sure the monitor does not log a network-local error. |
| 247 TEST_F(DomainReliabilityMonitorTest, LocalError) { |
| 248 RequestInfo request = MakeRequestInfo(); |
| 249 request.url = GURL("http://example/always_report"); |
| 250 request.status.set_status(net::URLRequestStatus::FAILED); |
| 251 request.status.set_error(net::ERR_PROXY_CONNECTION_FAILED); |
| 252 OnRequestLegComplete(request); |
| 253 |
| 254 EXPECT_EQ(0u, CountPendingBeacons(kAlwaysReportIndex)); |
| 255 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 0u, 0u)); |
| 256 } |
| 257 |
| 258 // Make sure the monitor does not log the proxy's IP if one was used. |
| 259 TEST_F(DomainReliabilityMonitorTest, WasFetchedViaProxy) { |
| 260 RequestInfo request = MakeRequestInfo(); |
| 261 request.url = GURL("http://example/always_report"); |
| 262 request.response_info.socket_address = |
| 263 net::HostPortPair::FromString("127.0.0.1:3128"); |
| 264 request.response_info.was_fetched_via_proxy = true; |
| 265 OnRequestLegComplete(request); |
| 266 |
| 267 EXPECT_EQ(1u, CountPendingBeacons(kAlwaysReportIndex)); |
| 268 EXPECT_TRUE(CheckRequestCounts(kAlwaysReportIndex, 1u, 0u)); |
| 269 |
| 270 BeaconVector beacons; |
| 271 context_->GetQueuedDataForTesting(kAlwaysReportIndex, &beacons, NULL, NULL); |
| 272 EXPECT_TRUE(beacons[0].server_ip.empty()); |
152 } | 273 } |
153 | 274 |
154 TEST_F(DomainReliabilityMonitorTest, AddBakedInConfigs) { | 275 TEST_F(DomainReliabilityMonitorTest, AddBakedInConfigs) { |
155 // AddBakedInConfigs DCHECKs that the baked-in configs parse correctly, so | 276 // AddBakedInConfigs DCHECKs that the baked-in configs parse correctly, so |
156 // this unittest will fail if someone tries to add an invalid config to the | 277 // this unittest will fail if someone tries to add an invalid config to the |
157 // source tree. | 278 // source tree. |
158 monitor_.AddBakedInConfigs(); | 279 monitor_.AddBakedInConfigs(); |
159 | 280 |
160 // Count the number of baked-in configs. | 281 // Count the number of baked-in configs. |
161 size_t num_baked_in_configs = 0; | 282 size_t num_baked_in_configs = 0; |
162 for (const char* const* p = kBakedInJsonConfigs; *p; ++p) | 283 for (const char* const* p = kBakedInJsonConfigs; *p; ++p) |
163 ++num_baked_in_configs; | 284 ++num_baked_in_configs; |
164 | 285 |
165 // The monitor should have contexts for all of the baked-in configs, plus the | 286 // The monitor should have contexts for all of the baked-in configs, plus the |
166 // test one added in the test constructor. | 287 // test one added in the test constructor. |
167 EXPECT_EQ(num_baked_in_configs + 1, monitor_.contexts_size_for_testing()); | 288 EXPECT_EQ(num_baked_in_configs + 1, monitor_.contexts_size_for_testing()); |
168 } | 289 } |
169 | 290 |
| 291 } // namespace |
| 292 |
170 } // namespace domain_reliability | 293 } // namespace domain_reliability |
OLD | NEW |