Chromium Code Reviews| 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/load_flags.h" | 19 #include "net/base/load_flags.h" |
| 20 #include "net/http/http_response_headers.h" | |
| 21 #include "net/http/http_util.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
| 22 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 26 |
| 25 namespace domain_reliability { | 27 namespace domain_reliability { |
| 26 | 28 |
| 29 namespace { | |
| 30 | |
| 27 typedef std::vector<DomainReliabilityBeacon> BeaconVector; | 31 typedef std::vector<DomainReliabilityBeacon> BeaconVector; |
| 28 | 32 |
| 33 scoped_refptr<net::HttpResponseHeaders> MakeHttpResponseHeaders( | |
| 34 const std::string& headers) { | |
| 35 return scoped_refptr<net::HttpResponseHeaders>( | |
| 36 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( | |
| 37 headers.c_str(), headers.length()))); | |
| 38 } | |
| 39 | |
| 40 static scoped_ptr<const DomainReliabilityConfig> MakeConfig() { | |
| 41 DomainReliabilityConfig* config = new DomainReliabilityConfig(); | |
| 42 | |
| 43 DomainReliabilityConfig::Resource* resource; | |
| 44 | |
| 45 resource = new DomainReliabilityConfig::Resource(); | |
| 46 resource->name = "always_report"; | |
| 47 resource->url_patterns.push_back( | |
| 48 new std::string("http://example/always_report")); | |
| 49 resource->success_sample_rate = 1.0; | |
| 50 resource->failure_sample_rate = 1.0; | |
| 51 EXPECT_TRUE(resource->IsValid()); | |
| 52 config->resources.push_back(resource); | |
| 53 | |
| 54 resource = new DomainReliabilityConfig::Resource(); | |
| 55 resource->name = "never_report"; | |
| 56 resource->url_patterns.push_back( | |
| 57 new std::string("http://example/never_report")); | |
| 58 resource->success_sample_rate = 0.0; | |
| 59 resource->failure_sample_rate = 0.0; | |
| 60 EXPECT_TRUE(resource->IsValid()); | |
| 61 config->resources.push_back(resource); | |
| 62 | |
| 63 DomainReliabilityConfig::Collector* collector; | |
| 64 collector = new DomainReliabilityConfig::Collector(); | |
| 65 collector->upload_url = GURL("https://example/upload"); | |
| 66 EXPECT_TRUE(collector->IsValid()); | |
| 67 config->collectors.push_back(collector); | |
| 68 | |
| 69 config->version = "1"; | |
| 70 config->valid_until = 1234567890.0; | |
| 71 config->domain = "example"; | |
| 72 EXPECT_TRUE(config->IsValid()); | |
| 73 | |
| 74 return scoped_ptr<const DomainReliabilityConfig>(config); | |
| 75 } | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 29 class DomainReliabilityMonitorTest : public testing::Test { | 79 class DomainReliabilityMonitorTest : public testing::Test { |
| 30 protected: | 80 protected: |
| 31 typedef DomainReliabilityMonitor::RequestInfo RequestInfo; | 81 typedef DomainReliabilityMonitor::RequestInfo RequestInfo; |
| 32 | 82 |
| 33 DomainReliabilityMonitorTest() | 83 DomainReliabilityMonitorTest() |
| 34 : bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 84 : bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 35 url_request_context_getter_(new net::TestURLRequestContextGetter( | 85 url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 36 base::MessageLoopProxy::current())), | 86 base::MessageLoopProxy::current())), |
| 37 time_(new MockTime()), | 87 time_(new MockTime()), |
| 38 monitor_(url_request_context_getter_->GetURLRequestContext(), | 88 monitor_(url_request_context_getter_->GetURLRequestContext(), |
| 39 "test-reporter", | 89 "test-reporter", |
| 40 scoped_ptr<MockableTime>(time_)), | 90 scoped_ptr<MockableTime>(time_)), |
| 41 context_(monitor_.AddContextForTesting(CreateConfig())) {} | 91 context_(monitor_.AddContextForTesting(MakeConfig())) {} |
| 42 | 92 |
| 43 static scoped_ptr<const DomainReliabilityConfig> CreateConfig() { | 93 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; | 94 RequestInfo request; |
| 82 request.status = net::URLRequestStatus(); | 95 request.status = net::URLRequestStatus(); |
| 83 request.response_code = 200; | 96 request.status.set_status(net::URLRequestStatus::SUCCESS); |
| 84 request.was_cached = false; | 97 request.status.set_error(net::OK); |
| 98 request.response_info.headers = MakeHttpResponseHeaders( | |
| 99 "HTTP/1.1 200 OK\n\n"); | |
| 100 request.response_info.network_accessed = true; | |
| 101 request.response_info.was_fetched_via_proxy = false; | |
| 85 request.load_flags = 0; | 102 request.load_flags = 0; |
| 86 request.is_upload = false; | 103 request.is_upload = false; |
| 87 return request; | 104 return request; |
| 88 } | 105 } |
| 89 | 106 |
| 90 bool CheckNoBeacons(size_t index) { | 107 bool CheckNoBeacons(size_t index) { |
| 91 BeaconVector beacons; | 108 BeaconVector beacons; |
| 92 unsigned successful, failed; | 109 unsigned successful, failed; |
| 93 context_->GetQueuedDataForTesting(index, &beacons, &successful, &failed); | 110 context_->GetQueuedDataForTesting(index, &beacons, &successful, &failed); |
| 94 return beacons.empty() && successful == 0 && failed == 0; | 111 return beacons.empty() && successful == 0 && failed == 0; |
| 95 } | 112 } |
|
Ryan Sleevi
2014/05/02 23:00:07
Interestingly, your test for beacons being sent ig
Deprecated (see juliatuttle)
2014/05/06 18:52:05
Done.
| |
| 96 | 113 |
| 97 void OnRequestLegComplete(const RequestInfo& info) { | 114 void OnRequestLegComplete(const RequestInfo& info) { |
| 98 monitor_.OnRequestLegComplete(info); | 115 monitor_.OnRequestLegComplete(info); |
| 99 } | 116 } |
| 100 | 117 |
| 101 content::TestBrowserThreadBundle bundle_; | 118 content::TestBrowserThreadBundle bundle_; |
| 102 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 119 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 103 MockTime* time_; | 120 MockTime* time_; |
| 104 DomainReliabilityMonitor monitor_; | 121 DomainReliabilityMonitor monitor_; |
| 105 DomainReliabilityContext* context_; | 122 DomainReliabilityContext* context_; |
| 106 DomainReliabilityMonitor::RequestInfo request_; | 123 DomainReliabilityMonitor::RequestInfo request_; |
| 107 }; | 124 }; |
| 108 | 125 |
| 109 TEST_F(DomainReliabilityMonitorTest, Create) { | 126 TEST_F(DomainReliabilityMonitorTest, Create) { |
| 110 EXPECT_TRUE(CheckNoBeacons(0)); | 127 EXPECT_TRUE(CheckNoBeacons(0)); |
|
Ryan Sleevi
2014/05/02 23:00:07
style nit: Having read this file a few times, the
Deprecated (see juliatuttle)
2014/05/06 18:52:05
Done.
| |
| 111 EXPECT_TRUE(CheckNoBeacons(1)); | 128 EXPECT_TRUE(CheckNoBeacons(1)); |
| 112 } | 129 } |
| 113 | 130 |
| 114 TEST_F(DomainReliabilityMonitorTest, NoContextRequest) { | 131 TEST_F(DomainReliabilityMonitorTest, NoRequest) { |
| 115 RequestInfo request = MakeRequestInfo(); | 132 RequestInfo request = MakeRequestInfo(); |
| 116 request.url = GURL("http://no-context/"); | 133 request.url = GURL("http://no-context/"); |
| 117 OnRequestLegComplete(request); | 134 OnRequestLegComplete(request); |
| 118 | 135 |
| 119 EXPECT_TRUE(CheckNoBeacons(0)); | 136 EXPECT_TRUE(CheckNoBeacons(0)); |
| 120 EXPECT_TRUE(CheckNoBeacons(1)); | 137 EXPECT_TRUE(CheckNoBeacons(1)); |
| 121 } | 138 } |
| 122 | 139 |
| 123 TEST_F(DomainReliabilityMonitorTest, ContextRequest) { | 140 namespace { |
| 141 | |
|
Ryan Sleevi
2014/05/02 23:00:07
Move this to line 125?
Deprecated (see juliatuttle)
2014/05/06 18:52:05
Done.
| |
| 142 TEST_F(DomainReliabilityMonitorTest, Request) { | |
| 124 RequestInfo request = MakeRequestInfo(); | 143 RequestInfo request = MakeRequestInfo(); |
| 125 request.url = GURL("http://example/always_report"); | 144 request.url = GURL("http://example/always_report"); |
| 126 OnRequestLegComplete(request); | 145 OnRequestLegComplete(request); |
| 127 | 146 |
| 128 BeaconVector beacons; | 147 BeaconVector beacons; |
| 129 context_->GetQueuedDataForTesting(0, &beacons, NULL, NULL); | 148 context_->GetQueuedDataForTesting(0, &beacons, NULL, NULL); |
| 130 EXPECT_EQ(1u, beacons.size()); | 149 EXPECT_EQ(1u, beacons.size()); |
| 131 EXPECT_TRUE(CheckNoBeacons(1)); | 150 EXPECT_TRUE(CheckNoBeacons(1)); |
| 132 } | 151 } |
| 133 | 152 |
| 134 TEST_F(DomainReliabilityMonitorTest, ContextRequestWithDoNotSendCookies) { | 153 // Make sure the monitor does not log requests that did not access the network. |
| 154 TEST_F(DomainReliabilityMonitorTest, DidNotAccessNetwork) { | |
| 155 RequestInfo request = MakeRequestInfo(); | |
| 156 request.url = GURL("http://example/always_report"); | |
| 157 request.response_info.network_accessed = false; | |
| 158 OnRequestLegComplete(request); | |
| 159 | |
| 160 EXPECT_TRUE(CheckNoBeacons(0)); | |
| 161 EXPECT_TRUE(CheckNoBeacons(1)); | |
| 162 } | |
| 163 | |
| 164 // Make sure the monitor does not log requests that don't send cookies. | |
| 165 TEST_F(DomainReliabilityMonitorTest, DoNotSendCookies) { | |
| 135 RequestInfo request = MakeRequestInfo(); | 166 RequestInfo request = MakeRequestInfo(); |
| 136 request.url = GURL("http://example/always_report"); | 167 request.url = GURL("http://example/always_report"); |
| 137 request.load_flags = net::LOAD_DO_NOT_SEND_COOKIES; | 168 request.load_flags = net::LOAD_DO_NOT_SEND_COOKIES; |
| 138 OnRequestLegComplete(request); | 169 OnRequestLegComplete(request); |
| 139 | 170 |
| 140 EXPECT_TRUE(CheckNoBeacons(0)); | 171 EXPECT_TRUE(CheckNoBeacons(0)); |
| 141 EXPECT_TRUE(CheckNoBeacons(1)); | 172 EXPECT_TRUE(CheckNoBeacons(1)); |
| 142 } | 173 } |
| 143 | 174 |
| 144 TEST_F(DomainReliabilityMonitorTest, ContextRequestThatIsUpload) { | 175 // Make sure the monitor does not log upload requests. |
| 176 TEST_F(DomainReliabilityMonitorTest, IsUpload) { | |
| 145 RequestInfo request = MakeRequestInfo(); | 177 RequestInfo request = MakeRequestInfo(); |
| 146 request.url = GURL("http://example/always_report"); | 178 request.url = GURL("http://example/always_report"); |
| 147 request.is_upload = true; | 179 request.is_upload = true; |
| 148 OnRequestLegComplete(request); | 180 OnRequestLegComplete(request); |
| 149 | 181 |
| 150 EXPECT_TRUE(CheckNoBeacons(0)); | 182 EXPECT_TRUE(CheckNoBeacons(0)); |
| 151 EXPECT_TRUE(CheckNoBeacons(1)); | 183 EXPECT_TRUE(CheckNoBeacons(1)); |
| 152 } | 184 } |
| 153 | 185 |
| 186 // Make sure the monitor logs a legitimate error. | |
| 187 TEST_F(DomainReliabilityMonitorTest, ReportedError) { | |
| 188 RequestInfo request = MakeRequestInfo(); | |
| 189 request.url = GURL("http://example/always_report"); | |
| 190 request.status.set_status(net::URLRequestStatus::FAILED); | |
| 191 request.status.set_error(net::ERR_CONNECTION_RESET); | |
| 192 OnRequestLegComplete(request); | |
| 193 | |
| 194 BeaconVector beacons; | |
| 195 context_->GetQueuedDataForTesting(0, &beacons, NULL, NULL); | |
| 196 EXPECT_EQ(1u, beacons.size()); | |
| 197 EXPECT_TRUE(CheckNoBeacons(1)); | |
| 198 } | |
| 199 | |
| 200 // Make sure the monitor does not log a network-local error. | |
| 201 TEST_F(DomainReliabilityMonitorTest, UnreportedError) { | |
| 202 RequestInfo request = MakeRequestInfo(); | |
| 203 request.url = GURL("http://example/always_report"); | |
| 204 request.status.set_status(net::URLRequestStatus::FAILED); | |
| 205 request.status.set_error(net::ERR_PROXY_CONNECTION_FAILED); | |
| 206 OnRequestLegComplete(request); | |
| 207 | |
| 208 EXPECT_TRUE(CheckNoBeacons(0)); | |
| 209 EXPECT_TRUE(CheckNoBeacons(1)); | |
| 210 } | |
| 211 | |
| 212 // Make sure the monitor does not log the proxy's IP if one was used. | |
| 213 TEST_F(DomainReliabilityMonitorTest, WasFetchedViaProxy) { | |
| 214 RequestInfo request = MakeRequestInfo(); | |
| 215 request.url = GURL("http://example/always_report"); | |
| 216 request.response_info.was_fetched_via_proxy = true; | |
| 217 OnRequestLegComplete(request); | |
| 218 | |
| 219 BeaconVector beacons; | |
| 220 context_->GetQueuedDataForTesting(0, &beacons, NULL, NULL); | |
| 221 EXPECT_EQ(1u, beacons.size()); | |
| 222 EXPECT_EQ("", beacons[0].server_ip); | |
|
Ryan Sleevi
2014/05/02 23:00:07
AFAICT, you never actually set server_ip, so it's
Deprecated (see juliatuttle)
2014/05/06 18:52:05
Done.
| |
| 223 EXPECT_TRUE(CheckNoBeacons(1)); | |
| 224 } | |
| 225 | |
| 154 TEST_F(DomainReliabilityMonitorTest, AddBakedInConfigs) { | 226 TEST_F(DomainReliabilityMonitorTest, AddBakedInConfigs) { |
| 155 // AddBakedInConfigs DCHECKs that the baked-in configs parse correctly, so | 227 // 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 | 228 // this unittest will fail if someone tries to add an invalid config to the |
| 157 // source tree. | 229 // source tree. |
| 158 monitor_.AddBakedInConfigs(); | 230 monitor_.AddBakedInConfigs(); |
| 159 | 231 |
| 160 // Count the number of baked-in configs. | 232 // Count the number of baked-in configs. |
| 161 size_t num_baked_in_configs = 0; | 233 size_t num_baked_in_configs = 0; |
| 162 for (const char* const* p = kBakedInJsonConfigs; *p; ++p) | 234 for (const char* const* p = kBakedInJsonConfigs; *p; ++p) |
| 163 ++num_baked_in_configs; | 235 ++num_baked_in_configs; |
| 164 | 236 |
| 165 // The monitor should have contexts for all of the baked-in configs, plus the | 237 // The monitor should have contexts for all of the baked-in configs, plus the |
| 166 // test one added in the test constructor. | 238 // test one added in the test constructor. |
| 167 EXPECT_EQ(num_baked_in_configs + 1, monitor_.contexts_size_for_testing()); | 239 EXPECT_EQ(num_baked_in_configs + 1, monitor_.contexts_size_for_testing()); |
| 168 } | 240 } |
| 169 | 241 |
| 242 } // namespace | |
| 243 | |
| 170 } // namespace domain_reliability | 244 } // namespace domain_reliability |
| OLD | NEW |