blob: 328c18c7807a0ef7a4f8e4200ad224d591b724a0 [file] [log] [blame]
[email protected]fd0ca112014-07-17 06:10:201// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]83693232012-07-23 20:40:142// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fd0ca112014-07-17 06:10:205#include "remoting/protocol/chromium_socket_factory.h"
[email protected]83693232012-07-23 20:40:146
avi5a080f012015-12-22 23:15:437#include <stddef.h>
8#include <stdint.h>
9
dcheng0765c492016-04-06 22:41:5310#include <memory>
11
[email protected]f368eeb2013-07-17 23:53:3212#include "base/message_loop/message_loop.h"
[email protected]83693232012-07-23 20:40:1413#include "base/run_loop.h"
fdoray2ad58be2016-06-22 20:36:1614#include "base/single_thread_task_runner.h"
[email protected]83693232012-07-23 20:40:1415#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
Henrik Kjellander5f4278232017-06-30 09:24:4717#include "third_party/webrtc/rtc_base/asyncpacketsocket.h"
18#include "third_party/webrtc/rtc_base/socketaddress.h"
[email protected]83693232012-07-23 20:40:1419
20namespace remoting {
[email protected]fd0ca112014-07-17 06:10:2021namespace protocol {
[email protected]83693232012-07-23 20:40:1422
23class ChromiumSocketFactoryTest : public testing::Test,
24 public sigslot::has_slots<> {
25 public:
dcheng440d8e1c2014-10-28 01:23:1526 void SetUp() override {
[email protected]83693232012-07-23 20:40:1427 socket_factory_.reset(new ChromiumPacketSocketFactory());
28
29 socket_.reset(socket_factory_->CreateUdpSocket(
[email protected]e758d4c2014-08-06 16:48:1630 rtc::SocketAddress("127.0.0.1", 0), 0, 0));
sergeyuc5f104b2015-01-09 19:33:2431 ASSERT_TRUE(socket_.get() != nullptr);
[email protected]e758d4c2014-08-06 16:48:1632 EXPECT_EQ(socket_->GetState(), rtc::AsyncPacketSocket::STATE_BOUND);
[email protected]83693232012-07-23 20:40:1433 socket_->SignalReadPacket.connect(
34 this, &ChromiumSocketFactoryTest::OnPacket);
35 }
36
[email protected]e758d4c2014-08-06 16:48:1637 void OnPacket(rtc::AsyncPacketSocket* socket,
[email protected]83693232012-07-23 20:40:1438 const char* data, size_t size,
[email protected]e758d4c2014-08-06 16:48:1639 const rtc::SocketAddress& address,
40 const rtc::PacketTime& packet_time) {
[email protected]83693232012-07-23 20:40:1441 EXPECT_EQ(socket, socket_.get());
42 last_packet_.assign(data, data + size);
43 last_address_ = address;
44 run_loop_.Quit();
45 }
46
[email protected]e758d4c2014-08-06 16:48:1647 void VerifyCanSendAndReceive(rtc::AsyncPacketSocket* sender) {
[email protected]5dc79fd6152014-06-18 10:05:2748 // UDP packets may be lost, so we have to retry sending it more than once.
49 const int kMaxAttempts = 3;
50 const base::TimeDelta kAttemptPeriod = base::TimeDelta::FromSeconds(1);
51 std::string test_packet("TEST PACKET");
52 int attempts = 0;
[email protected]e758d4c2014-08-06 16:48:1653 rtc::PacketOptions options;
[email protected]5dc79fd6152014-06-18 10:05:2754 while (last_packet_.empty() && attempts++ < kMaxAttempts) {
55 sender->SendTo(test_packet.data(), test_packet.size(),
56 socket_->GetLocalAddress(), options);
fdoray2ad58be2016-06-22 20:36:1657 message_loop_.task_runner()->PostDelayedTask(
58 FROM_HERE, run_loop_.QuitClosure(), kAttemptPeriod);
[email protected]5dc79fd6152014-06-18 10:05:2759 run_loop_.Run();
60 }
61 EXPECT_EQ(test_packet, last_packet_);
62 EXPECT_EQ(sender->GetLocalAddress(), last_address_);
63 }
64
[email protected]83693232012-07-23 20:40:1465 protected:
[email protected]faea9d2d2013-04-30 03:18:4466 base::MessageLoopForIO message_loop_;
[email protected]83693232012-07-23 20:40:1467 base::RunLoop run_loop_;
68
dcheng0765c492016-04-06 22:41:5369 std::unique_ptr<rtc::PacketSocketFactory> socket_factory_;
70 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
[email protected]83693232012-07-23 20:40:1471
72 std::string last_packet_;
[email protected]e758d4c2014-08-06 16:48:1673 rtc::SocketAddress last_address_;
[email protected]83693232012-07-23 20:40:1474};
75
76TEST_F(ChromiumSocketFactoryTest, SendAndReceive) {
dcheng0765c492016-04-06 22:41:5377 std::unique_ptr<rtc::AsyncPacketSocket> sending_socket(
78 socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0), 0,
79 0));
sergeyuc5f104b2015-01-09 19:33:2480 ASSERT_TRUE(sending_socket.get() != nullptr);
[email protected]83693232012-07-23 20:40:1481 EXPECT_EQ(sending_socket->GetState(),
[email protected]e758d4c2014-08-06 16:48:1682 rtc::AsyncPacketSocket::STATE_BOUND);
[email protected]83693232012-07-23 20:40:1483
[email protected]5dc79fd6152014-06-18 10:05:2784 VerifyCanSendAndReceive(sending_socket.get());
[email protected]83693232012-07-23 20:40:1485}
86
87TEST_F(ChromiumSocketFactoryTest, SetOptions) {
[email protected]e758d4c2014-08-06 16:48:1688 EXPECT_EQ(0, socket_->SetOption(rtc::Socket::OPT_SNDBUF, 4096));
89 EXPECT_EQ(0, socket_->SetOption(rtc::Socket::OPT_RCVBUF, 4096));
[email protected]83693232012-07-23 20:40:1490}
91
92TEST_F(ChromiumSocketFactoryTest, PortRange) {
avi5a080f012015-12-22 23:15:4393 const uint16_t kMinPort = 12400;
94 const uint16_t kMaxPort = 12410;
[email protected]83693232012-07-23 20:40:1495 socket_.reset(socket_factory_->CreateUdpSocket(
[email protected]e758d4c2014-08-06 16:48:1696 rtc::SocketAddress("127.0.0.1", 0), kMaxPort, kMaxPort));
sergeyuc5f104b2015-01-09 19:33:2497 ASSERT_TRUE(socket_.get() != nullptr);
[email protected]e758d4c2014-08-06 16:48:1698 EXPECT_EQ(socket_->GetState(), rtc::AsyncPacketSocket::STATE_BOUND);
[email protected]83693232012-07-23 20:40:1499 EXPECT_GE(socket_->GetLocalAddress().port(), kMinPort);
100 EXPECT_LE(socket_->GetLocalAddress().port(), kMaxPort);
101}
102
[email protected]5dc79fd6152014-06-18 10:05:27103TEST_F(ChromiumSocketFactoryTest, TransientError) {
dcheng0765c492016-04-06 22:41:53104 std::unique_ptr<rtc::AsyncPacketSocket> sending_socket(
105 socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0), 0,
106 0));
[email protected]5dc79fd6152014-06-18 10:05:27107 std::string test_packet("TEST");
108
109 // Try sending a packet to an IPv6 address from a socket that's bound to an
110 // IPv4 address. This send is expected to fail, but the socket should still be
111 // functional.
112 sending_socket->SendTo(test_packet.data(), test_packet.size(),
[email protected]e758d4c2014-08-06 16:48:16113 rtc::SocketAddress("::1", 0),
114 rtc::PacketOptions());
[email protected]5dc79fd6152014-06-18 10:05:27115
116 // Verify that socket is still usable.
117 VerifyCanSendAndReceive(sending_socket.get());
118}
119
[email protected]fd0ca112014-07-17 06:10:20120} // namespace protocol
[email protected]83693232012-07-23 20:40:14121} // namespace remoting