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