Land Recent QUIC Changes.
[chromium-blink-merge.git] / net / quic / test_tools / crypto_test_utils_chromium.cc
blob50d93e64b6f193450efd351704330dc0831be0d3
1 // Copyright 2013 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.
5 #include "net/quic/test_tools/crypto_test_utils.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/cert/test_root_certs.h"
12 #include "net/cert/x509_certificate.h"
13 #include "net/http/transport_security_state.h"
14 #include "net/quic/crypto/proof_source_chromium.h"
15 #include "net/quic/crypto/proof_verifier_chromium.h"
16 #include "net/test/cert_test_util.h"
18 namespace net {
20 namespace test {
22 namespace {
24 class TestProofVerifierChromium : public ProofVerifierChromium {
25 public:
26 TestProofVerifierChromium(CertVerifier* cert_verifier,
27 TransportSecurityState* transport_security_state,
28 const std::string& cert_file)
29 : ProofVerifierChromium(cert_verifier, transport_security_state),
30 cert_verifier_(cert_verifier),
31 transport_security_state_(transport_security_state) {
32 // Load and install the root for the validated chain.
33 scoped_refptr<X509Certificate> root_cert =
34 ImportCertFromFile(GetTestCertsDirectory(), cert_file);
35 scoped_root_.Reset(root_cert.get());
37 ~TestProofVerifierChromium() override {}
39 private:
40 ScopedTestRoot scoped_root_;
41 scoped_ptr<CertVerifier> cert_verifier_;
42 scoped_ptr<TransportSecurityState> transport_security_state_;
45 const char kLeafCert[] = "leaf";
46 const char kIntermediateCert[] = "intermediate";
47 const char kSignature[] = "signature";
49 class FakeProofSource : public ProofSource {
50 public:
51 FakeProofSource() : certs_(2) {
52 certs_[0] = kLeafCert;
53 certs_[1] = kIntermediateCert;
55 ~FakeProofSource() override {}
57 // ProofSource interface
58 bool GetProof(const IPEndPoint& server_ip,
59 const std::string& hostname,
60 const std::string& server_config,
61 bool ecdsa_ok,
62 const std::vector<std::string>** out_certs,
63 std::string* out_signature) override {
64 *out_certs = &certs_;
65 *out_signature = kSignature;
66 return true;
69 private:
70 std::vector<std::string> certs_;
71 DISALLOW_COPY_AND_ASSIGN(FakeProofSource);
74 class FakeProofVerifier : public ProofVerifier {
75 public:
76 FakeProofVerifier() {}
77 ~FakeProofVerifier() override {}
79 // ProofVerifier interface
80 QuicAsyncStatus VerifyProof(const std::string& hostname,
81 const std::string& server_config,
82 const std::vector<std::string>& certs,
83 const std::string& signature,
84 const ProofVerifyContext* verify_context,
85 std::string* error_details,
86 scoped_ptr<ProofVerifyDetails>* verify_details,
87 ProofVerifierCallback* callback) override {
88 error_details->clear();
89 scoped_ptr<ProofVerifyDetailsChromium> verify_details_chromium(
90 new ProofVerifyDetailsChromium);
91 if (certs.size() != 2 || certs[0] != kLeafCert ||
92 certs[1] != kIntermediateCert || signature != kSignature) {
93 *error_details = "Invalid proof";
94 verify_details_chromium->cert_verify_result.cert_status =
95 CERT_STATUS_INVALID;
96 *verify_details = verify_details_chromium.Pass();
97 return QUIC_FAILURE;
99 *verify_details = verify_details_chromium.Pass();
100 return QUIC_SUCCESS;
103 private:
104 DISALLOW_COPY_AND_ASSIGN(FakeProofVerifier);
107 } // namespace
109 // static
110 ProofSource* CryptoTestUtils::ProofSourceForTesting() {
111 return new ProofSourceChromium();
114 // static
115 ProofVerifier* CryptoTestUtils::ProofVerifierForTesting() {
116 TestProofVerifierChromium* proof_verifier =
117 new TestProofVerifierChromium(CertVerifier::CreateDefault(),
118 new TransportSecurityState,
119 "quic_root.crt");
120 return proof_verifier;
123 // static
124 ProofVerifyContext* CryptoTestUtils::ProofVerifyContextForTesting() {
125 return new ProofVerifyContextChromium(BoundNetLog());
128 // static
129 ProofSource* CryptoTestUtils::FakeProofSourceForTesting() {
130 return new FakeProofSource();
133 // static
134 ProofVerifier* CryptoTestUtils::FakeProofVerifierForTesting() {
135 return new FakeProofVerifier();
138 // static
139 ProofVerifyContext* CryptoTestUtils::FakeProofVerifyContextForTesting() {
140 return nullptr;
143 } // namespace test
145 } // namespace net