blob: 7b354df67355a13f25297a4d3e6fdcd145d3bc0f [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
kwibergfd8be342016-05-14 19:44:11 -070011#include <memory>
12
tommi5ce1a2a2016-05-14 03:19:31 -070013#include "webrtc/base/gunit.h"
14#include "webrtc/base/physicalsocketserver.h"
15#include "webrtc/base/thread.h"
16#include "webrtc/base/virtualsocketserver.h"
17#include "webrtc/p2p/base/basicpacketsocketfactory.h"
18#include "webrtc/p2p/base/tcpport.h"
19
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;
28static const SocketAddress kLocalAddr("11.11.11.11", 1);
29static const SocketAddress kRemoteAddr("22.22.22.22", 2);
30
31class TCPPortTest : public testing::Test, public sigslot::has_slots<> {
32 public:
33 TCPPortTest()
nisse7eaa4ea2017-05-08 05:25:41 -070034 : pss_(new rtc::PhysicalSocketServer),
tommi5ce1a2a2016-05-14 03:19:31 -070035 ss_(new rtc::VirtualSocketServer(pss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -070036 main_(ss_.get()),
tommi5ce1a2a2016-05-14 03:19:31 -070037 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32),
38 socket_factory_(rtc::Thread::Current()),
39 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)),
40 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) {
41 network_.AddIP(rtc::IPAddress(INADDR_ANY));
42 }
43
44 void ConnectSignalSocketCreated() {
45 ss_->SignalSocketCreated.connect(this, &TCPPortTest::OnSocketCreated);
46 }
47
48 void OnSocketCreated(rtc::VirtualSocket* socket) {
49 LOG(LS_INFO) << "socket created ";
50 socket->SignalAddressReady.connect(
51 this, &TCPPortTest::SetLocalhostAsAlternativeLocalAddress);
52 }
53
54 void SetLocalhostAsAlternativeLocalAddress(rtc::VirtualSocket* socket,
55 const SocketAddress& address) {
56 SocketAddress local_address("127.0.0.1", 2000);
57 socket->SetAlternativeLocalAddress(local_address);
58 }
59
60 TCPPort* CreateTCPPort(const SocketAddress& addr) {
nisse7eaa4ea2017-05-08 05:25:41 -070061 return TCPPort::Create(&main_, &socket_factory_, &network_, addr.ipaddr(),
62 0, 0, username_, password_, true);
tommi5ce1a2a2016-05-14 03:19:31 -070063 }
64
65 protected:
kwibergfd8be342016-05-14 19:44:11 -070066 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
67 std::unique_ptr<rtc::VirtualSocketServer> ss_;
nisse7eaa4ea2017-05-08 05:25:41 -070068 rtc::AutoSocketServerThread main_;
tommi5ce1a2a2016-05-14 03:19:31 -070069 rtc::Network network_;
70 rtc::BasicPacketSocketFactory socket_factory_;
71 std::string username_;
72 std::string password_;
73};
74
75TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) {
kwibergfd8be342016-05-14 19:44:11 -070076 std::unique_ptr<TCPPort> lport(CreateTCPPort(kLocalAddr));
77 std::unique_ptr<TCPPort> rport(CreateTCPPort(kRemoteAddr));
tommi5ce1a2a2016-05-14 03:19:31 -070078 lport->PrepareAddress();
79 rport->PrepareAddress();
80 // Start to listen to new socket creation event.
81 ConnectSignalSocketCreated();
82 Connection* conn =
83 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
84 EXPECT_TRUE_WAIT(conn->connected(), kTimeout);
85}
deadbeef06878292017-04-21 14:22:23 -070086
87class SentPacketCounter : public sigslot::has_slots<> {
88 public:
89 SentPacketCounter(TCPPort* p) {
90 p->SignalSentPacket.connect(this, &SentPacketCounter::OnSentPacket);
91 }
92
93 int sent_packets() const { return sent_packets_; }
94
95 private:
96 void OnSentPacket(const rtc::SentPacket&) { ++sent_packets_; }
97
98 int sent_packets_ = 0;
99};
100
101// Test that SignalSentPacket is fired when a packet is successfully sent, for
102// both TCP client and server sockets.
103TEST_F(TCPPortTest, SignalSentPacket) {
104 std::unique_ptr<TCPPort> client(CreateTCPPort(kLocalAddr));
105 std::unique_ptr<TCPPort> server(CreateTCPPort(kRemoteAddr));
106 client->SetIceRole(cricket::ICEROLE_CONTROLLING);
107 server->SetIceRole(cricket::ICEROLE_CONTROLLED);
108 client->PrepareAddress();
109 server->PrepareAddress();
110
111 Connection* client_conn =
112 client->CreateConnection(server->Candidates()[0], Port::ORIGIN_MESSAGE);
113 ASSERT_NE(nullptr, client_conn);
114 ASSERT_TRUE_WAIT(client_conn->connected(), kTimeout);
115
116 // Need to get the port of the actual outgoing socket, not the server socket..
117 cricket::Candidate client_candidate = client->Candidates()[0];
118 client_candidate.set_address(static_cast<cricket::TCPConnection*>(client_conn)
119 ->socket()
120 ->GetLocalAddress());
121 Connection* server_conn =
122 server->CreateConnection(client_candidate, Port::ORIGIN_THIS_PORT);
123 ASSERT_NE(nullptr, server_conn);
124 ASSERT_TRUE_WAIT(server_conn->connected(), kTimeout);
125
126 client_conn->Ping(rtc::TimeMillis());
127 server_conn->Ping(rtc::TimeMillis());
128 ASSERT_TRUE_WAIT(client_conn->writable(), kTimeout);
129 ASSERT_TRUE_WAIT(server_conn->writable(), kTimeout);
130
131 SentPacketCounter client_counter(client.get());
132 SentPacketCounter server_counter(server.get());
133 static const char kData[] = "hello";
134 for (int i = 0; i < 10; ++i) {
135 client_conn->Send(&kData, sizeof(kData), rtc::PacketOptions());
136 server_conn->Send(&kData, sizeof(kData), rtc::PacketOptions());
137 }
138 EXPECT_EQ_WAIT(10, client_counter.sent_packets(), kTimeout);
139 EXPECT_EQ_WAIT(10, server_counter.sent_packets(), kTimeout);
140}