1 // Copyright 2019 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_QUIC_TEST_SUPPORT_H_ 6 #define OSP_IMPL_QUIC_TESTING_QUIC_TEST_SUPPORT_H_ 7 8 #include <memory> 9 10 #include "gmock/gmock.h" 11 #include "osp/impl/quic/quic_client.h" 12 #include "osp/impl/quic/quic_server.h" 13 #include "osp/impl/quic/testing/fake_quic_connection_factory.h" 14 #include "osp/public/message_demuxer.h" 15 #include "osp/public/network_metrics.h" 16 #include "osp/public/protocol_connection_client.h" 17 #include "osp/public/protocol_connection_server.h" 18 #include "platform/api/time.h" 19 #include "platform/api/udp_socket.h" 20 #include "platform/base/ip_address.h" 21 #include "platform/test/fake_clock.h" 22 #include "platform/test/fake_task_runner.h" 23 #include "platform/test/fake_udp_socket.h" 24 25 namespace openscreen { 26 namespace osp { 27 28 class MockServiceObserver : public ProtocolConnectionServiceObserver { 29 public: 30 ~MockServiceObserver() override = default; 31 32 MOCK_METHOD0(OnRunning, void()); 33 MOCK_METHOD0(OnStopped, void()); 34 MOCK_METHOD1(OnMetrics, void(const NetworkMetrics& metrics)); 35 MOCK_METHOD1(OnError, void(const Error& error)); 36 }; 37 38 class MockServerObserver : public ProtocolConnectionServer::Observer { 39 public: 40 ~MockServerObserver() override = default; 41 42 MOCK_METHOD0(OnRunning, void()); 43 MOCK_METHOD0(OnStopped, void()); 44 MOCK_METHOD1(OnMetrics, void(const NetworkMetrics& metrics)); 45 MOCK_METHOD1(OnError, void(const Error& error)); 46 47 MOCK_METHOD0(OnSuspended, void()); 48 OnIncomingConnection(std::unique_ptr<ProtocolConnection> connection)49 void OnIncomingConnection( 50 std::unique_ptr<ProtocolConnection> connection) override { 51 OnIncomingConnectionMock(connection); 52 } 53 MOCK_METHOD1(OnIncomingConnectionMock, 54 void(std::unique_ptr<ProtocolConnection>& connection)); 55 }; 56 57 class FakeQuicBridge { 58 public: 59 FakeQuicBridge(FakeTaskRunner* task_runner, ClockNowFunctionPtr now_function); 60 ~FakeQuicBridge(); 61 62 const IPEndpoint kControllerEndpoint{{192, 168, 1, 3}, 4321}; 63 const IPEndpoint kReceiverEndpoint{{192, 168, 1, 17}, 1234}; 64 65 std::unique_ptr<MessageDemuxer> controller_demuxer; 66 std::unique_ptr<MessageDemuxer> receiver_demuxer; 67 std::unique_ptr<QuicClient> quic_client; 68 std::unique_ptr<QuicServer> quic_server; 69 std::unique_ptr<FakeQuicConnectionFactoryBridge> fake_bridge; 70 ::testing::NiceMock<MockServiceObserver> mock_client_observer; 71 ::testing::NiceMock<MockServerObserver> mock_server_observer; 72 73 void RunTasksUntilIdle(); 74 75 private: 76 void PostClientPacket(); 77 void PostServerPacket(); 78 void PostPacketsUntilIdle(); 79 FakeClientQuicConnectionFactory* GetClientFactory(); 80 FakeServerQuicConnectionFactory* GetServerFactory(); 81 FakeTaskRunner* task_runner_; 82 83 std::unique_ptr<FakeUdpSocket> client_socket_; 84 std::unique_ptr<FakeUdpSocket> server_socket_; 85 }; 86 87 } // namespace osp 88 } // namespace openscreen 89 90 #endif // OSP_IMPL_QUIC_TESTING_QUIC_TEST_SUPPORT_H_ 91