tomato/toxcore/network_test.cc
Green Sky 227425b90e Squashed 'external/toxcore/c-toxcore/' content from commit 67badf69
git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 67badf69416a74e74f6d7eb51dd96f37282b8455
2023-07-25 11:53:09 +02:00

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