1 // Copyright 2015 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.
5 #include "remoting/protocol/quic_channel_factory.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/test_completion_callback.h"
13 #include "net/quic/p2p/quic_p2p_session.h"
14 #include "net/quic/p2p/quic_p2p_stream.h"
15 #include "net/socket/socket.h"
16 #include "remoting/base/constants.h"
17 #include "remoting/protocol/connection_tester.h"
18 #include "remoting/protocol/fake_datagram_socket.h"
19 #include "remoting/protocol/p2p_stream_socket.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
24 using testing::AtMost
;
25 using testing::InvokeWithoutArgs
;
32 const int kMessageSize
= 1024;
33 const int kMessages
= 100;
35 const char kTestChannelName
[] = "test";
36 const char kTestChannelName2
[] = "test2";
40 class QuicChannelFactoryTest
: public testing::Test
,
41 public testing::WithParamInterface
<bool> {
44 host_channel1_
.reset();
45 host_channel2_
.reset();
46 client_channel1_
.reset();
47 client_channel2_
.reset();
52 void FailedReadDeleteAll(int result
) {
53 EXPECT_NE(net::OK
, result
);
57 void OnChannelConnected(scoped_ptr
<P2PStreamSocket
>* storage
,
59 base::RunLoop
* run_loop
,
60 scoped_ptr
<P2PStreamSocket
> socket
) {
61 *storage
= socket
.Pass();
64 EXPECT_GE(*counter
, 0);
70 void OnChannelConnectedExpectFail(scoped_ptr
<P2PStreamSocket
> socket
) {
72 host_quic_
->CancelChannelCreation(kTestChannelName2
);
76 void OnChannelConnectedNotReached(scoped_ptr
<P2PStreamSocket
> socket
) {
81 void TearDown() override
{
83 // QuicChannelFactory destroys the internals asynchronously. Run all pending
84 // tasks to avoid leaking memory.
85 base::RunLoop().RunUntilIdle();
89 host_base_channel_factory_
.PairWith(&client_base_channel_factory_
);
90 host_base_channel_factory_
.set_asynchronous_create(GetParam());
91 client_base_channel_factory_
.set_asynchronous_create(GetParam());
93 const char kTestSessionId
[] = "123123";
94 host_quic_
.reset(new QuicChannelFactory(kTestSessionId
, true));
95 client_quic_
.reset(new QuicChannelFactory(kTestSessionId
, false));
97 std::string message
= client_quic_
->CreateSessionInitiateConfigMessage();
98 EXPECT_TRUE(host_quic_
->ProcessSessionInitiateConfigMessage(message
));
99 message
= host_quic_
->CreateSessionAcceptConfigMessage();
100 EXPECT_TRUE(client_quic_
->ProcessSessionAcceptConfigMessage(message
));
102 const char kTestSharedSecret
[] = "Shared Secret";
103 host_quic_
->Start(&host_base_channel_factory_
, kTestSharedSecret
);
104 client_quic_
->Start(&client_base_channel_factory_
, kTestSharedSecret
);
106 FakeDatagramSocket
* host_base_channel
=
107 host_base_channel_factory_
.GetFakeChannel(kQuicChannelName
);
108 if (host_base_channel
)
109 host_base_channel
->set_async_send(GetParam());
111 FakeDatagramSocket
* client_base_channel
=
112 client_base_channel_factory_
.GetFakeChannel(kQuicChannelName
);
113 if (client_base_channel
)
114 client_base_channel
->set_async_send(GetParam());
117 void CreateChannel(const std::string
& name
,
118 scoped_ptr
<P2PStreamSocket
>* host_channel
,
119 scoped_ptr
<P2PStreamSocket
>* client_channel
) {
121 base::RunLoop run_loop
;
122 host_quic_
->CreateChannel(
124 base::Bind(&QuicChannelFactoryTest::OnChannelConnected
,
125 base::Unretained(this), host_channel
, &counter
, &run_loop
));
126 client_quic_
->CreateChannel(
127 name
, base::Bind(&QuicChannelFactoryTest::OnChannelConnected
,
128 base::Unretained(this), client_channel
, &counter
,
133 EXPECT_TRUE(host_channel
->get());
134 EXPECT_TRUE(client_channel
->get());
137 scoped_refptr
<net::IOBufferWithSize
> CreateTestBuffer(int size
) {
138 scoped_refptr
<net::IOBufferWithSize
> result
=
139 new net::IOBufferWithSize(size
);
140 for (int i
= 0; i
< size
; ++i
) {
141 result
->data()[i
] = rand() % 256;
146 base::MessageLoop message_loop_
;
148 FakeDatagramChannelFactory host_base_channel_factory_
;
149 FakeDatagramChannelFactory client_base_channel_factory_
;
151 scoped_ptr
<QuicChannelFactory
> host_quic_
;
152 scoped_ptr
<QuicChannelFactory
> client_quic_
;
154 scoped_ptr
<P2PStreamSocket
> host_channel1_
;
155 scoped_ptr
<P2PStreamSocket
> client_channel1_
;
156 scoped_ptr
<P2PStreamSocket
> host_channel2_
;
157 scoped_ptr
<P2PStreamSocket
> client_channel2_
;
160 INSTANTIATE_TEST_CASE_P(SyncWrite
,
161 QuicChannelFactoryTest
,
162 ::testing::Values(false));
163 INSTANTIATE_TEST_CASE_P(AsyncWrite
,
164 QuicChannelFactoryTest
,
165 ::testing::Values(true));
167 TEST_P(QuicChannelFactoryTest
, OneChannel
) {
170 scoped_ptr
<P2PStreamSocket
> host_channel
;
171 scoped_ptr
<P2PStreamSocket
> client_channel
;
172 ASSERT_NO_FATAL_FAILURE(
173 CreateChannel(kTestChannelName
, &host_channel
, &client_channel
));
175 StreamConnectionTester
tester(host_channel
.get(), client_channel
.get(),
176 kMessageSize
, kMessages
);
179 tester
.CheckResults();
182 TEST_P(QuicChannelFactoryTest
, TwoChannels
) {
185 scoped_ptr
<P2PStreamSocket
> host_channel1_
;
186 scoped_ptr
<P2PStreamSocket
> client_channel1_
;
187 ASSERT_NO_FATAL_FAILURE(
188 CreateChannel(kTestChannelName
, &host_channel1_
, &client_channel1_
));
190 scoped_ptr
<P2PStreamSocket
> host_channel2_
;
191 scoped_ptr
<P2PStreamSocket
> client_channel2_
;
192 ASSERT_NO_FATAL_FAILURE(
193 CreateChannel(kTestChannelName2
, &host_channel2_
, &client_channel2_
));
195 StreamConnectionTester
tester1(host_channel1_
.get(), client_channel1_
.get(),
196 kMessageSize
, kMessages
);
197 StreamConnectionTester
tester2(host_channel2_
.get(), client_channel2_
.get(),
198 kMessageSize
, kMessages
);
201 while (!tester1
.done() || !tester2
.done()) {
204 tester1
.CheckResults();
205 tester2
.CheckResults();
208 TEST_P(QuicChannelFactoryTest
, SendFail
) {
211 scoped_ptr
<P2PStreamSocket
> host_channel1_
;
212 scoped_ptr
<P2PStreamSocket
> client_channel1_
;
213 ASSERT_NO_FATAL_FAILURE(
214 CreateChannel(kTestChannelName
, &host_channel1_
, &client_channel1_
));
216 scoped_ptr
<P2PStreamSocket
> host_channel2_
;
217 scoped_ptr
<P2PStreamSocket
> client_channel2_
;
218 ASSERT_NO_FATAL_FAILURE(
219 CreateChannel(kTestChannelName2
, &host_channel2_
, &client_channel2_
));
221 host_base_channel_factory_
.GetFakeChannel(kQuicChannelName
)
222 ->set_next_send_error(net::ERR_FAILED
);
224 scoped_refptr
<net::IOBufferWithSize
> buf
= CreateTestBuffer(100);
227 // Try writing to a channel. This should result in all stream being closed due
230 net::TestCompletionCallback write_cb_1
;
231 host_channel1_
->Write(buf
.get(), buf
->size(), write_cb_1
.callback());
232 base::RunLoop().RunUntilIdle();
235 // Repeated attempt to write should result in an error.
237 net::TestCompletionCallback write_cb_1
;
238 net::TestCompletionCallback write_cb_2
;
239 EXPECT_NE(net::OK
, host_channel1_
->Write(buf
.get(), buf
->size(),
240 write_cb_1
.callback()));
241 EXPECT_FALSE(write_cb_1
.have_result());
242 EXPECT_NE(net::OK
, host_channel1_
->Write(buf
.get(), buf
->size(),
243 write_cb_2
.callback()));
244 EXPECT_FALSE(write_cb_2
.have_result());
248 TEST_P(QuicChannelFactoryTest
, DeleteWhenFailed
) {
251 ASSERT_NO_FATAL_FAILURE(
252 CreateChannel(kTestChannelName
, &host_channel1_
, &client_channel1_
));
253 ASSERT_NO_FATAL_FAILURE(
254 CreateChannel(kTestChannelName2
, &host_channel2_
, &client_channel2_
));
256 host_base_channel_factory_
.GetFakeChannel(kQuicChannelName
)
257 ->set_next_send_error(net::ERR_FAILED
);
259 scoped_refptr
<net::IOBufferWithSize
> read_buf
=
260 new net::IOBufferWithSize(100);
262 EXPECT_EQ(net::ERR_IO_PENDING
,
263 host_channel1_
->Read(
264 read_buf
.get(), read_buf
->size(),
265 base::Bind(&QuicChannelFactoryTest::FailedReadDeleteAll
,
266 base::Unretained(this))));
268 // Try writing to a channel. This should result it DeleteAll() called and the
269 // connection torn down.
270 scoped_refptr
<net::IOBufferWithSize
> buf
= CreateTestBuffer(100);
271 net::TestCompletionCallback write_cb_1
;
272 host_channel1_
->Write(buf
.get(), buf
->size(), write_cb_1
.callback());
274 base::RunLoop().RunUntilIdle();
276 // Check that the connection was torn down.
277 EXPECT_FALSE(host_quic_
);
280 TEST_P(QuicChannelFactoryTest
, SessionFail
) {
281 host_base_channel_factory_
.set_fail_create(true);
284 host_quic_
->CreateChannel(
286 base::Bind(&QuicChannelFactoryTest::OnChannelConnectedExpectFail
,
287 base::Unretained(this)));
289 // host_quic_ may be destroyed at this point in sync mode.
291 host_quic_
->CreateChannel(
293 base::Bind(&QuicChannelFactoryTest::OnChannelConnectedNotReached
,
294 base::Unretained(this)));
297 base::RunLoop().RunUntilIdle();
299 // Check that DeleteAll() was called and the connection was torn down.
300 EXPECT_FALSE(host_quic_
);
303 // Verify that the host just ignores incoming stream with unexpected name.
304 TEST_P(QuicChannelFactoryTest
, UnknownName
) {
307 // Create a new channel from the client side.
308 client_quic_
->CreateChannel(
309 kTestChannelName
, base::Bind(&QuicChannelFactoryTest::OnChannelConnected
,
310 base::Unretained(this), &client_channel1_
,
312 base::RunLoop().RunUntilIdle();
314 EXPECT_EQ(0U, host_quic_
->GetP2PSessionForTests()->GetNumOpenStreams());
317 // Verify that incoming streams that have received only partial name are
318 // destroyed correctly.
319 TEST_P(QuicChannelFactoryTest
, SendPartialName
) {
322 base::RunLoop().RunUntilIdle();
324 net::QuicP2PSession
* session
= client_quic_
->GetP2PSessionForTests();
325 net::QuicP2PStream
* stream
= session
->CreateOutgoingDynamicStream();
327 std::string name
= kTestChannelName
;
328 // Send only half of the name to the host.
329 stream
->WriteHeader(std::string(1, static_cast<char>(name
.size())) +
330 name
.substr(0, name
.size() / 2));
332 base::RunLoop().RunUntilIdle();
334 // Host should have received the new stream and is still waiting for the name.
335 EXPECT_EQ(1U, host_quic_
->GetP2PSessionForTests()->GetNumOpenStreams());
337 session
->CloseStream(stream
->id());
338 base::RunLoop().RunUntilIdle();
340 // Verify that the stream was closed on the host side.
341 EXPECT_EQ(0U, host_quic_
->GetP2PSessionForTests()->GetNumOpenStreams());
343 // Create another stream with only partial name and tear down connection while
344 // it's still pending.
345 stream
= session
->CreateOutgoingDynamicStream();
346 stream
->WriteHeader(std::string(1, static_cast<char>(name
.size())) +
347 name
.substr(0, name
.size() / 2));
348 base::RunLoop().RunUntilIdle();
349 EXPECT_EQ(1U, host_quic_
->GetP2PSessionForTests()->GetNumOpenStreams());
352 } // namespace protocol
353 } // namespace remoting