tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 11 | #include <list> |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 12 | #include <memory> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "p2p/base/basicpacketsocketfactory.h" |
| 15 | #include "p2p/base/tcpport.h" |
| 16 | #include "rtc_base/gunit.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame^] | 17 | #include "rtc_base/helpers.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/thread.h" |
| 19 | #include "rtc_base/virtualsocketserver.h" |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 20 | |
| 21 | using rtc::SocketAddress; |
| 22 | using cricket::Connection; |
| 23 | using cricket::Port; |
| 24 | using cricket::TCPPort; |
| 25 | using cricket::ICE_UFRAG_LENGTH; |
| 26 | using cricket::ICE_PWD_LENGTH; |
| 27 | |
| 28 | static int kTimeout = 1000; |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 29 | static const SocketAddress kLocalAddr("11.11.11.11", 0); |
Taylor Brandstetter | 01cb5f2 | 2018-03-07 15:49:32 -0800 | [diff] [blame] | 30 | static const SocketAddress kLocalIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3", |
| 31 | 0); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 32 | static const SocketAddress kAlternateLocalAddr("1.2.3.4", 0); |
| 33 | static const SocketAddress kRemoteAddr("22.22.22.22", 0); |
Taylor Brandstetter | 01cb5f2 | 2018-03-07 15:49:32 -0800 | [diff] [blame] | 34 | static const SocketAddress kRemoteIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c4", |
| 35 | 0); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 36 | |
| 37 | class ConnectionObserver : public sigslot::has_slots<> { |
| 38 | public: |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 39 | explicit ConnectionObserver(Connection* conn) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 40 | conn->SignalDestroyed.connect(this, &ConnectionObserver::OnDestroyed); |
| 41 | } |
| 42 | |
| 43 | bool connection_destroyed() { return connection_destroyed_; } |
| 44 | |
| 45 | private: |
| 46 | void OnDestroyed(Connection*) { connection_destroyed_ = true; } |
| 47 | |
| 48 | bool connection_destroyed_ = false; |
| 49 | }; |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 50 | |
| 51 | class TCPPortTest : public testing::Test, public sigslot::has_slots<> { |
| 52 | public: |
| 53 | TCPPortTest() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 54 | : ss_(new rtc::VirtualSocketServer()), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 55 | main_(ss_.get()), |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 56 | socket_factory_(rtc::Thread::Current()), |
| 57 | username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) {} |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 59 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 60 | rtc::Network* MakeNetwork(const SocketAddress& addr) { |
| 61 | networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32); |
| 62 | networks_.back().AddIP(addr.ipaddr()); |
| 63 | return &networks_.back(); |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 64 | } |
| 65 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 66 | std::unique_ptr<TCPPort> CreateTCPPort(const SocketAddress& addr) { |
| 67 | return std::unique_ptr<TCPPort>( |
| 68 | TCPPort::Create(&main_, &socket_factory_, MakeNetwork(addr), 0, 0, |
| 69 | username_, password_, true)); |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 70 | } |
| 71 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 72 | std::unique_ptr<TCPPort> CreateTCPPort(rtc::Network* network) { |
| 73 | return std::unique_ptr<TCPPort>(TCPPort::Create( |
| 74 | &main_, &socket_factory_, network, 0, 0, username_, password_, true)); |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | protected: |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 78 | // When a "create port" helper method is called with an IP, we create a |
| 79 | // Network with that IP and add it to this list. Using a list instead of a |
| 80 | // vector so that when it grows, pointers aren't invalidated. |
| 81 | std::list<rtc::Network> networks_; |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 82 | std::unique_ptr<rtc::VirtualSocketServer> ss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 83 | rtc::AutoSocketServerThread main_; |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 84 | rtc::BasicPacketSocketFactory socket_factory_; |
| 85 | std::string username_; |
| 86 | std::string password_; |
| 87 | }; |
| 88 | |
| 89 | TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 90 | SocketAddress local_address("127.0.0.1", 0); |
| 91 | // After calling this, when TCPPort attempts to get a socket bound to |
| 92 | // kLocalAddr, it will end up using localhost instead. |
| 93 | ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(), local_address.ipaddr()); |
| 94 | auto local_port = CreateTCPPort(kLocalAddr); |
| 95 | auto remote_port = CreateTCPPort(kRemoteAddr); |
| 96 | local_port->PrepareAddress(); |
| 97 | remote_port->PrepareAddress(); |
| 98 | Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0], |
| 99 | Port::ORIGIN_MESSAGE); |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 100 | EXPECT_TRUE_WAIT(conn->connected(), kTimeout); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 101 | // Verify that the socket actually used localhost, otherwise this test isn't |
| 102 | // doing what it meant to. |
| 103 | ASSERT_EQ(local_address.ipaddr(), |
| 104 | local_port->Candidates()[0].address().ipaddr()); |
| 105 | } |
| 106 | |
| 107 | // If the address the socket ends up bound to does not match any address of the |
| 108 | // TCPPort's Network, then the socket should be discarded and no candidates |
| 109 | // should be signaled. In the context of ICE, where one TCPPort is created for |
| 110 | // each Network, when this happens it's likely that the unexpected address is |
| 111 | // associated with some other Network, which another TCPPort is already |
| 112 | // covering. |
| 113 | TEST_F(TCPPortTest, TCPPortDiscardedIfBoundAddressDoesNotMatchNetwork) { |
| 114 | // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr. |
| 115 | ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(), |
| 116 | kAlternateLocalAddr.ipaddr()); |
| 117 | |
| 118 | // Create ports (local_port is the one whose IP will end up reassigned). |
| 119 | auto local_port = CreateTCPPort(kLocalAddr); |
| 120 | auto remote_port = CreateTCPPort(kRemoteAddr); |
| 121 | local_port->PrepareAddress(); |
| 122 | remote_port->PrepareAddress(); |
| 123 | |
| 124 | // Tell port to create a connection; it should be destroyed when it's |
| 125 | // realized that it's using an unexpected address. |
| 126 | Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0], |
| 127 | Port::ORIGIN_MESSAGE); |
| 128 | ConnectionObserver observer(conn); |
| 129 | EXPECT_TRUE_WAIT(observer.connection_destroyed(), kTimeout); |
| 130 | } |
| 131 | |
| 132 | // A caveat for the above logic: if the socket ends up bound to one of the IPs |
| 133 | // associated with the Network, just not the "best" one, this is ok. |
| 134 | TEST_F(TCPPortTest, TCPPortNotDiscardedIfNotBoundToBestIP) { |
| 135 | // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr. |
| 136 | ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(), |
| 137 | kAlternateLocalAddr.ipaddr()); |
| 138 | |
| 139 | // Set up a network with kLocalAddr1 as the "best" IP, and kAlternateLocalAddr |
| 140 | // as an alternate. |
| 141 | rtc::Network* network = MakeNetwork(kLocalAddr); |
| 142 | network->AddIP(kAlternateLocalAddr.ipaddr()); |
| 143 | ASSERT_EQ(kLocalAddr.ipaddr(), network->GetBestIP()); |
| 144 | |
| 145 | // Create ports (using our special 2-IP Network for local_port). |
| 146 | auto local_port = CreateTCPPort(network); |
| 147 | auto remote_port = CreateTCPPort(kRemoteAddr); |
| 148 | local_port->PrepareAddress(); |
| 149 | remote_port->PrepareAddress(); |
| 150 | |
| 151 | // Expect connection to succeed. |
| 152 | Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0], |
| 153 | Port::ORIGIN_MESSAGE); |
| 154 | EXPECT_TRUE_WAIT(conn->connected(), kTimeout); |
| 155 | |
| 156 | // Verify that the socket actually used the alternate address, otherwise this |
| 157 | // test isn't doing what it meant to. |
| 158 | ASSERT_EQ(kAlternateLocalAddr.ipaddr(), |
| 159 | local_port->Candidates()[0].address().ipaddr()); |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 160 | } |
deadbeef | 0687829 | 2017-04-21 14:22:23 -0700 | [diff] [blame] | 161 | |
Taylor Brandstetter | 01cb5f2 | 2018-03-07 15:49:32 -0800 | [diff] [blame] | 162 | // Regression test for crbug.com/webrtc/8972, caused by buggy comparison |
| 163 | // between rtc::IPAddress and rtc::InterfaceAddress. |
| 164 | TEST_F(TCPPortTest, TCPPortNotDiscardedIfBoundToTemporaryIP) { |
| 165 | networks_.emplace_back("unittest", "unittest", kLocalIPv6Addr.ipaddr(), 32); |
| 166 | networks_.back().AddIP(rtc::InterfaceAddress( |
| 167 | kLocalIPv6Addr.ipaddr(), rtc::IPV6_ADDRESS_FLAG_TEMPORARY)); |
| 168 | |
| 169 | auto local_port = CreateTCPPort(&networks_.back()); |
| 170 | auto remote_port = CreateTCPPort(kRemoteIPv6Addr); |
| 171 | local_port->PrepareAddress(); |
| 172 | remote_port->PrepareAddress(); |
| 173 | |
| 174 | // Connection should succeed if the port isn't discarded. |
| 175 | Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0], |
| 176 | Port::ORIGIN_MESSAGE); |
| 177 | ASSERT_NE(nullptr, conn); |
| 178 | EXPECT_TRUE_WAIT(conn->connected(), kTimeout); |
| 179 | } |
| 180 | |
deadbeef | 0687829 | 2017-04-21 14:22:23 -0700 | [diff] [blame] | 181 | class SentPacketCounter : public sigslot::has_slots<> { |
| 182 | public: |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 183 | explicit SentPacketCounter(TCPPort* p) { |
deadbeef | 0687829 | 2017-04-21 14:22:23 -0700 | [diff] [blame] | 184 | p->SignalSentPacket.connect(this, &SentPacketCounter::OnSentPacket); |
| 185 | } |
| 186 | |
| 187 | int sent_packets() const { return sent_packets_; } |
| 188 | |
| 189 | private: |
| 190 | void OnSentPacket(const rtc::SentPacket&) { ++sent_packets_; } |
| 191 | |
| 192 | int sent_packets_ = 0; |
| 193 | }; |
| 194 | |
| 195 | // Test that SignalSentPacket is fired when a packet is successfully sent, for |
| 196 | // both TCP client and server sockets. |
| 197 | TEST_F(TCPPortTest, SignalSentPacket) { |
| 198 | std::unique_ptr<TCPPort> client(CreateTCPPort(kLocalAddr)); |
| 199 | std::unique_ptr<TCPPort> server(CreateTCPPort(kRemoteAddr)); |
| 200 | client->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 201 | server->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 202 | client->PrepareAddress(); |
| 203 | server->PrepareAddress(); |
| 204 | |
| 205 | Connection* client_conn = |
| 206 | client->CreateConnection(server->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 207 | ASSERT_NE(nullptr, client_conn); |
| 208 | ASSERT_TRUE_WAIT(client_conn->connected(), kTimeout); |
| 209 | |
| 210 | // Need to get the port of the actual outgoing socket, not the server socket.. |
| 211 | cricket::Candidate client_candidate = client->Candidates()[0]; |
| 212 | client_candidate.set_address(static_cast<cricket::TCPConnection*>(client_conn) |
| 213 | ->socket() |
| 214 | ->GetLocalAddress()); |
| 215 | Connection* server_conn = |
| 216 | server->CreateConnection(client_candidate, Port::ORIGIN_THIS_PORT); |
| 217 | ASSERT_NE(nullptr, server_conn); |
| 218 | ASSERT_TRUE_WAIT(server_conn->connected(), kTimeout); |
| 219 | |
| 220 | client_conn->Ping(rtc::TimeMillis()); |
| 221 | server_conn->Ping(rtc::TimeMillis()); |
| 222 | ASSERT_TRUE_WAIT(client_conn->writable(), kTimeout); |
| 223 | ASSERT_TRUE_WAIT(server_conn->writable(), kTimeout); |
| 224 | |
| 225 | SentPacketCounter client_counter(client.get()); |
| 226 | SentPacketCounter server_counter(server.get()); |
| 227 | static const char kData[] = "hello"; |
| 228 | for (int i = 0; i < 10; ++i) { |
| 229 | client_conn->Send(&kData, sizeof(kData), rtc::PacketOptions()); |
| 230 | server_conn->Send(&kData, sizeof(kData), rtc::PacketOptions()); |
| 231 | } |
| 232 | EXPECT_EQ_WAIT(10, client_counter.sent_packets(), kTimeout); |
| 233 | EXPECT_EQ_WAIT(10, server_counter.sent_packets(), kTimeout); |
| 234 | } |