#include "net/base/ip_endpoint.h"
#include <string.h>
#include <optional>
#include <string>
#include <tuple>
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/sys_byteorder.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/ip_address.h"
#include "net/base/sockaddr_storage.h"
#include "net/base/sys_addrinfo.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 <winsock2.h>
#include <ws2bth.h>
#include "base/test/gtest_util.h"
#include "net/base/winsock_util.h"
#elif BUILDFLAG(IS_POSIX)
#include <netinet/in.h>
#endif
Optional;
namespace net {
namespace {
const uint16_t* GetPortFieldFromSockaddr(const struct sockaddr* address,
socklen_t address_len) { … }
int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { … }
struct TestData { … } tests[] = …;
class IPEndPointTest : public PlatformTest { … };
TEST_F(IPEndPointTest, Constructor) { … }
TEST_F(IPEndPointTest, Assignment) { … }
TEST_F(IPEndPointTest, Copy) { … }
TEST_F(IPEndPointTest, ToFromSockAddr) { … }
TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { … }
TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) { … }
#if BUILDFLAG(IS_WIN)
namespace {
constexpr uint8_t kBluetoothAddrBytes[kBluetoothAddressSize] = {1, 2, 3,
4, 5, 6};
constexpr uint8_t kBluetoothAddrBytes2[kBluetoothAddressSize] = {1, 2, 3,
4, 5, 7};
const IPAddress kBluetoothAddress(kBluetoothAddrBytes);
const IPAddress kBluetoothAddress2(kBluetoothAddrBytes2);
constexpr uint32_t kBluetoothPort = std::numeric_limits<uint16_t>::max() + 1;
SOCKADDR_BTH BuildBluetoothSockAddr(const IPAddress& ip_address,
uint32_t port) {
SOCKADDR_BTH addr = {};
addr.addressFamily = AF_BTH;
DCHECK_LE(ip_address.bytes().size(), sizeof(addr.btAddr));
memcpy(&addr.btAddr, ip_address.bytes().data(), ip_address.bytes().size());
addr.port = port;
return addr;
}
}
TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithSelf) {
IPEndPoint bt_endpoint;
SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
EXPECT_TRUE(bt_endpoint.FromSockAddr(
reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
EXPECT_EQ(bt_endpoint.address(), kBluetoothAddress);
EXPECT_EQ(bt_endpoint.GetFamily(), AddressFamily::ADDRESS_FAMILY_UNSPECIFIED);
EXPECT_EQ(bt_endpoint.GetSockAddrFamily(), AF_BTH);
EXPECT_FALSE(bt_endpoint < bt_endpoint);
EXPECT_FALSE(bt_endpoint != bt_endpoint);
EXPECT_TRUE(bt_endpoint == bt_endpoint);
EXPECT_DCHECK_DEATH(bt_endpoint.port());
SockaddrStorage storage;
EXPECT_DCHECK_DEATH(
std::ignore = bt_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
EXPECT_DCHECK_DEATH(bt_endpoint.ToString());
EXPECT_DCHECK_DEATH(bt_endpoint.ToStringWithoutPort());
}
TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithNonBluetooth) {
IPEndPoint bt_endpoint;
SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
EXPECT_TRUE(bt_endpoint.FromSockAddr(
reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
for (const auto& test : tests) {
IPEndPoint endpoint(test.ip_address, 80);
if (test.ip_address.IsIPv4()) {
EXPECT_FALSE(bt_endpoint < endpoint);
} else {
EXPECT_TRUE(test.ip_address.IsIPv6());
EXPECT_TRUE(bt_endpoint < endpoint);
}
EXPECT_TRUE(bt_endpoint != endpoint);
EXPECT_FALSE(bt_endpoint == endpoint);
}
}
TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithCopy) {
IPEndPoint bt_endpoint;
SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
EXPECT_TRUE(bt_endpoint.FromSockAddr(
reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
IPEndPoint bt_endpoint_other(bt_endpoint);
EXPECT_EQ(bt_endpoint.address(), bt_endpoint_other.address());
EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
bt_endpoint_other.GetSockAddrFamily());
EXPECT_FALSE(bt_endpoint < bt_endpoint_other);
EXPECT_FALSE(bt_endpoint != bt_endpoint_other);
EXPECT_TRUE(bt_endpoint == bt_endpoint_other);
EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
SockaddrStorage storage;
EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
storage.addr, &storage.addr_len));
EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
}
TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithDifferentPort) {
IPEndPoint bt_endpoint;
SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
EXPECT_TRUE(bt_endpoint.FromSockAddr(
reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
IPEndPoint bt_endpoint_other;
SOCKADDR_BTH addr2 =
BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort + 1);
EXPECT_TRUE(bt_endpoint_other.FromSockAddr(
reinterpret_cast<const struct sockaddr*>(&addr2), sizeof(addr2)));
EXPECT_EQ(bt_endpoint.address(), bt_endpoint_other.address());
EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
bt_endpoint_other.GetSockAddrFamily());
EXPECT_FALSE(bt_endpoint < bt_endpoint_other);
EXPECT_FALSE(bt_endpoint != bt_endpoint_other);
EXPECT_TRUE(bt_endpoint == bt_endpoint_other);
EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
SockaddrStorage storage;
EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
storage.addr, &storage.addr_len));
EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
}
TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithDifferentAddress) {
IPEndPoint bt_endpoint;
SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
EXPECT_TRUE(bt_endpoint.FromSockAddr(
reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
IPEndPoint bt_endpoint_other;
SOCKADDR_BTH addr2 =
BuildBluetoothSockAddr(kBluetoothAddress2, kBluetoothPort);
EXPECT_TRUE(bt_endpoint_other.FromSockAddr(
reinterpret_cast<const struct sockaddr*>(&addr2), sizeof(addr2)));
EXPECT_LT(bt_endpoint.address(), bt_endpoint_other.address());
EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
bt_endpoint_other.GetSockAddrFamily());
EXPECT_TRUE(bt_endpoint < bt_endpoint_other);
EXPECT_TRUE(bt_endpoint != bt_endpoint_other);
EXPECT_FALSE(bt_endpoint == bt_endpoint_other);
EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
SockaddrStorage storage;
EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
storage.addr, &storage.addr_len));
EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
}
#endif
TEST_F(IPEndPointTest, Equality) { … }
TEST_F(IPEndPointTest, LessThan) { … }
TEST_F(IPEndPointTest, ToString) { … }
TEST_F(IPEndPointTest, RoundtripThroughValue) { … }
TEST_F(IPEndPointTest, FromGarbageValue) { … }
TEST_F(IPEndPointTest, FromMalformedValues) { … }
}
}