xref: /aosp_15_r20/external/openscreen/osp/impl/presentation/presentation_connection_unittest.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
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 #include "osp/public/presentation/presentation_connection.h"
6 
7 #include <memory>
8 
9 #include "absl/strings/string_view.h"
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
12 #include "osp/impl/presentation/testing/mock_connection_delegate.h"
13 #include "osp/impl/quic/testing/fake_quic_connection.h"
14 #include "osp/impl/quic/testing/fake_quic_connection_factory.h"
15 #include "osp/impl/quic/testing/quic_test_support.h"
16 #include "osp/public/network_service_manager.h"
17 #include "osp/public/presentation/presentation_controller.h"
18 #include "platform/test/fake_clock.h"
19 #include "platform/test/fake_task_runner.h"
20 
21 namespace openscreen {
22 namespace osp {
23 
24 using ::testing::_;
25 using ::testing::Invoke;
26 using ::testing::NiceMock;
27 
28 namespace {
29 
30 class MockParentDelegate : public Connection::ParentDelegate {
31  public:
32   MockParentDelegate() = default;
33   ~MockParentDelegate() override = default;
34 
35   MOCK_METHOD2(CloseConnection, Error(Connection*, Connection::CloseReason));
36   MOCK_METHOD2(OnPresentationTerminated,
37                Error(const std::string&, TerminationReason));
38   MOCK_METHOD1(OnConnectionDestroyed, void(Connection*));
39 };
40 
41 class MockConnectRequest final
42     : public ProtocolConnectionClient::ConnectionRequestCallback {
43  public:
44   ~MockConnectRequest() override = default;
45 
OnConnectionOpened(uint64_t request_id,std::unique_ptr<ProtocolConnection> connection)46   void OnConnectionOpened(
47       uint64_t request_id,
48       std::unique_ptr<ProtocolConnection> connection) override {
49     OnConnectionOpenedMock(request_id, connection.release());
50   }
51   MOCK_METHOD2(OnConnectionOpenedMock,
52                void(uint64_t request_id, ProtocolConnection* connection));
53   MOCK_METHOD1(OnConnectionFailed, void(uint64_t request_id));
54 };
55 
56 }  // namespace
57 
58 class ConnectionTest : public ::testing::Test {
59  public:
ConnectionTest()60   ConnectionTest() {
61     fake_clock_ = std::make_unique<FakeClock>(
62         Clock::time_point(std::chrono::milliseconds(1298424)));
63     task_runner_ = std::make_unique<FakeTaskRunner>(fake_clock_.get());
64     quic_bridge_ =
65         std::make_unique<FakeQuicBridge>(task_runner_.get(), FakeClock::now);
66     controller_connection_manager_ = std::make_unique<ConnectionManager>(
67         quic_bridge_->controller_demuxer.get());
68     receiver_connection_manager_ = std::make_unique<ConnectionManager>(
69         quic_bridge_->receiver_demuxer.get());
70   }
71 
72  protected:
SetUp()73   void SetUp() override {
74     NetworkServiceManager::Create(nullptr, nullptr,
75                                   std::move(quic_bridge_->quic_client),
76                                   std::move(quic_bridge_->quic_server));
77   }
78 
TearDown()79   void TearDown() override { NetworkServiceManager::Dispose(); }
80 
MakeEchoResponse(const std::string & message)81   std::string MakeEchoResponse(const std::string& message) {
82     return std::string("echo: ") + message;
83   }
84 
MakeEchoResponse(const std::vector<uint8_t> & data)85   std::vector<uint8_t> MakeEchoResponse(const std::vector<uint8_t>& data) {
86     std::vector<uint8_t> response{13, 14, 15};
87     response.insert(response.end(), data.begin(), data.end());
88     return response;
89   }
90 
91   std::unique_ptr<FakeClock> fake_clock_;
92   std::unique_ptr<FakeTaskRunner> task_runner_;
93   std::unique_ptr<FakeQuicBridge> quic_bridge_;
94   std::unique_ptr<ConnectionManager> controller_connection_manager_;
95   std::unique_ptr<ConnectionManager> receiver_connection_manager_;
96   NiceMock<MockParentDelegate> mock_controller_;
97   NiceMock<MockParentDelegate> mock_receiver_;
98 };
99 
TEST_F(ConnectionTest,ConnectAndSend)100 TEST_F(ConnectionTest, ConnectAndSend) {
101   const std::string id{"deadbeef01234"};
102   const std::string url{"https://example.com/receiver.html"};
103   const uint64_t connection_id = 13;
104   MockConnectionDelegate mock_controller_delegate;
105   MockConnectionDelegate mock_receiver_delegate;
106   Connection controller(Connection::PresentationInfo{id, url},
107                         &mock_controller_delegate, &mock_controller_);
108   Connection receiver(Connection::PresentationInfo{id, url},
109                       &mock_receiver_delegate, &mock_receiver_);
110   ON_CALL(mock_controller_, OnPresentationTerminated(_, _))
111       .WillByDefault(Invoke([&receiver](const std::string& presentation_id,
112                                         TerminationReason reason) {
113         receiver.OnTerminated();
114         return Error::None();
115       }));
116   ON_CALL(mock_controller_, CloseConnection(_, _))
117       .WillByDefault(Invoke(
118           [&receiver](Connection* connection, Connection::CloseReason reason) {
119             receiver.OnClosedByRemote();
120             return Error::None();
121           }));
122   ON_CALL(mock_receiver_, OnPresentationTerminated(_, _))
123       .WillByDefault(Invoke([&controller](const std::string& presentation_id,
124                                           TerminationReason reason) {
125         controller.OnTerminated();
126         return Error::None();
127       }));
128   ON_CALL(mock_receiver_, CloseConnection(_, _))
129       .WillByDefault(Invoke([&controller](Connection* connection,
130                                           Connection::CloseReason reason) {
131         controller.OnClosedByRemote();
132         return Error::None();
133       }));
134 
135   EXPECT_EQ(id, controller.presentation_info().id);
136   EXPECT_EQ(url, controller.presentation_info().url);
137   EXPECT_EQ(id, receiver.presentation_info().id);
138   EXPECT_EQ(url, receiver.presentation_info().url);
139 
140   EXPECT_EQ(Connection::State::kConnecting, controller.state());
141   EXPECT_EQ(Connection::State::kConnecting, receiver.state());
142 
143   MockConnectRequest mock_connect_request;
144   std::unique_ptr<ProtocolConnection> controller_stream;
145   std::unique_ptr<ProtocolConnection> receiver_stream;
146   NetworkServiceManager::Get()->GetProtocolConnectionClient()->Connect(
147       quic_bridge_->kReceiverEndpoint, &mock_connect_request);
148   EXPECT_CALL(mock_connect_request, OnConnectionOpenedMock(_, _))
149       .WillOnce(Invoke([&controller_stream](uint64_t request_id,
150                                             ProtocolConnection* stream) {
151         controller_stream.reset(stream);
152       }));
153 
154   EXPECT_CALL(quic_bridge_->mock_server_observer, OnIncomingConnectionMock(_))
155       .WillOnce(testing::WithArgs<0>(testing::Invoke(
156           [&receiver_stream](std::unique_ptr<ProtocolConnection>& connection) {
157             receiver_stream = std::move(connection);
158           })));
159 
160   quic_bridge_->RunTasksUntilIdle();
161   ASSERT_TRUE(controller_stream);
162   ASSERT_TRUE(receiver_stream);
163 
164   EXPECT_CALL(mock_controller_delegate, OnConnected());
165   EXPECT_CALL(mock_receiver_delegate, OnConnected());
166   uint64_t controller_endpoint_id = receiver_stream->endpoint_id();
167   uint64_t receiver_endpoint_id = controller_stream->endpoint_id();
168   controller.OnConnected(connection_id, receiver_endpoint_id,
169                          std::move(controller_stream));
170   receiver.OnConnected(connection_id, controller_endpoint_id,
171                        std::move(receiver_stream));
172   controller_connection_manager_->AddConnection(&controller);
173   receiver_connection_manager_->AddConnection(&receiver);
174 
175   EXPECT_EQ(Connection::State::kConnected, controller.state());
176   EXPECT_EQ(Connection::State::kConnected, receiver.state());
177 
178   std::string message = "some connection message";
179   const std::string expected_message = message;
180   const std::string expected_response = MakeEchoResponse(expected_message);
181 
182   controller.SendString(message);
183 
184   std::string received;
185   EXPECT_CALL(mock_receiver_delegate,
186               OnStringMessage(static_cast<absl::string_view>(expected_message)))
187       .WillOnce(Invoke(
188           [&received](absl::string_view s) { received = std::string(s); }));
189   quic_bridge_->RunTasksUntilIdle();
190 
191   std::string string_response = MakeEchoResponse(received);
192   receiver.SendString(string_response);
193 
194   EXPECT_CALL(
195       mock_controller_delegate,
196       OnStringMessage(static_cast<absl::string_view>(expected_response)));
197   quic_bridge_->RunTasksUntilIdle();
198 
199   std::vector<uint8_t> data{0, 3, 2, 4, 4, 6, 1};
200   const std::vector<uint8_t> expected_data = data;
201   const std::vector<uint8_t> expected_response_data =
202       MakeEchoResponse(expected_data);
203 
204   controller.SendBinary(std::move(data));
205 
206   std::vector<uint8_t> received_data;
207   EXPECT_CALL(mock_receiver_delegate, OnBinaryMessage(expected_data))
208       .WillOnce(Invoke([&received_data](std::vector<uint8_t> d) {
209         received_data = std::move(d);
210       }));
211   quic_bridge_->RunTasksUntilIdle();
212 
213   receiver.SendBinary(MakeEchoResponse(received_data));
214   EXPECT_CALL(mock_controller_delegate,
215               OnBinaryMessage(expected_response_data));
216   quic_bridge_->RunTasksUntilIdle();
217 
218   EXPECT_CALL(mock_controller_delegate, OnClosedByRemote());
219   receiver.Close(Connection::CloseReason::kClosed);
220   quic_bridge_->RunTasksUntilIdle();
221   EXPECT_EQ(Connection::State::kClosed, controller.state());
222   EXPECT_EQ(Connection::State::kClosed, receiver.state());
223   controller_connection_manager_->RemoveConnection(&controller);
224   receiver_connection_manager_->RemoveConnection(&receiver);
225 }
226 
227 }  // namespace osp
228 }  // namespace openscreen
229