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

Unified 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: update a comment 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 side-by-side diff with in-line comments
Download patch
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..009ee6637242722f0178c01470a95706503409d8
--- /dev/null
+++ b/chrome/browser/net/certificate_error_reporter_unittest.cc
@@ -0,0 +1,225 @@
+// 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/base/upload_bytes_element_reader.h"
+#include "net/base/upload_data_stream.h"
+#include "net/base/upload_element_reader.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());
+ }
+}
+
+// Check that data uploaded in the request matches the test data (an
+// SSL report for |kHostname| as returned by |GetSSLInfo()|).
+void CheckUploadData(URLRequest* request) {
+ const net::UploadDataStream* upload = request->get_upload();
+ ASSERT_TRUE(upload);
+ ASSERT_TRUE(upload->GetElementReaders());
+ EXPECT_EQ(upload->GetElementReaders()->size(), 1u);
mmenke 2015/03/11 15:34:28 For EXPECT_EQ and ASSERT_EQ, the expected value sh
estark 2015/03/11 22:19:10 Done.
+
+ const net::UploadBytesElementReader* reader =
+ (*upload->GetElementReaders())[0]->AsBytesReader();
+ ASSERT_TRUE(reader);
+ std::string upload_data(reader->bytes(), reader->length());
+ chrome_browser_net::CertLoggerRequest uploaded_request;
+
+ uploaded_request.ParseFromString(upload_data);
+
+ EXPECT_EQ(uploaded_request.hostname(), kHostname);
+ EXPECT_EQ(uploaded_request.cert_chain(), GetPEMEncodedChain());
+ EXPECT_EQ(uploaded_request.pin().size(), 1);
+ EXPECT_EQ(uploaded_request.pin().Get(0), kDummyFailureLog);
+}
+
+// A network delegate that lets tests check that a certificate error
+// report was sent. It counts the number of requests and lets tests
+// register a callback to run when the request is destroyed. It also
+// checks that the uploaded data is as expected (a report for
+// |kHostname| and |GetSSLInfo()|).
+class CertificateErrorReporterTestNetworkDelegate : public NetworkDelegateImpl {
estark 2015/03/11 05:58:34 Is this name too disgustingly long? I was trying t
mmenke 2015/03/11 15:34:28 I'm fine with long names. Maybe call it TestCerti
estark 2015/03/11 22:19:10 Done.
+ public:
+ CertificateErrorReporterTestNetworkDelegate()
+ : url_request_destroyed_callback_(base::Bind(&base::DoNothing)),
+ num_requests_(0) {}
+
+ ~CertificateErrorReporterTestNetworkDelegate() override {}
+
+ int OnBeforeURLRequest(URLRequest* request,
+ const CompletionCallback& callback,
+ GURL* new_url) override {
+ num_requests_++;
+ EXPECT_EQ(request->url(), expected_url_);
+ EXPECT_EQ(request->method(), "POST");
+
+ CheckUploadData(request);
+ return net::OK;
+ }
+
+ void OnURLRequestDestroyed(URLRequest* request) override {
+ url_request_destroyed_callback_.Run();
+ }
+
+ void SetURLRequestDestroyedCallback(base::Closure callback) {
mmenke 2015/03/11 15:34:28 nit: const base::Closure&
estark 2015/03/11 22:19:10 Done.
+ url_request_destroyed_callback_ = callback;
+ }
+
+ void SetExpectedURL(const GURL& url) { expected_url_ = url; }
mmenke 2015/03/11 15:34:28 nit: set_expected_url. Also more comment to call
estark 2015/03/11 22:19:10 Done.
+
+ int num_requests() const { return num_requests_; }
+
+ private:
+ base::Closure url_request_destroyed_callback_;
+ int num_requests_;
+ GURL expected_url_;
mmenke 2015/03/11 15:34:28 nit: DISALLOW_COPY_AND_ASSIGN(CertificateErrorRep
estark 2015/03/11 22:19:10 Done.
+};
+
+class CertificateErrorReporterTest : public ::testing::Test {
+ public:
+ CertificateErrorReporterTest() : context_(true) {
+ EnableUrlRequestMocks(true);
+ context_.set_network_delegate(&network_delegate_);
+ context_.Init();
+ }
+
+ ~CertificateErrorReporterTest() override {
+ EnableUrlRequestMocks(false);
+ chrome_browser_net::SetUrlRequestMocksEnabled(false);
mmenke 2015/03/11 15:34:28 This isn't needed - it does the same thing as the
estark 2015/03/11 22:19:10 Done.
+ }
+
+ CertificateErrorReporterTestNetworkDelegate* network_delegate() {
+ return &network_delegate_;
+ }
+
+ TestURLRequestContext* context() { return &context_; }
+
+ private:
+ content::TestBrowserThreadBundle thread_bundle_;
mmenke 2015/03/11 15:34:28 Do we still needs this? Think we can get away wit
estark 2015/03/11 22:19:10 Done.
+ TestURLRequestContext context_;
+ CertificateErrorReporterTestNetworkDelegate network_delegate_;
mmenke 2015/03/11 15:34:28 The network delegate should be before the context,
estark 2015/03/11 22:19:10 Done.
+};
+
+// Test that CertificateErrorReporter::SendReport creates a URLRequest
+// for the endpoint and sends the expected data.
+TEST_F(CertificateErrorReporterTest, SendReportSendsRequest) {
+ base::RunLoop run_loop;
+ network_delegate()->SetURLRequestDestroyedCallback(run_loop.QuitClosure());
+
+ GURL url = net::URLRequestMockHTTPJob::GetMockHttpsUrl(
+ base::FilePath(FILE_PATH_LITERAL("empty.html")));
+ network_delegate()->SetExpectedURL(url);
+
+ CertificateErrorReporter reporter(context(), url);
+
+ EXPECT_EQ(0, network_delegate()->num_requests());
+
+ reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
+ kHostname, GetSSLInfo());
+ run_loop.Run();
+
+ EXPECT_EQ(1, network_delegate()->num_requests());
+}
+
+// Test that pending URLRequests get cleaned up when the reporter is
+// deleted.
+TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) {
+ base::RunLoop run_loop;
+ network_delegate()->SetURLRequestDestroyedCallback(run_loop.QuitClosure());
+
+ GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure(
+ base::FilePath(FILE_PATH_LITERAL("empty.html")),
+ net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING);
+ network_delegate()->SetExpectedURL(url);
+
+ EXPECT_EQ(0, network_delegate()->num_requests());
+
+ scoped_ptr<CertificateErrorReporter> reporter(
+ new CertificateErrorReporter(context(), url));
+ reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
+ kHostname, GetSSLInfo());
+ reporter.reset();
+
+ run_loop.Run();
+
+ EXPECT_EQ(1, network_delegate()->num_requests());
+}
+
+// Test that a request that returns an error gets cleaned up.
+TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) {
+ base::RunLoop run_loop;
+ network_delegate()->SetURLRequestDestroyedCallback(run_loop.QuitClosure());
+
+ GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED);
+ network_delegate()->SetExpectedURL(url);
+
+ EXPECT_EQ(0, network_delegate()->num_requests());
+
+ CertificateErrorReporter reporter(context(), url);
+ reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION,
+ kHostname, GetSSLInfo());
+ run_loop.Run();
+
+ EXPECT_EQ(1, network_delegate()->num_requests());
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698