aboutsummaryrefslogtreecommitdiff
path: root/osp/impl/quic/testing/fake_quic_connection_factory.h
blob: 128e6372241424f6da5b97bac0c38efe11f8b904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_FACTORY_H_
#define OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_FACTORY_H_

#include <vector>

#include "gmock/gmock.h"
#include "osp/impl/quic/quic_connection_factory.h"
#include "osp/impl/quic/testing/fake_quic_connection.h"
#include "osp/public/message_demuxer.h"

namespace openscreen {
namespace osp {

class FakeQuicConnectionFactoryBridge {
 public:
  FakeQuicConnectionFactoryBridge(const IPEndpoint& controller_endpoint);

  bool server_idle() const { return server_idle_; }
  bool client_idle() const { return client_idle_; }

  void OnConnectionClosed(QuicConnection* connection);
  void OnOutgoingStream(QuicConnection* connection, QuicStream* stream);

  void SetServerDelegate(QuicConnectionFactory::ServerDelegate* delegate,
                         const IPEndpoint& endpoint);
  void RunTasks(bool is_client);
  std::unique_ptr<QuicConnection> Connect(
      const IPEndpoint& endpoint,
      QuicConnection::Delegate* connection_delegate);

 private:
  struct ConnectionPair {
    FakeQuicConnection* controller;
    FakeQuicConnection* receiver;
  };

  const IPEndpoint controller_endpoint_;
  IPEndpoint receiver_endpoint_;
  bool client_idle_ = true;
  bool server_idle_ = true;
  uint64_t next_connection_id_ = 0;
  bool connections_pending_ = true;
  ConnectionPair connections_ = {};
  QuicConnectionFactory::ServerDelegate* delegate_ = nullptr;
};

class FakeClientQuicConnectionFactory final : public QuicConnectionFactory {
 public:
  explicit FakeClientQuicConnectionFactory(
      FakeQuicConnectionFactoryBridge* bridge);
  ~FakeClientQuicConnectionFactory() override;

  // UdpSocket::Client overrides.
  void OnError(platform::UdpSocket* socket, Error error) override;
  void OnSendError(platform::UdpSocket* socket, Error error) override;
  void OnRead(platform::UdpSocket* socket,
              ErrorOr<platform::UdpPacket> packet) override;

  // QuicConnectionFactory overrides.
  void SetServerDelegate(ServerDelegate* delegate,
                         const std::vector<IPEndpoint>& endpoints) override;
  std::unique_ptr<QuicConnection> Connect(
      const IPEndpoint& endpoint,
      QuicConnection::Delegate* connection_delegate) override;

  bool idle() const { return idle_; }

  std::unique_ptr<platform::UdpSocket> socket_;

 private:
  FakeQuicConnectionFactoryBridge* bridge_;
  bool idle_ = true;
};

class FakeServerQuicConnectionFactory final : public QuicConnectionFactory {
 public:
  explicit FakeServerQuicConnectionFactory(
      FakeQuicConnectionFactoryBridge* bridge);
  ~FakeServerQuicConnectionFactory() override;

  // UdpSocket::Client overrides.
  void OnError(platform::UdpSocket* socket, Error error) override;
  void OnSendError(platform::UdpSocket* socket, Error error) override;
  void OnRead(platform::UdpSocket* socket,
              ErrorOr<platform::UdpPacket> packet) override;

  // QuicConnectionFactory overrides.
  void SetServerDelegate(ServerDelegate* delegate,
                         const std::vector<IPEndpoint>& endpoints) override;
  std::unique_ptr<QuicConnection> Connect(
      const IPEndpoint& endpoint,
      QuicConnection::Delegate* connection_delegate) override;

  bool idle() const { return idle_; }

 private:
  FakeQuicConnectionFactoryBridge* bridge_;
  bool idle_ = true;
};

}  // namespace osp
}  // namespace openscreen

#endif  // OSP_IMPL_QUIC_TESTING_FAKE_QUIC_CONNECTION_FACTORY_H_