1 // Copyright 2018 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 "osp/impl/quic/testing/fake_quic_connection.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "osp/impl/quic/testing/fake_quic_connection_factory.h"
11 #include "util/osp_logging.h"
12
13 namespace openscreen {
14 namespace osp {
15
FakeQuicStream(Delegate * delegate,uint64_t id)16 FakeQuicStream::FakeQuicStream(Delegate* delegate, uint64_t id)
17 : QuicStream(delegate, id) {}
18
19 FakeQuicStream::~FakeQuicStream() = default;
20
ReceiveData(const uint8_t * data,size_t size)21 void FakeQuicStream::ReceiveData(const uint8_t* data, size_t size) {
22 OSP_DCHECK(!read_end_closed_);
23 read_buffer_.insert(read_buffer_.end(), data, data + size);
24 }
25
CloseReadEnd()26 void FakeQuicStream::CloseReadEnd() {
27 read_end_closed_ = true;
28 }
29
TakeReceivedData()30 std::vector<uint8_t> FakeQuicStream::TakeReceivedData() {
31 return std::move(read_buffer_);
32 }
33
TakeWrittenData()34 std::vector<uint8_t> FakeQuicStream::TakeWrittenData() {
35 return std::move(write_buffer_);
36 }
37
Write(const uint8_t * data,size_t size)38 void FakeQuicStream::Write(const uint8_t* data, size_t size) {
39 OSP_DCHECK(!write_end_closed_);
40 write_buffer_.insert(write_buffer_.end(), data, data + size);
41 }
42
CloseWriteEnd()43 void FakeQuicStream::CloseWriteEnd() {
44 write_end_closed_ = true;
45 }
46
FakeQuicConnection(FakeQuicConnectionFactoryBridge * parent_factory,uint64_t connection_id,Delegate * delegate)47 FakeQuicConnection::FakeQuicConnection(
48 FakeQuicConnectionFactoryBridge* parent_factory,
49 uint64_t connection_id,
50 Delegate* delegate)
51 : QuicConnection(delegate),
52 parent_factory_(parent_factory),
53 connection_id_(connection_id) {}
54
55 FakeQuicConnection::~FakeQuicConnection() = default;
56
MakeIncomingStream()57 std::unique_ptr<FakeQuicStream> FakeQuicConnection::MakeIncomingStream() {
58 uint64_t stream_id = next_stream_id_++;
59 auto result = std::make_unique<FakeQuicStream>(
60 delegate()->NextStreamDelegate(id(), stream_id), stream_id);
61 streams_.emplace(result->id(), result.get());
62 return result;
63 }
64
OnRead(UdpSocket * socket,ErrorOr<UdpPacket> data)65 void FakeQuicConnection::OnRead(UdpSocket* socket, ErrorOr<UdpPacket> data) {
66 OSP_NOTREACHED();
67 }
68
OnSendError(UdpSocket * socket,Error error)69 void FakeQuicConnection::OnSendError(UdpSocket* socket, Error error) {
70 OSP_NOTREACHED();
71 }
72
OnError(UdpSocket * socket,Error error)73 void FakeQuicConnection::OnError(UdpSocket* socket,
74 Error error){OSP_NOTREACHED()}
75
MakeOutgoingStream(QuicStream::Delegate * delegate)76 std::unique_ptr<QuicStream> FakeQuicConnection::MakeOutgoingStream(
77 QuicStream::Delegate* delegate) {
78 auto result = std::make_unique<FakeQuicStream>(delegate, next_stream_id_++);
79 streams_.emplace(result->id(), result.get());
80 parent_factory_->OnOutgoingStream(this, result.get());
81 return result;
82 }
83
Close()84 void FakeQuicConnection::Close() {
85 parent_factory_->OnConnectionClosed(this);
86 delegate()->OnConnectionClosed(connection_id_);
87 for (auto& stream : streams_) {
88 stream.second->delegate()->OnClose(stream.first);
89 stream.second->delegate()->OnReceived(stream.second, nullptr, 0);
90 }
91 }
92
93 } // namespace osp
94 } // namespace openscreen
95