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 <string> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/path_service.h" |
| 12 #include "chrome/browser/ssl/cert_logger.pb.h" |
| 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "net/base/test_data_directory.h" |
| 15 #include "net/ssl/ssl_info.h" |
| 16 #include "net/test/cert_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using chrome_browser_ssl::CertLoggerRequest; |
| 20 using chrome_browser_ssl::CertificateErrorReport; |
| 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 SSLInfo GetTestSSLInfo() { |
| 30 SSLInfo info; |
| 31 info.cert = |
| 32 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); |
| 33 info.is_issued_by_known_root = true; |
| 34 info.pinning_failure_log = kDummyFailureLog; |
| 35 return info; |
| 36 } |
| 37 |
| 38 std::string GetPEMEncodedChain() { |
| 39 base::FilePath cert_path = |
| 40 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); |
| 41 std::string cert_data; |
| 42 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); |
| 43 return cert_data; |
| 44 } |
| 45 |
| 46 class CertificateErrorReportTest : public testing::Test { |
| 47 public: |
| 48 CertificateErrorReportTest() {} |
| 49 ~CertificateErrorReportTest() override {} |
| 50 }; |
| 51 |
| 52 // Test that a serialized CertificateErrorReport can be deserialized as |
| 53 // a CertLoggerRequest protobuf (which is the format that the receiving |
| 54 // server expects it in) with the right data in it. |
| 55 TEST_F(CertificateErrorReportTest, SerializedReportAsProtobuf) { |
| 56 SSLInfo ssl_info = GetTestSSLInfo(); |
| 57 |
| 58 std::string serialized_report; |
| 59 CertificateErrorReport report(kDummyHostname, ssl_info); |
| 60 report.Serialize(&serialized_report); |
| 61 |
| 62 CertLoggerRequest deserialized_report; |
| 63 ASSERT_TRUE(deserialized_report.ParseFromString(serialized_report)); |
| 64 EXPECT_EQ(kDummyHostname, deserialized_report.hostname()); |
| 65 EXPECT_EQ(GetPEMEncodedChain(), deserialized_report.cert_chain()); |
| 66 EXPECT_EQ(1, deserialized_report.pin().size()); |
| 67 EXPECT_EQ(kDummyFailureLog, deserialized_report.pin().Get(0)); |
| 68 } |
| 69 |
| 70 // Test that a serialized report can be parsed. |
| 71 TEST_F(CertificateErrorReportTest, ParseSerializedReport) { |
| 72 SSLInfo ssl_info = GetTestSSLInfo(); |
| 73 |
| 74 std::string serialized_report; |
| 75 CertificateErrorReport report(kDummyHostname, ssl_info); |
| 76 EXPECT_EQ(kDummyHostname, report.hostname()); |
| 77 report.Serialize(&serialized_report); |
| 78 |
| 79 CertificateErrorReport parsed; |
| 80 ASSERT_TRUE(parsed.InitializeFromString(serialized_report)); |
| 81 EXPECT_EQ(report.hostname(), parsed.hostname()); |
| 82 } |
| 83 |
| 84 } // namespace |
OLD | NEW |