QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
vasilvv | 5f225b0 | 2020-10-08 11:49:09 -0400 | [diff] [blame] | 12 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 13 | #include "quic/core/crypto/proof_source.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | |
| 15 | namespace quic { |
| 16 | namespace test { |
| 17 | |
nharper | fbaacc0 | 2020-04-24 17:30:22 -0700 | [diff] [blame] | 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 |
nharper | 1f8289a | 2020-04-27 11:57:28 -0700 | [diff] [blame] | 24 | // FakeProofSource::GetTicketCrypter. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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, |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 38 | const QuicSocketAddress& client_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 39 | const std::string& hostname, |
| 40 | const std::string& server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | QuicTransportVersion transport_version, |
vasilvv | 5f225b0 | 2020-10-08 11:49:09 -0400 | [diff] [blame] | 42 | absl::string_view chlo_hash, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | std::unique_ptr<ProofSource::Callback> callback) override; |
| 44 | QuicReferenceCountedPointer<Chain> GetCertChain( |
| 45 | const QuicSocketAddress& server_address, |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 46 | const QuicSocketAddress& client_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 47 | const std::string& hostname) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 48 | void ComputeTlsSignature( |
| 49 | const QuicSocketAddress& server_address, |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 50 | const QuicSocketAddress& client_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 51 | const std::string& hostname, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 52 | uint16_t signature_algorithm, |
vasilvv | 5f225b0 | 2020-10-08 11:49:09 -0400 | [diff] [blame] | 53 | absl::string_view in, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | std::unique_ptr<ProofSource::SignatureCallback> callback) override; |
nharper | 1f8289a | 2020-04-27 11:57:28 -0700 | [diff] [blame] | 55 | TicketCrypter* GetTicketCrypter() override; |
nharper | 037c21b | 2020-04-23 14:41:35 -0700 | [diff] [blame] | 56 | |
nharper | fbaacc0 | 2020-04-24 17:30:22 -0700 | [diff] [blame] | 57 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 61 | // 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_; |
nharper | fbaacc0 | 2020-04-24 17:30:22 -0700 | [diff] [blame] | 70 | std::unique_ptr<TicketCrypter> ticket_crypter_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | 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, |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 82 | const QuicSocketAddress& client_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 83 | std::string hostname, |
| 84 | std::string server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 85 | QuicTransportVersion transport_version, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 86 | std::string chlo_hash, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | std::unique_ptr<ProofSource::Callback> callback, |
| 88 | ProofSource* delegate); |
| 89 | ~GetProofOp() override; |
| 90 | |
| 91 | void Run() override; |
| 92 | |
| 93 | private: |
| 94 | QuicSocketAddress server_address_; |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 95 | QuicSocketAddress client_address_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 96 | std::string hostname_; |
| 97 | std::string server_config_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 98 | QuicTransportVersion transport_version_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 99 | std::string chlo_hash_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 100 | std::unique_ptr<ProofSource::Callback> callback_; |
| 101 | ProofSource* delegate_; |
| 102 | }; |
| 103 | |
| 104 | class ComputeSignatureOp : public PendingOp { |
| 105 | public: |
| 106 | ComputeSignatureOp(const QuicSocketAddress& server_address, |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 107 | const QuicSocketAddress& client_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 108 | std::string hostname, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 109 | uint16_t sig_alg, |
vasilvv | 5f225b0 | 2020-10-08 11:49:09 -0400 | [diff] [blame] | 110 | absl::string_view in, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | std::unique_ptr<ProofSource::SignatureCallback> callback, |
| 112 | ProofSource* delegate); |
| 113 | ~ComputeSignatureOp() override; |
| 114 | |
| 115 | void Run() override; |
| 116 | |
| 117 | private: |
| 118 | QuicSocketAddress server_address_; |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 119 | QuicSocketAddress client_address_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 120 | std::string hostname_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | uint16_t sig_alg_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 122 | std::string in_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 123 | 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_ |