tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "webrtc/base/gunit.h" |
| 12 | #include "webrtc/base/physicalsocketserver.h" |
| 13 | #include "webrtc/base/thread.h" |
| 14 | #include "webrtc/base/virtualsocketserver.h" |
| 15 | #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 16 | #include "webrtc/p2p/base/tcpport.h" |
| 17 | |
| 18 | using rtc::SocketAddress; |
| 19 | using cricket::Connection; |
| 20 | using cricket::Port; |
| 21 | using cricket::TCPPort; |
| 22 | using cricket::ICE_UFRAG_LENGTH; |
| 23 | using cricket::ICE_PWD_LENGTH; |
| 24 | |
| 25 | static int kTimeout = 1000; |
| 26 | static const SocketAddress kLocalAddr("11.11.11.11", 1); |
| 27 | static const SocketAddress kRemoteAddr("22.22.22.22", 2); |
| 28 | |
| 29 | class TCPPortTest : public testing::Test, public sigslot::has_slots<> { |
| 30 | public: |
| 31 | TCPPortTest() |
| 32 | : main_(rtc::Thread::Current()), |
| 33 | pss_(new rtc::PhysicalSocketServer), |
| 34 | ss_(new rtc::VirtualSocketServer(pss_.get())), |
| 35 | ss_scope_(ss_.get()), |
| 36 | network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), |
| 37 | socket_factory_(rtc::Thread::Current()), |
| 38 | username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
| 39 | password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) { |
| 40 | network_.AddIP(rtc::IPAddress(INADDR_ANY)); |
| 41 | } |
| 42 | |
| 43 | void ConnectSignalSocketCreated() { |
| 44 | ss_->SignalSocketCreated.connect(this, &TCPPortTest::OnSocketCreated); |
| 45 | } |
| 46 | |
| 47 | void OnSocketCreated(rtc::VirtualSocket* socket) { |
| 48 | LOG(LS_INFO) << "socket created "; |
| 49 | socket->SignalAddressReady.connect( |
| 50 | this, &TCPPortTest::SetLocalhostAsAlternativeLocalAddress); |
| 51 | } |
| 52 | |
| 53 | void SetLocalhostAsAlternativeLocalAddress(rtc::VirtualSocket* socket, |
| 54 | const SocketAddress& address) { |
| 55 | SocketAddress local_address("127.0.0.1", 2000); |
| 56 | socket->SetAlternativeLocalAddress(local_address); |
| 57 | } |
| 58 | |
| 59 | TCPPort* CreateTCPPort(const SocketAddress& addr) { |
| 60 | return TCPPort::Create(main_, &socket_factory_, &network_, addr.ipaddr(), 0, |
| 61 | 0, username_, password_, true); |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | rtc::Thread* main_; |
| 66 | rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; |
| 67 | rtc::scoped_ptr<rtc::VirtualSocketServer> ss_; |
| 68 | rtc::SocketServerScope ss_scope_; |
| 69 | rtc::Network network_; |
| 70 | rtc::BasicPacketSocketFactory socket_factory_; |
| 71 | std::string username_; |
| 72 | std::string password_; |
| 73 | }; |
| 74 | |
| 75 | TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) { |
| 76 | rtc::scoped_ptr<TCPPort> lport(CreateTCPPort(kLocalAddr)); |
| 77 | rtc::scoped_ptr<TCPPort> rport(CreateTCPPort(kRemoteAddr)); |
| 78 | 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 | } |