lokinet/test/net/test_llarp_net.cpp

149 lines
3.7 KiB
C++
Raw Normal View History

2023-01-09 17:47:41 +00:00
#include <llarp/net/net_int.hpp>
#include <llarp/net/ip.hpp>
#include <llarp/net/ip_range.hpp>
#include <llarp/net/net.hpp>
2022-10-06 17:29:50 +00:00
#include <oxenc/hex.h>
2021-03-01 21:07:32 +00:00
#include <catch2/catch.hpp>
namespace
{
template<typename T>
bool IsBogon(T ip)
{
return llarp::net::Platform::Default_ptr()->IsBogon(ip);
}
}
2021-03-01 21:07:32 +00:00
TEST_CASE("In6Addr")
2019-06-11 16:44:05 +00:00
{
llarp::huint128_t ip;
2021-03-01 21:07:32 +00:00
SECTION("From string")
{
REQUIRE(ip.FromString("fc00::1"));
}
SECTION("From string fail")
{
REQUIRE_FALSE(ip.FromString("10.1.1.1"));
}
2019-06-11 16:44:05 +00:00
}
2021-03-01 21:07:32 +00:00
TEST_CASE("In6AddrToHUIntLoopback")
2019-06-11 16:44:05 +00:00
{
llarp::huint128_t loopback = {0};
2021-03-01 21:07:32 +00:00
REQUIRE(loopback.FromString("::1"));
2019-06-11 16:44:05 +00:00
in6_addr addr = IN6ADDR_LOOPBACK_INIT;
auto huint = llarp::net::In6ToHUInt(addr);
2021-03-01 21:07:32 +00:00
REQUIRE(huint == loopback);
2019-06-11 16:44:05 +00:00
}
2021-03-01 21:07:32 +00:00
TEST_CASE("In6AddrToHUInt")
2019-06-11 16:44:05 +00:00
{
llarp::huint128_t huint_parsed = {0};
2021-03-01 21:07:32 +00:00
REQUIRE(huint_parsed.FromString("fd00::1"));
in6_addr addr = {{{0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}}};
auto huint = llarp::net::In6ToHUInt(addr);
2021-03-01 21:07:32 +00:00
REQUIRE(huint == huint_parsed);
huint_parsed.h++;
2021-03-01 21:07:32 +00:00
REQUIRE(huint != huint_parsed);
2019-04-25 23:21:19 +00:00
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Range")
{
2021-03-01 21:07:32 +00:00
SECTION("Contains 8")
{
REQUIRE(
llarp::IPRange::FromIPv4(10, 0, 0, 1, 8).Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
}
SECTION("Contains 24")
{
REQUIRE(llarp::IPRange::FromIPv4(10, 200, 0, 1, 24)
.Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253)));
}
SECTION("Contains fail")
{
REQUIRE(!llarp::IPRange::FromIPv4(192, 168, 0, 1, 24)
.Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253)));
}
SECTION("Intersecting networks")
{
const auto range_16 = llarp::IPRange::FromIPv4(10,9,0,1, 16);
const auto range_24a = llarp::IPRange::FromIPv4(10,9,0,1, 24);
const auto range_24b = llarp::IPRange::FromIPv4(10,9,1,1, 24);
const auto range_unrelated = llarp::IPRange::FromIPv4(1,9,1,1, 8);
REQUIRE(range_16 * range_24a);
REQUIRE(range_16 * range_24b);
REQUIRE(not(range_24a * range_24b));
REQUIRE(not(range_16 * range_unrelated));
}
2019-04-25 23:21:19 +00:00
}
2021-03-01 21:07:32 +00:00
TEST_CASE("IPv4 netmask")
{
2021-03-01 21:07:32 +00:00
REQUIRE(llarp::netmask_ipv4_bits(8) == llarp::huint32_t{0xFF000000});
REQUIRE(llarp::netmask_ipv4_bits(24) == llarp::huint32_t{0xFFFFFF00});
2019-04-25 23:21:19 +00:00
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Bogon")
{
2021-03-01 21:07:32 +00:00
SECTION("Bogon_10_8")
{
REQUIRE(IsBogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
2021-03-01 21:07:32 +00:00
}
SECTION("Bogon_192_168_16")
{
REQUIRE(IsBogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111)));
2021-03-01 21:07:32 +00:00
}
SECTION("Bogon_127_8")
{
REQUIRE(IsBogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1)));
2021-03-01 21:07:32 +00:00
}
SECTION("Bogon_0_8")
{
REQUIRE(IsBogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0)));
2021-03-01 21:07:32 +00:00
}
SECTION("Non-bogon")
{
REQUIRE_FALSE(IsBogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1)));
REQUIRE_FALSE(IsBogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6)));
REQUIRE_FALSE(IsBogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99)));
REQUIRE_FALSE(IsBogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4)));
2021-03-01 21:07:32 +00:00
}
}
TEST_CASE("uint128_t")
{
SECTION("layout")
{
llarp::uint128_t i{0x0011223f44556677ULL, 0x8899aabbc3ddeeffULL};
2022-10-06 17:29:50 +00:00
REQUIRE(oxenc::to_hex(std::string_view{reinterpret_cast<const char*>(&i), sizeof(i)}) ==
#ifdef __BIG_ENDIAN__
"0011223f445566778899aabbc3ddeeff"
#else
"ffeeddc3bbaa9988776655443f221100"
#endif
);
}
SECTION("ntoh")
{
llarp::uint128_t i{0x0011223f44556677ULL, 0x8899aabbc3ddeeffULL};
auto be = ntoh128(i);
REQUIRE(be == llarp::uint128_t{0xffeeddc3bbaa9988ULL, 0x776655443f221100ULL});
}
SECTION("hton")
{
llarp::uint128_t i{0x0011223f44556677ULL, 0x8899aabbc3ddeeffULL};
auto be = ntoh128(i);
REQUIRE(be == llarp::uint128_t{0xffeeddc3bbaa9988ULL, 0x776655443f221100ULL});
}
}