Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/http/certificate_report_sender_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "net/base/load_flags.h" | |
| 13 #include "net/base/network_delegate_impl.h" | |
| 14 #include "net/base/upload_bytes_element_reader.h" | |
| 15 #include "net/base/upload_data_stream.h" | |
| 16 #include "net/base/upload_element_reader.h" | |
| 17 #include "net/test/url_request/url_request_failed_job.h" | |
| 18 #include "net/test/url_request/url_request_mock_data_job.h" | |
| 19 #include "net/url_request/url_request_filter.h" | |
| 20 #include "net/url_request/url_request_test_util.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char kDummyReport[] = "test.mail.google.com"; | |
| 26 const char kSecondDummyReport[] = "test2.mail.google.com"; | |
|
Ryan Sleevi
2015/06/26 20:03:47
Use IANA-reserved testing domains? (foo.test / foo
estark
2015/07/07 21:59:29
Done.
| |
| 27 | |
| 28 void EnableUrlRequestMocks(bool enable) { | |
| 29 net::URLRequestFilter::GetInstance()->ClearHandlers(); | |
| 30 if (!enable) | |
| 31 return; | |
| 32 | |
| 33 net::URLRequestFailedJob::AddUrlHandler(); | |
| 34 net::URLRequestMockDataJob::AddUrlHandler(); | |
| 35 } | |
| 36 | |
| 37 // Check that data uploaded in the request matches the test report | |
| 38 // data. The sent reports will be erased from |expect_reports|. | |
| 39 void CheckUploadData(net::URLRequest* request, | |
| 40 std::set<std::string>* expect_reports) { | |
| 41 const net::UploadDataStream* upload = request->get_upload(); | |
| 42 ASSERT_TRUE(upload); | |
| 43 ASSERT_TRUE(upload->GetElementReaders()); | |
| 44 EXPECT_EQ(1u, upload->GetElementReaders()->size()); | |
| 45 | |
| 46 const net::UploadBytesElementReader* reader = | |
| 47 (*upload->GetElementReaders())[0]->AsBytesReader(); | |
| 48 ASSERT_TRUE(reader); | |
| 49 std::string upload_data(reader->bytes(), reader->length()); | |
| 50 | |
| 51 EXPECT_EQ(1u, expect_reports->count(upload_data)); | |
| 52 expect_reports->erase(upload_data); | |
| 53 } | |
| 54 | |
| 55 // A network delegate that lets tests check that a certificate report | |
| 56 // was sent. It counts the number of requests and lets tests register a | |
| 57 // callback to run when the request is destroyed. It also checks that | |
| 58 // the uploaded data is as expected. | |
| 59 class TestCertificateReportSenderNetworkDelegate | |
| 60 : public net::NetworkDelegateImpl { | |
| 61 public: | |
| 62 TestCertificateReportSenderNetworkDelegate() | |
| 63 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | |
| 64 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), | |
| 65 num_requests_(0), | |
| 66 expect_cookies_(false) {} | |
| 67 | |
| 68 ~TestCertificateReportSenderNetworkDelegate() override {} | |
| 69 | |
| 70 void ExpectReport(const std::string& report) { | |
| 71 expect_reports_.insert(report); | |
| 72 } | |
| 73 | |
| 74 void set_all_url_requests_destroyed_callback( | |
| 75 const base::Closure& all_url_requests_destroyed_callback) { | |
| 76 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback; | |
| 77 } | |
| 78 | |
| 79 void set_url_request_destroyed_callback( | |
| 80 const base::Closure& url_request_destroyed_callback) { | |
| 81 url_request_destroyed_callback_ = url_request_destroyed_callback; | |
| 82 } | |
| 83 | |
| 84 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } | |
| 85 | |
| 86 int num_requests() const { return num_requests_; } | |
|
Ryan Sleevi
2015/06/26 20:03:47
STYLE: s/int/size_t - https://www.chromium.org/dev
estark
2015/07/07 21:59:29
Done.
| |
| 87 | |
| 88 // Sets whether cookies are expected to be sent on requests. | |
| 89 void set_expect_cookies(bool expect_cookies) { | |
| 90 expect_cookies_ = expect_cookies; | |
| 91 } | |
| 92 | |
| 93 // NetworkDelegateImpl implementation | |
| 94 int OnBeforeURLRequest(net::URLRequest* request, | |
| 95 const net::CompletionCallback& callback, | |
| 96 GURL* new_url) override { | |
| 97 num_requests_++; | |
| 98 EXPECT_EQ(expect_url_, request->url()); | |
| 99 EXPECT_EQ("POST", request->method()); | |
|
Ryan Sleevi
2015/06/26 20:03:47
I believe you want EXPECT_STRCASEEQ
[For example,
estark
2015/07/07 21:59:29
Done.
| |
| 100 | |
| 101 if (expect_cookies_) { | |
| 102 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | |
| 103 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 104 } else { | |
| 105 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | |
| 106 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 107 } | |
| 108 | |
| 109 CheckUploadData(request, &expect_reports_); | |
| 110 return net::OK; | |
| 111 } | |
| 112 | |
| 113 void OnURLRequestDestroyed(net::URLRequest* request) override { | |
| 114 url_request_destroyed_callback_.Run(); | |
| 115 if (expect_reports_.empty()) | |
| 116 all_url_requests_destroyed_callback_.Run(); | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 base::Closure url_request_destroyed_callback_; | |
| 121 base::Closure all_url_requests_destroyed_callback_; | |
| 122 int num_requests_; | |
| 123 GURL expect_url_; | |
| 124 std::set<std::string> expect_reports_; | |
| 125 bool expect_cookies_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(TestCertificateReportSenderNetworkDelegate); | |
| 128 }; | |
| 129 | |
| 130 class CertificateReportSenderImplTest : public ::testing::Test { | |
| 131 public: | |
| 132 CertificateReportSenderImplTest() : context_(true) { | |
| 133 EnableUrlRequestMocks(true); | |
| 134 context_.set_network_delegate(&network_delegate_); | |
| 135 context_.Init(); | |
| 136 } | |
| 137 | |
| 138 ~CertificateReportSenderImplTest() override { EnableUrlRequestMocks(false); } | |
| 139 | |
| 140 TestCertificateReportSenderNetworkDelegate* network_delegate() { | |
| 141 return &network_delegate_; | |
| 142 } | |
| 143 | |
| 144 net::TestURLRequestContext* context() { return &context_; } | |
| 145 | |
| 146 private: | |
| 147 TestCertificateReportSenderNetworkDelegate network_delegate_; | |
| 148 net::TestURLRequestContext context_; | |
| 149 }; | |
| 150 | |
| 151 void SendReport(net::CertificateReportSender* reporter, | |
| 152 TestCertificateReportSenderNetworkDelegate* network_delegate, | |
| 153 const std::string& report, | |
| 154 const GURL& url, | |
| 155 int request_sequence_number) { | |
| 156 base::RunLoop run_loop; | |
| 157 network_delegate->set_url_request_destroyed_callback(run_loop.QuitClosure()); | |
| 158 | |
| 159 network_delegate->set_expect_url(url); | |
| 160 network_delegate->ExpectReport(report); | |
| 161 | |
| 162 EXPECT_EQ(request_sequence_number, network_delegate->num_requests()); | |
| 163 | |
| 164 reporter->Send(url, report); | |
| 165 run_loop.Run(); | |
|
Ryan Sleevi
2015/06/26 20:03:47
Perhaps a comment explaining why you pump the loop
estark
2015/07/07 21:59:29
Done.
| |
| 166 | |
| 167 EXPECT_EQ(request_sequence_number + 1, network_delegate->num_requests()); | |
| 168 } | |
| 169 | |
| 170 // Test that CertificateReportSender::Send creates a URLRequest for the | |
| 171 // endpoint and sends the expected data. | |
| 172 TEST_F(CertificateReportSenderImplTest, SendsRequest) { | |
| 173 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 174 net::CertificateReportSenderImpl reporter( | |
| 175 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 176 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
| 177 } | |
| 178 | |
| 179 TEST_F(CertificateReportSenderImplTest, SendMultipleReportsSequentially) { | |
| 180 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 181 net::CertificateReportSenderImpl reporter( | |
| 182 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 183 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
| 184 SendReport(&reporter, network_delegate(), kDummyReport, url, 1); | |
| 185 } | |
| 186 | |
| 187 TEST_F(CertificateReportSenderImplTest, SendMultipleReportsSimultaneously) { | |
| 188 base::RunLoop run_loop; | |
| 189 network_delegate()->set_all_url_requests_destroyed_callback( | |
| 190 run_loop.QuitClosure()); | |
| 191 | |
| 192 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 193 network_delegate()->set_expect_url(url); | |
| 194 network_delegate()->ExpectReport(kDummyReport); | |
| 195 network_delegate()->ExpectReport(kSecondDummyReport); | |
| 196 | |
| 197 net::CertificateReportSenderImpl reporter( | |
| 198 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 199 | |
| 200 EXPECT_EQ(0, network_delegate()->num_requests()); | |
| 201 | |
| 202 reporter.Send(url, kDummyReport); | |
| 203 reporter.Send(url, kSecondDummyReport); | |
| 204 | |
| 205 run_loop.Run(); | |
| 206 | |
| 207 EXPECT_EQ(2, network_delegate()->num_requests()); | |
| 208 } | |
| 209 | |
| 210 // Test that pending URLRequests get cleaned up when the report sender | |
| 211 // is deleted. | |
| 212 TEST_F(CertificateReportSenderImplTest, PendingRequestGetsDeleted) { | |
| 213 base::RunLoop run_loop; | |
| 214 network_delegate()->set_url_request_destroyed_callback( | |
| 215 run_loop.QuitClosure()); | |
| 216 | |
| 217 GURL url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | |
| 218 net::URLRequestFailedJob::START, net::ERR_IO_PENDING); | |
| 219 network_delegate()->set_expect_url(url); | |
| 220 network_delegate()->ExpectReport(kDummyReport); | |
| 221 | |
| 222 EXPECT_EQ(0, network_delegate()->num_requests()); | |
| 223 | |
| 224 scoped_ptr<net::CertificateReportSender> reporter( | |
| 225 new net::CertificateReportSenderImpl( | |
| 226 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES)); | |
| 227 reporter->Send(url, kDummyReport); | |
| 228 reporter.reset(); | |
| 229 | |
| 230 run_loop.Run(); | |
| 231 | |
| 232 EXPECT_EQ(1, network_delegate()->num_requests()); | |
| 233 } | |
| 234 | |
| 235 // Test that a request that returns an error gets cleaned up. | |
| 236 TEST_F(CertificateReportSenderImplTest, ErroredRequestGetsDeleted) { | |
| 237 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED); | |
| 238 net::CertificateReportSenderImpl reporter( | |
| 239 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 240 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
| 241 } | |
| 242 | |
| 243 // Test that cookies are sent or not sent according to the error | |
| 244 // reporter's cookies preference. | |
| 245 | |
| 246 TEST_F(CertificateReportSenderImplTest, SendCookiesPreference) { | |
| 247 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 248 net::CertificateReportSenderImpl reporter( | |
| 249 context(), net::CertificateReportSender::SEND_COOKIES); | |
| 250 | |
| 251 network_delegate()->set_expect_cookies(true); | |
| 252 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
| 253 } | |
| 254 | |
| 255 TEST_F(CertificateReportSenderImplTest, DoNotSendCookiesPreference) { | |
| 256 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 257 net::CertificateReportSenderImpl reporter( | |
| 258 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 259 | |
| 260 network_delegate()->set_expect_cookies(false); | |
| 261 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
| 262 } | |
| 263 | |
| 264 } // namespace | |
| OLD | NEW |