summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/quic_channel_factory_unittest.cc
blob: e860664b2960a19faa5ae0e46fe760707f1ce727 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2015 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.

#include "remoting/protocol/quic_channel_factory.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/quic/p2p/quic_p2p_session.h"
#include "net/quic/p2p/quic_p2p_stream.h"
#include "net/socket/socket.h"
#include "remoting/base/constants.h"
#include "remoting/protocol/connection_tester.h"
#include "remoting/protocol/fake_datagram_socket.h"
#include "remoting/protocol/p2p_stream_socket.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::AtMost;
using testing::InvokeWithoutArgs;

namespace remoting {
namespace protocol {

namespace {

const int kMessageSize = 1024;
const int kMessages = 100;

const char kTestChannelName[] = "test";
const char kTestChannelName2[] = "test2";

}  // namespace

class QuicChannelFactoryTest : public testing::Test,
                               public testing::WithParamInterface<bool> {
 public:
  void DeleteAll() {
    host_channel1_.reset();
    host_channel2_.reset();
    client_channel1_.reset();
    client_channel2_.reset();
    host_quic_.reset();
    client_quic_.reset();
  }

  void FailedReadDeleteAll(int result) {
    EXPECT_NE(net::OK, result);
    DeleteAll();
  }

  void OnChannelConnected(scoped_ptr<P2PStreamSocket>* storage,
                          int* counter,
                          base::RunLoop* run_loop,
                          scoped_ptr<P2PStreamSocket> socket) {
    *storage = socket.Pass();
    if (counter) {
      --(*counter);
      EXPECT_GE(*counter, 0);
      if (*counter == 0)
        run_loop->Quit();
    }
  }

  void OnChannelConnectedExpectFail(scoped_ptr<P2PStreamSocket> socket) {
    EXPECT_FALSE(socket);
    host_quic_->CancelChannelCreation(kTestChannelName2);
    DeleteAll();
  }

  void OnChannelConnectedNotReached(scoped_ptr<P2PStreamSocket> socket) {
    NOTREACHED();
  }

 protected:
  void TearDown() override {
    DeleteAll();
    // QuicChannelFactory destroys the internals asynchronously. Run all pending
    // tasks to avoid leaking memory.
    base::RunLoop().RunUntilIdle();
  }

  void Initialize() {
    host_base_channel_factory_.PairWith(&client_base_channel_factory_);
    host_base_channel_factory_.set_asynchronous_create(GetParam());
    client_base_channel_factory_.set_asynchronous_create(GetParam());

    const char kTestSessionId[] = "123123";
    host_quic_.reset(new QuicChannelFactory(kTestSessionId, true));
    client_quic_.reset(new QuicChannelFactory(kTestSessionId, false));

    std::string message = client_quic_->CreateSessionInitiateConfigMessage();
    EXPECT_TRUE(host_quic_->ProcessSessionInitiateConfigMessage(message));
    message = host_quic_->CreateSessionAcceptConfigMessage();
    EXPECT_TRUE(client_quic_->ProcessSessionAcceptConfigMessage(message));

    const char kTestSharedSecret[] = "Shared Secret";
    host_quic_->Start(&host_base_channel_factory_, kTestSharedSecret);
    client_quic_->Start(&client_base_channel_factory_, kTestSharedSecret);

    FakeDatagramSocket* host_base_channel =
        host_base_channel_factory_.GetFakeChannel(kQuicChannelName);
    if (host_base_channel)
      host_base_channel->set_async_send(GetParam());

    FakeDatagramSocket* client_base_channel =
        client_base_channel_factory_.GetFakeChannel(kQuicChannelName);
    if (client_base_channel)
      client_base_channel->set_async_send(GetParam());
  }

  void CreateChannel(const std::string& name,
                     scoped_ptr<P2PStreamSocket>* host_channel,
                     scoped_ptr<P2PStreamSocket>* client_channel) {
    int counter = 2;
    base::RunLoop run_loop;
    host_quic_->CreateChannel(
        name,
        base::Bind(&QuicChannelFactoryTest::OnChannelConnected,
                   base::Unretained(this), host_channel, &counter, &run_loop));
    client_quic_->CreateChannel(
        name, base::Bind(&QuicChannelFactoryTest::OnChannelConnected,
                         base::Unretained(this), client_channel, &counter,
                         &run_loop));

    run_loop.Run();

    EXPECT_TRUE(host_channel->get());
    EXPECT_TRUE(client_channel->get());
  }

  scoped_refptr<net::IOBufferWithSize> CreateTestBuffer(int size) {
    scoped_refptr<net::IOBufferWithSize> result =
        new net::IOBufferWithSize(size);
    for (int i = 0; i < size; ++i) {
      result->data()[i] = rand() % 256;
    }
    return result;
  }

  base::MessageLoop message_loop_;

  FakeDatagramChannelFactory host_base_channel_factory_;
  FakeDatagramChannelFactory client_base_channel_factory_;

  scoped_ptr<QuicChannelFactory> host_quic_;
  scoped_ptr<QuicChannelFactory> client_quic_;

  scoped_ptr<P2PStreamSocket> host_channel1_;
  scoped_ptr<P2PStreamSocket> client_channel1_;
  scoped_ptr<P2PStreamSocket> host_channel2_;
  scoped_ptr<P2PStreamSocket> client_channel2_;
};

INSTANTIATE_TEST_CASE_P(SyncWrite,
                        QuicChannelFactoryTest,
                        ::testing::Values(false));
INSTANTIATE_TEST_CASE_P(AsyncWrite,
                        QuicChannelFactoryTest,
                        ::testing::Values(true));

TEST_P(QuicChannelFactoryTest, OneChannel) {
  Initialize();

  scoped_ptr<P2PStreamSocket> host_channel;
  scoped_ptr<P2PStreamSocket> client_channel;
  ASSERT_NO_FATAL_FAILURE(
      CreateChannel(kTestChannelName, &host_channel, &client_channel));

  StreamConnectionTester tester(host_channel.get(), client_channel.get(),
                                kMessageSize, kMessages);
  tester.Start();
  message_loop_.Run();
  tester.CheckResults();
}

TEST_P(QuicChannelFactoryTest, TwoChannels) {
  Initialize();

  scoped_ptr<P2PStreamSocket> host_channel1_;
  scoped_ptr<P2PStreamSocket> client_channel1_;
  ASSERT_NO_FATAL_FAILURE(
      CreateChannel(kTestChannelName, &host_channel1_, &client_channel1_));

  scoped_ptr<P2PStreamSocket> host_channel2_;
  scoped_ptr<P2PStreamSocket> client_channel2_;
  ASSERT_NO_FATAL_FAILURE(
      CreateChannel(kTestChannelName2, &host_channel2_, &client_channel2_));

  StreamConnectionTester tester1(host_channel1_.get(), client_channel1_.get(),
                                 kMessageSize, kMessages);
  StreamConnectionTester tester2(host_channel2_.get(), client_channel2_.get(),
                                 kMessageSize, kMessages);
  tester1.Start();
  tester2.Start();
  while (!tester1.done() || !tester2.done()) {
    message_loop_.Run();
  }
  tester1.CheckResults();
  tester2.CheckResults();
}

TEST_P(QuicChannelFactoryTest, SendFail) {
  Initialize();

  scoped_ptr<P2PStreamSocket> host_channel1_;
  scoped_ptr<P2PStreamSocket> client_channel1_;
  ASSERT_NO_FATAL_FAILURE(
      CreateChannel(kTestChannelName, &host_channel1_, &client_channel1_));

  scoped_ptr<P2PStreamSocket> host_channel2_;
  scoped_ptr<P2PStreamSocket> client_channel2_;
  ASSERT_NO_FATAL_FAILURE(
      CreateChannel(kTestChannelName2, &host_channel2_, &client_channel2_));

  host_base_channel_factory_.GetFakeChannel(kQuicChannelName)
      ->set_next_send_error(net::ERR_FAILED);

  scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100);


