Add get_ipv4 test.

GitOrigin-RevId: fe8d79634d3d4e5114cc8b03b0a38d988923c2ba
This commit is contained in:
levlam 2018-09-11 21:49:39 +03:00
parent 30ceb55fb4
commit fd96a6722d
4 changed files with 21 additions and 6 deletions

View File

@ -81,7 +81,7 @@ class DcOption {
switch (ip_port_ref.get_id()) {
case telegram_api::ipPort::ID: {
auto &ip_port = static_cast<const telegram_api::ipPort &>(ip_port_ref);
init_ip_address(IPAddress::ipv4_to_str(ip_port.ipv4_), ip_port.port_);
init_ip_address(IPAddress::ipv4_to_str(static_cast<uint32>(ip_port.ipv4_)), ip_port.port_);
break;
}
case telegram_api::ipPortSecret::ID: {
@ -91,7 +91,7 @@ class DcOption {
}
flags_ |= Flags::HasSecret;
secret_ = ip_port.secret_.as_slice().str();
init_ip_address(IPAddress::ipv4_to_str(ip_port.ipv4_), ip_port.port_);
init_ip_address(IPAddress::ipv4_to_str(static_cast<uint32>(ip_port.ipv4_)), ip_port.port_);
break;
}
default:

View File

@ -424,9 +424,9 @@ static CSlice get_ip_str(int family, const void *addr) {
}
}
CSlice IPAddress::ipv4_to_str(int32 ipv4) {
auto tmp_ipv4 = ntohl(ipv4);
return ::td::get_ip_str(AF_INET, &tmp_ipv4);
CSlice IPAddress::ipv4_to_str(uint32 ipv4) {
ipv4 = ntohl(ipv4);
return ::td::get_ip_str(AF_INET, &ipv4);
}
Slice IPAddress::get_ip_str() const {

View File

@ -57,7 +57,7 @@ class IPAddress {
const sockaddr *get_sockaddr() const;
size_t get_sockaddr_len() const;
int get_address_family() const;
static CSlice ipv4_to_str(int32 ipv4);
static CSlice ipv4_to_str(uint32 ipv4);
Status init_sockaddr(sockaddr *addr);
Status init_sockaddr(sockaddr *addr, socklen_t len) TD_WARN_UNUSED_RESULT;

View File

@ -435,3 +435,18 @@ TEST(BigNum, from_decimal) {
ASSERT_TRUE(BigNum::from_decimal("-999999999999999999999999999999999999999999999999").is_ok());
ASSERT_TRUE(BigNum::from_decimal("999999999999999999999999999999999999999999999999").is_ok());
}
static void test_get_ipv4(uint32 ip) {
td::IPAddress ip_address;
ip_address.init_ipv4_port(td::IPAddress::ipv4_to_str(ip), 80);
ASSERT_EQ(ip_address.get_ipv4(), ip);
}
TEST(Misc, IPAddress_get_ipv4) {
test_get_ipv4(0x00000000);
test_get_ipv4(0x010000FF);
test_get_ipv4(0xFF000001);
test_get_ipv4(0x01020304);
test_get_ipv4(0x04030201);
test_get_ipv4(0xFFFFFFFF);
}