xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/test_tools/fake_proof_source.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // 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 
15 namespace quic {
16 namespace 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.
25 class 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,
39                 const std::string& hostname, const std::string& server_config,
40                 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,
49       const QuicSocketAddress& client_address, const std::string& hostname,
50       uint16_t signature_algorithm, absl::string_view in,
51       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,
81                const QuicSocketAddress& client_address, std::string hostname,
82                std::string server_config,
83                QuicTransportVersion transport_version, std::string chlo_hash,
84                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,
105                        std::string hostname, uint16_t sig_alg,
106                        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_
130