blob: 8f1cd3768330deb873211dde68330e7c393e0184 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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#include "net/third_party/quiche/src/quic/test_tools/fake_proof_source.h"
6
7#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
8#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
9#include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
10
11namespace quic {
12namespace test {
13
14FakeProofSource::FakeProofSource()
15 : delegate_(crypto_test_utils::ProofSourceForTesting()) {}
16
17FakeProofSource::~FakeProofSource() {}
18
19FakeProofSource::PendingOp::~PendingOp() = default;
20
21FakeProofSource::GetProofOp::GetProofOp(
22 const QuicSocketAddress& server_addr,
vasilvvc48c8712019-03-11 13:38:16 -070023 std::string hostname,
24 std::string server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050025 QuicTransportVersion transport_version,
vasilvvc48c8712019-03-11 13:38:16 -070026 std::string chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -050027 std::unique_ptr<ProofSource::Callback> callback,
28 ProofSource* delegate)
29 : server_address_(server_addr),
30 hostname_(std::move(hostname)),
31 server_config_(std::move(server_config)),
32 transport_version_(transport_version),
33 chlo_hash_(std::move(chlo_hash)),
34 callback_(std::move(callback)),
35 delegate_(delegate) {}
36
37FakeProofSource::GetProofOp::~GetProofOp() = default;
38
39void FakeProofSource::GetProofOp::Run() {
40 // Note: relies on the callback being invoked synchronously
41 delegate_->GetProof(server_address_, hostname_, server_config_,
42 transport_version_, chlo_hash_, std::move(callback_));
43}
44
45FakeProofSource::ComputeSignatureOp::ComputeSignatureOp(
46 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070047 std::string hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -050048 uint16_t sig_alg,
49 QuicStringPiece in,
50 std::unique_ptr<ProofSource::SignatureCallback> callback,
51 ProofSource* delegate)
52 : server_address_(server_address),
53 hostname_(std::move(hostname)),
54 sig_alg_(sig_alg),
55 in_(in),
56 callback_(std::move(callback)),
57 delegate_(delegate) {}
58
59FakeProofSource::ComputeSignatureOp::~ComputeSignatureOp() = default;
60
61void FakeProofSource::ComputeSignatureOp::Run() {
62 delegate_->ComputeTlsSignature(server_address_, hostname_, sig_alg_, in_,
63 std::move(callback_));
64}
65
66void FakeProofSource::Activate() {
67 active_ = true;
68}
69
70void FakeProofSource::GetProof(
71 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070072 const std::string& hostname,
73 const std::string& server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050074 QuicTransportVersion transport_version,
75 QuicStringPiece chlo_hash,
76 std::unique_ptr<ProofSource::Callback> callback) {
77 if (!active_) {
78 delegate_->GetProof(server_address, hostname, server_config,
79 transport_version, chlo_hash, std::move(callback));
80 return;
81 }
82
83 pending_ops_.push_back(QuicMakeUnique<GetProofOp>(
84 server_address, hostname, server_config, transport_version,
vasilvvc48c8712019-03-11 13:38:16 -070085 std::string(chlo_hash), std::move(callback), delegate_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -050086}
87
88QuicReferenceCountedPointer<ProofSource::Chain> FakeProofSource::GetCertChain(
89 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070090 const std::string& hostname) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050091 return delegate_->GetCertChain(server_address, hostname);
92}
93
94void FakeProofSource::ComputeTlsSignature(
95 const QuicSocketAddress& server_address,
vasilvvc48c8712019-03-11 13:38:16 -070096 const std::string& hostname,
QUICHE teama6ef0a62019-03-07 20:34:33 -050097 uint16_t signature_algorithm,
98 QuicStringPiece in,
99 std::unique_ptr<ProofSource::SignatureCallback> callback) {
100 QUIC_LOG(INFO) << "FakeProofSource::ComputeTlsSignature";
101 if (!active_) {
102 QUIC_LOG(INFO) << "Not active - directly calling delegate";
103 delegate_->ComputeTlsSignature(
104 server_address, hostname, signature_algorithm, in, std::move(callback));
105 return;
106 }
107
108 QUIC_LOG(INFO) << "Adding pending op";
109 pending_ops_.push_back(QuicMakeUnique<ComputeSignatureOp>(
110 server_address, hostname, signature_algorithm, in, std::move(callback),
111 delegate_.get()));
112}
113
114int FakeProofSource::NumPendingCallbacks() const {
115 return pending_ops_.size();
116}
117
118void FakeProofSource::InvokePendingCallback(int n) {
119 CHECK(NumPendingCallbacks() > n);
120
121 pending_ops_[n]->Run();
122
123 auto it = pending_ops_.begin() + n;
124 pending_ops_.erase(it);
125}
126
127} // namespace test
128} // namespace quic