blob: b0069af376ac4099f4a3550db45b02a3b086c711 [file] [log] [blame]
Bence Békybac04052022-04-07 15:44:29 -04001// 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
12#include "absl/strings/string_view.h"
13#include "quiche/quic/core/crypto/proof_source.h"
14
15namespace quic {
16namespace test {
17
18// 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
24// FakeProofSource::GetTicketCrypter.
25class 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,
38 const QuicSocketAddress& client_address,
Bence Béky8247a392022-04-12 10:24:20 -040039 const std::string& hostname, const std::string& server_config,
Bence Békybac04052022-04-07 15:44:29 -040040 QuicTransportVersion transport_version,
41 absl::string_view chlo_hash,
42 std::unique_ptr<ProofSource::Callback> callback) override;
43 quiche::QuicheReferenceCountedPointer<Chain> GetCertChain(
44 const QuicSocketAddress& server_address,
45 const QuicSocketAddress& client_address, const std::string& hostname,
46 bool* cert_matched_sni) override;
47 void ComputeTlsSignature(
48 const QuicSocketAddress& server_address,
Bence Béky8247a392022-04-12 10:24:20 -040049 const QuicSocketAddress& client_address, const std::string& hostname,
50 uint16_t signature_algorithm, absl::string_view in,
Bence Békybac04052022-04-07 15:44:29 -040051 std::unique_ptr<ProofSource::SignatureCallback> callback) override;
52 absl::InlinedVector<uint16_t, 8> SupportedTlsSignatureAlgorithms()
53 const override;
54 TicketCrypter* GetTicketCrypter() override;
55
56 // Sets the TicketCrypter to use. If nullptr, the TicketCrypter from
57 // ProofSourceForTesting will be returned instead.
58 void SetTicketCrypter(std::unique_ptr<TicketCrypter> ticket_crypter);
59
60 // Get the number of callbacks which are pending
61 int NumPendingCallbacks() const;
62
63 // Invoke a pending callback. The index refers to the position in
64 // pending_ops_ of the callback to be completed.
65 void InvokePendingCallback(int n);
66
67 private:
68 std::unique_ptr<ProofSource> delegate_;
69 std::unique_ptr<TicketCrypter> ticket_crypter_;
70 bool active_ = false;
71
72 class PendingOp {
73 public:
74 virtual ~PendingOp();
75 virtual void Run() = 0;
76 };
77
78 class GetProofOp : public PendingOp {
79 public:
80 GetProofOp(const QuicSocketAddress& server_addr,
Bence Béky8247a392022-04-12 10:24:20 -040081 const QuicSocketAddress& client_address, std::string hostname,
Bence Békybac04052022-04-07 15:44:29 -040082 std::string server_config,
Bence Béky8247a392022-04-12 10:24:20 -040083 QuicTransportVersion transport_version, std::string chlo_hash,
Bence Békybac04052022-04-07 15:44:29 -040084 std::unique_ptr<ProofSource::Callback> callback,
85 ProofSource* delegate);
86 ~GetProofOp() override;
87
88 void Run() override;
89
90 private:
91 QuicSocketAddress server_address_;
92 QuicSocketAddress client_address_;
93 std::string hostname_;
94 std::string server_config_;
95 QuicTransportVersion transport_version_;
96 std::string chlo_hash_;
97 std::unique_ptr<ProofSource::Callback> callback_;
98 ProofSource* delegate_;
99 };
100
101 class ComputeSignatureOp : public PendingOp {
102 public:
103 ComputeSignatureOp(const QuicSocketAddress& server_address,
104 const QuicSocketAddress& client_address,
Bence Béky8247a392022-04-12 10:24:20 -0400105 std::string hostname, uint16_t sig_alg,
Bence Békybac04052022-04-07 15:44:29 -0400106 absl::string_view in,
107 std::unique_ptr<ProofSource::SignatureCallback> callback,
108 ProofSource* delegate);
109 ~ComputeSignatureOp() override;
110
111 void Run() override;
112
113 private:
114 QuicSocketAddress server_address_;
115 QuicSocketAddress client_address_;
116 std::string hostname_;
117 uint16_t sig_alg_;
118 std::string in_;
119 std::unique_ptr<ProofSource::SignatureCallback> callback_;
120 ProofSource* delegate_;
121 };
122
123 std::vector<std::unique_ptr<PendingOp>> pending_ops_;
124};
125
126} // namespace test
127} // namespace quic
128
129#endif // QUICHE_QUIC_TEST_TOOLS_FAKE_PROOF_SOURCE_H_