Chromium Code Reviews| Index: chrome/browser/ssl/certificate_error_report_unittest.cc |
| diff --git a/chrome/browser/ssl/certificate_error_report_unittest.cc b/chrome/browser/ssl/certificate_error_report_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9d6e913f6d151a4d4d4d10bedd18b0c81353c5cb |
| --- /dev/null |
| +++ b/chrome/browser/ssl/certificate_error_report_unittest.cc |
| @@ -0,0 +1,104 @@ |
| +// 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/ssl/certificate_error_report.h" |
| + |
| +#include <set> |
| +#include <string> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/path_service.h" |
| +#include "chrome/browser/ssl/cert_logger.pb.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "net/base/test_data_directory.h" |
| +#include "net/cert/cert_status_flags.h" |
| +#include "net/ssl/ssl_info.h" |
| +#include "net/test/cert_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using chrome_browser_ssl::CertLoggerRequest; |
| +using chrome_browser_ssl::CertificateErrorReport; |
| +using net::SSLInfo; |
| + |
| +namespace { |
| + |
| +const char kDummyHostname[] = "dummy.hostname.com"; |
| +const char kDummyFailureLog[] = "dummy failure log"; |
| +const char kTestCertFilename[] = "test_mail_google_com.pem"; |
| +const net::CertStatus kCertStatus = |
| + net::CERT_STATUS_COMMON_NAME_INVALID | net::CERT_STATUS_REVOKED; |
| +const size_t kNumCertErrors = 2; |
| +const chrome_browser_ssl::CertLoggerRequest::CertError kFirstReportedCertError = |
| + chrome_browser_ssl::CertLoggerRequest::ERR_CERT_COMMON_NAME_INVALID; |
| +const chrome_browser_ssl::CertLoggerRequest::CertError |
| + kSecondReportedCertError = |
| + chrome_browser_ssl::CertLoggerRequest::ERR_CERT_REVOKED; |
|
Ryan Sleevi
2015/05/13 01:02:12
Some appropriate newlines may help readability her
estark
2015/05/13 01:44:49
Done. Do you think this would be a place to do |us
|
| + |
| +SSLInfo GetTestSSLInfo() { |
| + SSLInfo info; |
| + info.cert = |
| + net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); |
| + info.is_issued_by_known_root = true; |
| + info.cert_status = kCertStatus; |
| + 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; |
| +} |
| + |
| +class CertificateErrorReportTest : public testing::Test { |
| + public: |
| + CertificateErrorReportTest() {} |
| + ~CertificateErrorReportTest() override {} |
| +}; |
|
Ryan Sleevi
2015/05/13 01:02:12
There's no need to use this common test fixture. Y
estark
2015/05/13 01:44:49
Done.
|
| + |
| +// Test that a serialized CertificateErrorReport can be deserialized as |
| +// a CertLoggerRequest protobuf (which is the format that the receiving |
| +// server expects it in) with the right data in it. |
| +TEST_F(CertificateErrorReportTest, SerializedReportAsProtobuf) { |
| + SSLInfo ssl_info = GetTestSSLInfo(); |
| + |
| + std::string serialized_report; |
| + CertificateErrorReport report(kDummyHostname, ssl_info); |
| + report.Serialize(&serialized_report); |
| + |
| + CertLoggerRequest deserialized_report; |
| + ASSERT_TRUE(deserialized_report.ParseFromString(serialized_report)); |
| + EXPECT_EQ(kDummyHostname, deserialized_report.hostname()); |
| + EXPECT_EQ(GetPEMEncodedChain(), deserialized_report.cert_chain()); |
| + EXPECT_EQ(1, deserialized_report.pin().size()); |
| + EXPECT_EQ(kDummyFailureLog, deserialized_report.pin().Get(0)); |
| + |
| + std::set<CertLoggerRequest::CertError> reported_errors; |
| + reported_errors.insert(static_cast<CertLoggerRequest::CertError>( |
| + deserialized_report.cert_error().Get(0))); |
| + reported_errors.insert(static_cast<CertLoggerRequest::CertError>( |
| + deserialized_report.cert_error().Get(1))); |
| + EXPECT_EQ(kNumCertErrors, reported_errors.size()); |
| + EXPECT_EQ(1u, reported_errors.count(kFirstReportedCertError)); |
| + EXPECT_EQ(1u, reported_errors.count(kSecondReportedCertError)); |
| +} |
| + |
| +// Test that a serialized report can be parsed. |
| +TEST_F(CertificateErrorReportTest, ParseSerializedReport) { |
| + SSLInfo ssl_info = GetTestSSLInfo(); |
| + |
| + std::string serialized_report; |
| + CertificateErrorReport report(kDummyHostname, ssl_info); |
| + EXPECT_EQ(kDummyHostname, report.hostname()); |
| + report.Serialize(&serialized_report); |
| + |
| + CertificateErrorReport parsed; |
| + ASSERT_TRUE(parsed.InitializeFromString(serialized_report)); |
| + EXPECT_EQ(report.hostname(), parsed.hostname()); |
| +} |
| + |
| +} // namespace |