  // Try writing to a channel. This should result in all stream being closed due
  // to an error.
  {
    net::TestCompletionCallback write_cb_1;
    host_channel1_->Write(buf.get(), buf->size(), write_cb_1.callback());
    base::RunLoop().RunUntilIdle();
  }

  // Repeated attempt to write should result in an error.
  {
    net::TestCompletionCallback write_cb_1;
    net::TestCompletionCallback write_cb_2;
    EXPECT_NE(net::OK, host_channel1_->Write(buf.get(), buf->size(),
                                             write_cb_1.callback()));
    EXPECT_FALSE(write_cb_1.have_result());
    EXPECT_NE(net::OK, host_channel1_->Write(buf.get(), buf->size(),
                                             write_cb_2.callback()));
    EXPECT_FALSE(write_cb_2.have_result());
  }
}

TEST_P(QuicChannelFactoryTest, DeleteWhenFailed) {
  Initialize();

  ASSERT_NO_FATAL_FAILURE(
      CreateChannel(kTestChannelName, &host_channel1_, &client_channel1_));
  ASSERT_NO_FATAL_FAILURE(
      CreateChannel(kTestChannelName2, &host_channel2_, &client_channel2_));

  host_base_channel_factory_.GetFakeChannel(kQuicChannelName)
      ->set_next_send_error(net::ERR_FAILED);

  scoped_refptr<net::IOBufferWithSize> read_buf =
      new net::IOBufferWithSize(100);

  EXPECT_EQ(net::ERR_IO_PENDING,
            host_channel1_->Read(
                read_buf.get(), read_buf->size(),
                base::Bind(&QuicChannelFactoryTest::FailedReadDeleteAll,
                           base::Unretained(this))));

  // Try writing to a channel. This should result it DeleteAll() called and the
  // connection torn down.
  scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100);
  net::TestCompletionCallback write_cb_1;
  host_channel1_->Write(buf.get(), buf->size(), write_cb_1.callback());

