test/net/TestIPv[46]Address: add more tests

This commit is contained in:
Max Kellermann 2023-01-28 08:13:33 +01:00
parent 1f5d50ccce
commit 78b13577ef
2 changed files with 129 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 Max Kellermann <max.kellermann@gmail.com> * Copyright 2012-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -29,6 +29,7 @@
#include "net/IPv4Address.hxx" #include "net/IPv4Address.hxx"
#include "net/ToString.hxx" #include "net/ToString.hxx"
#include "util/Compiler.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -38,6 +39,70 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#if GCC_CHECK_VERSION(11,0)
/* suppress warning for calling GetSize() on uninitialized object */
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
TEST(IPv4AddressTest, Basic)
{
IPv4Address dummy;
EXPECT_EQ(dummy.GetSize(), sizeof(struct sockaddr_in));
}
TEST(IPv4AddressTest, Port)
{
IPv4Address a(12345);
EXPECT_EQ(a.GetPort(), 12345u);
a.SetPort(42);
EXPECT_EQ(a.GetPort(), 42u);
}
TEST(IPv4AddressTest, NumericAddress)
{
IPv4Address a(12345);
EXPECT_EQ(a.GetNumericAddress(), 0u);
EXPECT_EQ(a.GetNumericAddressBE(), 0u);
a = IPv4Address(192, 168, 1, 2, 42);
EXPECT_EQ(a.GetNumericAddress(), 0xc0a80102);
EXPECT_EQ(a.GetNumericAddressBE(), ToBE32(0xc0a80102));
}
TEST(IPv4AddressTest, Mask)
{
EXPECT_EQ(IPv4Address::MaskFromPrefix(0).GetNumericAddress(),
IPv4Address(0, 0, 0, 0, 0).GetNumericAddress());
EXPECT_EQ(IPv4Address::MaskFromPrefix(1).GetNumericAddress(),
IPv4Address(128, 0, 0, 0, 0).GetNumericAddress());
EXPECT_EQ(IPv4Address::MaskFromPrefix(23).GetNumericAddress(),
IPv4Address(255, 255, 254, 0, 0).GetNumericAddress());
EXPECT_EQ(IPv4Address::MaskFromPrefix(24).GetNumericAddress(),
IPv4Address(255, 255, 255, 0, 0).GetNumericAddress());
EXPECT_EQ(IPv4Address::MaskFromPrefix(32).GetNumericAddress(),
IPv4Address(255, 255, 255, 255, 0).GetNumericAddress());
}
TEST(IPv4AddressTest, And)
{
EXPECT_EQ((IPv4Address::MaskFromPrefix(32) &
IPv4Address(192, 168, 1, 2, 0)).GetNumericAddress(),
IPv4Address(192, 168, 1, 2, 0).GetNumericAddress());
EXPECT_EQ((IPv4Address::MaskFromPrefix(24) &
IPv4Address(192, 168, 1, 2, 0)).GetNumericAddress(),
IPv4Address(192, 168, 1, 0, 0).GetNumericAddress());
EXPECT_EQ((IPv4Address::MaskFromPrefix(16) &
IPv4Address(192, 168, 1, 2, 0)).GetNumericAddress(),
IPv4Address(192, 168, 0, 0, 0).GetNumericAddress());
EXPECT_EQ((IPv4Address::MaskFromPrefix(8) &
IPv4Address(192, 168, 1, 2, 0)).GetNumericAddress(),
IPv4Address(192, 0, 0, 0, 0).GetNumericAddress());
EXPECT_EQ((IPv4Address::MaskFromPrefix(0) &
IPv4Address(192, 168, 1, 2, 0)).GetNumericAddress(),
IPv4Address(0, 0, 0, 0, 0).GetNumericAddress());
}
static std::string static std::string
ToString(const struct in_addr &a) ToString(const struct in_addr &a)
{ {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 Max Kellermann <max.kellermann@gmail.com> * Copyright 2012-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -29,6 +29,7 @@
#include "net/IPv6Address.hxx" #include "net/IPv6Address.hxx"
#include "net/ToString.hxx" #include "net/ToString.hxx"
#include "util/Compiler.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -38,6 +39,67 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#if GCC_CHECK_VERSION(11,0)
/* suppress warning for calling GetSize() on uninitialized object */
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
TEST(IPv6AddressTest, Basic)
{
IPv6Address dummy;
EXPECT_EQ(dummy.GetSize(), sizeof(struct sockaddr_in6));
}
TEST(IPv6AddressTest, Port)
{
IPv6Address a(12345);
EXPECT_EQ(a.GetPort(), 12345u);
a.SetPort(42);
EXPECT_EQ(a.GetPort(), 42u);
}
static bool
operator==(const struct in6_addr &a, const struct in6_addr &b)
{
return memcmp(&a, &b, sizeof(a)) == 0;
}
TEST(IPv6AddressTest, Mask)
{
EXPECT_EQ(IPv6Address::MaskFromPrefix(0).GetAddress(),
IPv6Address(0, 0, 0, 0, 0, 0, 0, 0, 0).GetAddress());
EXPECT_EQ(IPv6Address::MaskFromPrefix(128).GetAddress(),
IPv6Address(0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0).GetAddress());
EXPECT_EQ(IPv6Address::MaskFromPrefix(127).GetAddress(),
IPv6Address(0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xfffe, 0).GetAddress());
EXPECT_EQ(IPv6Address::MaskFromPrefix(64).GetAddress(),
IPv6Address(0xffff, 0xffff, 0xffff, 0xffff,
0, 0, 0, 0, 0).GetAddress());
EXPECT_EQ(IPv6Address::MaskFromPrefix(56).GetAddress(),
IPv6Address(0xffff, 0xffff, 0xffff, 0xff00,
0, 0, 0, 0, 0).GetAddress());
}
TEST(IPv6AddressTest, And)
{
EXPECT_EQ((IPv6Address::MaskFromPrefix(128) &
IPv6Address::MaskFromPrefix(56)).GetAddress(),
IPv6Address::MaskFromPrefix(56).GetAddress());
EXPECT_EQ((IPv6Address::MaskFromPrefix(48) &
IPv6Address(0x2a00, 0x1450, 0x4001, 0x816,
0, 0, 0, 0x200e, 0)).GetAddress(),
IPv6Address(0x2a00, 0x1450, 0x4001, 0,
0, 0, 0, 0, 0).GetAddress());
EXPECT_EQ((IPv6Address::MaskFromPrefix(24) &
IPv6Address(0x2a00, 0x1450, 0x4001, 0x816,
0, 0, 0, 0x200e, 0)).GetAddress(),
IPv6Address(0x2a00, 0x1400, 0, 0,
0, 0, 0, 0, 0).GetAddress());
}
static std::string static std::string
ToString(const struct in6_addr &a) ToString(const struct in6_addr &a)
{ {