blob: b5b1f64e88b92d1c0f46605fbcac0c0092435468 [file] [log] [blame]
tommi5ce1a2a2016-05-14 10:19:311/*
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
deadbeef5c3c1042017-08-04 22:01:5711#include <list>
kwibergfd8be342016-05-15 02:44:1112#include <memory>
Yves Gerey3e707812018-11-28 15:47:4913#include <vector>
kwibergfd8be342016-05-15 02:44:1114
Steve Anton10542f22019-01-11 17:11:0015#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 Bonadei92ea95e2017-09-15 04:47:3119#include "rtc_base/gunit.h"
Yves Gerey2e00abc2018-10-05 13:39:2420#include "rtc_base/helpers.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/ip_address.h"
Yves Gerey3e707812018-11-28 15:47:4922#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/time_utils.h"
25#include "rtc_base/virtual_socket_server.h"
Yves Gerey3e707812018-11-28 15:47:4926#include "test/gtest.h"
tommi5ce1a2a2016-05-14 10:19:3127
28using rtc::SocketAddress;
29using cricket::Connection;
30using cricket::Port;
31using cricket::TCPPort;
32using cricket::ICE_UFRAG_LENGTH;
33using cricket::ICE_PWD_LENGTH;
34
35static int kTimeout = 1000;
deadbeef5c3c1042017-08-04 22:01:5736static const SocketAddress kLocalAddr("11.11.11.11", 0);
Taylor Brandstetter01cb5f22018-03-07 23:49:3237static const SocketAddress kLocalIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
38 0);
deadbeef5c3c1042017-08-04 22:01:5739static const SocketAddress kAlternateLocalAddr("1.2.3.4", 0);
40static const SocketAddress kRemoteAddr("22.22.22.22", 0);
Taylor Brandstetter01cb5f22018-03-07 23:49:3241static const SocketAddress kRemoteIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c4",
42 0);
deadbeef5c3c1042017-08-04 22:01:5743
44class ConnectionObserver : public sigslot::has_slots<> {
45 public:
Steve Anton6c38cc72017-11-29 18:25:5846 explicit ConnectionObserver(Connection* conn) {
deadbeef5c3c1042017-08-04 22:01:5747 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};
tommi5ce1a2a2016-05-14 10:19:3157
58class TCPPortTest : public testing::Test, public sigslot::has_slots<> {
59 public:
60 TCPPortTest()
deadbeef98e186c2017-05-17 01:00:0661 : ss_(new rtc::VirtualSocketServer()),
nisse7eaa4ea2017-05-08 12:25:4162 main_(ss_.get()),
tommi5ce1a2a2016-05-14 10:19:3163 socket_factory_(rtc::Thread::Current()),
64 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)),
Yves Gerey665174f2018-06-19 13:03:0565 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) {}
tommi5ce1a2a2016-05-14 10:19:3166
deadbeef5c3c1042017-08-04 22:01:5767 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();
tommi5ce1a2a2016-05-14 10:19:3171 }
72
deadbeef5c3c1042017-08-04 22:01:5773 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));
tommi5ce1a2a2016-05-14 10:19:3177 }
78
deadbeef5c3c1042017-08-04 22:01:5779 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));
tommi5ce1a2a2016-05-14 10:19:3182 }
83
84 protected:
deadbeef5c3c1042017-08-04 22:01:5785 // 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_;
kwibergfd8be342016-05-15 02:44:1189 std::unique_ptr<rtc::VirtualSocketServer> ss_;
nisse7eaa4ea2017-05-08 12:25:4190 rtc::AutoSocketServerThread main_;
tommi5ce1a2a2016-05-14 10:19:3191 rtc::BasicPacketSocketFactory socket_factory_;
92 std::string username_;
93 std::string password_;
94};
95
96TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) {
deadbeef5c3c1042017-08-04 22:01:5797 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);
tommi5ce1a2a2016-05-14 10:19:31107 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
deadbeef5c3c1042017-08-04 22:01:57108 // 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.
120TEST_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.
141TEST_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());
tommi5ce1a2a2016-05-14 10:19:31167}
deadbeef06878292017-04-21 21:22:23168
Taylor Brandstetter01cb5f22018-03-07 23:49:32169// Regression test for crbug.com/webrtc/8972, caused by buggy comparison
170// between rtc::IPAddress and rtc::InterfaceAddress.
171TEST_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
deadbeef06878292017-04-21 21:22:23188class SentPacketCounter : public sigslot::has_slots<> {
189 public:
Steve Anton6c38cc72017-11-29 18:25:58190 explicit SentPacketCounter(TCPPort* p) {
deadbeef06878292017-04-21 21:22:23191 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.
204TEST_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}