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