webrtc_m130/p2p/base/port_allocator_unittest.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

355 lines
15 KiB
C++
Raw Normal View History

/*
* Copyright 2016 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.
*/
#include "p2p/base/port_allocator.h"
#include <memory>
#include "absl/strings/string_view.h"
#include "p2p/base/fake_port_allocator.h"
#include "rtc_base/thread.h"
#include "rtc_base/virtual_socket_server.h"
#include "test/gtest.h"
#include "test/scoped_key_value_config.h"
static const char kContentName[] = "test content";
// Based on ICE_UFRAG_LENGTH
static const char kIceUfrag[] = "UF00";
// Based on ICE_PWD_LENGTH
static const char kIcePwd[] = "TESTICEPWD00000000000000";
static const char kTurnUsername[] = "test";
static const char kTurnPassword[] = "test";
Revert "Let port allocator create ice tie breaker" This reverts commit 3f3f991c03bb4073a06da37c822daaa9deed9307. Reason for revert: API breaking change on PortAllocatorSession. Is it possible to duplicate the ctor of PortAllocatorSession and remove the deprecated one (the one without ice_tiebreaker) in another CL? Original change's description: > Let port allocator create ice tie breaker > > Moves the responsibility for creating the ICE tie breaker from the JSEP transport controller to the port allocator. This will allow a future change to separate the ICE tie breaker (which is sent over the network and hence known to the peer) from the "port allocator random" (that is used to seed the ICE candidate foundation crc32 checksum) as an implementation detail. > > BUG=webrtc:14626 > > Change-Id: I3a9a0980238d6108b1b154f45de2975b08793b1c > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/281660 > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > Commit-Queue: Philipp Hancke <phancke@microsoft.com> > Cr-Commit-Position: refs/heads/main@{#41707} Bug: webrtc:14626 Change-Id: I342c9a96ac1909244aedea6a7779f5682088a5fc Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/339280 Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Björn Terelius <terelius@webrtc.org> Owners-Override: Mirko Bonadei <mbonadei@webrtc.org> Cr-Commit-Position: refs/heads/main@{#41715}
2024-02-12 08:52:04 +00:00
constexpr uint64_t kTiebreakerDefault = 44444;
class PortAllocatorTest : public ::testing::Test, public sigslot::has_slots<> {
public:
PortAllocatorTest()
: vss_(std::make_unique<rtc::VirtualSocketServer>()),
main_(vss_.get()),
packet_socket_factory_(
std::make_unique<rtc::BasicPacketSocketFactory>(vss_.get())),
allocator_(std::make_unique<cricket::FakePortAllocator>(
rtc::Thread::Current(),
packet_socket_factory_.get(),
Revert "Let port allocator create ice tie breaker" This reverts commit 3f3f991c03bb4073a06da37c822daaa9deed9307. Reason for revert: API breaking change on PortAllocatorSession. Is it possible to duplicate the ctor of PortAllocatorSession and remove the deprecated one (the one without ice_tiebreaker) in another CL? Original change's description: > Let port allocator create ice tie breaker > > Moves the responsibility for creating the ICE tie breaker from the JSEP transport controller to the port allocator. This will allow a future change to separate the ICE tie breaker (which is sent over the network and hence known to the peer) from the "port allocator random" (that is used to seed the ICE candidate foundation crc32 checksum) as an implementation detail. > > BUG=webrtc:14626 > > Change-Id: I3a9a0980238d6108b1b154f45de2975b08793b1c > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/281660 > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > Commit-Queue: Philipp Hancke <phancke@microsoft.com> > Cr-Commit-Position: refs/heads/main@{#41707} Bug: webrtc:14626 Change-Id: I342c9a96ac1909244aedea6a7779f5682088a5fc Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/339280 Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Björn Terelius <terelius@webrtc.org> Owners-Override: Mirko Bonadei <mbonadei@webrtc.org> Cr-Commit-Position: refs/heads/main@{#41715}
2024-02-12 08:52:04 +00:00
&field_trials_)) {
allocator_->SetIceTiebreaker(kTiebreakerDefault);
}
protected:
void SetConfigurationWithPoolSize(int candidate_pool_size) {
EXPECT_TRUE(allocator_->SetConfiguration(
cricket::ServerAddresses(), std::vector<cricket::RelayServerConfig>(),
candidate_pool_size, webrtc::NO_PRUNE));
}
void SetConfigurationWithPoolSizeExpectFailure(int candidate_pool_size) {
EXPECT_FALSE(allocator_->SetConfiguration(
cricket::ServerAddresses(), std::vector<cricket::RelayServerConfig>(),
candidate_pool_size, webrtc::NO_PRUNE));
}
std::unique_ptr<cricket::FakePortAllocatorSession> CreateSession(
absl::string_view content_name,
int component,
absl::string_view ice_ufrag,
absl::string_view ice_pwd) {
return std::unique_ptr<cricket::FakePortAllocatorSession>(
static_cast<cricket::FakePortAllocatorSession*>(
allocator_
->CreateSession(content_name, component, ice_ufrag, ice_pwd)
.release()));
}
const cricket::FakePortAllocatorSession* GetPooledSession() const {
return static_cast<const cricket::FakePortAllocatorSession*>(
allocator_->GetPooledSession());
}
std::unique_ptr<cricket::FakePortAllocatorSession> TakePooledSession() {
return std::unique_ptr<cricket::FakePortAllocatorSession>(
static_cast<cricket::FakePortAllocatorSession*>(
allocator_->TakePooledSession(kContentName, 0, kIceUfrag, kIcePwd)
.release()));
}
int GetAllPooledSessionsReturnCount() {
int count = 0;
while (TakePooledSession() != nullptr) {
++count;
}
return count;
}
webrtc::test::ScopedKeyValueConfig field_trials_;
std::unique_ptr<rtc::VirtualSocketServer> vss_;
rtc::AutoSocketServerThread main_;
std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
std::unique_ptr<cricket::FakePortAllocator> allocator_;
rtc::SocketAddress stun_server_1{"11.11.11.11", 3478};
rtc::SocketAddress stun_server_2{"22.22.22.22", 3478};
cricket::RelayServerConfig turn_server_1{"11.11.11.11", 3478,
kTurnUsername, kTurnPassword,
cricket::PROTO_UDP, false};
cricket::RelayServerConfig turn_server_2{"22.22.22.22", 3478,
kTurnUsername, kTurnPassword,
cricket::PROTO_UDP, false};
};
TEST_F(PortAllocatorTest, TestDefaults) {
EXPECT_EQ(0UL, allocator_->stun_servers().size());
EXPECT_EQ(0UL, allocator_->turn_servers().size());
EXPECT_EQ(0, allocator_->candidate_pool_size());
EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
}
// Call CreateSession and verify that the parameters passed in and the
// candidate filter are applied as expected.
TEST_F(PortAllocatorTest, CreateSession) {
Reland "Surface ICE candidates that match an updated candidate filter." This is a reland of cd8d1cf68e4eeed71fba51c97006a91bfd41813d Original change's description: > Surface ICE candidates that match an updated candidate filter. > > After this change an ICE agent can surface candidates that do not match > the previous filter but are allowed by the updated one. The candidate > filter, as part of the internal implementation in the ICE transport, > manifests the RTCIceTransportPolicy field in RTCConfiguration. > > This new feature would allow an ICE agent to gather new candidates when > the transport policy changes from e.g. 'relay' to 'all' without an ICE > restart. > > A caveat in the current implementation remains, and a candidate can > surface multiple times if the transport policy, or the candidate filter > directly, performs multiple transitions from a value that disallows to > one that allows the underlying candidate type. For example, if the > transport policy is updated by 'all' -> 'relay' -> 'all', the same host > candidate can surface after the second update. > > > Bug: webrtc:8939 > Change-Id: I92c2e07dafab225c702c5de28f47958a0d3270cc > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/132282 > Commit-Queue: Qingsi Wang <qingsi@webrtc.org> > Reviewed-by: Jeroen de Borst <jeroendb@webrtc.org> > Reviewed-by: Seth Hampson <shampson@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#27674} Bug: webrtc:8939 Change-Id: I9c32b1ea05028ecd937ab4912779dd958faf734f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/133582 Reviewed-by: Seth Hampson <shampson@webrtc.org> Reviewed-by: Jeroen de Borst <jeroendb@webrtc.org> Commit-Queue: Qingsi Wang <qingsi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#27694}
2019-04-18 10:41:58 -07:00
allocator_->SetCandidateFilter(cricket::CF_RELAY);
auto session = CreateSession(kContentName, 1, kIceUfrag, kIcePwd);
ASSERT_NE(nullptr, session);
EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
EXPECT_EQ(kContentName, session->content_name());
EXPECT_EQ(1, session->component());
EXPECT_EQ(kIceUfrag, session->ice_ufrag());
EXPECT_EQ(kIcePwd, session->ice_pwd());
}
TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) {
cricket::ServerAddresses stun_servers_1 = {stun_server_1};
std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
EXPECT_TRUE(allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0,
webrtc::NO_PRUNE));
EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
// Update with a different set of servers.
cricket::ServerAddresses stun_servers_2 = {stun_server_2};
std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
EXPECT_TRUE(allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0,
webrtc::NO_PRUNE));
EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
}
TEST_F(PortAllocatorTest, SetConfigurationUpdatesCandidatePoolSize) {
SetConfigurationWithPoolSize(2);
EXPECT_EQ(2, allocator_->candidate_pool_size());
SetConfigurationWithPoolSize(3);
EXPECT_EQ(3, allocator_->candidate_pool_size());
SetConfigurationWithPoolSize(1);
EXPECT_EQ(1, allocator_->candidate_pool_size());
SetConfigurationWithPoolSize(4);
EXPECT_EQ(4, allocator_->candidate_pool_size());
}
// Test that if the candidate pool size is nonzero, pooled sessions are
// created, and StartGettingPorts is called on them.
TEST_F(PortAllocatorTest, SetConfigurationCreatesPooledSessions) {
SetConfigurationWithPoolSize(2);
auto session_1 = TakePooledSession();
auto session_2 = TakePooledSession();
ASSERT_NE(nullptr, session_1.get());
ASSERT_NE(nullptr, session_2.get());
EXPECT_EQ(1, session_1->port_config_count());
EXPECT_EQ(1, session_2->port_config_count());
EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
}
// Test that if the candidate pool size is increased, pooled sessions are
// created as necessary.
TEST_F(PortAllocatorTest, SetConfigurationCreatesMorePooledSessions) {
SetConfigurationWithPoolSize(1);
SetConfigurationWithPoolSize(2);
EXPECT_EQ(2, GetAllPooledSessionsReturnCount());
}
// Test that if the candidate pool size is reduced, extra sessions are
// destroyed.
TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) {
SetConfigurationWithPoolSize(2);
SetConfigurationWithPoolSize(1);
EXPECT_EQ(1, GetAllPooledSessionsReturnCount());
}
// According to JSEP, existing pooled sessions should be destroyed and new
// ones created when the ICE servers change.
TEST_F(PortAllocatorTest,
SetConfigurationRecreatesPooledSessionsWhenIceServersChange) {
cricket::ServerAddresses stun_servers_1 = {stun_server_1};
std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1,
webrtc::NO_PRUNE);
EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
// Update with a different set of servers (and also change pool size).
cricket::ServerAddresses stun_servers_2 = {stun_server_2};
std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2,
webrtc::NO_PRUNE);
EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
auto session_1 = TakePooledSession();
auto session_2 = TakePooledSession();
ASSERT_NE(nullptr, session_1.get());
ASSERT_NE(nullptr, session_2.get());
EXPECT_EQ(stun_servers_2, session_1->stun_servers());
EXPECT_EQ(turn_servers_2, session_1->turn_servers());
EXPECT_EQ(stun_servers_2, session_2->stun_servers());
EXPECT_EQ(turn_servers_2, session_2->turn_servers());
EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
}
TEST_F(PortAllocatorTest, GetPooledSessionReturnsNextSession) {
SetConfigurationWithPoolSize(2);
auto peeked_session_1 = GetPooledSession();
auto session_1 = TakePooledSession();
EXPECT_EQ(session_1.get(), peeked_session_1);
auto peeked_session_2 = GetPooledSession();
auto session_2 = TakePooledSession();
EXPECT_EQ(session_2.get(), peeked_session_2);
}
// Verify that subclasses of PortAllocatorSession are given a chance to update
// ICE parameters when TakePooledSession is called, and the base class updates
// the info itself.
TEST_F(PortAllocatorTest, TakePooledSessionUpdatesIceParameters) {
SetConfigurationWithPoolSize(1);
auto peeked_session = GetPooledSession();
ASSERT_NE(nullptr, peeked_session);
EXPECT_EQ(0, peeked_session->transport_info_update_count());
std::unique_ptr<cricket::FakePortAllocatorSession> session(
static_cast<cricket::FakePortAllocatorSession*>(
allocator_->TakePooledSession(kContentName, 1, kIceUfrag, kIcePwd)
.release()));
EXPECT_EQ(1, session->transport_info_update_count());
EXPECT_EQ(kContentName, session->content_name());
EXPECT_EQ(1, session->component());
EXPECT_EQ(kIceUfrag, session->ice_ufrag());
EXPECT_EQ(kIcePwd, session->ice_pwd());
}
// According to JSEP, candidate filtering should be done when the pooled
// candidates are surfaced to the application. This means when a pooled
// session is taken. So a pooled session should gather candidates
// unfiltered until it's returned by TakePooledSession.
TEST_F(PortAllocatorTest, TakePooledSessionUpdatesCandidateFilter) {
Reland "Surface ICE candidates that match an updated candidate filter." This is a reland of cd8d1cf68e4eeed71fba51c97006a91bfd41813d Original change's description: > Surface ICE candidates that match an updated candidate filter. > > After this change an ICE agent can surface candidates that do not match > the previous filter but are allowed by the updated one. The candidate > filter, as part of the internal implementation in the ICE transport, > manifests the RTCIceTransportPolicy field in RTCConfiguration. > > This new feature would allow an ICE agent to gather new candidates when > the transport policy changes from e.g. 'relay' to 'all' without an ICE > restart. > > A caveat in the current implementation remains, and a candidate can > surface multiple times if the transport policy, or the candidate filter > directly, performs multiple transitions from a value that disallows to > one that allows the underlying candidate type. For example, if the > transport policy is updated by 'all' -> 'relay' -> 'all', the same host > candidate can surface after the second update. > > > Bug: webrtc:8939 > Change-Id: I92c2e07dafab225c702c5de28f47958a0d3270cc > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/132282 > Commit-Queue: Qingsi Wang <qingsi@webrtc.org> > Reviewed-by: Jeroen de Borst <jeroendb@webrtc.org> > Reviewed-by: Seth Hampson <shampson@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#27674} Bug: webrtc:8939 Change-Id: I9c32b1ea05028ecd937ab4912779dd958faf734f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/133582 Reviewed-by: Seth Hampson <shampson@webrtc.org> Reviewed-by: Jeroen de Borst <jeroendb@webrtc.org> Commit-Queue: Qingsi Wang <qingsi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#27694}
2019-04-18 10:41:58 -07:00
allocator_->SetCandidateFilter(cricket::CF_RELAY);
SetConfigurationWithPoolSize(1);
auto peeked_session = GetPooledSession();
ASSERT_NE(nullptr, peeked_session);
EXPECT_EQ(cricket::CF_ALL, peeked_session->candidate_filter());
auto session = TakePooledSession();
EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
}
// Verify that after DiscardCandidatePool, TakePooledSession doesn't return
// anything.
TEST_F(PortAllocatorTest, DiscardCandidatePool) {
SetConfigurationWithPoolSize(1);
allocator_->DiscardCandidatePool();
EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
}
TEST_F(PortAllocatorTest, RestrictIceCredentialsChange) {
SetConfigurationWithPoolSize(1);
EXPECT_EQ(1, GetAllPooledSessionsReturnCount());
allocator_->DiscardCandidatePool();
// Only return pooled sessions with the ice credentials that
// match those requested in TakePooledSession().
allocator_->set_restrict_ice_credentials_change(true);
SetConfigurationWithPoolSize(1);
EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
allocator_->DiscardCandidatePool();
SetConfigurationWithPoolSize(1);
auto credentials = allocator_->GetPooledIceCredentials();
ASSERT_EQ(1u, credentials.size());
EXPECT_EQ(nullptr,
allocator_->TakePooledSession(kContentName, 0, kIceUfrag, kIcePwd));
EXPECT_NE(nullptr,
allocator_->TakePooledSession(kContentName, 0, credentials[0].ufrag,
credentials[0].pwd));
EXPECT_EQ(nullptr,
allocator_->TakePooledSession(kContentName, 0, credentials[0].ufrag,
credentials[0].pwd));
allocator_->DiscardCandidatePool();
}
// Constants for testing candidates
const char kIpv4Address[] = "12.34.56.78";
const char kIpv4AddressWithPort[] = "12.34.56.78:443";
TEST_F(PortAllocatorTest, SanitizeEmptyCandidateDefaultConfig) {
cricket::Candidate input;
cricket::Candidate output = allocator_->SanitizeCandidate(input);
EXPECT_EQ("", output.address().ipaddr().ToString());
}
TEST_F(PortAllocatorTest, SanitizeIpv4CandidateDefaultConfig) {
cricket::Candidate input(1, "udp", rtc::SocketAddress(kIpv4Address, 443), 1,
"username", "password", cricket::LOCAL_PORT_TYPE, 1,
"foundation", 1, 1);
cricket::Candidate output = allocator_->SanitizeCandidate(input);
EXPECT_EQ(kIpv4AddressWithPort, output.address().ToString());
EXPECT_EQ(kIpv4Address, output.address().ipaddr().ToString());
}
TEST_F(PortAllocatorTest, SanitizeIpv4CandidateMdnsObfuscationEnabled) {
allocator_->SetMdnsObfuscationEnabledForTesting(true);
cricket::Candidate input(1, "udp", rtc::SocketAddress(kIpv4Address, 443), 1,
"username", "password", cricket::LOCAL_PORT_TYPE, 1,
"foundation", 1, 1);
cricket::Candidate output = allocator_->SanitizeCandidate(input);
EXPECT_NE(kIpv4AddressWithPort, output.address().ToString());
EXPECT_EQ("", output.address().ipaddr().ToString());
}
TEST_F(PortAllocatorTest, SanitizePrflxCandidateMdnsObfuscationEnabled) {
allocator_->SetMdnsObfuscationEnabledForTesting(true);
// Create the candidate from an IP literal. This populates the hostname.
cricket::Candidate input(1, "udp", rtc::SocketAddress(kIpv4Address, 443), 1,
"username", "password", cricket::PRFLX_PORT_TYPE, 1,
"foundation", 1, 1);
cricket::Candidate output = allocator_->SanitizeCandidate(input);
EXPECT_NE(kIpv4AddressWithPort, output.address().ToString());
EXPECT_EQ("", output.address().ipaddr().ToString());
}
TEST_F(PortAllocatorTest,
SanitizePrflxCandidateMdnsObfuscationEnabledRelatedAddress) {
allocator_->SetMdnsObfuscationEnabledForTesting(true);
// Create the candidate from an IP literal. This populates the hostname.
cricket::Candidate input(1, "udp", rtc::SocketAddress(kIpv4Address, 443), 1,
"username", "password", cricket::PRFLX_PORT_TYPE, 1,
"foundation", 1, 1);
cricket::Candidate output = allocator_->SanitizeCandidate(input);
EXPECT_NE(kIpv4AddressWithPort, output.address().ToString());
EXPECT_EQ("", output.address().ipaddr().ToString());
EXPECT_NE(kIpv4AddressWithPort, output.related_address().ToString());
EXPECT_EQ("", output.related_address().ipaddr().ToString());
}
TEST_F(PortAllocatorTest, SanitizeIpv4NonLiteralMdnsObfuscationEnabled) {
// Create the candidate with an empty hostname.
allocator_->SetMdnsObfuscationEnabledForTesting(true);
rtc::IPAddress ip;
EXPECT_TRUE(IPFromString(kIpv4Address, &ip));
cricket::Candidate input(1, "udp", rtc::SocketAddress(ip, 443), 1, "username",
"password", cricket::LOCAL_PORT_TYPE, 1,
"foundation", 1, 1);
cricket::Candidate output = allocator_->SanitizeCandidate(input);
EXPECT_NE(kIpv4AddressWithPort, output.address().ToString());
EXPECT_EQ("", output.address().ipaddr().ToString());
}