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..0960d5a874359240d9b628445f814cc39ee32e15 |
| --- /dev/null |
| +++ b/chrome/browser/net/certificate_error_reporter_unittest.cc |
| @@ -0,0 +1,221 @@ |
| +// 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/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/path_service.h" |
| +#include "base/run_loop.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "chrome/browser/net/cert_logger.pb.h" |
| +#include "chrome/browser/net/url_request_mock_util.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread_bundle.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_filter.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[] = FILE_PATH_LITERAL("test_mail_google_com.pem"); |
| + |
| +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() { |
| + base::FilePath cert_path = |
| + net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); |
| + std::string cert_data; |
| + EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); |
| + return cert_data; |
| +} |
| + |
| +void EnableUrlRequestMocks(bool enable) { |
| + net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| + if (enable) { |
| + base::FilePath root_http; |
| + net::URLRequestFailedJob::AddUrlHandler(); |
| + PathService::Get(chrome::DIR_TEST_DATA, &root_http); |
| + net::URLRequestMockHTTPJob::AddUrlHandlers( |
| + root_http, BrowserThread::GetBlockingPool()); |
| + } |
| +} |
| + |
| +// A network delegate that lets tests check that requests were created |
| +// or destroyed. |
| +class TestNetworkDelegate : public NetworkDelegateImpl { |
| + public: |
| + TestNetworkDelegate() : NetworkDelegateImpl() { |
|
mmenke
2015/03/10 15:08:46
0-argument parent class constructor does not need
estark
2015/03/11 05:58:33
Done.
|
| + requests_ = 0; |
| + destroyed_requests_ = 0; |
|
mmenke
2015/03/10 15:08:46
These should be in the initializer list.
estark
2015/03/11 05:58:34
Done.
|
| + } |
| + ~TestNetworkDelegate() {} |
|
mmenke
2015/03/10 15:08:45
override
estark
2015/03/11 05:58:33
Done.
|
| + |
| + int OnBeforeURLRequest(URLRequest* request, |
| + const CompletionCallback& callback, |
| + GURL* new_url) override { |
| + requests_++; |
| + return net::OK; |
| + } |
| + |
| + void OnURLRequestDestroyed(URLRequest* request) { destroyed_requests_++; } |
|
mmenke
2015/03/10 15:08:46
override
estark
2015/03/11 05:58:34
Done.
|
| + |
| + int GetNumRequests() { return requests_; } |
| + int GetNumDestroyedRequests() { return destroyed_requests_; } |
|
mmenke
2015/03/10 15:08:46
these should both be const, and have the same name
estark
2015/03/11 05:58:33
Done.
|
| + |
| + private: |
| + int requests_; |
| + int destroyed_requests_; |
|
mmenke
2015/03/10 15:08:46
These should match the number of the getters. Add
estark
2015/03/11 05:58:34
Done.
|
| +}; |
| + |
| +class CertificateErrorReporterTest : public ::testing::Test { |
| + public: |
| + ~CertificateErrorReporterTest() { |
|
mmenke
2015/03/10 15:08:45
override
estark
2015/03/11 05:58:33
Done.
|
| + EnableUrlRequestMocks(false); |
| + chrome_browser_net::SetUrlRequestMocksEnabled(false); |
| + } |
| + |
| + protected: |
| + CertificateErrorReporterTest() : context_(true) { |
|
mmenke
2015/03/10 15:08:46
Optional: Suggest just making this public, before
estark
2015/03/11 05:58:33
Done.
|
| + EnableUrlRequestMocks(true); |
| + context_.set_network_delegate(&network_delegate_); |
| + context_.Init(); |
| + } |
| + |
| + content::TestBrowserThreadBundle thread_bundle_; |
|
mmenke
2015/03/10 15:08:46
The Google style guide requires all class variable
Ryan Sleevi
2015/03/10 17:39:30
Matt, test fixtures are explicitly allowed to be P
mmenke
2015/03/10 17:52:32
Hrm...Did the old version say that? I admittedly
estark
2015/03/11 05:58:34
Done.
|
| + TestURLRequestContext context_; |
| + TestNetworkDelegate network_delegate_; |
| +}; |
| + |
| +class TestReporter : public CertificateErrorReporter { |
|
mmenke
2015/03/10 15:08:45
Need a virtual destructor in child classes.
estark
2015/03/11 05:58:34
This class no longer exists. I moved the callback
|
| + public: |
| + TestReporter(net::URLRequestContext* context, const GURL& upload_url) |
| + : CertificateErrorReporter(context, upload_url), latest_request_url_() { |
|
mmenke
2015/03/10 15:08:46
Remove latest_request_url_() - the no argument con
estark
2015/03/11 05:58:33
no longer applicable here, but noted for future
|
| + response_started_callback_ = base::Bind(&base::DoNothing); |
|
mmenke
2015/03/10 15:08:46
This should be in an initializer list.
estark
2015/03/11 05:58:34
Done (in the network delegate subclass now).
|
| + } |
| + |
| + TestReporter(net::URLRequestContext* context, |
| + const GURL& upload_url, |
| + base::Closure response_started_callback) |
| + : CertificateErrorReporter(context, upload_url), |
| + latest_request_url_(), |
|
mmenke
2015/03/10 15:08:45
Remove latest_request_url_()
estark
2015/03/11 05:58:33
(no longer applicable)
|
| + response_started_callback_(response_started_callback) {} |
| + |
| + void OnResponseStarted(net::URLRequest* request) override { |
| + latest_request_url_ = request->url(); |
| + CertificateErrorReporter::OnResponseStarted(request); |
| + base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| + response_started_callback_); |
|
mmenke
2015/03/10 15:08:46
Can invoke this synchronously with just response_s
Ryan Sleevi
2015/03/10 17:39:30
I'm not sure I'd agree on the is_empty. By not che
mmenke
2015/03/10 17:52:32
You're right - I hadn't realized that the other co
estark
2015/03/11 05:58:34
I think this discussion is no longer applicable, s
|
| + } |
| + |
| + GURL& GetLatestRequestURL() { return latest_request_url_; } |
|
mmenke
2015/03/10 15:08:46
This should be: const GURL& latest_request_url()
estark
2015/03/11 05:58:33
(no longer applicable)
|
| + |
| + private: |
| + GURL latest_request_url_; |
| + base::Closure response_started_callback_; |
| +}; |
| + |
| +// Test that CertificateErrorReporter::SendReport creates a URLRequest |
| +// for the endpoint. |
| +TEST_F(CertificateErrorReporterTest, SendReportSendsRequest) { |
| + base::RunLoop run_loop; |
| + base::Closure callback = run_loop.QuitClosure(); |
|
mmenke
2015/03/10 15:08:45
optional nit: No need to store this on the stack.
estark
2015/03/11 05:58:34
Done.
|
| + GURL url = net::URLRequestMockHTTPJob::GetMockHttpsUrl( |
| + base::FilePath(FILE_PATH_LITERAL("empty.html"))); |
| + TestReporter reporter(&context_, url, callback); |
| + |
| + EXPECT_EQ(0, network_delegate_.GetNumRequests()); |
| + |
| + reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
| + kHostname, GetSSLInfo()); |
| + run_loop.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) { |
| + base::RunLoop run_loop; |
| + 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; |
|
mmenke
2015/03/10 15:08:45
Rather than calling delete, I suggest putting it i
estark
2015/03/11 05:58:34
Done.
|
| + |
| + run_loop.RunUntilIdle(); |
|
mmenke
2015/03/10 15:08:46
Is this needed?
estark
2015/03/10 15:54:15
rsleevi advised to add it to avoid coupling with t
mmenke
2015/03/10 16:08:52
Of course, it could *also* post a delayed task to
Ryan Sleevi
2015/03/10 17:39:30
On 2015/03/10 16:08:52, mmenke wrote:
mmenke
2015/03/10 17:52:33
I'm not suggesting adding anything to the main Net
estark
2015/03/11 05:58:34
Done -- the callback is now called from |OnURLRequ
|
| + |
| + 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); |
| + base::RunLoop run_loop; |
| + base::Closure callback = run_loop.QuitClosure(); |
| + |
| + EXPECT_EQ(0, network_delegate_.GetNumRequests()); |
| + EXPECT_EQ(0, network_delegate_.GetNumDestroyedRequests()); |
| + |
| + TestReporter reporter(&context_, url, callback); |
| + reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
| + kHostname, GetSSLInfo()); |
| + run_loop.Run(); |
| + |
| + EXPECT_EQ(1, network_delegate_.GetNumRequests()); |
| + EXPECT_EQ(1, network_delegate_.GetNumDestroyedRequests()); |
| +} |
| + |
|
mmenke
2015/03/10 15:08:46
Since we're using a std::set for requests, we shou
estark
2015/03/11 05:58:34
If it's okay with you, I'd like to do this in a fo
mmenke
2015/03/11 15:34:28
If this were a CL adding a feature, I'd definitely
felt
2015/03/11 15:51:23
My $0.02:
- This CL is blocking a lot of other wo
|
| +} // namespace |
| + |
| +namespace chrome_browser_net { |
|
mmenke
2015/03/10 15:08:46
Having a test in a separate namespace is weird. S
estark
2015/03/10 15:54:15
Sorry, don't follow. This test is in the chrome_br
mmenke
2015/03/10 16:08:52
Sorry, didn't notice that. I'm not a fan of frien
Ryan Sleevi
2015/03/10 17:39:30
On 2015/03/10 16:08:52, mmenke wrote:
estark
2015/03/11 05:58:34
Done -- added a check that the uploaded report is
|
| + |
| +// 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 |