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

Side by Side Diff: chrome/browser/net/certificate_error_reporter_unittest.cc

Issue 979893003: Refactor ChromeFraudulentCertReporter for code reuse by SSL reporting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add missing include Created 5 years, 9 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
OLDNEW
(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 "chrome/browser/net/certificate_error_reporter.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/net/cert_logger.pb.h"
15 #include "chrome/browser/net/url_request_mock_util.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/base/network_delegate_impl.h"
20 #include "net/base/test_data_directory.h"
21 #include "net/test/cert_test_util.h"
22 #include "net/test/url_request/url_request_failed_job.h"
23 #include "net/test/url_request/url_request_mock_http_job.h"
24 #include "net/url_request/url_request_filter.h"
25 #include "net/url_request/url_request_test_util.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 using chrome_browser_net::CertificateErrorReporter;
29 using content::BrowserThread;
30 using net::CompletionCallback;
31 using net::SSLInfo;
32 using net::NetworkDelegateImpl;
33 using net::TestURLRequestContext;
34 using net::URLRequest;
35
36 namespace {
37
38 const char kHostname[] = "test.mail.google.com";
39 const char kDummyFailureLog[] = "dummy failure log";
40 const char kTestCertFilename[] = FILE_PATH_LITERAL("test_mail_google_com.pem");
41
42 SSLInfo GetSSLInfo() {
43 SSLInfo info;
44 info.cert =
45 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename);
46 info.is_issued_by_known_root = true;
47 info.pinning_failure_log = kDummyFailureLog;
48 return info;
49 }
50
51 std::string GetPEMEncodedChain() {
52 base::FilePath cert_path =
53 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename);
54 std::string cert_data;
55 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data));
56 return cert_data;
57 }
58
59 void EnableUrlRequestMocks(bool enable) {
60 net::URLRequestFilter::GetInstance()->ClearHandlers();
61 if (enable) {
62 base::FilePath root_http;
63 net::URLRequestFailedJob::AddUrlHandler();
64 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
65 net::URLRequestMockHTTPJob::AddUrlHandlers(
66 root_http, BrowserThread::GetBlockingPool());
67 }
68 }
69
70 // A network delegate that lets tests check that requests were created
71 // or destroyed.
72 class TestNetworkDelegate : public NetworkDelegateImpl {
73 public:
74 TestNetworkDelegate() : NetworkDelegateImpl() {
75 requests_ = 0;
76 destroyed_requests_ = 0;
77 }
78 ~TestNetworkDelegate() {}
79
80 int OnBeforeURLRequest(URLRequest* request,
81 const CompletionCallback& callback,
82 GURL* new_url) override {
83 requests_++;
84 return net::OK;
85 }
86
87 void OnURLRequestDestroyed(URLRequest* request) { destroyed_requests_++; }
88
89 int GetNumRequests() { return requests_; }
90 int GetNumDestroyedRequests() { return destroyed_requests_; }
91
92 private:
93 int requests_;
94 int destroyed_requests_;
95 };
96
97 class CertificateErrorReporterTest : public ::testing::Test {
98 public:
99 ~CertificateErrorReporterTest() {
100 EnableUrlRequestMocks(false);
101 chrome_browser_net::SetUrlRequestMocksEnabled(false);
102 }
103
104 protected:
105 CertificateErrorReporterTest() : context_(true) {
106 EnableUrlRequestMocks(true);
107 context_.set_network_delegate(&network_delegate_);
108 context_.Init();
109 }
110
111 TestURLRequestContext context_;
112 TestNetworkDelegate network_delegate_;
113
114 content::TestBrowserThreadBundle thread_bundle_;
Ryan Sleevi 2015/03/09 23:44:56 Should make sure this is the first member, since e
estark 2015/03/10 00:07:38 Done.
115 };
116
117 class TestReporter : public CertificateErrorReporter {
118 public:
119 TestReporter(net::URLRequestContext* context, const GURL& upload_url)
120 : CertificateErrorReporter(context, upload_url), latest_request_url_() {
121 response_started_callback_ = base::Bind(&base::DoNothing);
122 }
123
124 TestReporter(net::URLRequestContext* context,
125 const GURL& upload_url,
126 base::Closure response_started_callback)
127 : CertificateErrorReporter(context, upload_url),
128 latest_request_url_(),
129 response_started_callback_(response_started_callback) {}
130
131 void OnResponseStarted(net::URLRequest* request) override {
132 latest_request_url_ = request->url();
133 CertificateErrorReporter::OnResponseStarted(request);
134 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
135 response_started_callback_);
136 }
137
138 GURL& GetLatestRequestURL() { return latest_request_url_; }
139
140 private:
141 GURL latest_request_url_;
142 base::Closure response_started_callback_;
143 };
144
145 // Test that CertificateErrorReporter::SendReport creates a URLRequest
146 // for the endpoint.
147 TEST_F(CertificateErrorReporterTest, SendReportSendsRequest) {
148 base::RunLoop run_loop;
149 base::Closure callback = run_loop.QuitClosure();
150 GURL url = net::URLRequestMockHTTPJob::GetMockHttpsUrl(
151 base::FilePath(FILE_PATH_LITERAL("empty.html")));
152 TestReporter reporter(&context_, url, callback);
153
154 EXPECT_EQ(0, network_delegate_.GetNumRequests());
155
156 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
157 kHostname, GetSSLInfo());
158 run_loop.Run();
159
160 EXPECT_EQ(1, network_delegate_.GetNumRequests());
161 EXPECT_EQ(url, reporter.GetLatestRequestURL());
162 }
163
164 // Test that pending URLRequests get cleaned up when the reporter is
165 // deleted.
166 TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) {
167 GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure(
168 base::FilePath(FILE_PATH_LITERAL("empty.html")),
169 net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING);
170
171 EXPECT_EQ(0, network_delegate_.GetNumRequests());
172 EXPECT_EQ(0, network_delegate_.GetNumDestroyedRequests());
173
174 TestReporter* reporter = new TestReporter(&context_, url);
175 reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
176 kHostname, GetSSLInfo());
177 delete reporter;
178
179 EXPECT_EQ(1, network_delegate_.GetNumRequests());
180 EXPECT_EQ(1, network_delegate_.GetNumDestroyedRequests());
181 }
182
183 // Test that a request that returns an error gets cleaned up.
184 TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) {
185 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED);
186 base::RunLoop run_loop;
187 base::Closure callback = run_loop.QuitClosure();
188
189 EXPECT_EQ(0, network_delegate_.GetNumRequests());
190 EXPECT_EQ(0, network_delegate_.GetNumDestroyedRequests());
191
192 TestReporter reporter(&context_, url, callback);
193 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
194 kHostname, GetSSLInfo());
195 run_loop.Run();
196
197 EXPECT_EQ(1, network_delegate_.GetNumRequests());
198 EXPECT_EQ(1, network_delegate_.GetNumDestroyedRequests());
199 }
200
201 } // namespace
202
203 namespace chrome_browser_net {
204
205 // Test that a built report has the correct data.
206 //
207 // This test is in the chrome_browser_net namespace so that it can be
208 // friended by CertificateErrorReporter.
209 TEST_F(CertificateErrorReporterTest, ReportIsBuiltCorrectly) {
210 SSLInfo info = GetSSLInfo();
211 CertLoggerRequest request;
212 CertificateErrorReporter::BuildReport(kHostname, info, &request);
213
214 EXPECT_EQ(request.hostname(), kHostname);
215 EXPECT_EQ(request.cert_chain(), GetPEMEncodedChain());
216 EXPECT_EQ(request.pin().size(), 1);
217 EXPECT_EQ(request.pin().Get(0), kDummyFailureLog);
218 }
219 } // namespace chrome_browser_net
OLDNEW
« no previous file with comments | « chrome/browser/net/certificate_error_reporter.cc ('k') | chrome/browser/net/chrome_fraudulent_certificate_reporter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698