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