/* * Copyright 2009 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
using cricket::ServerAddresses; using rtc::SocketAddress; using ::testing::_; using ::testing::DoAll; using ::testing::Return; using ::testing::ReturnPointee; using ::testing::SetArgPointee; using webrtc::IceCandidateType;
// Base class for tests connecting a StunPort to a fake STUN server // (cricket::StunServer). class StunPortTestBase : public ::testing::Test, public sigslot::has_slots<> { public:
StunPortTestBase()
: StunPortTestBase(
rtc::Network("unittest", "unittest", kLocalAddr.ipaddr(), 32),
kLocalAddr.ipaddr()) {}
class StunPortTestWithRealClock : public StunPortTestBase {};
class FakeClockBase { public:
rtc::ScopedFakeClock fake_clock;
};
class StunPortTest : public FakeClockBase, public StunPortTestBase {};
// Test that we can create a STUN port.
TEST_F(StunPortTest, TestCreateStunPort) {
CreateStunPort(kStunAddr1);
EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
EXPECT_EQ(0U, port()->Candidates().size());
}
// Test that we can create a UDP port.
TEST_F(StunPortTest, TestCreateUdpPort) {
CreateSharedUdpPort(kStunAddr1, nullptr);
EXPECT_EQ(IceCandidateType::kHost, port()->Type());
EXPECT_EQ(0U, port()->Candidates().size());
}
// Test that we can get an address from a STUN server.
TEST_F(StunPortTest, TestPrepareAddress) {
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
std::string expected_server_url = "stun:127.0.0.1:5000";
EXPECT_EQ(port()->Candidates()[0].url(), expected_server_url);
}
// Test that we fail properly if we can't get an address.
TEST_F(StunPortTest, TestPrepareAddressFail) {
CreateStunPort(kBadAddr);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE(error());
EXPECT_EQ(0U, port()->Candidates().size());
EXPECT_EQ_SIMULATED_WAIT(error_event_.error_code,
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs,
fake_clock);
EXPECT_NE(error_event_.error_text.find('.'), std::string::npos);
EXPECT_NE(error_event_.address.find(kLocalAddr.HostAsSensitiveURIString()),
std::string::npos);
std::string server_url = "stun:" + kBadAddr.ToString();
EXPECT_EQ(error_event_.url, server_url);
}
// Test that we fail without emitting an error if we try to get an address from // a STUN server with a different address family. IPv4 local, IPv6 STUN.
TEST_F(StunPortTest, TestServerAddressFamilyMismatch) {
CreateStunPort(kIPv6StunAddr1);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE(error());
EXPECT_EQ(0U, port()->Candidates().size());
EXPECT_EQ(0, error_event_.error_code);
}
class StunPortWithMockDnsResolverTest : public StunPortTest { public:
StunPortWithMockDnsResolverTest() : StunPortTest(), socket_factory_(ss()) {}
// Test that we can get an address from a STUN server specified by a hostname.
TEST_F(StunPortWithMockDnsResolverTest, TestPrepareAddressHostname) {
SetDnsResolverExpectations(
[](webrtc::MockAsyncDnsResolver* resolver,
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver, Start(kValidHostnameAddr, /*family=*/AF_INET, _))
.WillOnce([](const rtc::SocketAddress& addr, int family,
absl::AnyInvocable<void()> callback) { callback(); });
// Test that we handle hostname lookup failures properly.
TEST_F(StunPortTestWithRealClock, TestPrepareAddressHostnameFail) {
CreateStunPort(kBadHostnameAddr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE(error());
EXPECT_EQ(0U, port()->Candidates().size());
EXPECT_EQ_WAIT(error_event_.error_code,
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs);
}
// This test verifies keepalive response messages don't result in // additional candidate generation.
TEST_F(StunPortTest, TestKeepAliveResponse) {
SetKeepaliveDelay(500); // 500ms of keepalive delay.
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
SIMULATED_WAIT(false, 1000, fake_clock);
EXPECT_EQ(1U, port()->Candidates().size());
}
// Test that a local candidate can be generated using a shared socket.
TEST_F(StunPortTest, TestSharedSocketPrepareAddress) {
CreateSharedUdpPort(kStunAddr1, nullptr);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
}
// Test that we still get a local candidate with invalid stun server hostname. // Also verifing that UDPPort can receive packets when stun address can't be // resolved.
TEST_F(StunPortTestWithRealClock,
TestSharedSocketPrepareAddressInvalidHostname) {
CreateSharedUdpPort(kBadHostnameAddr, nullptr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
// Send data to port after it's ready. This is to make sure, UDP port can // handle data with unresolved stun server address.
std::string data = "some random data, sending to cricket::Port.";
SendData(data.c_str(), data.length()); // No crash is success.
}
// Test that a stun candidate (srflx candidate) is discarded whose address is // equal to that of a local candidate if mDNS obfuscation is not enabled.
TEST_F(StunPortTest, TestStunCandidateDiscardedWithMdnsObfuscationNotEnabled) {
CreateSharedUdpPort(kStunAddr1, nullptr);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
EXPECT_TRUE(port()->Candidates()[0].is_local());
}
// Test that a stun candidate (srflx candidate) is generated whose address is // equal to that of a local candidate if mDNS obfuscation is enabled.
TEST_F(StunPortTest, TestStunCandidateGeneratedWithMdnsObfuscationEnabled) {
EnableMdnsObfuscation();
CreateSharedUdpPort(kStunAddr1, nullptr);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(2U, port()->Candidates().size());
// The addresses of the candidates are both equal to kLocalAddr.
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[1].address()));
// One of the generated candidates is a local candidate and the other is a // stun candidate.
EXPECT_NE(port()->Candidates()[0].type(), port()->Candidates()[1].type()); if (port()->Candidates()[0].is_local()) {
EXPECT_TRUE(port()->Candidates()[1].is_stun());
} else {
EXPECT_TRUE(port()->Candidates()[0].is_stun());
EXPECT_TRUE(port()->Candidates()[1].is_local());
}
}
// Test that the same address is added only once if two STUN servers are in // use.
TEST_F(StunPortTest, TestNoDuplicatedAddressWithTwoStunServers) {
ServerAddresses stun_servers;
stun_servers.insert(kStunAddr1);
stun_servers.insert(kStunAddr2);
CreateStunPort(stun_servers);
EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_EQ(1U, port()->Candidates().size());
EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
}
// Test that candidates can be allocated for multiple STUN servers, one of // which is not reachable.
TEST_F(StunPortTest, TestMultipleStunServersWithBadServer) {
ServerAddresses stun_servers;
stun_servers.insert(kStunAddr1);
stun_servers.insert(kBadAddr);
CreateStunPort(stun_servers);
EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_EQ(1U, port()->Candidates().size());
std::string server_url = "stun:" + kBadAddr.ToString();
ASSERT_EQ_SIMULATED_WAIT(error_event_.url, server_url, kTimeoutMs,
fake_clock);
}
// Test that two candidates are allocated if the two STUN servers return // different mapped addresses.
TEST_F(StunPortTest, TestTwoCandidatesWithTwoStunServersAcrossNat) { const SocketAddress kStunMappedAddr1("77.77.77.77", 0); const SocketAddress kStunMappedAddr2("88.77.77.77", 0);
stun_server_1()->set_fake_stun_addr(kStunMappedAddr1);
stun_server_2()->set_fake_stun_addr(kStunMappedAddr2);
// Test that the stun_keepalive_lifetime is set correctly based on the network // type on a STUN port. Also test that it will be updated if the network type // changes.
TEST_F(StunPortTest, TestStunPortGetStunKeepaliveLifetime) { // Lifetime for the default (unknown) network type is `kInfiniteLifetime`.
CreateStunPort(kStunAddr1);
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); // Lifetime for the cellular network is `kHighCostPortKeepaliveLifetimeMs`
SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs,
port()->stun_keepalive_lifetime());
// Lifetime for the wifi network is `kInfiniteLifetime`.
SetNetworkType(rtc::ADAPTER_TYPE_WIFI);
CreateStunPort(kStunAddr2);
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
}
// Test that the stun_keepalive_lifetime is set correctly based on the network // type on a shared STUN port (UDPPort). Also test that it will be updated // if the network type changes.
TEST_F(StunPortTest, TestUdpPortGetStunKeepaliveLifetime) { // Lifetime for the default (unknown) network type is `kInfiniteLifetime`.
CreateSharedUdpPort(kStunAddr1, nullptr);
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); // Lifetime for the cellular network is `kHighCostPortKeepaliveLifetimeMs`.
SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs,
port()->stun_keepalive_lifetime());
// Lifetime for the wifi network type is `kInfiniteLifetime`.
SetNetworkType(rtc::ADAPTER_TYPE_WIFI);
CreateSharedUdpPort(kStunAddr2, nullptr);
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
}
// Test that STUN binding requests will be stopped shortly if the keep-alive // lifetime is short.
TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) {
SetKeepaliveDelay(101);
SetKeepaliveLifetime(100);
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE_SIMULATED_WAIT(!HasPendingRequest(cricket::STUN_BINDING_REQUEST),
2000, fake_clock);
}
// Test that by default, the STUN binding requests will last for a long time.
TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) {
SetKeepaliveDelay(101);
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE_SIMULATED_WAIT(HasPendingRequest(cricket::STUN_BINDING_REQUEST),
1000, fake_clock);
}
class MockAsyncPacketSocket : public rtc::AsyncPacketSocket { public:
~MockAsyncPacketSocket() = default;
// Test that outbound packets inherit the dscp value assigned to the socket.
TEST_F(StunPortTest, TestStunPacketsHaveDscpPacketOption) {
MockAsyncPacketSocket* socket = new MockAsyncPacketSocket();
CreateSharedUdpPort(kStunAddr1, socket);
EXPECT_CALL(*socket, GetLocalAddress()).WillRepeatedly(Return(kLocalAddr));
EXPECT_CALL(*socket, GetState())
.WillRepeatedly(Return(rtc::AsyncPacketSocket::STATE_BOUND));
EXPECT_CALL(*socket, SetOption(_, _)).WillRepeatedly(Return(0));
// If DSCP is not set on the socket, stun packets should have no value.
EXPECT_CALL(*socket,
SendTo(_, _, _,
::testing::Field(&rtc::PacketOptions::dscp,
::testing::Eq(rtc::DSCP_NO_CHANGE))))
.WillOnce(Return(100));
PrepareAddress();
// Once it is set transport wide, they should inherit that value.
port()->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_AF41);
EXPECT_CALL(*socket, SendTo(_, _, _,
::testing::Field(&rtc::PacketOptions::dscp,
::testing::Eq(rtc::DSCP_AF41))))
.WillRepeatedly(Return(100));
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
}
class StunIPv6PortTestWithRealClock : public StunIPv6PortTestBase {};
class StunIPv6PortTest : public FakeClockBase, public StunIPv6PortTestBase {};
// Test that we can get an address from a STUN server.
TEST_F(StunIPv6PortTest, TestPrepareAddress) {
CreateStunPort(kIPv6StunAddr1);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kIPv6LocalAddr.EqualIPs(port()->Candidates()[0].address()));
std::string expected_server_url = "stun:::1:5000";
EXPECT_EQ(port()->Candidates()[0].url(), expected_server_url);
}
// Test that we fail properly if we can't get an address.
TEST_F(StunIPv6PortTest, TestPrepareAddressFail) {
CreateStunPort(kIPv6BadAddr);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE(error());
EXPECT_EQ(0U, port()->Candidates().size());
EXPECT_EQ_SIMULATED_WAIT(error_event_.error_code,
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs,
fake_clock);
EXPECT_NE(error_event_.error_text.find('.'), std::string::npos);
EXPECT_NE(
error_event_.address.find(kIPv6LocalAddr.HostAsSensitiveURIString()),
std::string::npos);
std::string server_url = "stun:" + kIPv6BadAddr.ToString();
EXPECT_EQ(error_event_.url, server_url);
}
// Test that we fail without emitting an error if we try to get an address from // a STUN server with a different address family. IPv6 local, IPv4 STUN.
TEST_F(StunIPv6PortTest, TestServerAddressFamilyMismatch) {
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE(error());
EXPECT_EQ(0U, port()->Candidates().size());
EXPECT_EQ(0, error_event_.error_code);
}
// Test that we handle hostname lookup failures properly with a real clock.
TEST_F(StunIPv6PortTestWithRealClock, TestPrepareAddressHostnameFail) {
CreateStunPort(kBadHostnameAddr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE(error());
EXPECT_EQ(0U, port()->Candidates().size());
EXPECT_EQ_WAIT(error_event_.error_code,
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs);
}
class StunIPv6PortTestWithMockDnsResolver : public StunIPv6PortTest { public:
StunIPv6PortTestWithMockDnsResolver()
: StunIPv6PortTest(), socket_factory_(ss()) {}
// Test that we can get an address from a STUN server specified by a hostname.
TEST_F(StunIPv6PortTestWithMockDnsResolver, TestPrepareAddressHostname) {
SetDnsResolverExpectations(
[](webrtc::MockAsyncDnsResolver* resolver,
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver,
Start(kValidHostnameAddr, /*family=*/AF_INET6, _))
.WillOnce([](const rtc::SocketAddress& addr, int family,
absl::AnyInvocable<void()> callback) { callback(); });
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.