Chromium Code Reviews| Index: chrome/browser/net/certificate_error_reporter_unittest.cc |
| diff --git a/chrome/browser/net/certificate_error_reporter_unittest.cc b/chrome/browser/net/certificate_error_reporter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7b8d653de899899fa4aa14804a1cdb6f85f285b3 |
| --- /dev/null |
| +++ b/chrome/browser/net/certificate_error_reporter_unittest.cc |
| @@ -0,0 +1,193 @@ |
| +// Copyright 2015 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 "chrome/browser/net/certificate_error_reporter.h" |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/net/cert_logger.pb.h" |
| +#include "chrome/browser/net/url_request_mock_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "net/base/network_delegate_impl.h" |
| +#include "net/base/test_data_directory.h" |
| +#include "net/test/cert_test_util.h" |
| +#include "net/test/url_request/url_request_failed_job.h" |
| +#include "net/test/url_request/url_request_mock_http_job.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using chrome_browser_net::CertificateErrorReporter; |
| +using content::BrowserThread; |
| +using net::CompletionCallback; |
| +using net::SSLInfo; |
| +using net::NetworkDelegateImpl; |
| +using net::TestURLRequestContext; |
| +using net::URLRequest; |
| + |
| +namespace { |
| + |
| +const char kHostname[] = "test.mail.google.com"; |
| +const char kDummyFailureLog[] = "dummy failure log"; |
| +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.
|
| + |
| +SSLInfo GetSSLInfo() { |
| + SSLInfo info; |
| + info.cert = |
| + net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); |
| + info.is_issued_by_known_root = true; |
| + info.pinning_failure_log = kDummyFailureLog; |
| + return info; |
| +} |
| + |
| +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.
|
| + base::FilePath cert_path = |
| + net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); |
| + std::string cert_data; |
| + EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); |
| + return cert_data; |
| +} |
| + |
| +// A network delegate that lets tests check that requests were created |
| +// or destroyed. |
| +class TestNetworkDelegate : public NetworkDelegateImpl { |
| + public: |
| + TestNetworkDelegate() : NetworkDelegateImpl() { |
| + requests_ = 0; |
| + destroyed_requests_ = 0; |
| + } |
| + ~TestNetworkDelegate() {} |
| + |
| + int OnBeforeURLRequest(URLRequest* request, |
| + const CompletionCallback& callback, |
| + GURL* new_url) override { |
| + requests_++; |
| + return net::OK; |
| + } |
| + |
| + void OnURLRequestDestroyed(URLRequest* request) { destroyed_requests_++; } |
| + |
| + int GetNumRequests() { return requests_; } |
| + int GetNumDestroyedRequests() { return destroyed_requests_; } |
| + |
| + private: |
| + int requests_; |
| + int destroyed_requests_; |
| +}; |
| + |
| +class CertificateErrorReporterTest : public ::testing::Test { |
| + public: |
| + ~CertificateErrorReporterTest() { |
| + chrome_browser_net::SetUrlRequestMocksEnabled(false); |
| + } |
| + |
| + protected: |
| + CertificateErrorReporterTest() |
| + : context_(true), io_thread_(BrowserThread::IO, &message_loop_) { |
| + chrome_browser_net::SetUrlRequestMocksEnabled(true); |
| + context_.set_network_delegate(&network_delegate_); |
| + context_.Init(); |
| + } |
| + |
| + TestURLRequestContext context_; |
| + TestNetworkDelegate network_delegate_; |
| + 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.
|
| + content::TestBrowserThread io_thread_; |
| +}; |
| + |
| +class TestReporter : public CertificateErrorReporter { |
| + public: |
| + TestReporter(net::URLRequestContext* context, const GURL& upload_url) |
| + : CertificateErrorReporter(context, upload_url), latest_request_url_() {} |
| + |
| + void OnResponseStarted(net::URLRequest* request) override { |
| + latest_request_url_ = request->url(); |
| + 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
|
| + run_loop_->Quit(); |
| + } |
| + |
| + void Run() { |
| + run_loop_.reset(new base::RunLoop()); |
| + 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
|
| + } |
| + |
| + GURL& GetLatestRequestURL() { return latest_request_url_; } |
| + |
| + private: |
| + scoped_ptr<base::RunLoop> run_loop_; |
| + GURL latest_request_url_; |
| +}; |
| + |
| +// Test that CertificateErrorReporter::SendReport creates a URLRequest |
| +// for the endpoint. |
| +TEST_F(CertificateErrorReporterTest, SendReportSendsRequest) { |
| + GURL url = net::URLRequestMockHTTPJob::GetMockHttpsUrl( |
| + base::FilePath(FILE_PATH_LITERAL("empty.html"))); |
| + TestReporter reporter(&context_, url); |
| + |
| + EXPECT_EQ(0, network_delegate_.GetNumRequests()); |
| + |
| + reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
| + kHostname, GetSSLInfo()); |
| + reporter.Run(); |
| + |
| + EXPECT_EQ(1, network_delegate_.GetNumRequests()); |
| + EXPECT_EQ(url, reporter.GetLatestRequestURL()); |
| +} |
| + |
| +// Test that pending URLRequests get cleaned up when the reporter is |
| +// deleted. |
| +TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) { |
| + GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure( |
| + base::FilePath(FILE_PATH_LITERAL("empty.html")), |
| + net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING); |
| + |
| + EXPECT_EQ(0, network_delegate_.GetNumRequests()); |
| + EXPECT_EQ(0, network_delegate_.GetNumDestroyedRequests()); |
| + |
| + TestReporter* reporter = new TestReporter(&context_, url); |
| + reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
| + kHostname, GetSSLInfo()); |
| + 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.
|
| + |
| + EXPECT_EQ(1, network_delegate_.GetNumRequests()); |
| + EXPECT_EQ(1, network_delegate_.GetNumDestroyedRequests()); |
| +} |
| + |
| +// Test that a request that returns an error gets cleaned up. |
| +TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) { |
| + GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED); |
| + |
| + EXPECT_EQ(0, network_delegate_.GetNumRequests()); |
| + EXPECT_EQ(0, network_delegate_.GetNumDestroyedRequests()); |
| + |
| + TestReporter reporter(&context_, url); |
| + reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
| + kHostname, GetSSLInfo()); |
| + reporter.Run(); |
| + |
| + EXPECT_EQ(1, network_delegate_.GetNumRequests()); |
| + EXPECT_EQ(1, network_delegate_.GetNumDestroyedRequests()); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace chrome_browser_net { |
| + |
| +// Test that a built report has the correct data. |
| +// |
| +// This test is in the chrome_browser_net namespace so that it can be |
| +// friended by CertificateErrorReporter. |
| +TEST_F(CertificateErrorReporterTest, ReportIsBuiltCorrectly) { |
| + SSLInfo info = GetSSLInfo(); |
| + CertLoggerRequest request; |
| + CertificateErrorReporter::BuildReport(kHostname, info, request); |
| + |
| + EXPECT_EQ(request.hostname(), kHostname); |
| + EXPECT_EQ(request.cert_chain(), GetPEMEncodedChain()); |
| + EXPECT_EQ(request.pin().size(), 1); |
| + EXPECT_EQ(request.pin().Get(0), kDummyFailureLog); |
| +} |
| +} // namespace chrome_browser_net |