| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <list> |
| 11 #include <memory> | 12 #include <memory> |
| 12 | 13 |
| 13 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | 14 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 14 #include "webrtc/p2p/base/tcpport.h" | 15 #include "webrtc/p2p/base/tcpport.h" |
| 15 #include "webrtc/rtc_base/gunit.h" | 16 #include "webrtc/rtc_base/gunit.h" |
| 16 #include "webrtc/rtc_base/thread.h" | 17 #include "webrtc/rtc_base/thread.h" |
| 17 #include "webrtc/rtc_base/virtualsocketserver.h" | 18 #include "webrtc/rtc_base/virtualsocketserver.h" |
| 18 | 19 |
| 19 using rtc::SocketAddress; | 20 using rtc::SocketAddress; |
| 20 using cricket::Connection; | 21 using cricket::Connection; |
| 21 using cricket::Port; | 22 using cricket::Port; |
| 22 using cricket::TCPPort; | 23 using cricket::TCPPort; |
| 23 using cricket::ICE_UFRAG_LENGTH; | 24 using cricket::ICE_UFRAG_LENGTH; |
| 24 using cricket::ICE_PWD_LENGTH; | 25 using cricket::ICE_PWD_LENGTH; |
| 25 | 26 |
| 26 static int kTimeout = 1000; | 27 static int kTimeout = 1000; |
| 27 static const SocketAddress kLocalAddr("11.11.11.11", 1); | 28 static const SocketAddress kLocalAddr("11.11.11.11", 0); |
| 28 static const SocketAddress kRemoteAddr("22.22.22.22", 2); | 29 static const SocketAddress kAlternateLocalAddr("1.2.3.4", 0); |
| 30 static const SocketAddress kRemoteAddr("22.22.22.22", 0); |
| 31 |
| 32 class 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 }; |
| 29 | 45 |
| 30 class TCPPortTest : public testing::Test, public sigslot::has_slots<> { | 46 class TCPPortTest : public testing::Test, public sigslot::has_slots<> { |
| 31 public: | 47 public: |
| 32 TCPPortTest() | 48 TCPPortTest() |
| 33 : ss_(new rtc::VirtualSocketServer()), | 49 : ss_(new rtc::VirtualSocketServer()), |
| 34 main_(ss_.get()), | 50 main_(ss_.get()), |
| 35 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), | |
| 36 socket_factory_(rtc::Thread::Current()), | 51 socket_factory_(rtc::Thread::Current()), |
| 37 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), | 52 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
| 38 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) { | 53 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)) { |
| 39 network_.AddIP(rtc::IPAddress(INADDR_ANY)); | |
| 40 } | 54 } |
| 41 | 55 |
| 42 void ConnectSignalSocketCreated() { | 56 rtc::Network* MakeNetwork(const SocketAddress& addr) { |
| 43 ss_->SignalSocketCreated.connect(this, &TCPPortTest::OnSocketCreated); | 57 networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32); |
| 58 networks_.back().AddIP(addr.ipaddr()); |
| 59 return &networks_.back(); |
| 44 } | 60 } |
| 45 | 61 |
| 46 void OnSocketCreated(rtc::VirtualSocket* socket) { | 62 std::unique_ptr<TCPPort> CreateTCPPort(const SocketAddress& addr) { |
| 47 LOG(LS_INFO) << "socket created "; | 63 return std::unique_ptr<TCPPort>( |
| 48 socket->SignalAddressReady.connect( | 64 TCPPort::Create(&main_, &socket_factory_, MakeNetwork(addr), 0, 0, |
| 49 this, &TCPPortTest::SetLocalhostAsAlternativeLocalAddress); | 65 username_, password_, true)); |
| 50 } | 66 } |
| 51 | 67 |
| 52 void SetLocalhostAsAlternativeLocalAddress(rtc::VirtualSocket* socket, | 68 std::unique_ptr<TCPPort> CreateTCPPort(rtc::Network* network) { |
| 53 const SocketAddress& address) { | 69 return std::unique_ptr<TCPPort>(TCPPort::Create( |
| 54 SocketAddress local_address("127.0.0.1", 2000); | 70 &main_, &socket_factory_, network, 0, 0, username_, password_, true)); |
| 55 socket->SetAlternativeLocalAddress(local_address); | |
| 56 } | |
| 57 | |
| 58 TCPPort* CreateTCPPort(const SocketAddress& addr) { | |
| 59 return TCPPort::Create(&main_, &socket_factory_, &network_, addr.ipaddr(), | |
| 60 0, 0, username_, password_, true); | |
| 61 } | 71 } |
| 62 | 72 |
| 63 protected: | 73 protected: |
| 74 // 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_; |
| 64 std::unique_ptr<rtc::VirtualSocketServer> ss_; | 78 std::unique_ptr<rtc::VirtualSocketServer> ss_; |
| 65 rtc::AutoSocketServerThread main_; | 79 rtc::AutoSocketServerThread main_; |
| 66 rtc::Network network_; | |
| 67 rtc::BasicPacketSocketFactory socket_factory_; | 80 rtc::BasicPacketSocketFactory socket_factory_; |
| 68 std::string username_; | 81 std::string username_; |
| 69 std::string password_; | 82 std::string password_; |
| 70 }; | 83 }; |
| 71 | 84 |
| 72 TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) { | 85 TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) { |
| 73 std::unique_ptr<TCPPort> lport(CreateTCPPort(kLocalAddr)); | 86 SocketAddress local_address("127.0.0.1", 0); |
| 74 std::unique_ptr<TCPPort> rport(CreateTCPPort(kRemoteAddr)); | 87 // After calling this, when TCPPort attempts to get a socket bound to |
| 75 lport->PrepareAddress(); | 88 // kLocalAddr, it will end up using localhost instead. |
| 76 rport->PrepareAddress(); | 89 ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(), local_address.ipaddr()); |
| 77 // Start to listen to new socket creation event. | 90 auto local_port = CreateTCPPort(kLocalAddr); |
| 78 ConnectSignalSocketCreated(); | 91 auto remote_port = CreateTCPPort(kRemoteAddr); |
| 79 Connection* conn = | 92 local_port->PrepareAddress(); |
| 80 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); | 93 remote_port->PrepareAddress(); |
| 94 Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0], |
| 95 Port::ORIGIN_MESSAGE); |
| 81 EXPECT_TRUE_WAIT(conn->connected(), kTimeout); | 96 EXPECT_TRUE_WAIT(conn->connected(), kTimeout); |
| 97 // 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. |
| 109 TEST_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. |
| 130 TEST_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()); |
| 82 } | 156 } |
| 83 | 157 |
| 84 class SentPacketCounter : public sigslot::has_slots<> { | 158 class SentPacketCounter : public sigslot::has_slots<> { |
| 85 public: | 159 public: |
| 86 SentPacketCounter(TCPPort* p) { | 160 SentPacketCounter(TCPPort* p) { |
| 87 p->SignalSentPacket.connect(this, &SentPacketCounter::OnSentPacket); | 161 p->SignalSentPacket.connect(this, &SentPacketCounter::OnSentPacket); |
| 88 } | 162 } |
| 89 | 163 |
| 90 int sent_packets() const { return sent_packets_; } | 164 int sent_packets() const { return sent_packets_; } |
| 91 | 165 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 SentPacketCounter client_counter(client.get()); | 202 SentPacketCounter client_counter(client.get()); |
| 129 SentPacketCounter server_counter(server.get()); | 203 SentPacketCounter server_counter(server.get()); |
| 130 static const char kData[] = "hello"; | 204 static const char kData[] = "hello"; |
| 131 for (int i = 0; i < 10; ++i) { | 205 for (int i = 0; i < 10; ++i) { |
| 132 client_conn->Send(&kData, sizeof(kData), rtc::PacketOptions()); | 206 client_conn->Send(&kData, sizeof(kData), rtc::PacketOptions()); |
| 133 server_conn->Send(&kData, sizeof(kData), rtc::PacketOptions()); | 207 server_conn->Send(&kData, sizeof(kData), rtc::PacketOptions()); |
| 134 } | 208 } |
| 135 EXPECT_EQ_WAIT(10, client_counter.sent_packets(), kTimeout); | 209 EXPECT_EQ_WAIT(10, client_counter.sent_packets(), kTimeout); |
| 136 EXPECT_EQ_WAIT(10, server_counter.sent_packets(), kTimeout); | 210 EXPECT_EQ_WAIT(10, server_counter.sent_packets(), kTimeout); |
| 137 } | 211 } |
| OLD | NEW |