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

Side by Side Diff: chrome/browser/ssl/certificate_error_report_unittest.cc

Issue 1117173004: Split cert reporter class into report building/serializing and sending (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: document parse/serialize return value Created 5 years, 7 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 unified diff | Download patch
OLDNEW
(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 const net::CertStatus kCertStatus =
31 net::CERT_STATUS_COMMON_NAME_INVALID | net::CERT_STATUS_REVOKED;
32 const size_t kNumCertErrors = 2;
33 const chrome_browser_ssl::CertLoggerRequest::CertError kFirstReportedCertError =
34 chrome_browser_ssl::CertLoggerRequest::ERR_CERT_COMMON_NAME_INVALID;
35 const chrome_browser_ssl::CertLoggerRequest::CertError
36 kSecondReportedCertError =
37 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
38
39 SSLInfo GetTestSSLInfo() {
40 SSLInfo info;
41 info.cert =
42 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename);
43 info.is_issued_by_known_root = true;
44 info.cert_status = kCertStatus;
45 info.pinning_failure_log = kDummyFailureLog;
46 return info;
47 }
48
49 std::string GetPEMEncodedChain() {
50 base::FilePath cert_path =
51 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename);
52 std::string cert_data;
53 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data));
54 return cert_data;
55 }
56
57 class CertificateErrorReportTest : public testing::Test {
58 public:
59 CertificateErrorReportTest() {}
60 ~CertificateErrorReportTest() override {}
61 };
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.
62
63 // Test that a serialized CertificateErrorReport can be deserialized as
64 // a CertLoggerRequest protobuf (which is the format that the receiving
65 // server expects it in) with the right data in it.
66 TEST_F(CertificateErrorReportTest, SerializedReportAsProtobuf) {
67 SSLInfo ssl_info = GetTestSSLInfo();
68
69 std::string serialized_report;
70 CertificateErrorReport report(kDummyHostname, ssl_info);
71 report.Serialize(&serialized_report);
72
73 CertLoggerRequest deserialized_report;
74 ASSERT_TRUE(deserialized_report.ParseFromString(serialized_report));
75 EXPECT_EQ(kDummyHostname, deserialized_report.hostname());
76 EXPECT_EQ(GetPEMEncodedChain(), deserialized_report.cert_chain());
77 EXPECT_EQ(1, deserialized_report.pin().size());
78 EXPECT_EQ(kDummyFailureLog, deserialized_report.pin().Get(0));
79
80 std::set<CertLoggerRequest::CertError> reported_errors;
81 reported_errors.insert(static_cast<CertLoggerRequest::CertError>(
82 deserialized_report.cert_error().Get(0)));
83 reported_errors.insert(static_cast<CertLoggerRequest::CertError>(
84 deserialized_report.cert_error().Get(1)));
85 EXPECT_EQ(kNumCertErrors, reported_errors.size());
86 EXPECT_EQ(1u, reported_errors.count(kFirstReportedCertError));
87 EXPECT_EQ(1u, reported_errors.count(kSecondReportedCertError));
88 }
89
90 // Test that a serialized report can be parsed.
91 TEST_F(CertificateErrorReportTest, ParseSerializedReport) {
92 SSLInfo ssl_info = GetTestSSLInfo();
93
94 std::string serialized_report;
95 CertificateErrorReport report(kDummyHostname, ssl_info);
96 EXPECT_EQ(kDummyHostname, report.hostname());
97 report.Serialize(&serialized_report);
98
99 CertificateErrorReport parsed;
100 ASSERT_TRUE(parsed.InitializeFromString(serialized_report));
101 EXPECT_EQ(report.hostname(), parsed.hostname());
102 }
103
104 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698