unit tests for bogons

pull/33/head
Jeff Becker 6 years ago
parent 7960752430
commit 15c99f3192
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -221,18 +221,26 @@ namespace llarp
}
};
/// get a netmask with the higest numset bits set
constexpr uint32_t
__netmask_ipv4_bits(uint32_t numset)
{
return (32 - numset)
? (1 << (32 - (numset + 1))) | __netmask_ipv4_bits(numset + 1)
: 0;
}
/// get an ipv4 netmask given some /N range
constexpr huint32_t
netmask_ipv4_bits(byte_t netmask)
netmask_ipv4_bits(uint32_t num)
{
return (32 - netmask) ? (huint32_t{((uint32_t)1 << (32 - (netmask + 1)))}
| netmask_ipv4_bits(netmask + 1))
: huint32_t{0};
return huint32_t{__netmask_ipv4_bits(32 - num)};
}
constexpr huint32_t
ipaddr_ipv4_bits(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
{
return huint32_t{(a << 24) | (b << 16) | (c << 8) | d};
return huint32_t{(a) | (b << 8) | (c << 16) | (d << 24)};
}
constexpr IPRange

@ -1015,9 +1015,6 @@ namespace llarp
{
if(bogon.Contains(addr))
{
char strbuf[32] = {0};
inet_ntop(AF_INET, &addr, strbuf, sizeof(strbuf));
llarp::LogError("bogon: ", strbuf, " in ", bogon.ToString());
return true;
}
}

@ -0,0 +1,51 @@
#include <gtest/gtest.h>
#include <llarp/net.hpp>
struct NetTest : public ::testing::Test
{
};
TEST_F(NetTest, TestRangeContains)
{
ASSERT_TRUE(llarp::iprange_ipv4(10, 0, 0, 0, 8)
.Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
}
TEST_F(NetTest, TestIPv4Netmask)
{
ASSERT_TRUE(llarp::xhtonl(llarp::netmask_ipv4_bits(8))
== llarp::nuint32_t{0xFF000000});
};
TEST_F(NetTest, TestBogon_10_8)
{
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
};
TEST_F(NetTest, TestBogon_192_168_16)
{
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111)));
};
TEST_F(NetTest, TestBogon_DoD_8)
{
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(21, 3, 37, 70)));
};
TEST_F(NetTest, TestBogon_127_8)
{
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1)));
};
TEST_F(NetTest, TestBogon_0_8)
{
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0)));
};
TEST_F(NetTest, TestBogon_NonBogon)
{
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1)));
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6)));
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99)));
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4)));
}
Loading…
Cancel
Save