tomato/toxcore/network_test.cc
Green Sky b1fe064484 Squashed 'external/toxcore/c-toxcore/' changes from 6d634674a9..73d9b845a3
73d9b845a3 cleanup: Remove old type-ordered event getters.
b0840cc02d feat: add ngc events
7df9a51349 refactor: Make event dispatch ordered by receive time.
bcb6592af5 test: Add C++ classes wrapping system interfaces.
4cea4f9ca4 fix: Make all the fuzzers work again, and add a test for protodump.
c4e209ea1d refactor: Factor out malloc+memcpy into memdup.
87bcc4322d fix: Remove fatal error for non-erroneous case
REVERT: 6d634674a9 cleanup: Remove old type-ordered event getters.
REVERT: d1d48d1dfc feat: add ngc events
REVERT: 994ffecc6b refactor: Make event dispatch ordered by receive time.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 73d9b845a310c3f56d2d6d77ed56b93d84256d6e
2024-01-14 21:51:01 +01:00

86 lines
1.7 KiB
C++

#include "network.h"
#include <gtest/gtest.h>
#include "network_test_util.hh"
namespace {
TEST(TestUtil, ProducesNonNullNetwork)
{
Test_Network net;
const Network *ns = net;
EXPECT_NE(ns, nullptr);
}
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