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