diff options
Diffstat (limited to 'webrtc/p2p/base/port_unittest.cc')
-rw-r--r-- | webrtc/p2p/base/port_unittest.cc | 91 |
1 files changed, 86 insertions, 5 deletions
diff --git a/webrtc/p2p/base/port_unittest.cc b/webrtc/p2p/base/port_unittest.cc index 4a4ed32456..449021ad9f 100644 --- a/webrtc/p2p/base/port_unittest.cc +++ b/webrtc/p2p/base/port_unittest.cc @@ -17,6 +17,7 @@ #include "webrtc/p2p/base/testturnserver.h" #include "webrtc/p2p/base/transport.h" #include "webrtc/p2p/base/turnport.h" +#include "webrtc/base/arraysize.h" #include "webrtc/base/crc32.h" #include "webrtc/base/gunit.h" #include "webrtc/base/helpers.h" @@ -140,6 +141,10 @@ class TestPort : public Port { ICE_TYPE_PREFERENCE_HOST, 0, true); } + virtual bool SupportsProtocol(const std::string& protocol) const { + return true; + } + // Exposed for testing candidate building. void AddCandidateAddress(const rtc::SocketAddress& addr) { AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), @@ -199,9 +204,13 @@ class TestPort : public Port { } private: + void OnSentPacket(rtc::AsyncPacketSocket* socket, + const rtc::SentPacket& sent_packet) { + PortInterface::SignalSentPacket(sent_packet); + } rtc::scoped_ptr<ByteBuffer> last_stun_buf_; rtc::scoped_ptr<IceMessage> last_stun_msg_; - int type_preference_; + int type_preference_ = 0; }; class TestChannel : public sigslot::has_slots<> { @@ -456,9 +465,8 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { } UDPPort* CreateUdpPort(const SocketAddress& addr, PacketSocketFactory* socket_factory) { - return UDPPort::Create(main_, socket_factory, &network_, - addr.ipaddr(), 0, 0, username_, password_, - std::string(), false); + return UDPPort::Create(main_, socket_factory, &network_, addr.ipaddr(), 0, + 0, username_, password_, std::string(), true); } TCPPort* CreateTcpPort(const SocketAddress& addr) { return CreateTcpPort(addr, &socket_factory_); @@ -1235,6 +1243,58 @@ TEST_F(PortTest, TestSslTcpToSslTcpRelay) { } */ +// Test that a connection will be dead and deleted if +// i) it has never received anything for MIN_CONNECTION_LIFETIME milliseconds +// since it was created, or +// ii) it has not received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT +// milliseconds since last receiving. +TEST_F(PortTest, TestConnectionDead) { + UDPPort* port1 = CreateUdpPort(kLocalAddr1); + UDPPort* port2 = CreateUdpPort(kLocalAddr2); + TestChannel ch1(port1); + TestChannel ch2(port2); + // Acquire address. + ch1.Start(); + ch2.Start(); + ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout); + ASSERT_EQ_WAIT(1, ch2.complete_count(), kTimeout); + + // Test case that the connection has never received anything. + uint32_t before_created = rtc::Time(); + ch1.CreateConnection(GetCandidate(port2)); + uint32_t after_created = rtc::Time(); + Connection* conn = ch1.conn(); + ASSERT(conn != nullptr); + // It is not dead if it is after MIN_CONNECTION_LIFETIME but not pruned. + conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); + rtc::Thread::Current()->ProcessMessages(0); + EXPECT_TRUE(ch1.conn() != nullptr); + // It is not dead if it is before MIN_CONNECTION_LIFETIME and pruned. + conn->UpdateState(before_created + MIN_CONNECTION_LIFETIME - 1); + conn->Prune(); + rtc::Thread::Current()->ProcessMessages(0); + EXPECT_TRUE(ch1.conn() != nullptr); + // It will be dead after MIN_CONNECTION_LIFETIME and pruned. + conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); + EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kTimeout); + + // Test case that the connection has received something. + // Create a connection again and receive a ping. + ch1.CreateConnection(GetCandidate(port2)); + conn = ch1.conn(); + ASSERT(conn != nullptr); + uint32_t before_last_receiving = rtc::Time(); + conn->ReceivedPing(); + uint32_t after_last_receiving = rtc::Time(); + // The connection will be dead after DEAD_CONNECTION_RECEIVE_TIMEOUT + conn->UpdateState( + before_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT - 1); + rtc::Thread::Current()->ProcessMessages(100); + EXPECT_TRUE(ch1.conn() != nullptr); + conn->UpdateState(after_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT + 1); + EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kTimeout); +} + // This test case verifies standard ICE features in STUN messages. Currently it // verifies Message Integrity attribute in STUN messages and username in STUN // binding request will have colon (":") between remote and local username. @@ -2224,7 +2284,7 @@ TEST_F(PortTest, TestWritableState) { // Data should be unsendable until the connection is accepted. char data[] = "abcd"; - int data_size = ARRAY_SIZE(data); + int data_size = arraysize(data); rtc::PacketOptions options; EXPECT_EQ(SOCKET_ERROR, ch1.conn()->Send(data, data_size, options)); @@ -2450,3 +2510,24 @@ TEST_F(PortTest, TestControlledToControllingNotDestroyed) { rtc::Thread::Current()->ProcessMessages(kTimeout); EXPECT_FALSE(destroyed()); } + +TEST_F(PortTest, TestSupportsProtocol) { + rtc::scoped_ptr<Port> udp_port(CreateUdpPort(kLocalAddr1)); + EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); + EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); + + rtc::scoped_ptr<Port> stun_port( + CreateStunPort(kLocalAddr1, nat_socket_factory1())); + EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); + EXPECT_FALSE(stun_port->SupportsProtocol(TCP_PROTOCOL_NAME)); + + rtc::scoped_ptr<Port> tcp_port(CreateTcpPort(kLocalAddr1)); + EXPECT_TRUE(tcp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); + EXPECT_TRUE(tcp_port->SupportsProtocol(SSLTCP_PROTOCOL_NAME)); + EXPECT_FALSE(tcp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); + + rtc::scoped_ptr<Port> turn_port( + CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP)); + EXPECT_TRUE(turn_port->SupportsProtocol(UDP_PROTOCOL_NAME)); + EXPECT_FALSE(turn_port->SupportsProtocol(TCP_PROTOCOL_NAME)); +} |