xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/test_tools/fake_proof_source_handle.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2021 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_HANDLE_H_
6 #define QUICHE_QUIC_TEST_TOOLS_FAKE_PROOF_SOURCE_HANDLE_H_
7 
8 #include "quiche/quic/core/crypto/proof_source.h"
9 #include "quiche/quic/core/quic_connection_id.h"
10 
11 namespace quic {
12 namespace test {
13 
14 // FakeProofSourceHandle allows its behavior to be scripted for testing.
15 class FakeProofSourceHandle : public ProofSourceHandle {
16  public:
17   // What would an operation return when it is called.
18   enum class Action {
19     // Delegate the operation to |delegate_| immediately.
20     DELEGATE_SYNC = 0,
21     // Handle the operation asynchronously. Delegate the operation to
22     // |delegate_| when the caller calls CompletePendingOperation().
23     DELEGATE_ASYNC,
24     // Fail the operation immediately.
25     FAIL_SYNC,
26     // Handle the operation asynchronously. Fail the operation when the caller
27     // calls CompletePendingOperation().
28     FAIL_ASYNC,
29     // Similar to FAIL_SYNC, but do not QUICHE_CHECK(!closed_) when invoked.
30     FAIL_SYNC_DO_NOT_CHECK_CLOSED,
31   };
32 
33   // |delegate| must do cert selection and signature synchronously.
34   // |delayed_ssl_config| is the config passed to OnSelectCertificateDone.
35   FakeProofSourceHandle(
36       ProofSource* delegate, ProofSourceHandleCallback* callback,
37       Action select_cert_action, Action compute_signature_action,
38       QuicDelayedSSLConfig delayed_ssl_config = QuicDelayedSSLConfig());
39 
40   ~FakeProofSourceHandle() override = default;
41 
42   void CloseHandle() override;
43 
44   QuicAsyncStatus SelectCertificate(
45       const QuicSocketAddress& server_address,
46       const QuicSocketAddress& client_address,
47       const QuicConnectionId& original_connection_id,
48       absl::string_view ssl_capabilities, const std::string& hostname,
49       absl::string_view client_hello, const std::string& alpn,
50       std::optional<std::string> alps,
51       const std::vector<uint8_t>& quic_transport_params,
52       const std::optional<std::vector<uint8_t>>& early_data_context,
53       const QuicSSLConfig& ssl_config) override;
54 
55   QuicAsyncStatus ComputeSignature(const QuicSocketAddress& server_address,
56                                    const QuicSocketAddress& client_address,
57                                    const std::string& hostname,
58                                    uint16_t signature_algorithm,
59                                    absl::string_view in,
60                                    size_t max_signature_size) override;
61 
62   ProofSourceHandleCallback* callback() override;
63 
64   // Whether there's a pending operation in |this|.
65   bool HasPendingOperation() const;
66   void CompletePendingOperation();
67 
68   struct SelectCertArgs {
SelectCertArgsSelectCertArgs69     SelectCertArgs(QuicSocketAddress server_address,
70                    QuicSocketAddress client_address,
71                    QuicConnectionId original_connection_id,
72                    absl::string_view ssl_capabilities, std::string hostname,
73                    absl::string_view client_hello, std::string alpn,
74                    std::optional<std::string> alps,
75                    std::vector<uint8_t> quic_transport_params,
76                    std::optional<std::vector<uint8_t>> early_data_context,
77                    QuicSSLConfig ssl_config)
78         : server_address(server_address),
79           client_address(client_address),
80           original_connection_id(original_connection_id),
81           ssl_capabilities(ssl_capabilities),
82           hostname(hostname),
83           client_hello(client_hello),
84           alpn(alpn),
85           alps(alps),
86           quic_transport_params(quic_transport_params),
87           early_data_context(early_data_context),
88           ssl_config(ssl_config) {}
89 
90     QuicSocketAddress server_address;
91     QuicSocketAddress client_address;
92     QuicConnectionId original_connection_id;
93     std::string ssl_capabilities;
94     std::string hostname;
95     std::string client_hello;
96     std::string alpn;
97     std::optional<std::string> alps;
98     std::vector<uint8_t> quic_transport_params;
99     std::optional<std::vector<uint8_t>> early_data_context;
100     QuicSSLConfig ssl_config;
101   };
102 
103   struct ComputeSignatureArgs {
ComputeSignatureArgsComputeSignatureArgs104     ComputeSignatureArgs(QuicSocketAddress server_address,
105                          QuicSocketAddress client_address, std::string hostname,
106                          uint16_t signature_algorithm, absl::string_view in,
107                          size_t max_signature_size)
108         : server_address(server_address),
109           client_address(client_address),
110           hostname(hostname),
111           signature_algorithm(signature_algorithm),
112           in(in),
113           max_signature_size(max_signature_size) {}
114 
115     QuicSocketAddress server_address;
116     QuicSocketAddress client_address;
117     std::string hostname;
118     uint16_t signature_algorithm;
119     std::string in;
120     size_t max_signature_size;
121   };
122 
all_select_cert_args()123   std::vector<SelectCertArgs> all_select_cert_args() const {
124     return all_select_cert_args_;
125   }
126 
all_compute_signature_args()127   std::vector<ComputeSignatureArgs> all_compute_signature_args() const {
128     return all_compute_signature_args_;
129   }
130 
131  private:
132   class PendingOperation {
133    public:
PendingOperation(ProofSource * delegate,ProofSourceHandleCallback * callback,Action action)134     PendingOperation(ProofSource* delegate, ProofSourceHandleCallback* callback,
135                      Action action)
136         : delegate_(delegate), callback_(callback), action_(action) {}
137     virtual ~PendingOperation() = default;
138     virtual void Run() = 0;
139 
140    protected:
141     ProofSource* delegate_;
142     ProofSourceHandleCallback* callback_;
143     Action action_;
144   };
145 
146   class SelectCertOperation : public PendingOperation {
147    public:
148     SelectCertOperation(ProofSource* delegate,
149                         ProofSourceHandleCallback* callback, Action action,
150                         SelectCertArgs args,
151                         QuicDelayedSSLConfig delayed_ssl_config);
152 
153     ~SelectCertOperation() override = default;
154 
155     void Run() override;
156 
157    private:
158     const SelectCertArgs args_;
159     const QuicDelayedSSLConfig delayed_ssl_config_;
160   };
161 
162   class ComputeSignatureOperation : public PendingOperation {
163    public:
164     ComputeSignatureOperation(ProofSource* delegate,
165                               ProofSourceHandleCallback* callback,
166                               Action action, ComputeSignatureArgs args);
167 
168     ~ComputeSignatureOperation() override = default;
169 
170     void Run() override;
171 
172    private:
173     const ComputeSignatureArgs args_;
174   };
175 
176  private:
177   int NumPendingOperations() const;
178 
179   bool closed_ = false;
180   ProofSource* delegate_;
181   ProofSourceHandleCallback* callback_;
182   // Action for the next select cert operation.
183   Action select_cert_action_ = Action::DELEGATE_SYNC;
184   // Action for the next compute signature operation.
185   Action compute_signature_action_ = Action::DELEGATE_SYNC;
186   const QuicDelayedSSLConfig delayed_ssl_config_;
187   std::optional<SelectCertOperation> select_cert_op_;
188   std::optional<ComputeSignatureOperation> compute_signature_op_;
189 
190   // Save all the select cert and compute signature args for tests to inspect.
191   std::vector<SelectCertArgs> all_select_cert_args_;
192   std::vector<ComputeSignatureArgs> all_compute_signature_args_;
193 };
194 
195 }  // namespace test
196 }  // namespace quic
197 
198 #endif  // QUICHE_QUIC_TEST_TOOLS_FAKE_PROOF_SOURCE_HANDLE_H_
199