blob: 17e3874051c94e9c54a5b037428b725e42805aa1 [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
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
9
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#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 teama6ef0a62019-03-07 20:34:33 -050013#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
17namespace quic {
18
19class 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,
vasilvvc48c8712019-03-11 13:38:16 -070033 const std::string& error_details) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -050034 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
45class 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,
vasilvvc48c8712019-03-11 13:38:16 -070066 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050067 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.
vasilvvc48c8712019-03-11 13:38:16 -070088 const std::vector<std::string>& incoming_messages() {
QUICHE teama6ef0a62019-03-07 20:34:33 -050089 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_;
vasilvvc48c8712019-03-11 13:38:16 -070098 std::vector<std::string> incoming_messages_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050099 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
106class 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) {
vasilvvc48c8712019-03-11 13:38:16 -0700114 received_data_[stream->id()] += std::string(
115 static_cast<const char*>(iov[i].iov_base), iov[i].iov_len);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500116 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(); }
vasilvvc48c8712019-03-11 13:38:16 -0700128 std::map<QuicStreamId, std::string> data() { return received_data_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129
130 QuicRstStreamErrorCode stream_error(QuicStreamId id) { return errors_[id]; }
131
132 private:
vasilvvc48c8712019-03-11 13:38:16 -0700133 std::map<QuicStreamId, std::string> received_data_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134 std::map<QuicStreamId, QuicRstStreamErrorCode> errors_;
135};
136
137} // namespace quic
138
139#endif // QUICHE_QUIC_QUARTC_QUARTC_FAKES_H_