blob: 960544a821ed0af778ab09b545b135198b2d2bec [file] [log] [blame]
Bence Békybac04052022-04-07 15:44:29 -04001// 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"
QUICHE team12c0be12022-09-12 12:10:55 -07009#include "quiche/quic/core/quic_connection_id.h"
Bence Békybac04052022-04-07 15:44:29 -040010
11namespace quic {
12namespace test {
13
14// FakeProofSourceHandle allows its behavior to be scripted for testing.
15class 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 // |dealyed_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 dealyed_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,
QUICHE team12c0be12022-09-12 12:10:55 -070047 const QuicConnectionId& original_connection_id,
Bence Béky8247a392022-04-12 10:24:20 -040048 absl::string_view ssl_capabilities, const std::string& hostname,
49 absl::string_view client_hello, const std::string& alpn,
vasilvv243b2622023-11-07 17:01:30 -080050 std::optional<std::string> alps,
Bence Békybac04052022-04-07 15:44:29 -040051 const std::vector<uint8_t>& quic_transport_params,
vasilvv243b2622023-11-07 17:01:30 -080052 const std::optional<std::vector<uint8_t>>& early_data_context,
Bence Békybac04052022-04-07 15:44:29 -040053 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 {
69 SelectCertArgs(QuicSocketAddress server_address,
70 QuicSocketAddress client_address,
QUICHE team12c0be12022-09-12 12:10:55 -070071 QuicConnectionId original_connection_id,
Bence Béky8247a392022-04-12 10:24:20 -040072 absl::string_view ssl_capabilities, std::string hostname,
73 absl::string_view client_hello, std::string alpn,
vasilvv243b2622023-11-07 17:01:30 -080074 std::optional<std::string> alps,
Bence Békybac04052022-04-07 15:44:29 -040075 std::vector<uint8_t> quic_transport_params,
vasilvv243b2622023-11-07 17:01:30 -080076 std::optional<std::vector<uint8_t>> early_data_context,
Bence Békybac04052022-04-07 15:44:29 -040077 QuicSSLConfig ssl_config)
78 : server_address(server_address),
79 client_address(client_address),
QUICHE team12c0be12022-09-12 12:10:55 -070080 original_connection_id(original_connection_id),
Bence Békybac04052022-04-07 15:44:29 -040081 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;
QUICHE team12c0be12022-09-12 12:10:55 -070092 QuicConnectionId original_connection_id;
Bence Békybac04052022-04-07 15:44:29 -040093 std::string ssl_capabilities;
94 std::string hostname;
95 std::string client_hello;
96 std::string alpn;
vasilvv243b2622023-11-07 17:01:30 -080097 std::optional<std::string> alps;
Bence Békybac04052022-04-07 15:44:29 -040098 std::vector<uint8_t> quic_transport_params;
vasilvv243b2622023-11-07 17:01:30 -080099 std::optional<std::vector<uint8_t>> early_data_context;
Bence Békybac04052022-04-07 15:44:29 -0400100 QuicSSLConfig ssl_config;
101 };
102
103 struct ComputeSignatureArgs {
104 ComputeSignatureArgs(QuicSocketAddress server_address,
Bence Béky8247a392022-04-12 10:24:20 -0400105 QuicSocketAddress client_address, std::string hostname,
106 uint16_t signature_algorithm, absl::string_view in,
Bence Békybac04052022-04-07 15:44:29 -0400107 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
123 std::vector<SelectCertArgs> all_select_cert_args() const {
124 return all_select_cert_args_;
125 }
126
127 std::vector<ComputeSignatureArgs> all_compute_signature_args() const {
128 return all_compute_signature_args_;
129 }
130
131 private:
132 class PendingOperation {
133 public:
Bence Béky8247a392022-04-12 10:24:20 -0400134 PendingOperation(ProofSource* delegate, ProofSourceHandleCallback* callback,
Bence Békybac04052022-04-07 15:44:29 -0400135 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 dealyed_ssl_config);
152
153 ~SelectCertOperation() override = default;
154
155 void Run() override;
156
157 private:
158 const SelectCertArgs args_;
159 const QuicDelayedSSLConfig dealyed_ssl_config_;
160 };
161
162 class ComputeSignatureOperation : public PendingOperation {
163 public:
164 ComputeSignatureOperation(ProofSource* delegate,
165 ProofSourceHandleCallback* callback,
Bence Béky8247a392022-04-12 10:24:20 -0400166 Action action, ComputeSignatureArgs args);
Bence Békybac04052022-04-07 15:44:29 -0400167
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 dealyed_ssl_config_;
vasilvv243b2622023-11-07 17:01:30 -0800187 std::optional<SelectCertOperation> select_cert_op_;
188 std::optional<ComputeSignatureOperation> compute_signature_op_;
Bence Békybac04052022-04-07 15:44:29 -0400189
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_