blob: ef302f9026032d04320bd1cc08efe700598054ee [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)),
Yves Gerey665174f2018-06-19 15:03:05 +020057 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) {}
tommi5ce1a2a2016-05-14 03:19:31 -070058
deadbeef5c3c1042017-08-04 15:01:57 -070059 rtc::Network* MakeNetwork(const SocketAddress& addr) {
60 networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32);
61 networks_.back().AddIP(addr.ipaddr());
62 return &networks_.back();
tommi5ce1a2a2016-05-14 03:19:31 -070063 }
64
deadbeef5c3c1042017-08-04 15:01:57 -070065 std::unique_ptr<TCPPort> CreateTCPPort(const SocketAddress& addr) {
66 return std::unique_ptr<TCPPort>(
67 TCPPort::Create(&main_, &socket_factory_, MakeNetwork(addr), 0, 0,
68 username_, password_, true));
tommi5ce1a2a2016-05-14 03:19:31 -070069 }
70
deadbeef5c3c1042017-08-04 15:01:57 -070071 std::unique_ptr<TCPPort> CreateTCPPort(rtc::Network* network) {
72 return std::unique_ptr<TCPPort>(TCPPort::Create(
73 &main_, &socket_factory_, network, 0, 0, username_, password_, true));
tommi5ce1a2a2016-05-14 03:19:31 -070074 }
75
76 protected:
deadbeef5c3c1042017-08-04 15:01:57 -070077 // When a "create port" helper method is called with an IP, we create a
78 // Network with that IP and add it to this list. Using a list instead of a
79 // vector so that when it grows, pointers aren't invalidated.
80 std::list<rtc::Network> networks_;
kwibergfd8be342016-05-14 19:44:11 -070081 std::unique_ptr<rtc::VirtualSocketServer> ss_;
nisse7eaa4ea2017-05-08 05:25:41 -070082 rtc::AutoSocketServerThread main_;
tommi5ce1a2a2016-05-14 03:19:31 -070083 rtc::BasicPacketSocketFactory socket_factory_;
84 std::string username_;
85 std::string password_;
86};
87
88TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) {
deadbeef5c3c1042017-08-04 15:01:57 -070089 SocketAddress local_address("127.0.0.1", 0);
90 // After calling this, when TCPPort attempts to get a socket bound to
91 // kLocalAddr, it will end up using localhost instead.
92 ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(), local_address.ipaddr());
93 auto local_port = CreateTCPPort(kLocalAddr);
94 auto remote_port = CreateTCPPort(kRemoteAddr);
95 local_port->PrepareAddress();
96 remote_port->PrepareAddress();
97 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
98 Port::ORIGIN_MESSAGE);
tommi5ce1a2a2016-05-14 03:19:31 -070099 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
deadbeef5c3c1042017-08-04 15:01:57 -0700100 // Verify that the socket actually used localhost, otherwise this test isn't
101 // doing what it meant to.
102 ASSERT_EQ(local_address.ipaddr(),
103 local_port->Candidates()[0].address().ipaddr());
104}
105
106// If the address the socket ends up bound to does not match any address of the
107// TCPPort's Network, then the socket should be discarded and no candidates
108// should be signaled. In the context of ICE, where one TCPPort is created for
109// each Network, when this happens it's likely that the unexpected address is
110// associated with some other Network, which another TCPPort is already
111// covering.
112TEST_F(TCPPortTest, TCPPortDiscardedIfBoundAddressDoesNotMatchNetwork) {
113 // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr.
114 ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(),
115 kAlternateLocalAddr.ipaddr());
116
117 // Create ports (local_port is the one whose IP will end up reassigned).
118 auto local_port = CreateTCPPort(kLocalAddr);
119 auto remote_port = CreateTCPPort(kRemoteAddr);
120 local_port->PrepareAddress();
121 remote_port->PrepareAddress();
122
123 // Tell port to create a connection; it should be destroyed when it's
124 // realized that it's using an unexpected address.
125 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
126 Port::ORIGIN_MESSAGE);
127 ConnectionObserver observer(conn);
128 EXPECT_TRUE_WAIT(observer.connection_destroyed(), kTimeout);
129}
130
131// A caveat for the above logic: if the socket ends up bound to one of the IPs
132// associated with the Network, just not the "best" one, this is ok.
133TEST_F(TCPPortTest, TCPPortNotDiscardedIfNotBoundToBestIP) {
134 // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr.
135 ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(),
136 kAlternateLocalAddr.ipaddr());
137
138 // Set up a network with kLocalAddr1 as the "best" IP, and kAlternateLocalAddr
139 // as an alternate.
140 rtc::Network* network = MakeNetwork(kLocalAddr);
141 network->AddIP(kAlternateLocalAddr.ipaddr());
142 ASSERT_EQ(kLocalAddr.ipaddr(), network->GetBestIP());
143
144 // Create ports (using our special 2-IP Network for local_port).
145 auto local_port = CreateTCPPort(network);
146 auto remote_port = CreateTCPPort(kRemoteAddr);
147 local_port->PrepareAddress();
148 remote_port->PrepareAddress();
149
150 // Expect connection to succeed.
151 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
152 Port::ORIGIN_MESSAGE);
153 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
154
155 // Verify that the socket actually used the alternate address, otherwise this
156 // test isn't doing what it meant to.
157 ASSERT_EQ(kAlternateLocalAddr.ipaddr(),
158 local_port->Candidates()[0].address().ipaddr());
tommi5ce1a2a2016-05-14 03:19:31 -0700159}
deadbeef06878292017-04-21 14:22:23 -0700160
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800161// Regression test for crbug.com/webrtc/8972, caused by buggy comparison
162// between rtc::IPAddress and rtc::InterfaceAddress.
163TEST_F(TCPPortTest, TCPPortNotDiscardedIfBoundToTemporaryIP) {
164 networks_.emplace_back("unittest", "unittest", kLocalIPv6Addr.ipaddr(), 32);
165 networks_.back().AddIP(rtc::InterfaceAddress(
166 kLocalIPv6Addr.ipaddr(), rtc::IPV6_ADDRESS_FLAG_TEMPORARY));
167
168 auto local_port = CreateTCPPort(&networks_.back());
169 auto remote_port = CreateTCPPort(kRemoteIPv6Addr);
170 local_port->PrepareAddress();
171 remote_port->PrepareAddress();
172
173 // Connection should succeed if the port isn't discarded.
174 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
175 Port::ORIGIN_MESSAGE);
176 ASSERT_NE(nullptr, conn);
177 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
178}
179
deadbeef06878292017-04-21 14:22:23 -0700180class SentPacketCounter : public sigslot::has_slots<> {
181 public:
Steve Anton6c38cc72017-11-29 10:25:58 -0800182 explicit SentPacketCounter(TCPPort* p) {
deadbeef06878292017-04-21 14:22:23 -0700183 p->SignalSentPacket.connect(this, &SentPacketCounter::OnSentPacket);
184 }
185
186 int sent_packets() const { return sent_packets_; }
187
188 private:
189 void OnSentPacket(const rtc::SentPacket&) { ++sent_packets_; }
190
191 int sent_packets_ = 0;
192};
193
194// Test that SignalSentPacket is fired when a packet is successfully sent, for
195// both TCP client and server sockets.
196TEST_F(TCPPortTest, SignalSentPacket) {
197 std::unique_ptr<TCPPort> client(CreateTCPPort(kLocalAddr));
198 std::unique_ptr<TCPPort> server(CreateTCPPort(kRemoteAddr));
199 client->SetIceRole(cricket::ICEROLE_CONTROLLING);
200 server->SetIceRole(cricket::ICEROLE_CONTROLLED);
201 client->PrepareAddress();
202 server->PrepareAddress();
203
204 Connection* client_conn =
205 client->CreateConnection(server->Candidates()[0], Port::ORIGIN_MESSAGE);
206 ASSERT_NE(nullptr, client_conn);
207 ASSERT_TRUE_WAIT(client_conn->connected(), kTimeout);
208
209 // Need to get the port of the actual outgoing socket, not the server socket..
210 cricket::Candidate client_candidate = client->Candidates()[0];
211 client_candidate.set_address(static_cast<cricket::TCPConnection*>(client_conn)
212 ->socket()
213 ->GetLocalAddress());
214 Connection* server_conn =
215 server->CreateConnection(client_candidate, Port::ORIGIN_THIS_PORT);
216 ASSERT_NE(nullptr, server_conn);
217 ASSERT_TRUE_WAIT(server_conn->connected(), kTimeout);
218
219 client_conn->Ping(rtc::TimeMillis());
220 server_conn->Ping(rtc::TimeMillis());
221 ASSERT_TRUE_WAIT(client_conn->writable(), kTimeout);
222 ASSERT_TRUE_WAIT(server_conn->writable(), kTimeout);
223
224 SentPacketCounter client_counter(client.get());
225 SentPacketCounter server_counter(server.get());
226 static const char kData[] = "hello";
227 for (int i = 0; i < 10; ++i) {
228 client_conn->Send(&kData, sizeof(kData), rtc::PacketOptions());
229 server_conn->Send(&kData, sizeof(kData), rtc::PacketOptions());
230 }
231 EXPECT_EQ_WAIT(10, client_counter.sent_packets(), kTimeout);
232 EXPECT_EQ_WAIT(10, server_counter.sent_packets(), kTimeout);
233}