mirror of
https://github.com/TokTok/c-toxcore
synced 2026-06-07 11:02:48 +00:00
7a3ead591f
Every use of this function needs to allocate the same buffer. None of the callers uses a differently sized buffer, so we might as well put it in a struct and have the type checker prove the buffer size is correct. Also rename `ip_ntoa` to `net_ip_ntoa` to avoid clashes with ESP-IDF system libraries which define this function as well.
77 lines
1.6 KiB
C++
77 lines
1.6 KiB
C++
#include "network.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
namespace {
|
|
|
|
TEST(IpNtoa, DoesntWriteOutOfBounds)
|
|
{
|
|
Ip_Ntoa ip_str;
|
|
IP ip;
|
|
ip.family = net_family_ipv6;
|
|
ip.ip.v6.uint64[0] = -1;
|
|
ip.ip.v6.uint64[1] = -1;
|
|
|
|
net_ip_ntoa(&ip, &ip_str);
|
|
|
|
EXPECT_EQ(std::string(ip_str.buf), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
|
|
EXPECT_LT(std::string(ip_str.buf).length(), IP_NTOA_LEN);
|
|
}
|
|
|
|
TEST(IpNtoa, ReportsInvalidIpFamily)
|
|
{
|
|
Ip_Ntoa ip_str;
|
|
IP ip;
|
|
ip.family.value = 255 - net_family_ipv6.value;
|
|
ip.ip.v4.uint32 = 0;
|
|
|
|
net_ip_ntoa(&ip, &ip_str);
|
|
|
|
EXPECT_EQ(std::string(ip_str.buf), "(IP invalid, family 245)");
|
|
}
|
|
|
|
TEST(IpNtoa, FormatsIPv4)
|
|
{
|
|
Ip_Ntoa ip_str;
|
|
IP ip;
|
|
ip.family = net_family_ipv4;
|
|
ip.ip.v4.uint8[0] = 192;
|
|
ip.ip.v4.uint8[1] = 168;
|
|
ip.ip.v4.uint8[2] = 0;
|
|
ip.ip.v4.uint8[3] = 13;
|
|
|
|
net_ip_ntoa(&ip, &ip_str);
|
|
|
|
EXPECT_EQ(std::string(ip_str.buf), "192.168.0.13");
|
|
}
|
|
|
|
TEST(IpParseAddr, FormatsIPv4)
|
|
{
|
|
char ip_str[IP_NTOA_LEN];
|
|
IP ip;
|
|
ip.family = net_family_ipv4;
|
|
ip.ip.v4.uint8[0] = 192;
|
|
ip.ip.v4.uint8[1] = 168;
|
|
ip.ip.v4.uint8[2] = 0;
|
|
ip.ip.v4.uint8[3] = 13;
|
|
|
|
ip_parse_addr(&ip, ip_str, sizeof(ip_str));
|
|
|
|
EXPECT_EQ(std::string(ip_str), "192.168.0.13");
|
|
}
|
|
|
|
TEST(IpParseAddr, FormatsIPv6)
|
|
{
|
|
char ip_str[IP_NTOA_LEN];
|
|
IP ip;
|
|
ip.family = net_family_ipv6;
|
|
ip.ip.v6.uint64[0] = -1;
|
|
ip.ip.v6.uint64[1] = -1;
|
|
|
|
ip_parse_addr(&ip, ip_str, sizeof(ip_str));
|
|
|
|
EXPECT_EQ(std::string(ip_str), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
|
|
}
|
|
|
|
} // namespace
|