blob: e33dd433a7dd8e09e61ebc5bab214e3672e30b2c [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}