QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 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_QUARTC_QUARTC_FAKES_H_ |
| 6 | #define QUICHE_QUIC_QUARTC_QUARTC_FAKES_H_ |
| 7 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
vasilvv | 89713d0 | 2020-02-11 14:33:26 -0800 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/core/quic_clock.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/quartc/quartc_endpoint.h" |
| 14 | #include "net/third_party/quiche/src/quic/quartc/quartc_session.h" |
| 15 | #include "net/third_party/quiche/src/quic/quartc/quartc_stream.h" |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | |
| 18 | namespace quic { |
| 19 | |
| 20 | class FakeQuartcEndpointDelegate : public QuartcEndpoint::Delegate { |
| 21 | public: |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 22 | explicit FakeQuartcEndpointDelegate(QuartcStream::Delegate* stream_delegate, |
| 23 | const QuicClock* clock) |
| 24 | : stream_delegate_(stream_delegate), clock_(clock) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | |
| 26 | void OnSessionCreated(QuartcSession* session) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | CHECK_NE(session, nullptr); |
| 28 | session_ = session; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | session_->StartCryptoHandshake(); |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 30 | ++num_sessions_created_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | } |
| 32 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | void OnConnectionWritable() override { |
dschinazi | 87c39c1 | 2019-05-07 21:01:12 -0700 | [diff] [blame] | 34 | QUIC_LOG(INFO) << "Connection writable!"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | if (!writable_time_.IsInitialized()) { |
| 36 | writable_time_ = clock_->Now(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Called when peers have established forward-secure encryption |
| 41 | void OnCryptoHandshakeComplete() override { |
dschinazi | 87c39c1 | 2019-05-07 21:01:12 -0700 | [diff] [blame] | 42 | QUIC_LOG(INFO) << "Crypto handshake complete!"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | crypto_handshake_time_ = clock_->Now(); |
| 44 | } |
| 45 | |
| 46 | // Called when connection closes locally, or remotely by peer. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 47 | void OnConnectionClosed(const QuicConnectionCloseFrame& /*frame*/, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 48 | ConnectionCloseSource /*source*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 49 | connected_ = false; |
| 50 | } |
| 51 | |
| 52 | // Called when an incoming QUIC stream is created. |
| 53 | void OnIncomingStream(QuartcStream* quartc_stream) override { |
| 54 | last_incoming_stream_ = quartc_stream; |
| 55 | last_incoming_stream_->SetDelegate(stream_delegate_); |
| 56 | } |
| 57 | |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 58 | void OnMessageReceived(quiche::QuicheStringPiece message) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 59 | incoming_messages_.emplace_back(message); |
| 60 | } |
| 61 | |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 62 | void OnMessageSent(int64_t datagram_id) override { |
| 63 | sent_datagram_ids_.push_back(datagram_id); |
| 64 | } |
| 65 | |
dschinazi | 61eb643 | 2019-06-14 16:27:16 -0700 | [diff] [blame] | 66 | void OnMessageAcked(int64_t datagram_id, |
| 67 | QuicTime receive_timestamp) override { |
QUICHE team | 68d15a8 | 2019-05-31 15:27:25 -0700 | [diff] [blame] | 68 | acked_datagram_id_to_receive_timestamp_.emplace(datagram_id, |
| 69 | receive_timestamp); |
| 70 | } |
| 71 | |
dschinazi | 61eb643 | 2019-06-14 16:27:16 -0700 | [diff] [blame] | 72 | void OnMessageLost(int64_t datagram_id) override { |
QUICHE team | 68d15a8 | 2019-05-31 15:27:25 -0700 | [diff] [blame] | 73 | lost_datagram_ids_.push_back(datagram_id); |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 76 | void OnCongestionControlChange(QuicBandwidth /*bandwidth_estimate*/, |
| 77 | QuicBandwidth /*pacing_rate*/, |
| 78 | QuicTime::Delta /*latest_rtt*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 80 | QuartcSession* session() { return session_; } |
| 81 | |
| 82 | int num_sessions_created() const { return num_sessions_created_; } |
| 83 | |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 84 | QuartcStream* last_incoming_stream() const { return last_incoming_stream_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 85 | |
| 86 | // Returns all received messages. |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 87 | const std::vector<std::string>& incoming_messages() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | return incoming_messages_; |
| 89 | } |
| 90 | |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 91 | // Returns all sent datagram ids in the order sent. |
| 92 | const std::vector<int64_t>& sent_datagram_ids() const { |
| 93 | return sent_datagram_ids_; |
| 94 | } |
| 95 | |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 96 | // Returns all ACKEd datagram ids in the order ACKs were received. |
QUICHE team | 68d15a8 | 2019-05-31 15:27:25 -0700 | [diff] [blame] | 97 | const std::map<int64_t, QuicTime>& acked_datagram_id_to_receive_timestamp() |
| 98 | const { |
| 99 | return acked_datagram_id_to_receive_timestamp_; |
| 100 | } |
| 101 | |
| 102 | const std::vector<int64_t>& lost_datagram_ids() const { |
| 103 | return lost_datagram_ids_; |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 104 | } |
| 105 | |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 106 | bool connected() const { return connected_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 107 | QuicTime writable_time() const { return writable_time_; } |
| 108 | QuicTime crypto_handshake_time() const { return crypto_handshake_time_; } |
| 109 | |
| 110 | private: |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 111 | // Current session. |
| 112 | QuartcSession* session_ = nullptr; |
| 113 | |
| 114 | // Number of new sessions created by the endpoint. |
| 115 | int num_sessions_created_ = 0; |
| 116 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | QuartcStream* last_incoming_stream_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 118 | std::vector<std::string> incoming_messages_; |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 119 | std::vector<int64_t> sent_datagram_ids_; |
QUICHE team | 68d15a8 | 2019-05-31 15:27:25 -0700 | [diff] [blame] | 120 | std::map<int64_t, QuicTime> acked_datagram_id_to_receive_timestamp_; |
| 121 | std::vector<int64_t> lost_datagram_ids_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | bool connected_ = true; |
| 123 | QuartcStream::Delegate* stream_delegate_; |
| 124 | QuicTime writable_time_ = QuicTime::Zero(); |
| 125 | QuicTime crypto_handshake_time_ = QuicTime::Zero(); |
| 126 | const QuicClock* clock_; |
| 127 | }; |
| 128 | |
| 129 | class FakeQuartcStreamDelegate : public QuartcStream::Delegate { |
| 130 | public: |
| 131 | size_t OnReceived(QuartcStream* stream, |
| 132 | iovec* iov, |
| 133 | size_t iov_length, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 134 | bool /*fin*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 135 | size_t bytes_consumed = 0; |
| 136 | for (size_t i = 0; i < iov_length; ++i) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 137 | received_data_[stream->id()] += std::string( |
| 138 | static_cast<const char*>(iov[i].iov_base), iov[i].iov_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 139 | bytes_consumed += iov[i].iov_len; |
| 140 | } |
| 141 | return bytes_consumed; |
| 142 | } |
| 143 | |
| 144 | void OnClose(QuartcStream* stream) override { |
| 145 | errors_[stream->id()] = stream->stream_error(); |
| 146 | } |
| 147 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 148 | void OnBufferChanged(QuartcStream* /*stream*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | |
| 150 | bool has_data() { return !received_data_.empty(); } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 151 | std::map<QuicStreamId, std::string> data() { return received_data_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | |
| 153 | QuicRstStreamErrorCode stream_error(QuicStreamId id) { return errors_[id]; } |
| 154 | |
| 155 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 156 | std::map<QuicStreamId, std::string> received_data_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 157 | std::map<QuicStreamId, QuicRstStreamErrorCode> errors_; |
| 158 | }; |
| 159 | |
| 160 | } // namespace quic |
| 161 | |
| 162 | #endif // QUICHE_QUIC_QUARTC_QUARTC_FAKES_H_ |