summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2019-02-01 18:57:17 -0800
committerWilliam A. Kennington III <wak@google.com>2019-02-14 12:10:12 -0800
commita00b1c3f74d329c1ca65826e430efa9105a72465 (patch)
treed72155b9b79b7de937dac308e34cbe9562b571a0 /test
parent5058f57a88d697034a8ed20145489d9cdb8c5275 (diff)
downloadphosphor-networkd-a00b1c3f74d329c1ca65826e430efa9105a72465.tar.gz
phosphor-networkd-a00b1c3f74d329c1ca65826e430efa9105a72465.zip
util: Add InAddrAny population function
We commonly want to be able to turn netlink network order address data into the corresponding InAddrAny when parsing netlink data. This makes it trivial given the address family and data buffer. Tested: Ran throgh unit tests and builds fine in a BMC image. Change-Id: I4ebe1dbd284f150ea03ee091cddda5a06806c01f Signed-off-by: William A. Kennington III <wak@google.com>
Diffstat (limited to 'test')
-rw-r--r--test/test_util.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/test/test_util.cpp b/test/test_util.cpp
index 25953a1..0e58081 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -45,6 +45,30 @@ TEST_F(TestUtil, MacToString)
EXPECT_EQ("70:FF:84:09:35:09", mac_address::toString(mac2));
}
+TEST_F(TestUtil, AddrFromBuf)
+{
+ std::string tooSmall(1, 'a');
+ std::string tooLarge(24, 'a');
+
+ struct in_addr ip1;
+ EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
+ std::string_view buf1(reinterpret_cast<char*>(&ip1), sizeof(ip1));
+ InAddrAny res1 = addrFromBuf(AF_INET, buf1);
+ EXPECT_EQ(0, memcmp(&ip1, &std::get<struct in_addr>(res1), sizeof(ip1)));
+ EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error);
+ EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error);
+ EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::runtime_error);
+
+ struct in6_addr ip2;
+ EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
+ std::string_view buf2(reinterpret_cast<char*>(&ip2), sizeof(ip2));
+ InAddrAny res2 = addrFromBuf(AF_INET6, buf2);
+ EXPECT_EQ(0, memcmp(&ip2, &std::get<struct in6_addr>(res2), sizeof(ip2)));
+ EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error);
+ EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error);
+ EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::runtime_error);
+}
+
TEST_F(TestUtil, IpToString)
{
struct in_addr ip1;
OpenPOWER on IntegriCloud