 Chromium Code Reviews
 Chromium Code Reviews Issue 2040513003:
  Implement Expect-Staple  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 2040513003:
  Implement Expect-Staple  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: net/cert/ocsp_staple_unittest.cc | 
| diff --git a/net/cert/ocsp_staple_unittest.cc b/net/cert/ocsp_staple_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..5f86dc160677a2635946e0a25f140b3330289ddb | 
| --- /dev/null | 
| +++ b/net/cert/ocsp_staple_unittest.cc | 
| @@ -0,0 +1,126 @@ | 
| +// Copyright 2016 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 "net/cert/ocsp_staple.h" | 
| 
estark
2016/06/14 02:10:28
missing newline above
 
dadrian
2016/06/14 18:40:01
Done.
 | 
| + | 
| +#include "net/base/host_port_pair.h" | 
| +#include "net/cert/internal/test_helpers.h" | 
| + | 
| +namespace net { | 
| + | 
| +namespace { | 
| + | 
| +const char kOCSPPathPrefix[] = "net/data/parse_ocsp_unittest/"; | 
| + | 
| +const base::TimeDelta kAgeTenYears = base::TimeDelta::FromDays(3650); | 
| + | 
| +struct OCSPTest { | 
| + std::string response; | 
| + scoped_refptr<X509Certificate> certificate; | 
| +}; | 
| + | 
| +bool LoadOCSPFromFile(std::string file_name, OCSPTest* ocsp) { | 
| + std::string ca_data; | 
| + std::string cert_data; | 
| + const PemBlockMapping mappings[] = { | 
| + {"OCSP RESPONSE", &ocsp->response}, | 
| + {"CA CERTIFICATE", &ca_data}, | 
| + {"CERTIFICATE", &cert_data}, | 
| + }; | 
| + std::string full_path = std::string(kOCSPPathPrefix) + file_name; | 
| + if (!ReadTestDataFromPemFile(full_path, mappings)) | 
| + return false; | 
| + | 
| + // Parse the server certificate | 
| + CertificateList server_cert_list = | 
| + X509Certificate::CreateCertificateListFromBytes( | 
| + cert_data.data(), cert_data.size(), | 
| + X509Certificate::FORMAT_SINGLE_CERTIFICATE); | 
| + ocsp->certificate = server_cert_list[0]; | 
| + return true; | 
| +} | 
| + | 
| +} // namespace | 
| + | 
| +class ExpectStapleReportTest : public testing::Test { | 
| + protected: | 
| + base::Time verify_time_; | 
| + | 
| + void SetUp() override { verify_time_ = base::Time::Now(); } | 
| + | 
| + std::unique_ptr<ExpectStapleReport> MakeReport(const OCSPTest& ocsp) { | 
| + std::unique_ptr<ExpectStapleReport> report = | 
| + ExpectStapleReport::FromRawOCSPResponse( | 
| + ocsp.response, verify_time_, kAgeTenYears, *ocsp.certificate); | 
| + return report; | 
| + } | 
| +}; | 
| + | 
| +TEST_F(ExpectStapleReportTest, Valid) { | 
| + OCSPTest ocsp; | 
| + ASSERT_TRUE(LoadOCSPFromFile("good_response.pem", &ocsp)); | 
| + auto report = MakeReport(ocsp); | 
| + ASSERT_TRUE(report); | 
| + EXPECT_EQ(ExpectStapleReport::StapleError::OK, report->staple_error()); | 
| + EXPECT_EQ(verify_time_, report->verify_time()); | 
| + const auto& stapled_responses = report->stapled_responses(); | 
| + ASSERT_EQ(1u, stapled_responses.size()); | 
| + EXPECT_TRUE(stapled_responses[0].is_date_valid); | 
| + EXPECT_TRUE(stapled_responses[0].is_correct_certificate); | 
| + EXPECT_EQ(OCSPCertStatus::Status::GOOD, stapled_responses[0].status); | 
| +}; | 
| + | 
| +TEST_F(ExpectStapleReportTest, ValidWithExtension) { | 
| + OCSPTest ocsp; | 
| + ASSERT_TRUE(LoadOCSPFromFile("has_extension.pem", &ocsp)); | 
| + auto report = MakeReport(ocsp); | 
| + ASSERT_TRUE(report); | 
| + EXPECT_EQ(ExpectStapleReport::StapleError::OK, report->staple_error()); | 
| + EXPECT_EQ(verify_time_, report->verify_time()); | 
| +}; | 
| + | 
| +TEST_F(ExpectStapleReportTest, MissingSingleResponse) { | 
| + OCSPTest ocsp; | 
| + ASSERT_TRUE(LoadOCSPFromFile("missing_response.pem", &ocsp)); | 
| + auto report = MakeReport(ocsp); | 
| + ASSERT_TRUE(report); | 
| + EXPECT_EQ(ExpectStapleReport::StapleError::NO_MATCHING_RESPONSE, | 
| + report->staple_error()); | 
| + EXPECT_EQ(verify_time_, report->verify_time()); | 
| + const auto& stapled_responses = report->stapled_responses(); | 
| + EXPECT_EQ(0u, stapled_responses.size()); | 
| +}; | 
| + | 
| +TEST_F(ExpectStapleReportTest, MultipleResponse) { | 
| + OCSPTest ocsp; | 
| + ASSERT_TRUE(LoadOCSPFromFile("multiple_response.pem", &ocsp)); | 
| + auto report = MakeReport(ocsp); | 
| + ASSERT_TRUE(report); | 
| + EXPECT_EQ(ExpectStapleReport::StapleError::OK, report->staple_error()); | 
| + EXPECT_EQ(verify_time_, report->verify_time()); | 
| + const auto& stapled_responses = report->stapled_responses(); | 
| + ASSERT_EQ(2u, stapled_responses.size()); | 
| + for (const auto& staple : stapled_responses) { | 
| + EXPECT_TRUE(staple.is_date_valid); | 
| + EXPECT_TRUE(staple.is_correct_certificate); | 
| + } | 
| + EXPECT_EQ(OCSPCertStatus::Status::GOOD, stapled_responses[0].status); | 
| + EXPECT_EQ(OCSPCertStatus::Status::UNKNOWN, stapled_responses[1].status); | 
| +}; | 
| + | 
| +TEST_F(ExpectStapleReportTest, RevokeResponse) { | 
| + OCSPTest ocsp; | 
| + ASSERT_TRUE(LoadOCSPFromFile("revoke_response.pem", &ocsp)); | 
| + auto report = MakeReport(ocsp); | 
| + ASSERT_TRUE(report); | 
| + EXPECT_EQ(ExpectStapleReport::StapleError::NO_MATCHING_RESPONSE, | 
| + report->staple_error()); | 
| + EXPECT_EQ(verify_time_, report->verify_time()); | 
| + const auto& stapled_responses = report->stapled_responses(); | 
| + ASSERT_EQ(1u, stapled_responses.size()); | 
| + EXPECT_TRUE(stapled_responses[0].is_date_valid); | 
| + EXPECT_TRUE(stapled_responses[0].is_correct_certificate); | 
| + EXPECT_EQ(OCSPCertStatus::Status::REVOKED, stapled_responses[0].status); | 
| +}; | 
| + | 
| +} // namespace |