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

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: fix GURL #include placement 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/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/net/cert_logger.pb.h"
11 #include "chrome/browser/net/url_request_mock_util.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "net/base/network_delegate_impl.h"
15 #include "net/base/test_data_directory.h"
16 #include "net/test/cert_test_util.h"
17 #include "net/test/url_request/url_request_failed_job.h"
18 #include "net/test/url_request/url_request_mock_http_job.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using chrome_browser_net::CertificateErrorReporter;
23 using content::BrowserThread;
24 using net::CompletionCallback;
25 using net::SSLInfo;
26 using net::NetworkDelegateImpl;
27 using net::TestURLRequestContext;
28 using net::URLRequest;
29
30 namespace {
31
32 const char kHostname[] = "test.mail.google.com";
33 const char kDummyFailureLog[] = "dummy failure log";
34 const char kTestCertFilename[] = "test_mail_google_com.pem";
Ryan Sleevi 2015/03/09 22:14:06 const base::FilePath::CharType kTestCertFilename[]
estark 2015/03/09 23:21:17 Done.
35
36 SSLInfo GetSSLInfo() {
37 SSLInfo info;
38 info.cert =
39 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename);
40 info.is_issued_by_known_root = true;
41 info.pinning_failure_log = kDummyFailureLog;
42 return info;
43 }
44
45 std::string GetPEMEncodedChain() {
Ryan Sleevi 2015/03/09 22:14:07 This seems to indirectly couple the test with the
estark 2015/03/09 23:21:17 Well, I'd like to test that the output is in the f
Ryan Sleevi 2015/03/09 23:40:36 I think it's fine; in that case, however, you shou
estark 2015/03/10 00:07:38 Done.
46 base::FilePath cert_path =
47 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename);
48 std::string cert_data;
49 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data));
50 return cert_data;
51 }
52
53 // A network delegate that lets tests check that requests were created
54 // or destroyed.
55 class TestNetworkDelegate : public NetworkDelegateImpl {
56 public:
57 TestNetworkDelegate() : NetworkDelegateImpl() {
58 requests_ = 0;
59 destroyed_requests_ = 0;
60 }
61 ~TestNetworkDelegate() {}
62
63 int OnBeforeURLRequest(URLRequest* request,
64 const CompletionCallback& callback,
65 GURL* new_url) override {
66 requests_++;
67 return net::OK;
68 }
69
70 void OnURLRequestDestroyed(URLRequest* request) { destroyed_requests_++; }
71
72 int GetNumRequests() { return requests_; }
73 int GetNumDestroyedRequests() { return destroyed_requests_; }
74
75 private:
76 int requests_;
77 int destroyed_requests_;
78 };
79
80 class CertificateErrorReporterTest : public ::testing::Test {
81 public:
82 ~CertificateErrorReporterTest() {
83 chrome_browser_net::SetUrlRequestMocksEnabled(false);
84 }
85
86 protected:
87 CertificateErrorReporterTest()
88 : context_(true), io_thread_(BrowserThread::IO, &message_loop_) {
89 chrome_browser_net::SetUrlRequestMocksEnabled(true);
90 context_.set_network_delegate(&network_delegate_);
91 context_.Init();
92 }
93
94 TestURLRequestContext context_;
95 TestNetworkDelegate network_delegate_;
96 base::MessageLoopForUI message_loop_;
Ryan Sleevi 2015/03/09 22:14:07 This is usually a warning sign. I believe you prob
estark 2015/03/09 23:21:17 Done.
97 content::TestBrowserThread io_thread_;
98 };
99
100 class TestReporter : public CertificateErrorReporter {
101 public:
102 TestReporter(net::URLRequestContext* context, const GURL& upload_url)
103 : CertificateErrorReporter(context, upload_url), latest_request_url_() {}
104
105 void OnResponseStarted(net::URLRequest* request) override {
106 latest_request_url_ = request->url();
107 CertificateErrorReporter::OnResponseStarted(request);
Ryan Sleevi 2015/03/09 22:14:06 Do you actually need to call this for your unit te
estark 2015/03/09 23:21:17 I think so, at least for some of them. |Certificat
Ryan Sleevi 2015/03/09 23:40:36 So from a design perspective then, it's usually se
108 run_loop_->Quit();
109 }
110
111 void Run() {
112 run_loop_.reset(new base::RunLoop());
113 run_loop_->Run();
Ryan Sleevi 2015/03/09 22:14:07 If the response is never started, this will never
estark 2015/03/09 23:21:17 Is there any way to avoid that? It seems like a fu
Ryan Sleevi 2015/03/09 23:40:36 RunUntilIdle() is usually a good way to avoid this
estark 2015/03/10 00:07:38 So are you saying that all the `run_loop.Run`s sho
mmenke 2015/03/10 14:25:46 I think RunUntilIdle() is a bit of an anti-pattern
114 }
115
116 GURL& GetLatestRequestURL() { return latest_request_url_; }
117
118 private:
119 scoped_ptr<base::RunLoop> run_loop_;
120 GURL latest_request_url_;
121 };
122
123 // Test that CertificateErrorReporter::SendReport creates a URLRequest
124 // for the endpoint.
125 TEST_F(CertificateErrorReporterTest, SendReportSendsRequest) {
126 GURL url = net::URLRequestMockHTTPJob::GetMockHttpsUrl(
127 base::FilePath(FILE_PATH_LITERAL("empty.html")));
128 TestReporter reporter(&context_, url);
129
130 EXPECT_EQ(0, network_delegate_.GetNumRequests());
131
132 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
133 kHostname, GetSSLInfo());
134 reporter.Run();
135
136 EXPECT_EQ(1, network_delegate_.GetNumRequests());
137 EXPECT_EQ(url, reporter.GetLatestRequestURL());
138 }
139
140 // Test that pending URLRequests get cleaned up when the reporter is
141 // deleted.
142 TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) {
143 GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure(
144 base::FilePath(FILE_PATH_LITERAL("empty.html")),
145 net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING);
146
147 EXPECT_EQ(0, network_delegate_.GetNumRequests());
148 EXPECT_EQ(0, network_delegate_.GetNumDestroyedRequests());
149
150 TestReporter* reporter = new TestReporter(&context_, url);
151 reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
152 kHostname, GetSSLInfo());
153 delete reporter;
Ryan Sleevi 2015/03/09 22:14:06 This seems to couple tightly with the RunLoop inte
estark 2015/03/09 23:21:17 I need some clarification on this one. IIUC RunUnt
Ryan Sleevi 2015/03/09 23:40:36 Well, if you're using a TestBrowserThreadBundle, i
estark 2015/03/10 00:07:38 Ah, I see. Done.
154
155 EXPECT_EQ(1, network_delegate_.GetNumRequests());
156 EXPECT_EQ(1, network_delegate_.GetNumDestroyedRequests());
157 }
158
159 // Test that a request that returns an error gets cleaned up.
160 TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) {
161 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED);
162
163 EXPECT_EQ(0, network_delegate_.GetNumRequests());
164 EXPECT_EQ(0, network_delegate_.GetNumDestroyedRequests());
165
166 TestReporter reporter(&context_, url);
167 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
168 kHostname, GetSSLInfo());
169 reporter.Run();
170
171 EXPECT_EQ(1, network_delegate_.GetNumRequests());
172 EXPECT_EQ(1, network_delegate_.GetNumDestroyedRequests());
173 }
174
175 } // namespace
176
177 namespace chrome_browser_net {
178
179 // Test that a built report has the correct data.
180 //
181 // This test is in the chrome_browser_net namespace so that it can be
182 // friended by CertificateErrorReporter.
183 TEST_F(CertificateErrorReporterTest, ReportIsBuiltCorrectly) {
184 SSLInfo info = GetSSLInfo();
185 CertLoggerRequest request;
186 CertificateErrorReporter::BuildReport(kHostname, info, request);
187
188 EXPECT_EQ(request.hostname(), kHostname);
189 EXPECT_EQ(request.cert_chain(), GetPEMEncodedChain());
190 EXPECT_EQ(request.pin().size(), 1);
191 EXPECT_EQ(request.pin().Get(0), kDummyFailureLog);
192 }
193 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698