blob: 7c426c0b93882a20ce086f9bdea6e37fbd05bb10 [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);
29static const SocketAddress kAlternateLocalAddr("1.2.3.4", 0);
30static const SocketAddress kRemoteAddr("22.22.22.22", 0);
31
32class ConnectionObserver : public sigslot::has_slots<> {
33 public:
34 ConnectionObserver(Connection* conn) {
35 conn->SignalDestroyed.connect(this, &ConnectionObserver::OnDestroyed);
36 }
37
38 bool connection_destroyed() { return connection_destroyed_; }
39
40 private:
41 void OnDestroyed(Connection*) { connection_destroyed_ = true; }
42
43 bool connection_destroyed_ = false;
44};
tommi5ce1a2a2016-05-14 03:19:31 -070045
46class TCPPortTest : public testing::Test, public sigslot::has_slots<> {
47 public:
48 TCPPortTest()
deadbeef98e186c2017-05-16 18:00:06 -070049 : ss_(new rtc::VirtualSocketServer()),
nisse7eaa4ea2017-05-08 05:25:41 -070050 main_(ss_.get()),
tommi5ce1a2a2016-05-14 03:19:31 -070051 socket_factory_(rtc::Thread::Current()),
52 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)),
53 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) {
tommi5ce1a2a2016-05-14 03:19:31 -070054 }
55
deadbeef5c3c1042017-08-04 15:01:57 -070056 rtc::Network* MakeNetwork(const SocketAddress& addr) {
57 networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32);
58 networks_.back().AddIP(addr.ipaddr());
59 return &networks_.back();
tommi5ce1a2a2016-05-14 03:19:31 -070060 }
61
deadbeef5c3c1042017-08-04 15:01:57 -070062 std::unique_ptr<TCPPort> CreateTCPPort(const SocketAddress& addr) {
63 return std::unique_ptr<TCPPort>(
64 TCPPort::Create(&main_, &socket_factory_, MakeNetwork(addr), 0, 0,
65 username_, password_, true));
tommi5ce1a2a2016-05-14 03:19:31 -070066 }
67
deadbeef5c3c1042017-08-04 15:01:57 -070068 std::unique_ptr<TCPPort> CreateTCPPort(rtc::Network* network) {
69 return std::unique_ptr<TCPPort>(TCPPort::Create(
70 &main_, &socket_factory_, network, 0, 0, username_, password_, true));
tommi5ce1a2a2016-05-14 03:19:31 -070071 }
72
73 protected:
deadbeef5c3c1042017-08-04 15:01:57 -070074 // When a "create port" helper method is called with an IP, we create a
75 // Network with that IP and add it to this list. Using a list instead of a
76 // vector so that when it grows, pointers aren't invalidated.
77 std::list<rtc::Network> networks_;
kwibergfd8be342016-05-14 19:44:11 -070078 std::unique_ptr<rtc::VirtualSocketServer> ss_;
nisse7eaa4ea2017-05-08 05:25:41 -070079 rtc::AutoSocketServerThread main_;
tommi5ce1a2a2016-05-14 03:19:31 -070080 rtc::BasicPacketSocketFactory socket_factory_;
81 std::string username_;
82 std::string password_;
83};
84
85TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) {
deadbeef5c3c1042017-08-04 15:01:57 -070086 SocketAddress local_address("127.0.0.1", 0);
87 // After calling this, when TCPPort attempts to get a socket bound to
88 // kLocalAddr, it will end up using localhost instead.
89 ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(), local_address.ipaddr());
90 auto local_port = CreateTCPPort(kLocalAddr);
91 auto remote_port = CreateTCPPort(kRemoteAddr);
92 local_port->PrepareAddress();
93 remote_port->PrepareAddress();
94 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
95 Port::ORIGIN_MESSAGE);
tommi5ce1a2a2016-05-14 03:19:31 -070096 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
deadbeef5c3c1042017-08-04 15:01:57 -070097 // Verify that the socket actually used localhost, otherwise this test isn't
98 // doing what it meant to.
99 ASSERT_EQ(local_address.ipaddr(),
100 local_port->Candidates()[0].address().ipaddr());
101}
102
103// If the address the socket ends up bound to does not match any address of the
104// TCPPort's Network, then the socket should be discarded and no candidates
105// should be signaled. In the context of ICE, where one TCPPort is created for
106// each Network, when this happens it's likely that the unexpected address is
107// associated with some other Network, which another TCPPort is already
108// covering.
109TEST_F(TCPPortTest, TCPPortDiscardedIfBoundAddressDoesNotMatchNetwork) {
110 // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr.
111 ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(),
112 kAlternateLocalAddr.ipaddr());
113
114 // Create ports (local_port is the one whose IP will end up reassigned).
115 auto local_port = CreateTCPPort(kLocalAddr);
116 auto remote_port = CreateTCPPort(kRemoteAddr);
117 local_port->PrepareAddress();
118 remote_port->PrepareAddress();
119
120 // Tell port to create a connection; it should be destroyed when it's
121 // realized that it's using an unexpected address.
122 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
123 Port::ORIGIN_MESSAGE);
124 ConnectionObserver observer(conn);
125 EXPECT_TRUE_WAIT(observer.connection_destroyed(), kTimeout);
126}
127
128// A caveat for the above logic: if the socket ends up bound to one of the IPs
129// associated with the Network, just not the "best" one, this is ok.
130TEST_F(TCPPortTest, TCPPortNotDiscardedIfNotBoundToBestIP) {
131 // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr.
132 ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(),
133 kAlternateLocalAddr.ipaddr());
134
135 // Set up a network with kLocalAddr1 as the "best" IP, and kAlternateLocalAddr
136 // as an alternate.
137 rtc::Network* network = MakeNetwork(kLocalAddr);
138 network->AddIP(kAlternateLocalAddr.ipaddr());
139 ASSERT_EQ(kLocalAddr.ipaddr(), network->GetBestIP());
140
141 // Create ports (using our special 2-IP Network for local_port).
142 auto local_port = CreateTCPPort(network);
143 auto remote_port = CreateTCPPort(kRemoteAddr);
144 local_port->PrepareAddress();
145 remote_port->PrepareAddress();
146
147 // Expect connection to succeed.
148 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
149 Port::ORIGIN_MESSAGE);
150 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
151
152 // Verify that the socket actually used the alternate address, otherwise this
153 // test isn't doing what it meant to.
154 ASSERT_EQ(kAlternateLocalAddr.ipaddr(),
155 local_port->Candidates()[0].address().ipaddr());
tommi5ce1a2a2016-05-14 03:19:31 -0700156}
deadbeef06878292017-04-21 14:22:23 -0700157
158class SentPacketCounter : public sigslot::has_slots<> {
159 public:
160 SentPacketCounter(TCPPort* p) {
161 p->SignalSentPacket.connect(this, &SentPacketCounter::OnSentPacket);
162 }
163
164 int sent_packets() const { return sent_packets_; }
165
166 private:
167 void OnSentPacket(const rtc::SentPacket&) { ++sent_packets_; }
168
169 int sent_packets_ = 0;
170};
171
172// Test that SignalSentPacket is fired when a packet is successfully sent, for
173// both TCP client and server sockets.
174TEST_F(TCPPortTest, SignalSentPacket) {
175 std::unique_ptr<TCPPort> client(CreateTCPPort(kLocalAddr));
176 std::unique_ptr<TCPPort> server(CreateTCPPort(kRemoteAddr));
177 client->SetIceRole(cricket::ICEROLE_CONTROLLING);
178 server->SetIceRole(cricket::ICEROLE_CONTROLLED);
179 client->PrepareAddress();
180 server->PrepareAddress();
181
182 Connection* client_conn =
183 client->CreateConnection(server->Candidates()[0], Port::ORIGIN_MESSAGE);
184 ASSERT_NE(nullptr, client_conn);
185 ASSERT_TRUE_WAIT(client_conn->connected(), kTimeout);
186
187 // Need to get the port of the actual outgoing socket, not the server socket..
188 cricket::Candidate client_candidate = client->Candidates()[0];
189 client_candidate.set_address(static_cast<cricket::TCPConnection*>(client_conn)
190 ->socket()
191 ->GetLocalAddress());
192 Connection* server_conn =
193 server->CreateConnection(client_candidate, Port::ORIGIN_THIS_PORT);
194 ASSERT_NE(nullptr, server_conn);
195 ASSERT_TRUE_WAIT(server_conn->connected(), kTimeout);
196
197 client_conn->Ping(rtc::TimeMillis());
198 server_conn->Ping(rtc::TimeMillis());
199 ASSERT_TRUE_WAIT(client_conn->writable(), kTimeout);
200 ASSERT_TRUE_WAIT(server_conn->writable(), kTimeout);
201
202 SentPacketCounter client_counter(client.get());
203 SentPacketCounter server_counter(server.get());
204 static const char kData[] = "hello";
205 for (int i = 0; i < 10; ++i) {
206 client_conn->Send(&kData, sizeof(kData), rtc::PacketOptions());
207 server_conn->Send(&kData, sizeof(kData), rtc::PacketOptions());
208 }
209 EXPECT_EQ_WAIT(10, client_counter.sent_packets(), kTimeout);
210 EXPECT_EQ_WAIT(10, server_counter.sent_packets(), kTimeout);
211}