blob: 077f34d074fa12b75e045b9471fa9131a0a8bef3 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2016 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#ifndef QUICHE_QUIC_TEST_TOOLS_FAKE_PROOF_SOURCE_H_
6#define QUICHE_QUIC_TEST_TOOLS_FAKE_PROOF_SOURCE_H_
7
8#include <memory>
9#include <string>
10#include <vector>
11
vasilvv5f225b02020-10-08 11:49:09 -040012#include "absl/strings/string_view.h"
QUICHE team5be974e2020-12-29 18:35:24 -050013#include "quic/core/crypto/proof_source.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050014
15namespace quic {
16namespace test {
17
nharperfbaacc02020-04-24 17:30:22 -070018// Implementation of ProofSource which delegates to a ProofSourceForTesting, but
19// allows for overriding certain functionality. FakeProofSource allows
20// intercepting calls to GetProof and ComputeTlsSignature to force them to run
21// asynchronously, and allow the caller to see that the call is pending and
22// resume the operation at the caller's choosing. FakeProofSource also allows
23// the caller to replace the TicketCrypter provided by
nharper1f8289a2020-04-27 11:57:28 -070024// FakeProofSource::GetTicketCrypter.
QUICHE teama6ef0a62019-03-07 20:34:33 -050025class FakeProofSource : public ProofSource {
26 public:
27 FakeProofSource();
28 ~FakeProofSource() override;
29
30 // Before this object is "active", all calls to GetProof will be delegated
31 // immediately. Once "active", the async ones will be intercepted. This
32 // distinction is necessary to ensure that GetProof can be called without
33 // interference during test case setup.
34 void Activate();
35
36 // ProofSource interface
37 void GetProof(const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -070038 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -070039 const std::string& hostname,
40 const std::string& server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050041 QuicTransportVersion transport_version,
vasilvv5f225b02020-10-08 11:49:09 -040042 absl::string_view chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -050043 std::unique_ptr<ProofSource::Callback> callback) override;
44 QuicReferenceCountedPointer<Chain> GetCertChain(
45 const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -070046 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -070047 const std::string& hostname) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050048 void ComputeTlsSignature(
49 const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -070050 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -070051 const std::string& hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -050052 uint16_t signature_algorithm,
vasilvv5f225b02020-10-08 11:49:09 -040053 absl::string_view in,
QUICHE teama6ef0a62019-03-07 20:34:33 -050054 std::unique_ptr<ProofSource::SignatureCallback> callback) override;
nharper1f8289a2020-04-27 11:57:28 -070055 TicketCrypter* GetTicketCrypter() override;
nharper037c21b2020-04-23 14:41:35 -070056
nharperfbaacc02020-04-24 17:30:22 -070057 // Sets the TicketCrypter to use. If nullptr, the TicketCrypter from
58 // ProofSourceForTesting will be returned instead.
59 void SetTicketCrypter(std::unique_ptr<TicketCrypter> ticket_crypter);
60
QUICHE teama6ef0a62019-03-07 20:34:33 -050061 // Get the number of callbacks which are pending
62 int NumPendingCallbacks() const;
63
64 // Invoke a pending callback. The index refers to the position in
65 // pending_ops_ of the callback to be completed.
66 void InvokePendingCallback(int n);
67
68 private:
69 std::unique_ptr<ProofSource> delegate_;
nharperfbaacc02020-04-24 17:30:22 -070070 std::unique_ptr<TicketCrypter> ticket_crypter_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050071 bool active_ = false;
72
73 class PendingOp {
74 public:
75 virtual ~PendingOp();
76 virtual void Run() = 0;
77 };
78
79 class GetProofOp : public PendingOp {
80 public:
81 GetProofOp(const QuicSocketAddress& server_addr,
danzhd1fc5912020-05-01 15:29:04 -070082 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -070083 std::string hostname,
84 std::string server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050085 QuicTransportVersion transport_version,
vasilvvc48c8712019-03-11 13:38:16 -070086 std::string chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -050087 std::unique_ptr<ProofSource::Callback> callback,
88 ProofSource* delegate);
89 ~GetProofOp() override;
90
91 void Run() override;
92
93 private:
94 QuicSocketAddress server_address_;
danzhd1fc5912020-05-01 15:29:04 -070095 QuicSocketAddress client_address_;
vasilvvc48c8712019-03-11 13:38:16 -070096 std::string hostname_;
97 std::string server_config_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050098 QuicTransportVersion transport_version_;
vasilvvc48c8712019-03-11 13:38:16 -070099 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500100 std::unique_ptr<ProofSource::Callback> callback_;
101 ProofSource* delegate_;
102 };
103
104 class ComputeSignatureOp : public PendingOp {
105 public:
106 ComputeSignatureOp(const QuicSocketAddress& server_address,
danzhd1fc5912020-05-01 15:29:04 -0700107 const QuicSocketAddress& client_address,
vasilvvc48c8712019-03-11 13:38:16 -0700108 std::string hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500109 uint16_t sig_alg,
vasilvv5f225b02020-10-08 11:49:09 -0400110 absl::string_view in,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500111 std::unique_ptr<ProofSource::SignatureCallback> callback,
112 ProofSource* delegate);
113 ~ComputeSignatureOp() override;
114
115 void Run() override;
116
117 private:
118 QuicSocketAddress server_address_;
danzhd1fc5912020-05-01 15:29:04 -0700119 QuicSocketAddress client_address_;
vasilvvc48c8712019-03-11 13:38:16 -0700120 std::string hostname_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500121 uint16_t sig_alg_;
vasilvvc48c8712019-03-11 13:38:16 -0700122 std::string in_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500123 std::unique_ptr<ProofSource::SignatureCallback> callback_;
124 ProofSource* delegate_;
125 };
126
127 std::vector<std::unique_ptr<PendingOp>> pending_ops_;
128};
129
130} // namespace test
131} // namespace quic
132
133#endif // QUICHE_QUIC_TEST_TOOLS_FAKE_PROOF_SOURCE_H_