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 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_clock.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" |
| 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | class FakeQuartcEndpointDelegate : public QuartcEndpoint::Delegate { |
| 20 | public: |
| 21 | explicit FakeQuartcEndpointDelegate(QuartcSession::Delegate* session_delegate) |
| 22 | : session_delegate_(session_delegate) {} |
| 23 | |
| 24 | void OnSessionCreated(QuartcSession* session) override { |
| 25 | CHECK_EQ(session_, nullptr); |
| 26 | CHECK_NE(session, nullptr); |
| 27 | session_ = session; |
| 28 | session_->SetDelegate(session_delegate_); |
| 29 | session_->StartCryptoHandshake(); |
| 30 | } |
| 31 | |
| 32 | void OnConnectError(QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 33 | const std::string& error_details) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 34 | LOG(FATAL) << "Unexpected error during QuartcEndpoint::Connect(); error=" |
| 35 | << error << ", error_details=" << error_details; |
| 36 | } |
| 37 | |
| 38 | QuartcSession* session() { return session_; } |
| 39 | |
| 40 | private: |
| 41 | QuartcSession::Delegate* session_delegate_; |
| 42 | QuartcSession* session_ = nullptr; |
| 43 | }; |
| 44 | |
| 45 | class FakeQuartcSessionDelegate : public QuartcSession::Delegate { |
| 46 | public: |
| 47 | explicit FakeQuartcSessionDelegate(QuartcStream::Delegate* stream_delegate, |
| 48 | const QuicClock* clock) |
| 49 | : stream_delegate_(stream_delegate), clock_(clock) {} |
| 50 | |
| 51 | void OnConnectionWritable() override { |
| 52 | LOG(INFO) << "Connection writable!"; |
| 53 | if (!writable_time_.IsInitialized()) { |
| 54 | writable_time_ = clock_->Now(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Called when peers have established forward-secure encryption |
| 59 | void OnCryptoHandshakeComplete() override { |
| 60 | LOG(INFO) << "Crypto handshake complete!"; |
| 61 | crypto_handshake_time_ = clock_->Now(); |
| 62 | } |
| 63 | |
| 64 | // Called when connection closes locally, or remotely by peer. |
| 65 | void OnConnectionClosed(QuicErrorCode error_code, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 66 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 67 | ConnectionCloseSource source) override { |
| 68 | connected_ = false; |
| 69 | } |
| 70 | |
| 71 | // Called when an incoming QUIC stream is created. |
| 72 | void OnIncomingStream(QuartcStream* quartc_stream) override { |
| 73 | last_incoming_stream_ = quartc_stream; |
| 74 | last_incoming_stream_->SetDelegate(stream_delegate_); |
| 75 | } |
| 76 | |
| 77 | void OnMessageReceived(QuicStringPiece message) override { |
| 78 | incoming_messages_.emplace_back(message); |
| 79 | } |
| 80 | |
| 81 | void OnCongestionControlChange(QuicBandwidth bandwidth_estimate, |
| 82 | QuicBandwidth pacing_rate, |
| 83 | QuicTime::Delta latest_rtt) override {} |
| 84 | |
| 85 | QuartcStream* last_incoming_stream() { return last_incoming_stream_; } |
| 86 | |
| 87 | // Returns all received messages. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 88 | const std::vector<std::string>& incoming_messages() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | return incoming_messages_; |
| 90 | } |
| 91 | |
| 92 | bool connected() { return connected_; } |
| 93 | QuicTime writable_time() const { return writable_time_; } |
| 94 | QuicTime crypto_handshake_time() const { return crypto_handshake_time_; } |
| 95 | |
| 96 | private: |
| 97 | QuartcStream* last_incoming_stream_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 98 | std::vector<std::string> incoming_messages_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | bool connected_ = true; |
| 100 | QuartcStream::Delegate* stream_delegate_; |
| 101 | QuicTime writable_time_ = QuicTime::Zero(); |
| 102 | QuicTime crypto_handshake_time_ = QuicTime::Zero(); |
| 103 | const QuicClock* clock_; |
| 104 | }; |
| 105 | |
| 106 | class FakeQuartcStreamDelegate : public QuartcStream::Delegate { |
| 107 | public: |
| 108 | size_t OnReceived(QuartcStream* stream, |
| 109 | iovec* iov, |
| 110 | size_t iov_length, |
| 111 | bool fin) override { |
| 112 | size_t bytes_consumed = 0; |
| 113 | for (size_t i = 0; i < iov_length; ++i) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 114 | received_data_[stream->id()] += std::string( |
| 115 | static_cast<const char*>(iov[i].iov_base), iov[i].iov_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 116 | bytes_consumed += iov[i].iov_len; |
| 117 | } |
| 118 | return bytes_consumed; |
| 119 | } |
| 120 | |
| 121 | void OnClose(QuartcStream* stream) override { |
| 122 | errors_[stream->id()] = stream->stream_error(); |
| 123 | } |
| 124 | |
| 125 | void OnBufferChanged(QuartcStream* stream) override {} |
| 126 | |
| 127 | bool has_data() { return !received_data_.empty(); } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 128 | std::map<QuicStreamId, std::string> data() { return received_data_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 129 | |
| 130 | QuicRstStreamErrorCode stream_error(QuicStreamId id) { return errors_[id]; } |
| 131 | |
| 132 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 133 | std::map<QuicStreamId, std::string> received_data_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 134 | std::map<QuicStreamId, QuicRstStreamErrorCode> errors_; |
| 135 | }; |
| 136 | |
| 137 | } // namespace quic |
| 138 | |
| 139 | #endif // QUICHE_QUIC_QUARTC_QUARTC_FAKES_H_ |