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 CAST_COMMON_CHANNEL_TESTING_FAKE_CAST_SOCKET_H_ 6 #define CAST_COMMON_CHANNEL_TESTING_FAKE_CAST_SOCKET_H_ 7 8 #include <memory> 9 10 #include "cast/common/channel/proto/cast_channel.pb.h" 11 #include "cast/common/public/cast_socket.h" 12 #include "gmock/gmock.h" 13 #include "platform/test/mock_tls_connection.h" 14 15 namespace openscreen { 16 namespace cast { 17 18 class MockCastSocketClient final : public CastSocket::Client { 19 public: 20 ~MockCastSocketClient() override = default; 21 22 MOCK_METHOD(void, OnError, (CastSocket * socket, Error error), (override)); 23 MOCK_METHOD(void, 24 OnMessage, 25 (CastSocket * socket, ::cast::channel::CastMessage message), 26 (override)); 27 }; 28 29 struct FakeCastSocket { FakeCastSocketFakeCastSocket30 FakeCastSocket() 31 : FakeCastSocket({{10, 0, 1, 7}, 1234}, {{10, 0, 1, 9}, 4321}) {} FakeCastSocketFakeCastSocket32 FakeCastSocket(const IPEndpoint& local_endpoint, 33 const IPEndpoint& remote_endpoint) 34 : local_endpoint(local_endpoint), 35 remote_endpoint(remote_endpoint), 36 moved_connection(std::make_unique<MockTlsConnection>(local_endpoint, 37 remote_endpoint)), 38 connection(moved_connection.get()), 39 socket(std::move(moved_connection), &mock_client) {} 40 41 IPEndpoint local_endpoint; 42 IPEndpoint remote_endpoint; 43 std::unique_ptr<MockTlsConnection> moved_connection; 44 MockTlsConnection* connection; 45 MockCastSocketClient mock_client; 46 CastSocket socket; 47 }; 48 49 // Two FakeCastSockets that are piped together via their MockTlsConnection 50 // read/write methods. Calling SendMessage on |socket| will result in an 51 // OnMessage callback on |mock_peer_client| and vice versa for |peer_socket| and 52 // |mock_client|. 53 struct FakeCastSocketPair { FakeCastSocketPairFakeCastSocketPair54 FakeCastSocketPair() 55 : FakeCastSocketPair({{10, 0, 1, 7}, 1234}, {{10, 0, 1, 9}, 4321}) {} 56 FakeCastSocketPairFakeCastSocketPair57 FakeCastSocketPair(const IPEndpoint& local_endpoint, 58 const IPEndpoint& remote_endpoint) 59 : local_endpoint(local_endpoint), remote_endpoint(remote_endpoint) { 60 using ::testing::_; 61 using ::testing::Invoke; 62 63 auto moved_connection = 64 std::make_unique<::testing::NiceMock<MockTlsConnection>>( 65 local_endpoint, remote_endpoint); 66 connection = moved_connection.get(); 67 socket = 68 std::make_unique<CastSocket>(std::move(moved_connection), &mock_client); 69 70 auto moved_peer = std::make_unique<::testing::NiceMock<MockTlsConnection>>( 71 remote_endpoint, local_endpoint); 72 peer_connection = moved_peer.get(); 73 peer_socket = 74 std::make_unique<CastSocket>(std::move(moved_peer), &mock_peer_client); 75 76 ON_CALL(*connection, Send(_, _)) 77 .WillByDefault(Invoke([this](const void* data, size_t len) { 78 peer_connection->OnRead(std::vector<uint8_t>( 79 reinterpret_cast<const uint8_t*>(data), 80 reinterpret_cast<const uint8_t*>(data) + len)); 81 return true; 82 })); 83 ON_CALL(*peer_connection, Send(_, _)) 84 .WillByDefault(Invoke([this](const void* data, size_t len) { 85 connection->OnRead(std::vector<uint8_t>( 86 reinterpret_cast<const uint8_t*>(data), 87 reinterpret_cast<const uint8_t*>(data) + len)); 88 return true; 89 })); 90 } 91 ~FakeCastSocketPair() = default; 92 93 IPEndpoint local_endpoint; 94 IPEndpoint remote_endpoint; 95 96 ::testing::NiceMock<MockTlsConnection>* connection; 97 MockCastSocketClient mock_client; 98 std::unique_ptr<CastSocket> socket; 99 100 ::testing::NiceMock<MockTlsConnection>* peer_connection; 101 MockCastSocketClient mock_peer_client; 102 std::unique_ptr<CastSocket> peer_socket; 103 }; 104 105 } // namespace cast 106 } // namespace openscreen 107 108 #endif // CAST_COMMON_CHANNEL_TESTING_FAKE_CAST_SOCKET_H_ 109