Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: components/domain_reliability/context_unittest.cc

Issue 252613002: Domain Reliability: More security review. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: ;_; Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/domain_reliability/context.cc ('k') | components/domain_reliability/dispatcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/context.h" 5 #include "components/domain_reliability/context.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop_proxy.h" 12 #include "base/message_loop/message_loop_proxy.h"
13 #include "components/domain_reliability/dispatcher.h" 13 #include "components/domain_reliability/dispatcher.h"
14 #include "components/domain_reliability/scheduler.h" 14 #include "components/domain_reliability/scheduler.h"
15 #include "components/domain_reliability/test_util.h" 15 #include "components/domain_reliability/test_util.h"
16 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
17 #include "net/url_request/url_request_test_util.h" 17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 namespace domain_reliability { 20 namespace domain_reliability {
21
22 namespace { 21 namespace {
23 22
24 typedef std::vector<DomainReliabilityBeacon> BeaconVector; 23 typedef std::vector<DomainReliabilityBeacon> BeaconVector;
25 24
26 DomainReliabilityBeacon MakeBeacon(MockableTime* time) { 25 DomainReliabilityBeacon MakeBeacon(MockableTime* time) {
27 DomainReliabilityBeacon beacon; 26 DomainReliabilityBeacon beacon;
28 beacon.status = "ok"; 27 beacon.status = "ok";
29 beacon.chrome_error = net::OK; 28 beacon.chrome_error = net::OK;
30 beacon.server_ip = "127.0.0.1"; 29 beacon.server_ip = "127.0.0.1";
31 beacon.http_response_code = 200; 30 beacon.http_response_code = 200;
32 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); 31 beacon.elapsed = base::TimeDelta::FromMilliseconds(250);
33 beacon.start_time = time->NowTicks() - beacon.elapsed; 32 beacon.start_time = time->NowTicks() - beacon.elapsed;
34 return beacon; 33 return beacon;
35 } 34 }
36 35
37 } // namespace 36 } // namespace
38 37
39 class DomainReliabilityContextTest : public testing::Test { 38 class DomainReliabilityContextTest : public testing::Test {
40 protected: 39 protected:
41 DomainReliabilityContextTest() 40 DomainReliabilityContextTest()
42 : dispatcher_(&time_), 41 : dispatcher_(&time_),
43 params_(CreateParams()), 42 params_(CreateParams()),
44 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, 43 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest,
45 base::Unretained(this))), 44 base::Unretained(this))),
45 upload_reporter_string_("test-reporter"),
46 context_(&time_, 46 context_(&time_,
47 params_, 47 params_,
48 upload_reporter_string_,
48 &dispatcher_, 49 &dispatcher_,
49 &uploader_, 50 &uploader_,
50 CreateConfig().Pass()), 51 CreateConfig().Pass()),
51 upload_pending_(false) {} 52 upload_pending_(false) {}
52 53
53 TimeDelta min_delay() const { return params_.minimum_upload_delay; } 54 TimeDelta min_delay() const { return params_.minimum_upload_delay; }
54 TimeDelta max_delay() const { return params_.maximum_upload_delay; } 55 TimeDelta max_delay() const { return params_.maximum_upload_delay; }
55 TimeDelta retry_interval() const { return params_.upload_retry_interval; } 56 TimeDelta retry_interval() const { return params_.upload_retry_interval; }
56 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } 57 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); }
57 58
58 bool upload_pending() { return upload_pending_; } 59 bool upload_pending() { return upload_pending_; }
59 60
60 const std::string& upload_report() { 61 const std::string& upload_report() {
61 DCHECK(upload_pending_); 62 DCHECK(upload_pending_);
62 return upload_report_; 63 return upload_report_;
63 } 64 }
64 65
65 const GURL& upload_url() { 66 const GURL& upload_url() {
66 DCHECK(upload_pending_); 67 DCHECK(upload_pending_);
67 return upload_url_; 68 return upload_url_;
68 } 69 }
69 70
70 void CallUploadCallback(bool success) { 71 void CallUploadCallback(bool success) {
71 DCHECK(upload_pending_); 72 DCHECK(upload_pending_);
72 upload_callback_.Run(success); 73 upload_callback_.Run(success);
73 upload_pending_ = false; 74 upload_pending_ = false;
74 } 75 }
75 76
76 bool CheckNoBeacons(int index) { 77 bool CheckNoBeacons(size_t index) {
77 BeaconVector beacons; 78 BeaconVector beacons;
78 context_.GetQueuedDataForTesting(index, &beacons, NULL, NULL); 79 context_.GetQueuedDataForTesting(index, &beacons, NULL, NULL);
79 return beacons.empty(); 80 return beacons.empty();
80 } 81 }
81 82
82 bool CheckCounts(int index, int expected_successful, int expected_failed) { 83 bool CheckCounts(size_t index,
83 int successful, failed; 84 unsigned expected_successful,
85 unsigned expected_failed) {
86 unsigned successful, failed;
84 context_.GetQueuedDataForTesting(index, NULL, &successful, &failed); 87 context_.GetQueuedDataForTesting(index, NULL, &successful, &failed);
85 return successful == expected_successful && failed == expected_failed; 88 return successful == expected_successful && failed == expected_failed;
86 } 89 }
87 90
88 MockTime time_; 91 MockTime time_;
89 DomainReliabilityDispatcher dispatcher_; 92 DomainReliabilityDispatcher dispatcher_;
90 DomainReliabilityScheduler::Params params_; 93 DomainReliabilityScheduler::Params params_;
91 MockUploader uploader_; 94 MockUploader uploader_;
95 std::string upload_reporter_string_;
92 DomainReliabilityContext context_; 96 DomainReliabilityContext context_;
93 97
94 private: 98 private:
95 void OnUploadRequest( 99 void OnUploadRequest(
96 const std::string& report_json, 100 const std::string& report_json,
97 const GURL& upload_url, 101 const GURL& upload_url,
98 const DomainReliabilityUploader::UploadCallback& callback) { 102 const DomainReliabilityUploader::UploadCallback& callback) {
99 DCHECK(!upload_pending_); 103 DCHECK(!upload_pending_);
100 upload_report_ = report_json; 104 upload_report_ = report_json;
101 upload_url_ = upload_url; 105 upload_url_ = upload_url;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 }; 150 };
147 151
148 TEST_F(DomainReliabilityContextTest, Create) { 152 TEST_F(DomainReliabilityContextTest, Create) {
149 EXPECT_TRUE(CheckNoBeacons(0)); 153 EXPECT_TRUE(CheckNoBeacons(0));
150 EXPECT_TRUE(CheckCounts(0, 0, 0)); 154 EXPECT_TRUE(CheckCounts(0, 0, 0));
151 EXPECT_TRUE(CheckNoBeacons(1)); 155 EXPECT_TRUE(CheckNoBeacons(1));
152 EXPECT_TRUE(CheckCounts(1, 0, 0)); 156 EXPECT_TRUE(CheckCounts(1, 0, 0));
153 } 157 }
154 158
155 TEST_F(DomainReliabilityContextTest, NoResource) { 159 TEST_F(DomainReliabilityContextTest, NoResource) {
160 GURL url("http://example/no_resource");
156 DomainReliabilityBeacon beacon = MakeBeacon(&time_); 161 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
157 context_.AddBeacon(beacon, GURL("http://example/no_resource")); 162 context_.OnBeacon(url, beacon);
158 163
159 EXPECT_TRUE(CheckNoBeacons(0)); 164 EXPECT_TRUE(CheckNoBeacons(0));
160 EXPECT_TRUE(CheckCounts(0, 0, 0)); 165 EXPECT_TRUE(CheckCounts(0, 0, 0));
161 EXPECT_TRUE(CheckNoBeacons(1)); 166 EXPECT_TRUE(CheckNoBeacons(1));
162 EXPECT_TRUE(CheckCounts(1, 0, 0)); 167 EXPECT_TRUE(CheckCounts(1, 0, 0));
163 } 168 }
164 169
165 TEST_F(DomainReliabilityContextTest, NeverReport) { 170 TEST_F(DomainReliabilityContextTest, NeverReport) {
171 GURL url("http://example/never_report");
166 DomainReliabilityBeacon beacon = MakeBeacon(&time_); 172 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
167 context_.AddBeacon(beacon, GURL("http://example/never_report")); 173 context_.OnBeacon(url, beacon);
168 174
169 EXPECT_TRUE(CheckNoBeacons(0)); 175 EXPECT_TRUE(CheckNoBeacons(0));
170 EXPECT_TRUE(CheckCounts(0, 0, 0)); 176 EXPECT_TRUE(CheckCounts(0, 0, 0));
171 EXPECT_TRUE(CheckNoBeacons(1)); 177 EXPECT_TRUE(CheckNoBeacons(1));
172 EXPECT_TRUE(CheckCounts(1, 1, 0)); 178 EXPECT_TRUE(CheckCounts(1, 1, 0));
173 } 179 }
174 180
175 TEST_F(DomainReliabilityContextTest, AlwaysReport) { 181 TEST_F(DomainReliabilityContextTest, AlwaysReport) {
182 GURL url("http://example/always_report");
176 DomainReliabilityBeacon beacon = MakeBeacon(&time_); 183 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
177 context_.AddBeacon(beacon, GURL("http://example/always_report")); 184 context_.OnBeacon(url, beacon);
178 185
179 BeaconVector beacons; 186 BeaconVector beacons;
180 context_.GetQueuedDataForTesting(0, &beacons, NULL, NULL); 187 context_.GetQueuedDataForTesting(0, &beacons, NULL, NULL);
181 EXPECT_EQ(1u, beacons.size()); 188 EXPECT_EQ(1u, beacons.size());
182 EXPECT_TRUE(CheckCounts(0, 1, 0)); 189 EXPECT_TRUE(CheckCounts(0, 1, 0));
183 EXPECT_TRUE(CheckNoBeacons(1)); 190 EXPECT_TRUE(CheckNoBeacons(1));
184 EXPECT_TRUE(CheckCounts(1, 0, 0)); 191 EXPECT_TRUE(CheckCounts(1, 0, 0));
185 } 192 }
186 193
187 TEST_F(DomainReliabilityContextTest, ReportUpload) { 194 TEST_F(DomainReliabilityContextTest, ReportUpload) {
195 GURL url("http://example/always_report");
188 DomainReliabilityBeacon beacon = MakeBeacon(&time_); 196 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
189 context_.AddBeacon(beacon, GURL("http://example/always_report")); 197 context_.OnBeacon(url, beacon);
190 198
191 const char* kExpectedReport = "{\"reporter\":\"chrome\"," 199 const char* kExpectedReport = "{\"reporter\":\"test-reporter\","
192 "\"resource_reports\":[{\"beacons\":[{\"http_response_code\":200," 200 "\"resource_reports\":[{\"beacons\":[{\"http_response_code\":200,"
193 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,\"server_ip\":" 201 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,\"server_ip\":"
194 "\"127.0.0.1\",\"status\":\"ok\"}],\"failed_requests\":0," 202 "\"127.0.0.1\",\"status\":\"ok\"}],\"failed_requests\":0,"
195 "\"resource_name\":\"always_report\",\"successful_requests\":1}," 203 "\"resource_name\":\"always_report\",\"successful_requests\":1},"
196 "{\"beacons\":[],\"failed_requests\":0,\"resource_name\":" 204 "{\"beacons\":[],\"failed_requests\":0,\"resource_name\":"
197 "\"never_report\",\"successful_requests\":0}]}"; 205 "\"never_report\",\"successful_requests\":0}]}";
198 206
199 time_.Advance(max_delay()); 207 time_.Advance(max_delay());
200 EXPECT_TRUE(upload_pending()); 208 EXPECT_TRUE(upload_pending());
201 EXPECT_EQ(kExpectedReport, upload_report()); 209 EXPECT_EQ(kExpectedReport, upload_report());
202 EXPECT_EQ(GURL("https://example/upload"), upload_url()); 210 EXPECT_EQ(GURL("https://example/upload"), upload_url());
203 CallUploadCallback(true); 211 CallUploadCallback(true);
204 } 212 }
205 213
206 } // namespace domain_reliability 214 } // namespace domain_reliability
OLDNEW
« no previous file with comments | « components/domain_reliability/context.cc ('k') | components/domain_reliability/dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698