2014-10-28 22:20:11 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/stun_port.h"
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2016-04-27 07:22:53 -07:00
|
|
|
#include <memory>
|
|
|
|
|
|
2022-08-10 21:12:50 +02:00
|
|
|
#include "api/test/mock_async_dns_resolver.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/basic_packet_socket_factory.h"
|
2022-12-02 17:07:07 +01:00
|
|
|
#include "p2p/base/mock_dns_resolving_packet_socket_factory.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/test_stun_server.h"
|
2023-11-20 12:21:34 +01:00
|
|
|
#include "rtc_base/async_packet_socket.h"
|
2024-06-06 07:31:07 -07:00
|
|
|
#include "rtc_base/crypto_random.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/gunit.h"
|
2023-11-20 12:21:34 +01:00
|
|
|
#include "rtc_base/network/received_packet.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/socket_address.h"
|
|
|
|
|
#include "rtc_base/ssl_adapter.h"
|
|
|
|
|
#include "rtc_base/virtual_socket_server.h"
|
2018-09-17 17:06:08 -07:00
|
|
|
#include "test/gmock.h"
|
2022-03-28 14:58:26 +02:00
|
|
|
#include "test/scoped_key_value_config.h"
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2022-08-10 21:12:50 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
using cricket::ServerAddresses;
|
|
|
|
|
using rtc::SocketAddress;
|
2018-09-17 17:06:08 -07:00
|
|
|
using ::testing::_;
|
2022-08-10 21:12:50 +02:00
|
|
|
using ::testing::DoAll;
|
2018-09-17 17:06:08 -07:00
|
|
|
using ::testing::Return;
|
2022-08-10 21:12:50 +02:00
|
|
|
using ::testing::ReturnPointee;
|
|
|
|
|
using ::testing::SetArgPointee;
|
2024-03-11 16:54:12 +01:00
|
|
|
using webrtc::IceCandidateType;
|
2022-08-10 21:12:50 +02:00
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
static const SocketAddress kLocalAddr("127.0.0.1", 0);
|
2022-08-10 21:12:50 +02:00
|
|
|
static const SocketAddress kIPv6LocalAddr("::1", 0);
|
2014-10-28 22:20:11 +00:00
|
|
|
static const SocketAddress kStunAddr1("127.0.0.1", 5000);
|
|
|
|
|
static const SocketAddress kStunAddr2("127.0.0.1", 4000);
|
2016-05-20 15:08:29 -07:00
|
|
|
static const SocketAddress kStunAddr3("127.0.0.1", 3000);
|
2022-08-10 21:12:50 +02:00
|
|
|
static const SocketAddress kIPv6StunAddr1("::1", 5000);
|
2014-10-28 22:20:11 +00:00
|
|
|
static const SocketAddress kBadAddr("0.0.0.1", 5000);
|
2024-08-13 12:58:52 -07:00
|
|
|
static const SocketAddress kIPv6BadAddr("::ffff:0:1", 5000);
|
2022-08-10 21:12:50 +02:00
|
|
|
static const SocketAddress kValidHostnameAddr("valid-hostname", 5000);
|
2014-10-28 22:20:11 +00:00
|
|
|
static const SocketAddress kBadHostnameAddr("not-a-real-hostname", 5000);
|
2017-02-08 14:42:22 -08:00
|
|
|
// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
|
2016-05-23 12:49:30 -07:00
|
|
|
// Add some margin of error for slow bots.
|
2017-02-08 14:42:22 -08:00
|
|
|
static const int kTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
|
2022-08-10 21:12:50 +02:00
|
|
|
// stun prio = 100 (srflx) << 24 | 30 (IPv4) << 8 | 256 - 1 (component)
|
|
|
|
|
static const uint32_t kStunCandidatePriority =
|
|
|
|
|
(100 << 24) | (30 << 8) | (256 - 1);
|
|
|
|
|
// stun prio = 100 (srflx) << 24 | 60 (loopback IPv6) << 8 | 256 - 1 (component)
|
|
|
|
|
static const uint32_t kIPv6StunCandidatePriority =
|
|
|
|
|
(100 << 24) | (60 << 8) | (256 - 1);
|
2016-03-03 08:27:47 -08:00
|
|
|
static const int kInfiniteLifetime = -1;
|
|
|
|
|
static const int kHighCostPortKeepaliveLifetimeMs = 2 * 60 * 1000;
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2022-10-27 20:08:23 +02:00
|
|
|
constexpr uint64_t kTiebreakerDefault = 44444;
|
|
|
|
|
|
2022-11-30 21:29:02 +09:00
|
|
|
class FakeMdnsResponder : public webrtc::MdnsResponderInterface {
|
|
|
|
|
public:
|
|
|
|
|
void CreateNameForAddress(const rtc::IPAddress& addr,
|
|
|
|
|
NameCreatedCallback callback) override {
|
|
|
|
|
callback(addr, std::string("unittest-mdns-host-name.local"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveNameForAddress(const rtc::IPAddress& addr,
|
|
|
|
|
NameRemovedCallback callback) override {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FakeMdnsResponderProvider : public rtc::MdnsResponderProvider {
|
|
|
|
|
public:
|
|
|
|
|
FakeMdnsResponderProvider() : mdns_responder_(new FakeMdnsResponder()) {}
|
|
|
|
|
|
|
|
|
|
webrtc::MdnsResponderInterface* GetMdnsResponder() const override {
|
|
|
|
|
return mdns_responder_.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder_;
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-10 21:12:50 +02:00
|
|
|
// Base class for tests connecting a StunPort to a fake STUN server
|
|
|
|
|
// (cricket::StunServer).
|
2019-04-09 15:11:12 +02:00
|
|
|
class StunPortTestBase : public ::testing::Test, public sigslot::has_slots<> {
|
2014-10-28 22:20:11 +00:00
|
|
|
public:
|
2017-02-08 13:18:00 -08:00
|
|
|
StunPortTestBase()
|
2022-08-10 21:12:50 +02:00
|
|
|
: StunPortTestBase(
|
|
|
|
|
rtc::Network("unittest", "unittest", kLocalAddr.ipaddr(), 32),
|
|
|
|
|
kLocalAddr.ipaddr()) {}
|
|
|
|
|
|
|
|
|
|
StunPortTestBase(rtc::Network network, const rtc::IPAddress address)
|
2017-05-16 18:00:06 -07:00
|
|
|
: ss_(new rtc::VirtualSocketServer()),
|
2017-05-08 05:25:41 -07:00
|
|
|
thread_(ss_.get()),
|
2022-08-10 21:12:50 +02:00
|
|
|
network_(network),
|
2021-09-07 09:16:49 +02:00
|
|
|
socket_factory_(ss_.get()),
|
2023-11-22 14:08:58 +01:00
|
|
|
stun_server_1_(
|
|
|
|
|
cricket::TestStunServer::Create(ss_.get(), kStunAddr1, thread_)),
|
|
|
|
|
stun_server_2_(
|
|
|
|
|
cricket::TestStunServer::Create(ss_.get(), kStunAddr2, thread_)),
|
2022-11-30 21:29:02 +09:00
|
|
|
mdns_responder_provider_(new FakeMdnsResponderProvider()),
|
2016-03-03 08:27:47 -08:00
|
|
|
done_(false),
|
|
|
|
|
error_(false),
|
2017-02-08 13:18:00 -08:00
|
|
|
stun_keepalive_delay_(1),
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
stun_keepalive_lifetime_(-1) {
|
2022-08-10 21:12:50 +02:00
|
|
|
network_.AddIP(address);
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2022-08-10 21:12:50 +02:00
|
|
|
virtual rtc::PacketSocketFactory* socket_factory() {
|
|
|
|
|
return &socket_factory_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rtc::VirtualSocketServer* ss() const { return ss_.get(); }
|
2016-03-03 08:27:47 -08:00
|
|
|
cricket::UDPPort* port() const { return stun_port_.get(); }
|
2018-09-17 17:06:08 -07:00
|
|
|
rtc::AsyncPacketSocket* socket() const { return socket_.get(); }
|
2014-10-28 22:20:11 +00:00
|
|
|
bool done() const { return done_; }
|
|
|
|
|
bool error() const { return error_; }
|
|
|
|
|
|
2022-07-06 12:26:48 +02:00
|
|
|
bool HasPendingRequest(int msg_type) {
|
|
|
|
|
return stun_port_->request_manager().HasRequestForTest(msg_type);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 08:27:47 -08:00
|
|
|
void SetNetworkType(rtc::AdapterType adapter_type) {
|
|
|
|
|
network_.set_type(adapter_type);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 13:47:20 +02:00
|
|
|
void CreateStunPort(const rtc::SocketAddress& server_addr,
|
|
|
|
|
const webrtc::FieldTrialsView* field_trials = nullptr) {
|
2014-10-28 22:20:11 +00:00
|
|
|
ServerAddresses stun_servers;
|
|
|
|
|
stun_servers.insert(server_addr);
|
2022-08-11 13:47:20 +02:00
|
|
|
CreateStunPort(stun_servers, field_trials);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-11 13:47:20 +02:00
|
|
|
void CreateStunPort(const ServerAddresses& stun_servers,
|
|
|
|
|
const webrtc::FieldTrialsView* field_trials = nullptr) {
|
2018-10-10 11:29:44 -07:00
|
|
|
stun_port_ = cricket::StunPort::Create(
|
2024-04-09 17:35:41 -07:00
|
|
|
{.network_thread = rtc::Thread::Current(),
|
|
|
|
|
.socket_factory = socket_factory(),
|
|
|
|
|
.network = &network_,
|
|
|
|
|
.ice_username_fragment = rtc::CreateRandomString(16),
|
|
|
|
|
.ice_password = rtc::CreateRandomString(22),
|
2024-05-31 09:28:48 +00:00
|
|
|
.field_trials = field_trials},
|
2024-08-29 13:00:40 +00:00
|
|
|
0, 0, stun_servers, std::nullopt);
|
2024-05-31 09:28:48 +00:00
|
|
|
stun_port_->SetIceTiebreaker(kTiebreakerDefault);
|
2014-10-28 22:20:11 +00:00
|
|
|
stun_port_->set_stun_keepalive_delay(stun_keepalive_delay_);
|
2021-07-26 15:12:41 +02:00
|
|
|
// If `stun_keepalive_lifetime_` is negative, let the stun port
|
2016-03-03 08:27:47 -08:00
|
|
|
// choose its lifetime from the network type.
|
|
|
|
|
if (stun_keepalive_lifetime_ >= 0) {
|
|
|
|
|
stun_port_->set_stun_keepalive_lifetime(stun_keepalive_lifetime_);
|
|
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
stun_port_->SignalPortComplete.connect(this,
|
2017-02-08 13:18:00 -08:00
|
|
|
&StunPortTestBase::OnPortComplete);
|
|
|
|
|
stun_port_->SignalPortError.connect(this, &StunPortTestBase::OnPortError);
|
2019-06-01 12:23:43 +03:00
|
|
|
stun_port_->SignalCandidateError.connect(
|
|
|
|
|
this, &StunPortTestBase::OnCandidateError);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-11 13:47:20 +02:00
|
|
|
void CreateSharedUdpPort(
|
|
|
|
|
const rtc::SocketAddress& server_addr,
|
|
|
|
|
rtc::AsyncPacketSocket* socket,
|
|
|
|
|
const webrtc::FieldTrialsView* field_trials = nullptr) {
|
2018-09-17 17:06:08 -07:00
|
|
|
if (socket) {
|
|
|
|
|
socket_.reset(socket);
|
|
|
|
|
} else {
|
2022-08-10 21:12:50 +02:00
|
|
|
socket_.reset(socket_factory()->CreateUdpSocket(
|
2018-09-17 17:06:08 -07:00
|
|
|
rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0));
|
|
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
ASSERT_TRUE(socket_ != NULL);
|
2023-11-20 12:21:34 +01:00
|
|
|
socket_->RegisterReceivedPacketCallback(
|
|
|
|
|
[&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) {
|
|
|
|
|
OnReadPacket(socket, packet);
|
|
|
|
|
});
|
2018-10-10 11:29:44 -07:00
|
|
|
stun_port_ = cricket::UDPPort::Create(
|
2024-04-09 17:35:41 -07:00
|
|
|
{.network_thread = rtc::Thread::Current(),
|
|
|
|
|
.socket_factory = socket_factory(),
|
|
|
|
|
.network = &network_,
|
|
|
|
|
.ice_username_fragment = rtc::CreateRandomString(16),
|
|
|
|
|
.ice_password = rtc::CreateRandomString(22),
|
2024-05-31 09:28:48 +00:00
|
|
|
.field_trials = field_trials},
|
2024-08-29 13:00:40 +00:00
|
|
|
socket_.get(), false, std::nullopt);
|
2014-10-28 22:20:11 +00:00
|
|
|
ASSERT_TRUE(stun_port_ != NULL);
|
2024-05-31 09:28:48 +00:00
|
|
|
stun_port_->SetIceTiebreaker(kTiebreakerDefault);
|
2014-10-28 22:20:11 +00:00
|
|
|
ServerAddresses stun_servers;
|
|
|
|
|
stun_servers.insert(server_addr);
|
|
|
|
|
stun_port_->set_server_addresses(stun_servers);
|
|
|
|
|
stun_port_->SignalPortComplete.connect(this,
|
2017-02-08 13:18:00 -08:00
|
|
|
&StunPortTestBase::OnPortComplete);
|
|
|
|
|
stun_port_->SignalPortError.connect(this, &StunPortTestBase::OnPortError);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PrepareAddress() { stun_port_->PrepareAddress(); }
|
|
|
|
|
|
|
|
|
|
void OnReadPacket(rtc::AsyncPacketSocket* socket,
|
2023-11-20 12:21:34 +01:00
|
|
|
const rtc::ReceivedPacket& packet) {
|
|
|
|
|
stun_port_->HandleIncomingPacket(socket, packet);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendData(const char* data, size_t len) {
|
2023-11-20 12:21:34 +01:00
|
|
|
stun_port_->HandleIncomingPacket(socket_.get(),
|
|
|
|
|
rtc::ReceivedPacket::CreateFromLegacy(
|
|
|
|
|
data, len, /* packet_time_us */ -1,
|
|
|
|
|
rtc::SocketAddress("22.22.22.22", 0)));
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 21:29:02 +09:00
|
|
|
void EnableMdnsObfuscation() {
|
|
|
|
|
network_.set_mdns_responder_provider(mdns_responder_provider_.get());
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
protected:
|
2019-06-04 09:01:51 +02:00
|
|
|
static void SetUpTestSuite() {
|
2014-10-28 22:20:11 +00:00
|
|
|
// Ensure the RNG is inited.
|
|
|
|
|
rtc::InitRandom(NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 12:15:38 +02:00
|
|
|
void OnPortComplete(cricket::Port* /* port */) {
|
2014-10-28 22:20:11 +00:00
|
|
|
ASSERT_FALSE(done_);
|
|
|
|
|
done_ = true;
|
|
|
|
|
error_ = false;
|
|
|
|
|
}
|
2024-11-20 12:15:38 +02:00
|
|
|
void OnPortError(cricket::Port* /* port */) {
|
2014-10-28 22:20:11 +00:00
|
|
|
done_ = true;
|
|
|
|
|
error_ = true;
|
|
|
|
|
}
|
2024-11-20 12:15:38 +02:00
|
|
|
void OnCandidateError(cricket::Port* /* port */,
|
2019-06-01 12:23:43 +03:00
|
|
|
const cricket::IceCandidateErrorEvent& event) {
|
|
|
|
|
error_event_ = event;
|
|
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
void SetKeepaliveDelay(int delay) { stun_keepalive_delay_ = delay; }
|
|
|
|
|
|
2016-03-03 08:27:47 -08:00
|
|
|
void SetKeepaliveLifetime(int lifetime) {
|
|
|
|
|
stun_keepalive_lifetime_ = lifetime;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
cricket::TestStunServer* stun_server_1() { return stun_server_1_.get(); }
|
|
|
|
|
cricket::TestStunServer* stun_server_2() { return stun_server_2_.get(); }
|
|
|
|
|
|
2023-11-22 14:08:58 +01:00
|
|
|
rtc::AutoSocketServerThread& thread() { return thread_; }
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
private:
|
2016-04-27 07:22:53 -07:00
|
|
|
std::unique_ptr<rtc::VirtualSocketServer> ss_;
|
2017-05-08 05:25:41 -07:00
|
|
|
rtc::AutoSocketServerThread thread_;
|
2014-10-28 22:20:11 +00:00
|
|
|
rtc::Network network_;
|
|
|
|
|
rtc::BasicPacketSocketFactory socket_factory_;
|
2016-04-27 07:22:53 -07:00
|
|
|
std::unique_ptr<cricket::UDPPort> stun_port_;
|
2023-11-22 14:08:58 +01:00
|
|
|
cricket::TestStunServer::StunServerPtr stun_server_1_;
|
|
|
|
|
cricket::TestStunServer::StunServerPtr stun_server_2_;
|
2016-04-27 07:22:53 -07:00
|
|
|
std::unique_ptr<rtc::AsyncPacketSocket> socket_;
|
2022-11-30 21:29:02 +09:00
|
|
|
std::unique_ptr<rtc::MdnsResponderProvider> mdns_responder_provider_;
|
2014-10-28 22:20:11 +00:00
|
|
|
bool done_;
|
|
|
|
|
bool error_;
|
|
|
|
|
int stun_keepalive_delay_;
|
2016-03-03 08:27:47 -08:00
|
|
|
int stun_keepalive_lifetime_;
|
2019-06-01 12:23:43 +03:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
cricket::IceCandidateErrorEvent error_event_;
|
2014-10-28 22:20:11 +00:00
|
|
|
};
|
|
|
|
|
|
2017-02-08 13:18:00 -08:00
|
|
|
class StunPortTestWithRealClock : public StunPortTestBase {};
|
|
|
|
|
|
|
|
|
|
class FakeClockBase {
|
|
|
|
|
public:
|
|
|
|
|
rtc::ScopedFakeClock fake_clock;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class StunPortTest : public FakeClockBase, public StunPortTestBase {};
|
|
|
|
|
|
2016-06-28 09:44:47 -07:00
|
|
|
// Test that we can create a STUN port.
|
|
|
|
|
TEST_F(StunPortTest, TestCreateStunPort) {
|
2014-10-28 22:20:11 +00:00
|
|
|
CreateStunPort(kStunAddr1);
|
2024-03-11 16:54:12 +01:00
|
|
|
EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
|
2014-10-28 22:20:11 +00:00
|
|
|
EXPECT_EQ(0U, port()->Candidates().size());
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-28 09:44:47 -07:00
|
|
|
// Test that we can create a UDP port.
|
|
|
|
|
TEST_F(StunPortTest, TestCreateUdpPort) {
|
2018-09-17 17:06:08 -07:00
|
|
|
CreateSharedUdpPort(kStunAddr1, nullptr);
|
2024-03-11 16:54:12 +01:00
|
|
|
EXPECT_EQ(IceCandidateType::kHost, port()->Type());
|
2016-06-28 09:44:47 -07:00
|
|
|
EXPECT_EQ(0U, port()->Candidates().size());
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
// Test that we can get an address from a STUN server.
|
|
|
|
|
TEST_F(StunPortTest, TestPrepareAddress) {
|
|
|
|
|
CreateStunPort(kStunAddr1);
|
|
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
2017-02-13 12:47:27 -08:00
|
|
|
std::string expected_server_url = "stun:127.0.0.1:5000";
|
|
|
|
|
EXPECT_EQ(port()->Candidates()[0].url(), expected_server_url);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that we fail properly if we can't get an address.
|
|
|
|
|
TEST_F(StunPortTest, TestPrepareAddressFail) {
|
|
|
|
|
CreateStunPort(kBadAddr);
|
|
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
EXPECT_TRUE(error());
|
|
|
|
|
EXPECT_EQ(0U, port()->Candidates().size());
|
2019-06-01 12:23:43 +03:00
|
|
|
EXPECT_EQ_SIMULATED_WAIT(error_event_.error_code,
|
2024-08-13 12:58:52 -07:00
|
|
|
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs,
|
2019-06-01 12:23:43 +03:00
|
|
|
fake_clock);
|
2024-08-13 12:58:52 -07:00
|
|
|
EXPECT_NE(error_event_.error_text.find('.'), std::string::npos);
|
|
|
|
|
EXPECT_NE(error_event_.address.find(kLocalAddr.HostAsSensitiveURIString()),
|
2019-12-02 22:22:07 +02:00
|
|
|
std::string::npos);
|
2019-06-01 12:23:43 +03:00
|
|
|
std::string server_url = "stun:" + kBadAddr.ToString();
|
2024-08-13 12:58:52 -07:00
|
|
|
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);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-10 21:12:50 +02:00
|
|
|
class StunPortWithMockDnsResolverTest : public StunPortTest {
|
|
|
|
|
public:
|
|
|
|
|
StunPortWithMockDnsResolverTest() : StunPortTest(), socket_factory_(ss()) {}
|
|
|
|
|
|
|
|
|
|
rtc::PacketSocketFactory* socket_factory() override {
|
|
|
|
|
return &socket_factory_;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 17:07:07 +01:00
|
|
|
void SetDnsResolverExpectations(
|
|
|
|
|
rtc::MockDnsResolvingPacketSocketFactory::Expectations expectations) {
|
2022-08-10 21:12:50 +02:00
|
|
|
socket_factory_.SetExpectations(expectations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2022-12-02 17:07:07 +01:00
|
|
|
rtc::MockDnsResolvingPacketSocketFactory socket_factory_;
|
2022-08-10 21:12:50 +02:00
|
|
|
};
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
// Test that we can get an address from a STUN server specified by a hostname.
|
2022-08-10 22:48:03 +02:00
|
|
|
TEST_F(StunPortWithMockDnsResolverTest, TestPrepareAddressHostname) {
|
2022-08-10 21:12:50 +02:00
|
|
|
SetDnsResolverExpectations(
|
|
|
|
|
[](webrtc::MockAsyncDnsResolver* resolver,
|
|
|
|
|
webrtc::MockAsyncDnsResolverResult* resolver_result) {
|
2023-04-13 10:08:14 +02:00
|
|
|
EXPECT_CALL(*resolver, Start(kValidHostnameAddr, /*family=*/AF_INET, _))
|
2024-11-20 12:15:38 +02:00
|
|
|
.WillOnce([](const rtc::SocketAddress& /* addr */, int /* family */,
|
2023-08-30 19:39:05 +00:00
|
|
|
absl::AnyInvocable<void()> callback) { callback(); });
|
|
|
|
|
|
2022-08-10 21:12:50 +02:00
|
|
|
EXPECT_CALL(*resolver, result)
|
|
|
|
|
.WillRepeatedly(ReturnPointee(resolver_result));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetResolvedAddress(AF_INET, _))
|
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(SocketAddress("127.0.0.1", 5000)),
|
|
|
|
|
Return(true)));
|
|
|
|
|
});
|
|
|
|
|
CreateStunPort(kValidHostnameAddr);
|
2014-10-28 22:20:11 +00:00
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
|
EXPECT_EQ(kStunCandidatePriority, port()->Candidates()[0].priority());
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 13:58:45 +02:00
|
|
|
TEST_F(StunPortWithMockDnsResolverTest,
|
|
|
|
|
TestPrepareAddressHostnameWithPriorityAdjustment) {
|
|
|
|
|
webrtc::test::ScopedKeyValueConfig field_trials(
|
|
|
|
|
"WebRTC-IncreaseIceCandidatePriorityHostSrflx/Enabled/");
|
|
|
|
|
SetDnsResolverExpectations(
|
|
|
|
|
[](webrtc::MockAsyncDnsResolver* resolver,
|
|
|
|
|
webrtc::MockAsyncDnsResolverResult* resolver_result) {
|
|
|
|
|
EXPECT_CALL(*resolver, Start(kValidHostnameAddr, /*family=*/AF_INET, _))
|
2024-11-20 12:15:38 +02:00
|
|
|
.WillOnce([](const rtc::SocketAddress& /* addr */, int /* family */,
|
2023-08-30 19:39:05 +00:00
|
|
|
absl::AnyInvocable<void()> callback) { callback(); });
|
2023-06-16 13:58:45 +02:00
|
|
|
EXPECT_CALL(*resolver, result)
|
|
|
|
|
.WillRepeatedly(ReturnPointee(resolver_result));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetResolvedAddress(AF_INET, _))
|
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(SocketAddress("127.0.0.1", 5000)),
|
|
|
|
|
Return(true)));
|
|
|
|
|
});
|
|
|
|
|
CreateStunPort(kValidHostnameAddr);
|
|
|
|
|
PrepareAddress();
|
|
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
|
EXPECT_EQ(kStunCandidatePriority + (cricket::kMaxTurnServers << 8),
|
|
|
|
|
port()->Candidates()[0].priority());
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
// Test that we handle hostname lookup failures properly.
|
2017-02-08 13:18:00 -08:00
|
|
|
TEST_F(StunPortTestWithRealClock, TestPrepareAddressHostnameFail) {
|
2014-10-28 22:20:11 +00:00
|
|
|
CreateStunPort(kBadHostnameAddr);
|
|
|
|
|
PrepareAddress();
|
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
|
EXPECT_TRUE(error());
|
|
|
|
|
EXPECT_EQ(0U, port()->Candidates().size());
|
2024-08-13 12:58:52 -07:00
|
|
|
EXPECT_EQ_WAIT(error_event_.error_code,
|
|
|
|
|
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This test verifies keepalive response messages don't result in
|
|
|
|
|
// additional candidate generation.
|
|
|
|
|
TEST_F(StunPortTest, TestKeepAliveResponse) {
|
|
|
|
|
SetKeepaliveDelay(500); // 500ms of keepalive delay.
|
2017-05-15 19:43:33 -07:00
|
|
|
CreateStunPort(kStunAddr1);
|
2014-10-28 22:20:11 +00:00
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
2017-02-08 13:18:00 -08:00
|
|
|
SIMULATED_WAIT(false, 1000, fake_clock);
|
|
|
|
|
EXPECT_EQ(1U, port()->Candidates().size());
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that a local candidate can be generated using a shared socket.
|
|
|
|
|
TEST_F(StunPortTest, TestSharedSocketPrepareAddress) {
|
2018-09-17 17:06:08 -07:00
|
|
|
CreateSharedUdpPort(kStunAddr1, nullptr);
|
2014-10-28 22:20:11 +00:00
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 13:47:20 +02:00
|
|
|
// Test that we still get a local candidate with invalid stun server hostname.
|
2014-10-28 22:20:11 +00:00
|
|
|
// Also verifing that UDPPort can receive packets when stun address can't be
|
|
|
|
|
// resolved.
|
2017-02-08 13:18:00 -08:00
|
|
|
TEST_F(StunPortTestWithRealClock,
|
|
|
|
|
TestSharedSocketPrepareAddressInvalidHostname) {
|
2018-09-17 17:06:08 -07:00
|
|
|
CreateSharedUdpPort(kBadHostnameAddr, nullptr);
|
2014-10-28 22:20:11 +00:00
|
|
|
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.
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 21:29:02 +09:00
|
|
|
// 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()));
|
2024-01-24 08:36:45 +01:00
|
|
|
EXPECT_TRUE(port()->Candidates()[0].is_local());
|
2022-11-30 21:29:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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());
|
2024-01-19 13:11:37 +01:00
|
|
|
if (port()->Candidates()[0].is_local()) {
|
|
|
|
|
EXPECT_TRUE(port()->Candidates()[1].is_stun());
|
2022-11-30 21:29:02 +09:00
|
|
|
} else {
|
2024-01-19 13:11:37 +01:00
|
|
|
EXPECT_TRUE(port()->Candidates()[0].is_stun());
|
|
|
|
|
EXPECT_TRUE(port()->Candidates()[1].is_local());
|
2022-11-30 21:29:02 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 13:47:20 +02:00
|
|
|
// Test that the same address is added only once if two STUN servers are in
|
|
|
|
|
// use.
|
2014-10-28 22:20:11 +00:00
|
|
|
TEST_F(StunPortTest, TestNoDuplicatedAddressWithTwoStunServers) {
|
|
|
|
|
ServerAddresses stun_servers;
|
|
|
|
|
stun_servers.insert(kStunAddr1);
|
|
|
|
|
stun_servers.insert(kStunAddr2);
|
|
|
|
|
CreateStunPort(stun_servers);
|
2024-03-11 16:54:12 +01:00
|
|
|
EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
|
2014-10-28 22:20:11 +00:00
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
EXPECT_EQ(1U, port()->Candidates().size());
|
2015-08-19 16:51:15 -07:00
|
|
|
EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-11 13:47:20 +02:00
|
|
|
// Test that candidates can be allocated for multiple STUN servers, one of
|
|
|
|
|
// which is not reachable.
|
2014-10-28 22:20:11 +00:00
|
|
|
TEST_F(StunPortTest, TestMultipleStunServersWithBadServer) {
|
|
|
|
|
ServerAddresses stun_servers;
|
|
|
|
|
stun_servers.insert(kStunAddr1);
|
|
|
|
|
stun_servers.insert(kBadAddr);
|
|
|
|
|
CreateStunPort(stun_servers);
|
2024-03-11 16:54:12 +01:00
|
|
|
EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
|
2014-10-28 22:20:11 +00:00
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
EXPECT_EQ(1U, port()->Candidates().size());
|
2019-06-01 12:23:43 +03:00
|
|
|
std::string server_url = "stun:" + kBadAddr.ToString();
|
|
|
|
|
ASSERT_EQ_SIMULATED_WAIT(error_event_.url, server_url, kTimeoutMs,
|
|
|
|
|
fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
ServerAddresses stun_servers;
|
|
|
|
|
stun_servers.insert(kStunAddr1);
|
|
|
|
|
stun_servers.insert(kStunAddr2);
|
|
|
|
|
CreateStunPort(stun_servers);
|
2024-03-11 16:54:12 +01:00
|
|
|
EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
|
2014-10-28 22:20:11 +00:00
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2014-10-28 22:20:11 +00:00
|
|
|
EXPECT_EQ(2U, port()->Candidates().size());
|
2015-08-19 16:51:15 -07:00
|
|
|
EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
|
|
|
|
|
EXPECT_EQ(port()->Candidates()[1].relay_protocol(), "");
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
2016-03-03 08:27:47 -08:00
|
|
|
|
|
|
|
|
// Test that the stun_keepalive_lifetime is set correctly based on the network
|
2016-05-20 15:08:29 -07:00
|
|
|
// type on a STUN port. Also test that it will be updated if the network type
|
|
|
|
|
// changes.
|
2016-03-03 08:27:47 -08:00
|
|
|
TEST_F(StunPortTest, TestStunPortGetStunKeepaliveLifetime) {
|
2021-07-26 15:12:41 +02:00
|
|
|
// Lifetime for the default (unknown) network type is `kInfiniteLifetime`.
|
2016-03-03 08:27:47 -08:00
|
|
|
CreateStunPort(kStunAddr1);
|
|
|
|
|
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
|
2021-07-26 15:12:41 +02:00
|
|
|
// Lifetime for the cellular network is `kHighCostPortKeepaliveLifetimeMs`
|
2016-03-03 08:27:47 -08:00
|
|
|
SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
|
|
|
|
|
EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs,
|
|
|
|
|
port()->stun_keepalive_lifetime());
|
2016-05-20 15:08:29 -07:00
|
|
|
|
2021-07-26 15:12:41 +02:00
|
|
|
// Lifetime for the wifi network is `kInfiniteLifetime`.
|
2016-05-20 15:08:29 -07:00
|
|
|
SetNetworkType(rtc::ADAPTER_TYPE_WIFI);
|
|
|
|
|
CreateStunPort(kStunAddr2);
|
|
|
|
|
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
|
2016-03-03 08:27:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that the stun_keepalive_lifetime is set correctly based on the network
|
2016-05-20 15:08:29 -07:00
|
|
|
// type on a shared STUN port (UDPPort). Also test that it will be updated
|
|
|
|
|
// if the network type changes.
|
2016-03-03 08:27:47 -08:00
|
|
|
TEST_F(StunPortTest, TestUdpPortGetStunKeepaliveLifetime) {
|
2021-07-26 15:12:41 +02:00
|
|
|
// Lifetime for the default (unknown) network type is `kInfiniteLifetime`.
|
2018-09-17 17:06:08 -07:00
|
|
|
CreateSharedUdpPort(kStunAddr1, nullptr);
|
2016-03-03 08:27:47 -08:00
|
|
|
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
|
2021-07-26 15:12:41 +02:00
|
|
|
// Lifetime for the cellular network is `kHighCostPortKeepaliveLifetimeMs`.
|
2016-03-03 08:27:47 -08:00
|
|
|
SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
|
|
|
|
|
EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs,
|
|
|
|
|
port()->stun_keepalive_lifetime());
|
2016-05-20 15:08:29 -07:00
|
|
|
|
2021-07-26 15:12:41 +02:00
|
|
|
// Lifetime for the wifi network type is `kInfiniteLifetime`.
|
2016-05-20 15:08:29 -07:00
|
|
|
SetNetworkType(rtc::ADAPTER_TYPE_WIFI);
|
2018-09-17 17:06:08 -07:00
|
|
|
CreateSharedUdpPort(kStunAddr2, nullptr);
|
2016-05-20 15:08:29 -07:00
|
|
|
EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
|
2016-03-03 08:27:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2022-07-06 12:26:48 +02:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(!HasPendingRequest(cricket::STUN_BINDING_REQUEST),
|
|
|
|
|
2000, fake_clock);
|
2016-03-03 08:27:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that by default, the STUN binding requests will last for a long time.
|
|
|
|
|
TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) {
|
|
|
|
|
SetKeepaliveDelay(101);
|
|
|
|
|
CreateStunPort(kStunAddr1);
|
|
|
|
|
PrepareAddress();
|
2017-02-08 13:18:00 -08:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
2022-07-06 12:26:48 +02:00
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(HasPendingRequest(cricket::STUN_BINDING_REQUEST),
|
|
|
|
|
1000, fake_clock);
|
2016-03-03 08:27:47 -08:00
|
|
|
}
|
2018-09-17 17:06:08 -07:00
|
|
|
|
|
|
|
|
class MockAsyncPacketSocket : public rtc::AsyncPacketSocket {
|
|
|
|
|
public:
|
|
|
|
|
~MockAsyncPacketSocket() = default;
|
|
|
|
|
|
2020-05-25 09:36:40 +02:00
|
|
|
MOCK_METHOD(SocketAddress, GetLocalAddress, (), (const, override));
|
|
|
|
|
MOCK_METHOD(SocketAddress, GetRemoteAddress, (), (const, override));
|
|
|
|
|
MOCK_METHOD(int,
|
|
|
|
|
Send,
|
|
|
|
|
(const void* pv, size_t cb, const rtc::PacketOptions& options),
|
|
|
|
|
(override));
|
|
|
|
|
|
|
|
|
|
MOCK_METHOD(int,
|
|
|
|
|
SendTo,
|
|
|
|
|
(const void* pv,
|
|
|
|
|
size_t cb,
|
|
|
|
|
const SocketAddress& addr,
|
|
|
|
|
const rtc::PacketOptions& options),
|
|
|
|
|
(override));
|
|
|
|
|
MOCK_METHOD(int, Close, (), (override));
|
|
|
|
|
MOCK_METHOD(State, GetState, (), (const, override));
|
|
|
|
|
MOCK_METHOD(int,
|
|
|
|
|
GetOption,
|
|
|
|
|
(rtc::Socket::Option opt, int* value),
|
|
|
|
|
(override));
|
|
|
|
|
MOCK_METHOD(int, SetOption, (rtc::Socket::Option opt, int value), (override));
|
|
|
|
|
MOCK_METHOD(int, GetError, (), (const, override));
|
|
|
|
|
MOCK_METHOD(void, SetError, (int error), (override));
|
2018-09-17 17:06:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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.
|
2019-04-09 15:11:12 +02:00
|
|
|
EXPECT_CALL(*socket,
|
|
|
|
|
SendTo(_, _, _,
|
|
|
|
|
::testing::Field(&rtc::PacketOptions::dscp,
|
|
|
|
|
::testing::Eq(rtc::DSCP_NO_CHANGE))))
|
2018-09-17 17:06:08 -07:00
|
|
|
.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(_, _, _,
|
2019-04-09 15:11:12 +02:00
|
|
|
::testing::Field(&rtc::PacketOptions::dscp,
|
|
|
|
|
::testing::Eq(rtc::DSCP_AF41))))
|
2018-09-17 17:06:08 -07:00
|
|
|
.WillRepeatedly(Return(100));
|
|
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
|
|
|
|
}
|
2022-08-10 21:12:50 +02:00
|
|
|
|
|
|
|
|
class StunIPv6PortTestBase : public StunPortTestBase {
|
|
|
|
|
public:
|
|
|
|
|
StunIPv6PortTestBase()
|
|
|
|
|
: StunPortTestBase(rtc::Network("unittestipv6",
|
|
|
|
|
"unittestipv6",
|
|
|
|
|
kIPv6LocalAddr.ipaddr(),
|
|
|
|
|
128),
|
|
|
|
|
kIPv6LocalAddr.ipaddr()) {
|
2023-11-22 14:08:58 +01:00
|
|
|
stun_server_ipv6_1_ =
|
|
|
|
|
cricket::TestStunServer::Create(ss(), kIPv6StunAddr1, thread());
|
2022-08-10 21:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2023-11-22 14:08:58 +01:00
|
|
|
cricket::TestStunServer::StunServerPtr stun_server_ipv6_1_;
|
2022-08-10 21:12:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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) {
|
2024-08-13 12:58:52 -07:00
|
|
|
CreateStunPort(kIPv6BadAddr);
|
2022-08-10 21:12:50 +02:00
|
|
|
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,
|
2024-08-13 12:58:52 -07:00
|
|
|
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs,
|
2022-08-10 21:12:50 +02:00
|
|
|
fake_clock);
|
2024-08-13 12:58:52 -07:00
|
|
|
EXPECT_NE(error_event_.error_text.find('.'), std::string::npos);
|
|
|
|
|
EXPECT_NE(
|
2022-08-10 21:12:50 +02:00
|
|
|
error_event_.address.find(kIPv6LocalAddr.HostAsSensitiveURIString()),
|
|
|
|
|
std::string::npos);
|
2024-08-13 12:58:52 -07:00
|
|
|
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);
|
2022-08-10 21:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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());
|
2024-08-13 12:58:52 -07:00
|
|
|
EXPECT_EQ_WAIT(error_event_.error_code,
|
|
|
|
|
cricket::STUN_ERROR_SERVER_NOT_REACHABLE, kTimeoutMs);
|
2022-08-10 21:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class StunIPv6PortTestWithMockDnsResolver : public StunIPv6PortTest {
|
|
|
|
|
public:
|
|
|
|
|
StunIPv6PortTestWithMockDnsResolver()
|
|
|
|
|
: StunIPv6PortTest(), socket_factory_(ss()) {}
|
|
|
|
|
|
|
|
|
|
rtc::PacketSocketFactory* socket_factory() override {
|
|
|
|
|
return &socket_factory_;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 17:07:07 +01:00
|
|
|
void SetDnsResolverExpectations(
|
|
|
|
|
rtc::MockDnsResolvingPacketSocketFactory::Expectations expectations) {
|
2022-08-10 21:12:50 +02:00
|
|
|
socket_factory_.SetExpectations(expectations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2022-12-02 17:07:07 +01:00
|
|
|
rtc::MockDnsResolvingPacketSocketFactory socket_factory_;
|
2022-08-10 21:12:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Test that we can get an address from a STUN server specified by a hostname.
|
2022-08-10 22:48:03 +02:00
|
|
|
TEST_F(StunIPv6PortTestWithMockDnsResolver, TestPrepareAddressHostname) {
|
2022-08-10 21:12:50 +02:00
|
|
|
SetDnsResolverExpectations(
|
|
|
|
|
[](webrtc::MockAsyncDnsResolver* resolver,
|
|
|
|
|
webrtc::MockAsyncDnsResolverResult* resolver_result) {
|
2022-09-02 15:02:04 +02:00
|
|
|
EXPECT_CALL(*resolver,
|
|
|
|
|
Start(kValidHostnameAddr, /*family=*/AF_INET6, _))
|
2023-08-30 19:39:05 +00:00
|
|
|
.WillOnce([](const rtc::SocketAddress& addr, int family,
|
|
|
|
|
absl::AnyInvocable<void()> callback) { callback(); });
|
|
|
|
|
|
2022-08-11 13:47:20 +02:00
|
|
|
EXPECT_CALL(*resolver, result)
|
|
|
|
|
.WillRepeatedly(ReturnPointee(resolver_result));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetResolvedAddress(AF_INET6, _))
|
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(SocketAddress("::1", 5000)),
|
|
|
|
|
Return(true)));
|
|
|
|
|
});
|
2023-04-13 10:08:14 +02:00
|
|
|
CreateStunPort(kValidHostnameAddr);
|
2022-08-11 13:47:20 +02:00
|
|
|
PrepareAddress();
|
|
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
|
EXPECT_TRUE(kIPv6LocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
|
EXPECT_EQ(kIPv6StunCandidatePriority, port()->Candidates()[0].priority());
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 13:58:45 +02:00
|
|
|
// Same as before but with a field trial that changes the priority.
|
|
|
|
|
TEST_F(StunIPv6PortTestWithMockDnsResolver,
|
|
|
|
|
TestPrepareAddressHostnameWithPriorityAdjustment) {
|
|
|
|
|
webrtc::test::ScopedKeyValueConfig field_trials(
|
|
|
|
|
"WebRTC-IncreaseIceCandidatePriorityHostSrflx/Enabled/");
|
|
|
|
|
SetDnsResolverExpectations(
|
|
|
|
|
[](webrtc::MockAsyncDnsResolver* resolver,
|
|
|
|
|
webrtc::MockAsyncDnsResolverResult* resolver_result) {
|
|
|
|
|
EXPECT_CALL(*resolver,
|
|
|
|
|
Start(kValidHostnameAddr, /*family=*/AF_INET6, _))
|
2023-08-30 19:39:05 +00:00
|
|
|
.WillOnce([](const rtc::SocketAddress& addr, int family,
|
|
|
|
|
absl::AnyInvocable<void()> callback) { callback(); });
|
2023-06-16 13:58:45 +02:00
|
|
|
EXPECT_CALL(*resolver, result)
|
|
|
|
|
.WillRepeatedly(ReturnPointee(resolver_result));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
|
|
|
|
|
EXPECT_CALL(*resolver_result, GetResolvedAddress(AF_INET6, _))
|
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(SocketAddress("::1", 5000)),
|
|
|
|
|
Return(true)));
|
|
|
|
|
});
|
|
|
|
|
CreateStunPort(kValidHostnameAddr, &field_trials);
|
|
|
|
|
PrepareAddress();
|
|
|
|
|
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
|
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
|
EXPECT_TRUE(kIPv6LocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
|
EXPECT_EQ(kIPv6StunCandidatePriority + (cricket::kMaxTurnServers << 8),
|
|
|
|
|
port()->Candidates()[0].priority());
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 21:12:50 +02:00
|
|
|
} // namespace
|