2019-01-20 20:46:35 +01:00
|
|
|
/*
|
2023-01-28 08:13:33 +01:00
|
|
|
* Copyright 2012-2022 Max Kellermann <max.kellermann@gmail.com>
|
2019-01-20 20:46:35 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/IPv4Address.hxx"
|
|
|
|
#include "net/ToString.hxx"
|
2023-01-28 08:13:33 +01:00
|
|
|
#include "util/Compiler.h"
|
2019-01-20 20:46:35 +01:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2020-04-01 15:53:43 +02:00
|
|
|
#ifndef _WIN32
|
2019-01-20 20:46:35 +01:00
|
|
|
#include <arpa/inet.h>
|
2020-04-01 15:53:43 +02:00
|
|
|
#endif
|
2019-01-20 20:46:35 +01:00
|
|
|
|
2023-01-28 08:13:33 +01:00
|
|
|
#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());
|
|
|
|
}
|
|
|
|
|
2019-01-20 20:46:35 +01:00
|
|
|
static std::string
|
|
|
|
ToString(const struct in_addr &a)
|
|
|
|
{
|
2020-04-01 15:53:43 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* on mingw32, the parameter is non-const (PVOID) */
|
|
|
|
const auto p = const_cast<struct in_addr *>(&a);
|
|
|
|
#else
|
|
|
|
const auto p = &a;
|
|
|
|
#endif
|
|
|
|
|
2019-01-20 20:46:35 +01:00
|
|
|
char buffer[256];
|
2020-04-01 15:53:43 +02:00
|
|
|
const char *result = inet_ntop(AF_INET, p, buffer, sizeof(buffer));
|
2019-01-20 20:46:35 +01:00
|
|
|
if (result == nullptr)
|
|
|
|
throw std::runtime_error("inet_ntop() failed");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IPv4Address, Octets)
|
|
|
|
{
|
|
|
|
static constexpr auto a = IPv4Address(11, 22, 33, 44, 1234);
|
|
|
|
EXPECT_EQ(ToString(a.GetAddress()), "11.22.33.44");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IPv4Address, Any)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(ToString(IPv4Address(1234).GetAddress()), "0.0.0.0");
|
|
|
|
EXPECT_EQ(ToString(IPv4Address(1234)), "0.0.0.0:1234");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IPv4Address, Port)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(IPv4Address(0).GetPort(), 0);
|
|
|
|
EXPECT_EQ(IPv4Address(1).GetPort(), 1);
|
|
|
|
EXPECT_EQ(IPv4Address(1234).GetPort(), 1234);
|
|
|
|
EXPECT_EQ(IPv4Address(0xffff).GetPort(), 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IPv4Address, Loopback)
|
|
|
|
{
|
|
|
|
static constexpr auto a = IPv4Address(IPv4Address::Loopback(), 1234);
|
|
|
|
EXPECT_EQ(ToString(a.GetAddress()), "127.0.0.1");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IPv4Address, MaskFromPrefix)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(ToString(IPv4Address::MaskFromPrefix(0).GetAddress()), "0.0.0.0");
|
|
|
|
EXPECT_EQ(ToString(IPv4Address::MaskFromPrefix(4).GetAddress()), "240.0.0.0");
|
|
|
|
EXPECT_EQ(ToString(IPv4Address::MaskFromPrefix(8).GetAddress()), "255.0.0.0");
|
|
|
|
EXPECT_EQ(ToString(IPv4Address::MaskFromPrefix(16).GetAddress()), "255.255.0.0");
|
|
|
|
EXPECT_EQ(ToString(IPv4Address::MaskFromPrefix(24).GetAddress()), "255.255.255.0");
|
|
|
|
EXPECT_EQ(ToString(IPv4Address::MaskFromPrefix(31).GetAddress()), "255.255.255.254");
|
|
|
|
EXPECT_EQ(ToString(IPv4Address::MaskFromPrefix(32).GetAddress()), "255.255.255.255");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IPv4Address, Numeric)
|
|
|
|
{
|
2019-01-21 14:39:24 +01:00
|
|
|
EXPECT_EQ(IPv4Address(1, 2, 3, 4, 0).GetNumericAddress(), 0x01020304u);
|
2019-01-20 20:46:35 +01:00
|
|
|
EXPECT_EQ(IPv4Address(1, 2, 3, 4, 0).GetNumericAddressBE(), htonl(0x01020304));
|
|
|
|
}
|