#include "net/socket/udp_socket.h"
#include <algorithm>
#include "base/containers/circular_deque.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_clear_last_error.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "net/base/features.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/network_interfaces.h"
#include "net/base/test_completion_callback.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source.h"
#include "net/log/test_net_log.h"
#include "net/log/test_net_log_util.h"
#include "net/socket/socket_test_util.h"
#include "net/socket/udp_client_socket.h"
#include "net/socket/udp_server_socket.h"
#include "net/socket/udp_socket_global_limits.h"
#include "net/test/gtest_util.h"
#include "net/test/test_with_task_environment.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if !BUILDFLAG(IS_WIN)
#include <netinet/in.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#include "net/android/network_change_notifier_factory_android.h"
#include "net/base/network_change_notifier.h"
#endif
#if BUILDFLAG(IS_IOS)
#include <TargetConditionals.h>
#endif
#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif
IsError;
IsOk;
DoAll;
Not;
namespace net {
namespace {
bool CreateUDPAddress(const std::string& ip_str,
uint16_t port,
IPEndPoint* address) { … }
class UDPSocketTest : public PlatformTest, public WithTaskEnvironment { … };
const int UDPSocketTest::kMaxRead;
void ReadCompleteCallback(int* result_out,
base::OnceClosure callback,
int result) { … }
void UDPSocketTest::ConnectTest(bool use_nonblocking_io, bool use_async) { … }
TEST_F(UDPSocketTest, Connect) { … }
#if BUILDFLAG(IS_WIN)
TEST_F(UDPSocketTest, ConnectNonBlocking) {
ConnectTest(true, false);
ConnectTest(true, true);
}
#endif
TEST_F(UDPSocketTest, PartialRecv) { … }
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_ANDROID)
#define MAYBE_LocalBroadcast …
#else
#define MAYBE_LocalBroadcast …
#endif
TEST_F(UDPSocketTest, MAYBE_LocalBroadcast) { … }
TEST_F(UDPSocketTest, ConnectRandomBind) { … }
TEST_F(UDPSocketTest, ConnectFail) { … }
TEST_F(UDPSocketTest, AdoptedSocket) { … }
TEST_F(UDPSocketTest, LimitAdoptSocket) { … }
TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { … }
TEST_F(UDPSocketTest, ClientGetLocalPeerAddresses) { … }
TEST_F(UDPSocketTest, ServerGetLocalAddress) { … }
TEST_F(UDPSocketTest, ServerGetPeerAddress) { … }
TEST_F(UDPSocketTest, ClientSetDoNotFragment) { … }
TEST_F(UDPSocketTest, ServerSetDoNotFragment) { … }
TEST_F(UDPSocketTest, CloseWithPendingRead) { … }
#if !BUILDFLAG(IS_ANDROID)
TEST_F(UDPSocketTest, JoinMulticastGroup) { … }
#if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_MAC)
#define MAYBE_SharedMulticastAddress …
#else
#define MAYBE_SharedMulticastAddress …
#endif
TEST_F(UDPSocketTest, MAYBE_SharedMulticastAddress) { … }
#endif
TEST_F(UDPSocketTest, MulticastOptions) { … }
TEST_F(UDPSocketTest, SetDSCP) { … }
TEST_F(UDPSocketTest, VerifyDscpAndEcnExchangeV4) { … }
TEST_F(UDPSocketTest, VerifyDscpAndEcnExchangeV6) { … }
TEST_F(UDPSocketTest, VerifyDscpAndEcnExchangeDualStack) { … }
TEST_F(UDPSocketTest, VerifyDscpAndEcnExchangeDualStackV4Mapped) { … }
TEST_F(UDPSocketTest, VerifyDscpAndEcnExchangeNonBlocking) { … }
TEST_F(UDPSocketTest, ConnectUsingNetwork) { … }
TEST_F(UDPSocketTest, ConnectUsingNetworkAsync) { … }
}
#if BUILDFLAG(IS_WIN)
namespace {
const HANDLE kFakeHandle1 = (HANDLE)12;
const HANDLE kFakeHandle2 = (HANDLE)13;
const QOS_FLOWID kFakeFlowId1 = (QOS_FLOWID)27;
const QOS_FLOWID kFakeFlowId2 = (QOS_FLOWID)38;
class TestUDPSocketWin : public UDPSocketWin {
public:
TestUDPSocketWin(QwaveApi* qos,
DatagramSocket::BindType bind_type,
net::NetLog* net_log,
const net::NetLogSource& source)
: UDPSocketWin(bind_type, net_log, source), qos_(qos) {}
TestUDPSocketWin(const TestUDPSocketWin&) = delete;
TestUDPSocketWin& operator=(const TestUDPSocketWin&) = delete;
QwaveApi* GetQwaveApi() const override { return qos_; }
private:
raw_ptr<QwaveApi> qos_;
};
class MockQwaveApi : public QwaveApi {
public:
MOCK_CONST_METHOD0(qwave_supported, bool());
MOCK_METHOD0(OnFatalError, void());
MOCK_METHOD2(CreateHandle, BOOL(PQOS_VERSION version, PHANDLE handle));
MOCK_METHOD1(CloseHandle, BOOL(HANDLE handle));
MOCK_METHOD6(AddSocketToFlow,
BOOL(HANDLE handle,
SOCKET socket,
PSOCKADDR addr,
QOS_TRAFFIC_TYPE traffic_type,
DWORD flags,
PQOS_FLOWID flow_id));
MOCK_METHOD4(
RemoveSocketFromFlow,
BOOL(HANDLE handle, SOCKET socket, QOS_FLOWID flow_id, DWORD reserved));
MOCK_METHOD7(SetFlow,
BOOL(HANDLE handle,
QOS_FLOWID flow_id,
QOS_SET_FLOW op,
ULONG size,
PVOID data,
DWORD reserved,
LPOVERLAPPED overlapped));
};
std::unique_ptr<UDPSocket> OpenedDscpTestClient(QwaveApi* api,
IPEndPoint bind_address) {
auto client = std::make_unique<TestUDPSocketWin>(
api, DatagramSocket::DEFAULT_BIND, nullptr, NetLogSource());
int rv = client->Open(bind_address.GetFamily());
EXPECT_THAT(rv, IsOk());
return client;
}
std::unique_ptr<UDPSocket> ConnectedDscpTestClient(QwaveApi* api) {
IPEndPoint bind_address;
EXPECT_TRUE(CreateUDPAddress("8.8.8.8", 9999, &bind_address));
auto client = OpenedDscpTestClient(api, bind_address);
EXPECT_THAT(client->Connect(bind_address), IsOk());
return client;
}
std::unique_ptr<UDPSocket> UnconnectedDscpTestClient(QwaveApi* api) {
IPEndPoint bind_address;
EXPECT_TRUE(CreateUDPAddress("0.0.0.0", 9999, &bind_address));
auto client = OpenedDscpTestClient(api, bind_address);
EXPECT_THAT(client->Bind(bind_address), IsOk());
return client;
}
}
using ::testing::Return;
using ::testing::SetArgPointee;
using ::testing::_;
TEST_F(UDPSocketTest, SetDSCPNoopIfPassedNoChange) {
MockQwaveApi api;
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(true));
EXPECT_CALL(api, AddSocketToFlow(_, _, _, _, _, _)).Times(0);
std::unique_ptr<UDPSocket> client = ConnectedDscpTestClient(&api);
EXPECT_THAT(client->SetDiffServCodePoint(DSCP_NO_CHANGE), IsOk());
}
TEST_F(UDPSocketTest, SetDSCPFailsIfQOSDoesntLink) {
MockQwaveApi api;
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(false));
EXPECT_CALL(api, CreateHandle(_, _)).Times(0);
std::unique_ptr<UDPSocket> client = ConnectedDscpTestClient(&api);
EXPECT_EQ(ERR_NOT_IMPLEMENTED, client->SetDiffServCodePoint(DSCP_AF41));
}
TEST_F(UDPSocketTest, SetDSCPFailsIfHandleCantBeCreated) {
MockQwaveApi api;
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(true));
EXPECT_CALL(api, CreateHandle(_, _)).WillOnce(Return(false));
EXPECT_CALL(api, OnFatalError()).Times(1);
std::unique_ptr<UDPSocket> client = ConnectedDscpTestClient(&api);
EXPECT_EQ(ERR_INVALID_HANDLE, client->SetDiffServCodePoint(DSCP_AF41));
RunUntilIdle();
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(false));
EXPECT_EQ(ERR_NOT_IMPLEMENTED, client->SetDiffServCodePoint(DSCP_AF41));
}
MATCHER_P(DscpPointee, dscp, "") {
return *(DWORD*)arg == (DWORD)dscp;
}
TEST_F(UDPSocketTest, ConnectedSocketDelayedInitAndUpdate) {
MockQwaveApi api;
std::unique_ptr<UDPSocket> client = ConnectedDscpTestClient(&api);
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(true));
EXPECT_CALL(api, CreateHandle(_, _))
.WillOnce(DoAll(SetArgPointee<1>(kFakeHandle1), Return(true)));
EXPECT_CALL(api, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api, SetFlow(_, _, _, _, _, _, _));
EXPECT_EQ(ERR_INVALID_HANDLE, client->SetDiffServCodePoint(DSCP_AF41));
RunUntilIdle();
EXPECT_THAT(client->SetDiffServCodePoint(DSCP_AF41), IsOk());
EXPECT_CALL(api, RemoveSocketFromFlow(_, _, kFakeFlowId1, _));
EXPECT_CALL(api, AddSocketToFlow(_, _, _, QOSTrafficTypeBestEffort, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId2), Return(true)));
EXPECT_CALL(api, SetFlow(_, _, QOSSetOutgoingDSCPValue, _,
DscpPointee(DSCP_DEFAULT), _, _));
EXPECT_THAT(client->SetDiffServCodePoint(DSCP_DEFAULT), IsOk());
EXPECT_CALL(api, RemoveSocketFromFlow(_, _, kFakeFlowId2, _));
EXPECT_CALL(api, CloseHandle(kFakeHandle1));
}
TEST_F(UDPSocketTest, UnonnectedSocketDelayedInitAndUpdate) {
MockQwaveApi api;
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(true));
EXPECT_CALL(api, CreateHandle(_, _))
.WillOnce(DoAll(SetArgPointee<1>(kFakeHandle1), Return(true)));
std::unique_ptr<UDPSocket> client = UnconnectedDscpTestClient(&api);
EXPECT_THAT(client->SetDiffServCodePoint(DSCP_AF41), IsOk());
RunUntilIdle();
EXPECT_THAT(client->SetDiffServCodePoint(DSCP_AF42), IsOk());
EXPECT_CALL(api, CloseHandle(kFakeHandle1));
}
TEST_F(UDPSocketTest, SendToCallsQwaveApis) {
MockQwaveApi api;
std::unique_ptr<UDPSocket> client = UnconnectedDscpTestClient(&api);
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(true));
EXPECT_CALL(api, CreateHandle(_, _))
.WillOnce(DoAll(SetArgPointee<1>(kFakeHandle1), Return(true)));
EXPECT_THAT(client->SetDiffServCodePoint(DSCP_AF41), IsOk());
RunUntilIdle();
EXPECT_CALL(api, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api, SetFlow(_, _, _, _, _, _, _));
std::string simple_message("hello world");
IPEndPoint server_address(IPAddress::IPv4Localhost(), 9438);
int rv = SendToSocket(client.get(), simple_message, server_address);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
rv = SendToSocket(client.get(), simple_message, server_address);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
EXPECT_CALL(api, AddSocketToFlow(_, _, _, _, _, _)).WillOnce(Return(true));
IPEndPoint server_address2(IPAddress::IPv4Localhost(), 9439);
rv = SendToSocket(client.get(), simple_message, server_address2);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
EXPECT_CALL(api, RemoveSocketFromFlow(_, _, _, _));
EXPECT_CALL(api, CloseHandle(kFakeHandle1));
}
TEST_F(UDPSocketTest, SendToCallsApisAfterDeferredInit) {
MockQwaveApi api;
std::unique_ptr<UDPSocket> client = UnconnectedDscpTestClient(&api);
EXPECT_CALL(api, qwave_supported()).WillRepeatedly(Return(true));
EXPECT_CALL(api, CreateHandle(_, _))
.WillOnce(DoAll(SetArgPointee<1>(kFakeHandle1), Return(true)));
EXPECT_THAT(client->SetDiffServCodePoint(DSCP_CS7), IsOk());
std::string simple_message("hello world");
IPEndPoint server_address(IPAddress::IPv4Localhost(), 9438);
EXPECT_CALL(api, AddSocketToFlow(_, _, _, _, _, _)).Times(0);
int rv = SendToSocket(client.get(), simple_message, server_address);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
RunUntilIdle();
EXPECT_CALL(api, AddSocketToFlow(_, _, _, QOSTrafficTypeControl, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api, SetFlow(_, _, _, _, _, _, _)).WillOnce(Return(true));
rv = SendToSocket(client.get(), simple_message, server_address);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
EXPECT_CALL(api, RemoveSocketFromFlow(_, _, kFakeFlowId1, _));
EXPECT_CALL(api, CloseHandle(kFakeHandle1));
}
class DscpManagerTest : public TestWithTaskEnvironment {
protected:
DscpManagerTest() {
EXPECT_CALL(api_, qwave_supported()).WillRepeatedly(Return(true));
EXPECT_CALL(api_, CreateHandle(_, _))
.WillOnce(DoAll(SetArgPointee<1>(kFakeHandle1), Return(true)));
dscp_manager_ = std::make_unique<DscpManager>(&api_, INVALID_SOCKET);
CreateUDPAddress("1.2.3.4", 9001, &address1_);
CreateUDPAddress("1234:5678:90ab:cdef:1234:5678:90ab:cdef", 9002,
&address2_);
}
MockQwaveApi api_;
std::unique_ptr<DscpManager> dscp_manager_;
IPEndPoint address1_;
IPEndPoint address2_;
};
TEST_F(DscpManagerTest, PrepareForSendIsNoopIfNoSet) {
RunUntilIdle();
dscp_manager_->PrepareForSend(address1_);
}
TEST_F(DscpManagerTest, PrepareForSendCallsQwaveApisAfterSet) {
RunUntilIdle();
dscp_manager_->Set(DSCP_CS2);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api_, SetFlow(_, kFakeFlowId1, _, _, _, _, _));
dscp_manager_->PrepareForSend(address1_);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api_, SetFlow(_, _, _, _, _, _, _)).Times(0);
dscp_manager_->PrepareForSend(address2_);
EXPECT_CALL(api_, RemoveSocketFromFlow(_, _, kFakeFlowId1, _));
EXPECT_CALL(api_, CloseHandle(kFakeHandle1));
}
TEST_F(DscpManagerTest, PrepareForSendCallsQwaveApisOncePerAddress) {
RunUntilIdle();
dscp_manager_->Set(DSCP_CS2);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api_, SetFlow(_, kFakeFlowId1, _, _, _, _, _));
dscp_manager_->PrepareForSend(address1_);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _)).Times(0);
EXPECT_CALL(api_, SetFlow(_, _, _, _, _, _, _)).Times(0);
dscp_manager_->PrepareForSend(address1_);
EXPECT_CALL(api_, RemoveSocketFromFlow(_, _, kFakeFlowId1, _));
EXPECT_CALL(api_, CloseHandle(kFakeHandle1));
}
TEST_F(DscpManagerTest, SetDestroysExistingFlow) {
RunUntilIdle();
dscp_manager_->Set(DSCP_CS2);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api_, SetFlow(_, kFakeFlowId1, _, _, _, _, _));
dscp_manager_->PrepareForSend(address1_);
EXPECT_CALL(api_, RemoveSocketFromFlow(_, NULL, kFakeFlowId1, _));
dscp_manager_->Set(DSCP_CS5);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId2), Return(true)));
EXPECT_CALL(api_, SetFlow(_, kFakeFlowId2, _, _, _, _, _));
dscp_manager_->PrepareForSend(address1_);
EXPECT_CALL(api_, RemoveSocketFromFlow(_, _, kFakeFlowId2, _));
EXPECT_CALL(api_, CloseHandle(kFakeHandle1));
}
TEST_F(DscpManagerTest, SocketReAddedOnRecreateHandle) {
RunUntilIdle();
dscp_manager_->Set(DSCP_CS2);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId1), Return(true)));
EXPECT_CALL(api_, SetFlow(_, kFakeFlowId1, _, _, _, _, _))
.WillOnce(Return(true));
EXPECT_THAT(dscp_manager_->PrepareForSend(address1_), IsOk());
EXPECT_CALL(api_, RemoveSocketFromFlow(_, _, kFakeFlowId1, _))
.WillOnce(Return(true));
dscp_manager_->Set(DSCP_CS7);
auto error = std::make_unique<base::ScopedClearLastError>();
::SetLastError(ERROR_DEVICE_REINITIALIZATION_NEEDED);
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, _, _, _)).WillOnce(Return(false));
EXPECT_CALL(api_, SetFlow(_, _, _, _, _, _, _)).Times(0);
EXPECT_CALL(api_, CloseHandle(kFakeHandle1));
EXPECT_CALL(api_, CreateHandle(_, _))
.WillOnce(DoAll(SetArgPointee<1>(kFakeHandle2), Return(true)));
EXPECT_EQ(ERR_INVALID_HANDLE, dscp_manager_->PrepareForSend(address1_));
error = nullptr;
RunUntilIdle();
EXPECT_CALL(api_, AddSocketToFlow(_, _, _, QOSTrafficTypeControl, _, _))
.WillOnce(DoAll(SetArgPointee<5>(kFakeFlowId2), Return(true)));
EXPECT_CALL(api_, SetFlow(_, kFakeFlowId2, _, _, _, _, _))
.WillOnce(Return(true));
EXPECT_THAT(dscp_manager_->PrepareForSend(address1_), IsOk());
EXPECT_CALL(api_, RemoveSocketFromFlow(_, _, kFakeFlowId2, _));
EXPECT_CALL(api_, CloseHandle(kFakeHandle2));
}
#endif
TEST_F(UDPSocketTest, ReadWithSocketOptimization) { … }
TEST_F(UDPSocketTest, ReadWithSocketOptimizationTruncation) { … }
#if BUILDFLAG(IS_ANDROID)
TEST_F(UDPSocketTest, Tag) {
if (!CanGetTaggedBytes()) {
DVLOG(0) << "Skipping test - GetTaggedBytes unsupported.";
return;
}
UDPServerSocket server(nullptr, NetLogSource());
ASSERT_THAT(server.Listen(IPEndPoint(IPAddress::IPv4Localhost(), 0)), IsOk());
IPEndPoint server_address;
ASSERT_THAT(server.GetLocalAddress(&server_address), IsOk());
UDPClientSocket client(DatagramSocket::DEFAULT_BIND, nullptr, NetLogSource());
ASSERT_THAT(client.Connect(server_address), IsOk());
int32_t tag_val1 = 0x12345678;
uint64_t old_traffic = GetTaggedBytes(tag_val1);
SocketTag tag1(SocketTag::UNSET_UID, tag_val1);
client.ApplySocketTag(tag1);
std::string simple_message("hello world!");
int rv = WriteSocket(&client, simple_message);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
std::string str = RecvFromSocket(&server);
EXPECT_EQ(simple_message, str);
rv = SendToSocket(&server, simple_message);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
str = ReadSocket(&client);
EXPECT_EQ(simple_message, str);
EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
int32_t tag_val2 = 0x87654321;
old_traffic = GetTaggedBytes(tag_val2);
SocketTag tag2(getuid(), tag_val2);
client.ApplySocketTag(tag2);
rv = WriteSocket(&client, simple_message);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
str = RecvFromSocket(&server);
EXPECT_EQ(simple_message, str);
rv = SendToSocket(&server, simple_message);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
str = ReadSocket(&client);
EXPECT_EQ(simple_message, str);
EXPECT_GT(GetTaggedBytes(tag_val2), old_traffic);
old_traffic = GetTaggedBytes(tag_val1);
client.ApplySocketTag(tag1);
rv = WriteSocket(&client, simple_message);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
str = RecvFromSocket(&server);
EXPECT_EQ(simple_message, str);
rv = SendToSocket(&server, simple_message);
EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
str = ReadSocket(&client);
EXPECT_EQ(simple_message, str);
EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
}
TEST_F(UDPSocketTest, BindToNetwork) {
const IPEndPoint fake_server_address(IPAddress::IPv4Localhost(), 8080);
NetworkChangeNotifierFactoryAndroid ncn_factory;
NetworkChangeNotifier::DisableForTest ncn_disable_for_test;
std::unique_ptr<NetworkChangeNotifier> ncn(ncn_factory.CreateInstance());
if (!NetworkChangeNotifier::AreNetworkHandlesSupported())
GTEST_SKIP() << "Network handles are required to test BindToNetwork.";
const handles::NetworkHandle wrong_network_handle = 65536;
UDPClientSocket wrong_socket(DatagramSocket::RANDOM_BIND, nullptr,
NetLogSource(), wrong_network_handle);
int rv = wrong_socket.Connect(fake_server_address);
EXPECT_NE(OK, rv);
EXPECT_NE(ERR_NOT_IMPLEMENTED, rv);
EXPECT_NE(wrong_network_handle, wrong_socket.GetBoundNetwork());
const handles::NetworkHandle network_handle =
NetworkChangeNotifier::GetDefaultNetwork();
if (network_handle != handles::kInvalidNetworkHandle) {
UDPClientSocket correct_socket(DatagramSocket::RANDOM_BIND, nullptr,
NetLogSource(), network_handle);
EXPECT_EQ(OK, correct_socket.Connect(fake_server_address));
EXPECT_EQ(network_handle, correct_socket.GetBoundNetwork());
}
}
#endif
class OverrideUDPSocketLimit { … };
TEST_F(UDPSocketTest, LimitClientSocket) { … }
TEST_F(UDPSocketTest, LimitConnectFail) { … }
TEST_F(UDPSocketTest, LimitConnectMultithreaded) { … }
}