blob: 6d3fe32fe314fd3a25ea4d2c37e392a5785089ab [file] [log] [blame]
tommi5ce1a2a2016-05-14 03:19:31 -07001/*
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 15:01:57 -070011#include <list>
kwibergfd8be342016-05-14 19:44:11 -070012#include <memory>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/basicpacketsocketfactory.h"
15#include "p2p/base/tcpport.h"
16#include "rtc_base/gunit.h"
17#include "rtc_base/thread.h"
18#include "rtc_base/virtualsocketserver.h"
tommi5ce1a2a2016-05-14 03:19:31 -070019
20using rtc::SocketAddress;
21using cricket::Connection;
22using cricket::Port;
23using cricket::TCPPort;
24using cricket::ICE_UFRAG_LENGTH;
25using cricket::ICE_PWD_LENGTH;
26
27static int kTimeout = 1000;
deadbeef5c3c1042017-08-04 15:01:57 -070028static const SocketAddress kLocalAddr("11.11.11.11", 0);
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -080029static const SocketAddress kLocalIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
30 0);
deadbeef5c3c1042017-08-04 15:01:57 -070031static const SocketAddress kAlternateLocalAddr("1.2.3.4", 0);
32static const SocketAddress kRemoteAddr("22.22.22.22", 0);
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -080033static const SocketAddress kRemoteIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c4",
34 0);
deadbeef5c3c1042017-08-04 15:01:57 -070035
36class ConnectionObserver : public sigslot::has_slots<> {
37 public:
Steve Anton6c38cc72017-11-29 10:25:58 -080038 explicit ConnectionObserver(Connection* conn) {
deadbeef5c3c1042017-08-04 15:01:57 -070039 conn->SignalDestroyed.connect(this, &ConnectionObserver::OnDestroyed);
40 }
41
42 bool connection_destroyed() { return connection_destroyed_; }
43
44 private:
45 void OnDestroyed(Connection*) { connection_destroyed_ = true; }
46
47 bool connection_destroyed_ = false;
48};
tommi5ce1a2a2016-05-14 03:19:31 -070049
50class TCPPortTest : public testing::Test, public sigslot::has_slots<> {
51 public:
52 TCPPortTest()
deadbeef98e186c2017-05-16 18:00:06 -070053 : ss_(new rtc::VirtualSocketServer()),
nisse7eaa4ea2017-05-08 05:25:41 -070054 main_(ss_.get()),
tommi5ce1a2a2016-05-14 03:19:31 -070055 socket_factory_(rtc::Thread::Current()),
56 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)),
57 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) {
tommi5ce1a2a2016-05-14 03:19:31 -070058 }
59
deadbeef5c3c1042017-08-04 15:01:57 -070060 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();
tommi5ce1a2a2016-05-14 03:19:31 -070064 }
65
deadbeef5c3c1042017-08-04 15:01:57 -070066 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));
tommi5ce1a2a2016-05-14 03:19:31 -070070 }
71
deadbeef5c3c1042017-08-04 15:01:57 -070072 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));
tommi5ce1a2a2016-05-14 03:19:31 -070075 }
76
77 protected:
deadbeef5c3c1042017-08-04 15:01:57 -070078 // 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_;
kwibergfd8be342016-05-14 19:44:11 -070082 std::unique_ptr<rtc::VirtualSocketServer> ss_;
nisse7eaa4ea2017-05-08 05:25:41 -070083 rtc::AutoSocketServerThread main_;
tommi5ce1a2a2016-05-14 03:19:31 -070084 rtc::BasicPacketSocketFactory socket_factory_;
85 std::string username_;
86 std::string password_;
87};
88
89TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) {
deadbeef5c3c1042017-08-04 15:01:57 -070090 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);
tommi5ce1a2a2016-05-14 03:19:31 -0700100 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
deadbeef5c3c1042017-08-04 15:01:57 -0700101 // 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.
113TEST_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.
134TEST_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());
tommi5ce1a2a2016-05-14 03:19:31 -0700160}
deadbeef06878292017-04-21 14:22:23 -0700161
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800162// Regression test for crbug.com/webrtc/8972, caused by buggy comparison
163// between rtc::IPAddress and rtc::InterfaceAddress.
164TEST_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
deadbeef06878292017-04-21 14:22:23 -0700181class SentPacketCounter : public sigslot::has_slots<> {
182 public:
Steve Anton6c38cc72017-11-29 10:25:58 -0800183 explicit SentPacketCounter(TCPPort* p) {
deadbeef06878292017-04-21 14:22:23 -0700184 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.
197TEST_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}