[email protected] | fd0ca11 | 2014-07-17 06:10:20 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | fd0ca11 | 2014-07-17 06:10:20 | [diff] [blame] | 5 | #include "remoting/protocol/chromium_socket_factory.h" |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 6 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | f368eeb | 2013-07-17 23:53:32 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 13 | #include "base/run_loop.h" |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 14 | #include "base/single_thread_task_runner.h" |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 15 | #include "testing/gmock/include/gmock/gmock.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
Henrik Kjellander | 5f427823 | 2017-06-30 09:24:47 | [diff] [blame] | 17 | #include "third_party/webrtc/rtc_base/asyncpacketsocket.h" |
| 18 | #include "third_party/webrtc/rtc_base/socketaddress.h" |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 19 | |
| 20 | namespace remoting { |
[email protected] | fd0ca11 | 2014-07-17 06:10:20 | [diff] [blame] | 21 | namespace protocol { |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 22 | |
| 23 | class ChromiumSocketFactoryTest : public testing::Test, |
| 24 | public sigslot::has_slots<> { |
| 25 | public: |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 26 | void SetUp() override { |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 27 | socket_factory_.reset(new ChromiumPacketSocketFactory()); |
| 28 | |
| 29 | socket_.reset(socket_factory_->CreateUdpSocket( |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 30 | rtc::SocketAddress("127.0.0.1", 0), 0, 0)); |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 31 | ASSERT_TRUE(socket_.get() != nullptr); |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 32 | EXPECT_EQ(socket_->GetState(), rtc::AsyncPacketSocket::STATE_BOUND); |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 33 | socket_->SignalReadPacket.connect( |
| 34 | this, &ChromiumSocketFactoryTest::OnPacket); |
| 35 | } |
| 36 | |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 37 | void OnPacket(rtc::AsyncPacketSocket* socket, |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 38 | const char* data, size_t size, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 39 | const rtc::SocketAddress& address, |
| 40 | const rtc::PacketTime& packet_time) { |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 41 | EXPECT_EQ(socket, socket_.get()); |
| 42 | last_packet_.assign(data, data + size); |
| 43 | last_address_ = address; |
| 44 | run_loop_.Quit(); |
| 45 | } |
| 46 | |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 47 | void VerifyCanSendAndReceive(rtc::AsyncPacketSocket* sender) { |
[email protected] | 5dc79fd615 | 2014-06-18 10:05:27 | [diff] [blame] | 48 | // 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] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 53 | rtc::PacketOptions options; |
[email protected] | 5dc79fd615 | 2014-06-18 10:05:27 | [diff] [blame] | 54 | while (last_packet_.empty() && attempts++ < kMaxAttempts) { |
| 55 | sender->SendTo(test_packet.data(), test_packet.size(), |
| 56 | socket_->GetLocalAddress(), options); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 57 | message_loop_.task_runner()->PostDelayedTask( |
| 58 | FROM_HERE, run_loop_.QuitClosure(), kAttemptPeriod); |
[email protected] | 5dc79fd615 | 2014-06-18 10:05:27 | [diff] [blame] | 59 | run_loop_.Run(); |
| 60 | } |
| 61 | EXPECT_EQ(test_packet, last_packet_); |
| 62 | EXPECT_EQ(sender->GetLocalAddress(), last_address_); |
| 63 | } |
| 64 | |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 65 | protected: |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 66 | base::MessageLoopForIO message_loop_; |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 67 | base::RunLoop run_loop_; |
| 68 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 69 | std::unique_ptr<rtc::PacketSocketFactory> socket_factory_; |
| 70 | std::unique_ptr<rtc::AsyncPacketSocket> socket_; |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 71 | |
| 72 | std::string last_packet_; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 73 | rtc::SocketAddress last_address_; |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | TEST_F(ChromiumSocketFactoryTest, SendAndReceive) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 77 | std::unique_ptr<rtc::AsyncPacketSocket> sending_socket( |
| 78 | socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0), 0, |
| 79 | 0)); |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 80 | ASSERT_TRUE(sending_socket.get() != nullptr); |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 81 | EXPECT_EQ(sending_socket->GetState(), |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 82 | rtc::AsyncPacketSocket::STATE_BOUND); |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 83 | |
[email protected] | 5dc79fd615 | 2014-06-18 10:05:27 | [diff] [blame] | 84 | VerifyCanSendAndReceive(sending_socket.get()); |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST_F(ChromiumSocketFactoryTest, SetOptions) { |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 88 | EXPECT_EQ(0, socket_->SetOption(rtc::Socket::OPT_SNDBUF, 4096)); |
| 89 | EXPECT_EQ(0, socket_->SetOption(rtc::Socket::OPT_RCVBUF, 4096)); |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(ChromiumSocketFactoryTest, PortRange) { |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 93 | const uint16_t kMinPort = 12400; |
| 94 | const uint16_t kMaxPort = 12410; |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 95 | socket_.reset(socket_factory_->CreateUdpSocket( |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 96 | rtc::SocketAddress("127.0.0.1", 0), kMaxPort, kMaxPort)); |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 97 | ASSERT_TRUE(socket_.get() != nullptr); |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 98 | EXPECT_EQ(socket_->GetState(), rtc::AsyncPacketSocket::STATE_BOUND); |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 99 | EXPECT_GE(socket_->GetLocalAddress().port(), kMinPort); |
| 100 | EXPECT_LE(socket_->GetLocalAddress().port(), kMaxPort); |
| 101 | } |
| 102 | |
[email protected] | 5dc79fd615 | 2014-06-18 10:05:27 | [diff] [blame] | 103 | TEST_F(ChromiumSocketFactoryTest, TransientError) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 104 | std::unique_ptr<rtc::AsyncPacketSocket> sending_socket( |
| 105 | socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0), 0, |
| 106 | 0)); |
[email protected] | 5dc79fd615 | 2014-06-18 10:05:27 | [diff] [blame] | 107 | 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] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 113 | rtc::SocketAddress("::1", 0), |
| 114 | rtc::PacketOptions()); |
[email protected] | 5dc79fd615 | 2014-06-18 10:05:27 | [diff] [blame] | 115 | |
| 116 | // Verify that socket is still usable. |
| 117 | VerifyCanSendAndReceive(sending_socket.get()); |
| 118 | } |
| 119 | |
[email protected] | fd0ca11 | 2014-07-17 06:10:20 | [diff] [blame] | 120 | } // namespace protocol |
[email protected] | 8369323 | 2012-07-23 20:40:14 | [diff] [blame] | 121 | } // namespace remoting |