util: Add non-family isValidIP
Change-Id: Ib039658d466c0b10b6bc3d1dde9bf5926a124c65
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 87c660d..e9f658a 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -297,7 +297,7 @@
ObjectPath EthernetInterface::neighbor(std::string ipAddress,
std::string macAddress)
{
- if (!isValidIP(AF_INET, ipAddress) && !isValidIP(AF_INET6, ipAddress))
+ if (!isValidIP(ipAddress))
{
log<level::ERR>("Not a valid IP address",
entry("ADDRESS=%s", ipAddress.c_str()));
@@ -773,8 +773,7 @@
{
for (const auto& nameserverip : value)
{
- if (!isValidIP(AF_INET, nameserverip) &&
- !isValidIP(AF_INET6, nameserverip))
+ if (!isValidIP(nameserverip))
{
log<level::ERR>("Not a valid IP address"),
entry("ADDRESS=%s", nameserverip.c_str());
diff --git a/src/util.cpp b/src/util.cpp
index 7ede99c..b1f4d39 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -189,6 +189,11 @@
return inet_pton(family, address.c_str(), buf) > 0;
}
+bool isValidIP(stdplus::const_zstring address) noexcept
+{
+ return isValidIP(AF_INET, address) || isValidIP(AF_INET6, address);
+}
+
bool isValidPrefix(int family, uint8_t prefix)
{
return familyVisit(
diff --git a/src/util.hpp b/src/util.hpp
index 52abfb7..16dca64 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -131,6 +131,7 @@
* @returns true if it is valid otherwise false.
*/
bool isValidIP(int family, stdplus::const_zstring address) noexcept;
+bool isValidIP(stdplus::const_zstring address) noexcept;
/* @brief checks that the given prefix is valid or not.
* @param[in] family - IP address family(AF_INET/AF_INET6).
diff --git a/test/test_util.cpp b/test/test_util.cpp
index 712fe98..cfbab77 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -70,41 +70,32 @@
TEST_F(TestUtil, IpValidation)
{
- std::string ipaddress = "0.0.0.0";
- EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET, "0.0.0.0"));
+ EXPECT_TRUE(isValidIP("0.0.0.0"));
- ipaddress = "9.3.185.83";
- EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET, "9.3.185.83"));
- ipaddress = "9.3.185.a";
- EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
+ EXPECT_FALSE(isValidIP(AF_INET, "9.3.185.a"));
+ EXPECT_FALSE(isValidIP("9.3.185.a"));
- ipaddress = "9.3.a.83";
- EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
+ EXPECT_FALSE(isValidIP(AF_INET, "9.3.a.83"));
- ipaddress = "x.x.x.x";
- EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
+ EXPECT_FALSE(isValidIP(AF_INET, "x.x.x.x"));
- ipaddress = "0:0:0:0:0:0:0:0";
- EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:0:0:0"));
+ EXPECT_TRUE(isValidIP("0:0:0:0:0:0:0:0"));
- ipaddress = "1:0:0:0:0:0:0:8";
- EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET6, "1:0:0:0:0:0:0:8"));
- ipaddress = "1::8";
- EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET6, "1::8"));
- ipaddress = "0:0:0:0:0:FFFF:204.152.189.116";
- EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116"));
- ipaddress = "::ffff:204.152.189.116";
- EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET6, "::ffff:204.152.189.116"));
- ipaddress = "a:0:0:0:0:FFFF:204.152.189.116";
- EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET6, "a:0:0:0:0:FFFF:204.152.189.116"));
- ipaddress = "1::8";
- EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
+ EXPECT_TRUE(isValidIP(AF_INET6, "1::8"));
}
TEST_F(TestUtil, PrefixValidation)