OLD | NEW |
(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/ssl/certificate_error_report.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/path_service.h" |
| 13 #include "chrome/browser/ssl/cert_logger.pb.h" |
| 14 #include "chrome/common/chrome_paths.h" |
| 15 #include "net/base/test_data_directory.h" |
| 16 #include "net/cert/cert_status_flags.h" |
| 17 #include "net/ssl/ssl_info.h" |
| 18 #include "net/test/cert_test_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 using chrome_browser_ssl::CertLoggerRequest; |
| 22 using chrome_browser_ssl::CertificateErrorReport; |
| 23 using net::SSLInfo; |
| 24 |
| 25 namespace { |
| 26 |
| 27 const char kDummyHostname[] = "dummy.hostname.com"; |
| 28 const char kDummyFailureLog[] = "dummy failure log"; |
| 29 const char kTestCertFilename[] = "test_mail_google_com.pem"; |
| 30 |
| 31 const net::CertStatus kCertStatus = |
| 32 net::CERT_STATUS_COMMON_NAME_INVALID | net::CERT_STATUS_REVOKED; |
| 33 const size_t kNumCertErrors = 2; |
| 34 |
| 35 const chrome_browser_ssl::CertLoggerRequest::CertError kFirstReportedCertError = |
| 36 chrome_browser_ssl::CertLoggerRequest::ERR_CERT_COMMON_NAME_INVALID; |
| 37 const chrome_browser_ssl::CertLoggerRequest::CertError |
| 38 kSecondReportedCertError = |
| 39 chrome_browser_ssl::CertLoggerRequest::ERR_CERT_REVOKED; |
| 40 |
| 41 SSLInfo GetTestSSLInfo() { |
| 42 SSLInfo info; |
| 43 info.cert = |
| 44 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); |
| 45 info.is_issued_by_known_root = true; |
| 46 info.cert_status = kCertStatus; |
| 47 info.pinning_failure_log = kDummyFailureLog; |
| 48 return info; |
| 49 } |
| 50 |
| 51 std::string GetPEMEncodedChain() { |
| 52 base::FilePath cert_path = |
| 53 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); |
| 54 std::string cert_data; |
| 55 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); |
| 56 return cert_data; |
| 57 } |
| 58 |
| 59 // Test that a serialized CertificateErrorReport can be deserialized as |
| 60 // a CertLoggerRequest protobuf (which is the format that the receiving |
| 61 // server expects it in) with the right data in it. |
| 62 TEST(CertificateErrorReportTest, SerializedReportAsProtobuf) { |
| 63 SSLInfo ssl_info = GetTestSSLInfo(); |
| 64 |
| 65 std::string serialized_report; |
| 66 CertificateErrorReport report(kDummyHostname, ssl_info); |
| 67 report.Serialize(&serialized_report); |
| 68 |
| 69 CertLoggerRequest deserialized_report; |
| 70 ASSERT_TRUE(deserialized_report.ParseFromString(serialized_report)); |
| 71 EXPECT_EQ(kDummyHostname, deserialized_report.hostname()); |
| 72 EXPECT_EQ(GetPEMEncodedChain(), deserialized_report.cert_chain()); |
| 73 EXPECT_EQ(1, deserialized_report.pin().size()); |
| 74 EXPECT_EQ(kDummyFailureLog, deserialized_report.pin().Get(0)); |
| 75 |
| 76 std::set<CertLoggerRequest::CertError> reported_errors; |
| 77 reported_errors.insert(static_cast<CertLoggerRequest::CertError>( |
| 78 deserialized_report.cert_error().Get(0))); |
| 79 reported_errors.insert(static_cast<CertLoggerRequest::CertError>( |
| 80 deserialized_report.cert_error().Get(1))); |
| 81 EXPECT_EQ(kNumCertErrors, reported_errors.size()); |
| 82 EXPECT_EQ(1u, reported_errors.count(kFirstReportedCertError)); |
| 83 EXPECT_EQ(1u, reported_errors.count(kSecondReportedCertError)); |
| 84 } |
| 85 |
| 86 // Test that a serialized report can be parsed. |
| 87 TEST(CertificateErrorReportTest, ParseSerializedReport) { |
| 88 SSLInfo ssl_info = GetTestSSLInfo(); |
| 89 |
| 90 std::string serialized_report; |
| 91 CertificateErrorReport report(kDummyHostname, ssl_info); |
| 92 EXPECT_EQ(kDummyHostname, report.hostname()); |
| 93 report.Serialize(&serialized_report); |
| 94 |
| 95 CertificateErrorReport parsed; |
| 96 ASSERT_TRUE(parsed.InitializeFromString(serialized_report)); |
| 97 EXPECT_EQ(report.hostname(), parsed.hostname()); |
| 98 } |
| 99 |
| 100 } // namespace |
OLD | NEW |