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