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 #ifndef OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_H_ 6 #define OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_H_ 7 8 #include <map> 9 #include <vector> 10 11 #include "osp/impl/quic/quic_connection.h" 12 13 namespace openscreen { 14 namespace osp { 15 16 class FakeQuicConnectionFactoryBridge; 17 18 class FakeQuicStream final : public QuicStream { 19 public: 20 FakeQuicStream(Delegate* delegate, uint64_t id); 21 ~FakeQuicStream() override; 22 23 void ReceiveData(const uint8_t* data, size_t size); 24 void CloseReadEnd(); 25 26 std::vector<uint8_t> TakeReceivedData(); 27 std::vector<uint8_t> TakeWrittenData(); 28 both_ends_closed()29 bool both_ends_closed() const { 30 return write_end_closed_ && read_end_closed_; 31 } write_end_closed()32 bool write_end_closed() const { return write_end_closed_; } read_end_closed()33 bool read_end_closed() const { return read_end_closed_; } 34 delegate()35 Delegate* delegate() { return delegate_; } 36 37 void Write(const uint8_t* data, size_t size) override; 38 void CloseWriteEnd() override; 39 40 private: 41 bool write_end_closed_ = false; 42 bool read_end_closed_ = false; 43 std::vector<uint8_t> write_buffer_; 44 std::vector<uint8_t> read_buffer_; 45 }; 46 47 class FakeQuicConnection final : public QuicConnection { 48 public: 49 FakeQuicConnection(FakeQuicConnectionFactoryBridge* parent_factory, 50 uint64_t connection_id, 51 Delegate* delegate); 52 ~FakeQuicConnection() override; 53 delegate()54 Delegate* delegate() { return delegate_; } id()55 uint64_t id() const { return connection_id_; } streams()56 std::map<uint64_t, FakeQuicStream*>& streams() { return streams_; } 57 58 std::unique_ptr<FakeQuicStream> MakeIncomingStream(); 59 60 // UdpSocket::Client overrides. 61 void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> data) override; 62 void OnSendError(UdpSocket* socket, Error error) override; 63 void OnError(UdpSocket* socket, Error error) override; 64 65 // QuicConnection overrides. 66 std::unique_ptr<QuicStream> MakeOutgoingStream( 67 QuicStream::Delegate* delegate) override; 68 void Close() override; 69 70 private: 71 FakeQuicConnectionFactoryBridge* const parent_factory_; 72 const uint64_t connection_id_; 73 uint64_t next_stream_id_ = 1; 74 std::map<uint64_t, FakeQuicStream*> streams_; 75 }; 76 77 } // namespace osp 78 } // namespace openscreen 79 80 #endif // OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_H_ 81