  base::RunLoop().RunUntilIdle();

  // Check that the connection was torn down.
  EXPECT_FALSE(host_quic_);
}

TEST_P(QuicChannelFactoryTest, SessionFail) {
  host_base_channel_factory_.set_fail_create(true);
  Initialize();

  host_quic_->CreateChannel(
      kTestChannelName,
      base::Bind(&QuicChannelFactoryTest::OnChannelConnectedExpectFail,
                 base::Unretained(this)));

  // host_quic_ may be destroyed at this point in sync mode.
  if (host_quic_) {
    host_quic_->CreateChannel(
        kTestChannelName2,
        base::Bind(&QuicChannelFactoryTest::OnChannelConnectedNotReached,
                   base::Unretained(this)));
  }

  base::RunLoop().RunUntilIdle();

  // Check that DeleteAll() was called and the connection was torn down.
  EXPECT_FALSE(host_quic_);
}

// Verify that the host just ignores incoming stream with unexpected name.
TEST_P(QuicChannelFactoryTest, UnknownName) {
  Initialize();

  // Create a new channel from the client side.
  client_quic_->CreateChannel(
      kTestChannelName, base::Bind(&QuicChannelFactoryTest::OnChannelConnected,
                                   base::Unretained(this), &client_channel1_,
                                   nullptr, nullptr));
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(0U, host_quic_->GetP2PSessionForTests()->GetNumOpenStreams());
}

// Verify that incoming streams that have received only partial name are
// destroyed correctly.
TEST_P(QuicChannelFactoryTest, SendPartialName) {
  Initialize();

  base::RunLoop().RunUntilIdle();

  net::QuicP2PSession* session = client_quic_->GetP2PSessionForTests();
  net::QuicP2PStream* stream = session->CreateOutgoingDynamicStream();

  std::string name = kTestChannelName;
  // Send only half of the name to the host.
  stream->WriteHeader(std::string(1, static_cast<char>(name.size())) +
                      name.substr(0, name.size() / 2));

  base::RunLoop().RunUntilIdle();

  // Host should have received the new stream and is still waiting for the name.
  EXPECT_EQ(1U, host_quic_->GetP2PSessionForTests()->GetNumOpenStreams());

  session->CloseStream(stream->id());
  base::RunLoop().RunUntilIdle();

  // Verify that the stream was closed on the host side.
  EXPECT_EQ(0U, host_quic_->GetP2PSessionForTests()->GetNumOpenStreams());

  // Create another stream with only partial name and tear down connection while
  // it's still pending.
  stream = session->CreateOutgoingDynamicStream();
  stream->WriteHeader(std::string(1, static_cast<char>(name.size())) +
                      name.substr(0, name.size() / 2));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1U, host_quic_->GetP2PSessionForTests()->GetNumOpenStreams());
}

}  // namespace protocol
}  // namespace remoting