• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FACTORY_H_
6 #define OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_FACTORY_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "gmock/gmock.h"
12 #include "osp/impl/quic/quic_connection_factory.h"
13 #include "osp/impl/quic/testing/fake_quic_connection.h"
14 #include "osp/public/message_demuxer.h"
15 
16 namespace openscreen {
17 namespace osp {
18 
19 class FakeQuicConnectionFactoryBridge {
20  public:
21   explicit FakeQuicConnectionFactoryBridge(
22       const IPEndpoint& controller_endpoint);
23 
server_idle()24   bool server_idle() const { return server_idle_; }
client_idle()25   bool client_idle() const { return client_idle_; }
26 
27   void OnConnectionClosed(QuicConnection* connection);
28   void OnOutgoingStream(QuicConnection* connection, QuicStream* stream);
29 
30   void SetServerDelegate(QuicConnectionFactory::ServerDelegate* delegate,
31                          const IPEndpoint& endpoint);
32   void RunTasks(bool is_client);
33   std::unique_ptr<QuicConnection> Connect(
34       const IPEndpoint& endpoint,
35       QuicConnection::Delegate* connection_delegate);
36 
37  private:
38   struct ConnectionPair {
39     FakeQuicConnection* controller;
40     FakeQuicConnection* receiver;
41   };
42 
43   const IPEndpoint controller_endpoint_;
44   IPEndpoint receiver_endpoint_;
45   bool client_idle_ = true;
46   bool server_idle_ = true;
47   uint64_t next_connection_id_ = 0;
48   bool connections_pending_ = true;
49   ConnectionPair connections_ = {};
50   QuicConnectionFactory::ServerDelegate* delegate_ = nullptr;
51 };
52 
53 class FakeClientQuicConnectionFactory final : public QuicConnectionFactory {
54  public:
55   explicit FakeClientQuicConnectionFactory(
56       FakeQuicConnectionFactoryBridge* bridge);
57   ~FakeClientQuicConnectionFactory() override;
58 
59   // UdpSocket::Client overrides.
60   void OnError(UdpSocket* socket, Error error) override;
61   void OnSendError(UdpSocket* socket, Error error) override;
62   void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) override;
63 
64   // QuicConnectionFactory overrides.
65   void SetServerDelegate(ServerDelegate* delegate,
66                          const std::vector<IPEndpoint>& endpoints) override;
67   std::unique_ptr<QuicConnection> Connect(
68       const IPEndpoint& endpoint,
69       QuicConnection::Delegate* connection_delegate) override;
70 
idle()71   bool idle() const { return idle_; }
72 
73   std::unique_ptr<UdpSocket> socket_;
74 
75  private:
76   FakeQuicConnectionFactoryBridge* bridge_;
77   bool idle_ = true;
78 };
79 
80 class FakeServerQuicConnectionFactory final : public QuicConnectionFactory {
81  public:
82   explicit FakeServerQuicConnectionFactory(
83       FakeQuicConnectionFactoryBridge* bridge);
84   ~FakeServerQuicConnectionFactory() override;
85 
86   // UdpSocket::Client overrides.
87   void OnError(UdpSocket* socket, Error error) override;
88   void OnSendError(UdpSocket* socket, Error error) override;
89   void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) override;
90 
91   // QuicConnectionFactory overrides.
92   void SetServerDelegate(ServerDelegate* delegate,
93                          const std::vector<IPEndpoint>& endpoints) override;
94   std::unique_ptr<QuicConnection> Connect(
95       const IPEndpoint& endpoint,
96       QuicConnection::Delegate* connection_delegate) override;
97 
idle()98   bool idle() const { return idle_; }
99 
100  private:
101   FakeQuicConnectionFactoryBridge* bridge_;
102   bool idle_ = true;
103 };
104 
105 }  // namespace osp
106 }  // namespace openscreen
107 
108 #endif  // OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_FACTORY_H_
109