types: Flesh out address comparison and string formatting
Change-Id: Ib505232e15e8db012d07759163f780b00ea4b7d3
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 2d07e8b..09fd9dd 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -159,7 +159,7 @@
EthernetInterfaceIntf::linkUp(info.running);
if (info.mac)
{
- MacAddressIntf::macAddress(mac_address::toString(*info.mac));
+ MacAddressIntf::macAddress(std::to_string(*info.mac));
}
if (info.mtu)
{
@@ -219,7 +219,7 @@
{
continue;
}
- auto address = toString(addr.address);
+ auto address = std::to_string(addr.address);
IP::Protocol addressType = getProtocol(addr.address);
IP::AddressOrigin origin = IP::AddressOrigin::Static;
if (dhcpIsEnabled(addressType))
@@ -257,8 +257,8 @@
{
continue;
}
- auto ip = toString(neighbor.address);
- auto mac = mac_address::toString(*neighbor.mac);
+ auto ip = std::to_string(neighbor.address);
+ auto mac = std::to_string(*neighbor.mac);
auto objectPath = generateStaticNeighborObjectPath(ip, mac);
staticNeighbors.emplace(
ip, std::make_unique<Neighbor>(bus, objectPath, *this, ip, mac,
@@ -764,40 +764,8 @@
{
int addressFamily = std::get<0>(*i);
std::vector<uint8_t>& ipaddress = std::get<1>(*i);
-
- switch (addressFamily)
- {
- case AF_INET:
- if (ipaddress.size() == sizeof(struct in_addr))
- {
- servers.push_back(toString(
- *reinterpret_cast<struct in_addr*>(ipaddress.data())));
- }
- else
- {
- log<level::ERR>(
- "Invalid data recived from Systemd-Resolved");
- }
- break;
-
- case AF_INET6:
- if (ipaddress.size() == sizeof(struct in6_addr))
- {
- servers.push_back(toString(
- *reinterpret_cast<struct in6_addr*>(ipaddress.data())));
- }
- else
- {
- log<level::ERR>(
- "Invalid data recived from Systemd-Resolved");
- }
- break;
-
- default:
- log<level::ERR>(
- "Unsupported address family in DNS from Systemd-Resolved");
- break;
- }
+ servers.push_back(std::to_string(
+ addrFromBuf(addressFamily, stdplus::raw::asView<char>(ipaddress))));
}
return servers;
}
@@ -1032,11 +1000,11 @@
}
auto interface = interfaceName();
- std::string validMAC = mac_address::toString(newMAC);
+ auto validMAC = std::to_string(newMAC);
// We don't need to update the system if the address is unchanged
ether_addr oldMAC = mac_address::fromString(MacAddressIntf::macAddress());
- if (!stdplus::raw::equal(newMAC, oldMAC))
+ if (newMAC != oldMAC)
{
// Update everything that depends on the MAC value
for (const auto& [name, intf] : vlanInterfaces)
diff --git a/src/meson.build b/src/meson.build
index 184eb4a..2e07084 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -55,6 +55,7 @@
'network_manager.cpp',
'system_configuration.cpp',
'system_queries.cpp',
+ 'types.cpp',
'util.cpp',
'routing_table.cpp',
'config_parser.cpp',
diff --git a/src/network_manager_main.cpp b/src/network_manager_main.cpp
index 73df65d..5dc5b8d 100644
--- a/src/network_manager_main.cpp
+++ b/src/network_manager_main.cpp
@@ -64,14 +64,14 @@
try
{
auto inventoryMAC = mac_address::getfromInventory(bus, intfname);
- if (!mac_address::toString(inventoryMAC).empty())
+ if (inventoryMAC != ether_addr{})
{
- log<level::INFO>("Mac Address in Inventory on "),
- entry("Interface : ", intfname.c_str()),
- entry("MAC Address :",
- (mac_address::toString(inventoryMAC)).c_str());
- manager->setFistBootMACOnInterface(std::make_pair(
- intfname.c_str(), mac_address::toString(inventoryMAC)));
+ auto macStr = std::to_string(inventoryMAC);
+ log<level::INFO>("Mac Address in Inventory on ",
+ entry("Interface : ", intfname.c_str()),
+ entry("MAC Address :", macStr.c_str()));
+ manager->setFistBootMACOnInterface(
+ std::make_pair(intfname.c_str(), std::move(macStr)));
first_boot_status.push_back(intfname.c_str());
bool status = true;
for (const auto& keys : configJson.items())
diff --git a/src/routing_table.cpp b/src/routing_table.cpp
index bae3926..20535e5 100644
--- a/src/routing_table.cpp
+++ b/src/routing_table.cpp
@@ -1,6 +1,7 @@
#include "routing_table.hpp"
#include "netlink.hpp"
+#include "types.hpp"
#include "util.hpp"
#include <net/if.h>
@@ -77,26 +78,16 @@
}
}
- std::string dstStr;
- if (dstAddr)
- {
- dstStr = toString(*dstAddr);
- }
- std::string gatewayStr;
- if (gateWayAddr)
- {
- gatewayStr = toString(*gateWayAddr);
- }
if (rtm.rtm_dst_len == 0 && gateWayAddr)
{
std::string ifNameStr(ifName);
if (rtm.rtm_family == AF_INET)
{
- defaultGateway[ifNameStr] = gatewayStr;
+ defaultGateway.emplace(ifNameStr, std::to_string(*gateWayAddr));
}
else if (rtm.rtm_family == AF_INET6)
{
- defaultGateway6[ifNameStr] = gatewayStr;
+ defaultGateway6.emplace(ifNameStr, std::to_string(*gateWayAddr));
}
}
}
diff --git a/src/types.cpp b/src/types.cpp
new file mode 100644
index 0000000..0d642e5
--- /dev/null
+++ b/src/types.cpp
@@ -0,0 +1,114 @@
+#include "types.hpp"
+
+#include <arpa/inet.h>
+#include <byteswap.h>
+
+#include <charconv>
+
+namespace phosphor::network::detail
+{
+
+std::string_view AddrBufMaker<ether_addr>::operator()(ether_addr val) noexcept
+{
+ for (char* ptr = buf.data() + 2; ptr < buf.end(); ptr += 3)
+ {
+ *ptr = ':';
+ }
+ for (size_t i = 0; i < 6; ++i)
+ {
+ char* tmp = buf.data() + i * 3;
+ uint8_t byte = val.ether_addr_octet[i];
+ if (byte < 16)
+ {
+ *(tmp++) = '0';
+ }
+ std::to_chars(tmp, buf.end(), byte, 16);
+ }
+ return {buf.begin(), buf.size()};
+}
+
+std::string_view AddrBufMaker<in_addr>::operator()(in_addr val) noexcept
+{
+ auto v = bswap_32(ntohl(val.s_addr));
+ char* ptr = buf.begin();
+ for (size_t i = 0; i < 3; ++i)
+ {
+ const auto res = std::to_chars(ptr, buf.end(), v & 0xff, 10);
+ *res.ptr = '.';
+ ptr = res.ptr + 1;
+ v >>= 8;
+ }
+ const auto res = std::to_chars(ptr, buf.end(), v & 0xff, 10);
+ return {buf.data(), res.ptr};
+}
+
+std::string_view AddrBufMaker<in6_addr>::operator()(in6_addr val) noexcept
+{
+ size_t skip_start = 0;
+ size_t skip_size = 0;
+ {
+ size_t new_start = 0;
+ size_t new_size = 0;
+ for (size_t i = 0; i < 9; ++i)
+ {
+ if (i < 8 && val.s6_addr16[i] == 0)
+ {
+ if (new_start + new_size == i)
+ {
+ new_size++;
+ }
+ else
+ {
+ new_start = i;
+ new_size = 1;
+ }
+ }
+ else if (new_start + new_size == i && new_size > skip_size)
+ {
+ skip_start = new_start;
+ skip_size = new_size;
+ }
+ }
+ }
+ char* ptr = buf.begin();
+ for (size_t i = 0; i < 8; ++i)
+ {
+ if (i == skip_start && skip_size > 0)
+ {
+ if (i == 0)
+ {
+ *(ptr++) = ':';
+ }
+ *(ptr++) = ':';
+ i += skip_size - 1;
+ continue;
+ }
+ const auto res =
+ std::to_chars(ptr, buf.end(), ntohs(val.s6_addr16[i]), 16);
+ ptr = res.ptr;
+ if (i < 7)
+ {
+ *(ptr++) = ':';
+ }
+ }
+ return {buf.data(), ptr};
+}
+
+} // namespace phosphor::network::detail
+
+std::string std::to_string(ether_addr value)
+{
+ return string(phosphor::network::detail::AddrBufMaker<ether_addr>{}(value));
+}
+std::string std::to_string(in_addr value)
+{
+ return string(phosphor::network::detail::AddrBufMaker<in_addr>{}(value));
+}
+std::string std::to_string(in6_addr value)
+{
+ return string(phosphor::network::detail::AddrBufMaker<in6_addr>{}(value));
+}
+std::string std::to_string(phosphor::network::InAddrAny value)
+{
+ return std::visit([](auto v) { return std::to_string(v); }, value);
+}
diff --git a/src/types.hpp b/src/types.hpp
index a78253c..a6011ff 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -1,12 +1,18 @@
#pragma once
+#include <fmt/core.h>
+#include <net/ethernet.h>
#include <netinet/in.h>
#include <systemd/sd-event.h>
+#include <algorithm>
+#include <array>
#include <chrono>
#include <memory>
#include <sdeventplus/clock.hpp>
#include <sdeventplus/utility/timer.hpp>
#include <string>
+#include <string_view>
+#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <variant>
@@ -36,7 +42,7 @@
using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
// Byte representations for common address types in network byte order
-using InAddrAny = std::variant<struct in_addr, struct in6_addr>;
+using InAddrAny = std::variant<in_addr, in6_addr>;
using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
@@ -62,6 +68,94 @@
return seed ^ (std::hash<T>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
+namespace detail
+{
+
+template <typename T>
+constexpr bool vcontains() noexcept
+{
+ return false;
+}
+
+template <typename T, typename V, typename... Vs>
+constexpr bool vcontains() noexcept
+{
+ return vcontains<T, Vs...>() || std::is_same_v<T, V>;
+}
+
+template <typename T, typename... Types>
+constexpr std::enable_if_t<vcontains<T, Types...>(), bool>
+ veq(T t, std::variant<Types...> v) noexcept
+{
+ return std::visit(
+ [t](auto v) {
+ if constexpr (std::is_same_v<T, decltype(v)>)
+ {
+ return v == t;
+ }
+ else
+ {
+ return false;
+ }
+ },
+ v);
+}
+
+template <typename T>
+struct AddrBufMaker
+{
+};
+
+template <>
+struct AddrBufMaker<ether_addr>
+{
+ public:
+ std::string_view operator()(ether_addr val) noexcept;
+
+ private:
+ std::array<char, /*octet*/ 2 * /*octets*/ 6 + /*seps*/ 5> buf;
+};
+
+template <>
+struct AddrBufMaker<in_addr>
+{
+ public:
+ std::string_view operator()(in_addr val) noexcept;
+
+ private:
+ std::array<char, /*octet*/ 3 * /*octets*/ 4 + /*seps*/ 3> buf;
+};
+
+template <>
+struct AddrBufMaker<in6_addr>
+{
+ public:
+ std::string_view operator()(in6_addr val) noexcept;
+
+ private:
+ std::array<char, /*hextet*/ 4 * /*hextets*/ 8 + /*seps*/ 7> buf;
+};
+
+template <typename BufMaker>
+struct FormatFromBuf
+{
+ private:
+ fmt::formatter<std::string_view> formatter;
+
+ public:
+ template <typename ParseContext>
+ constexpr auto parse(ParseContext& ctx)
+ {
+ return ctx.begin();
+ }
+
+ template <typename FormatContext>
+ auto format(auto v, FormatContext& ctx) const
+ {
+ return formatter.format(BufMaker{}(v), ctx);
+ }
+};
+} // namespace detail
} // namespace network
} // namespace phosphor
@@ -73,3 +167,116 @@
return std::apply(phosphor::network::hash_multi<Ts...>, t);
}
};
+
+namespace fmt
+{
+template <>
+struct formatter<ether_addr>
+ : phosphor::network::detail::FormatFromBuf<
+ phosphor::network::detail::AddrBufMaker<ether_addr>>
+{
+};
+template <>
+struct formatter<in_addr>
+ : phosphor::network::detail::FormatFromBuf<
+ phosphor::network::detail::AddrBufMaker<in_addr>>
+{
+};
+template <>
+struct formatter<in6_addr>
+ : phosphor::network::detail::FormatFromBuf<
+ phosphor::network::detail::AddrBufMaker<in6_addr>>
+{
+};
+template <>
+struct formatter<phosphor::network::InAddrAny>
+{
+ private:
+ fmt::formatter<std::string_view> formatter;
+
+ public:
+ template <typename ParseContext>
+ constexpr auto parse(ParseContext& ctx)
+ {
+ return ctx.begin();
+ }
+
+ template <typename FormatContext>
+ auto format(auto v, FormatContext& ctx) const
+ {
+ return std::visit(
+ [&](auto v) {
+ auto abm =
+ phosphor::network::detail::AddrBufMaker<decltype(v)>{};
+ return formatter.format(abm(v), ctx);
+ },
+ v);
+ }
+};
+} // namespace fmt
+
+namespace std
+{
+string to_string(ether_addr value);
+string to_string(in_addr value);
+string to_string(in6_addr value);
+string to_string(phosphor::network::InAddrAny value);
+} // namespace std
+
+constexpr bool operator==(ether_addr lhs, ether_addr rhs) noexcept
+{
+ return std::equal(lhs.ether_addr_octet, lhs.ether_addr_octet + 6,
+ rhs.ether_addr_octet);
+}
+
+constexpr bool operator==(in_addr lhs, in_addr rhs) noexcept
+{
+ return lhs.s_addr == rhs.s_addr;
+}
+
+constexpr bool operator==(in6_addr lhs, in6_addr rhs) noexcept
+{
+ return std::equal(lhs.s6_addr32, lhs.s6_addr32 + 4, rhs.s6_addr32);
+}
+
+template <typename T>
+constexpr std::enable_if_t<!std::is_same_v<phosphor::network::InAddrAny, T>,
+ bool>
+ operator==(phosphor::network::InAddrAny lhs, T rhs) noexcept
+{
+ return phosphor::network::detail::veq(rhs, lhs);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+ operator<<(std::basic_ostream<CharT, Traits>& os, ether_addr v)
+{
+ return os << phosphor::network::detail::AddrBufMaker<ether_addr>{}(v);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+ operator<<(std::basic_ostream<CharT, Traits>& os, in_addr v)
+{
+ return os << phosphor::network::detail::AddrBufMaker<in_addr>{}(v);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+ operator<<(std::basic_ostream<CharT, Traits>& os, in6_addr v)
+{
+ return os << phosphor::network::detail::AddrBufMaker<in6_addr>{}(v);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+ operator<<(std::basic_ostream<CharT, Traits>& os,
+ phosphor::network::InAddrAny v)
+{
+ return os << std::visit(
+ [](auto v) {
+ return phosphor::network::detail::AddrBufMaker<
+ decltype(v)>{}(v);
+ },
+ v);
+}
diff --git a/src/util.cpp b/src/util.cpp
index 186d0c2..111c090 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -159,25 +159,6 @@
[=]<int f>() -> InAddrAny { return addrFromBuf<f>(buf); }, family);
}
-template <typename Addr>
-std::string toString(const Addr& addr)
-{
- static constexpr int family = AddrToFamily<Addr>::value;
- std::string ret(FamilyTraits<family>::strlen, '\0');
- if (inet_ntop(family, &addr, ret.data(), ret.size()) == nullptr)
- {
- throw std::runtime_error("Failed to convert IP to string");
- }
-
- ret.resize(strlen(ret.c_str()));
- return ret;
-}
-
-std::string toString(const InAddrAny& addr)
-{
- return std::visit([](auto&& a) { return toString(a); }, addr);
-}
-
bool isValidIP(int family, stdplus::const_zstring address) noexcept
{
unsigned char buf[sizeof(struct in6_addr)];
@@ -476,15 +457,9 @@
return ret;
}
-std::string toString(const ether_addr& mac)
-{
- return fmt::format(FMT_COMPILE("{:02x}"),
- fmt::join(mac.ether_addr_octet, ":"));
-}
-
bool isEmpty(const ether_addr& mac)
{
- return stdplus::raw::equal(mac, ether_addr{});
+ return mac == ether_addr{};
}
bool isMulticast(const ether_addr& mac)
diff --git a/src/util.hpp b/src/util.hpp
index f6b9796..46353a4 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -43,12 +43,6 @@
*/
ether_addr fromString(std::string_view str);
-/** @brief Converts the given mac address bytes into a string
- * @param[in] mac - The mac address
- * @returns A valid mac address string
- */
-std::string toString(const ether_addr& mac);
-
/** @brief Determines if the mac address is empty
* @param[in] mac - The mac address
* @return True if 00:00:00:00:00:00
@@ -78,31 +72,12 @@
struct FamilyTraits<AF_INET>
{
using addr = in_addr;
- static constexpr std::size_t strlen = INET_ADDRSTRLEN;
};
template <>
struct FamilyTraits<AF_INET6>
{
using addr = in6_addr;
- static constexpr std::size_t strlen = INET6_ADDRSTRLEN;
-};
-
-template <typename Addr>
-struct AddrToFamily
-{
-};
-
-template <>
-struct AddrToFamily<in_addr>
-{
- static constexpr int value = AF_INET;
-};
-
-template <>
-struct AddrToFamily<in6_addr>
-{
- static constexpr int value = AF_INET6;
};
/* @brief converts a sockaddr for the specified address family into
@@ -114,14 +89,6 @@
typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf);
InAddrAny addrFromBuf(int family, std::string_view buf);
-/* @brief converts the ip bytes into a string representation
- * @param[in] addr - input ip address to convert.
- * @returns String representation of the ip.
- */
-template <typename Addr>
-std::string toString(const Addr& addr);
-std::string toString(const InAddrAny& addr);
-
/* @brief checks that the given ip address valid or not.
* @param[in] family - IP address family(AF_INET/AF_INET6).
* @param[in] address - IP address.
diff --git a/test/meson.build b/test/meson.build
index b501080..62ca28b 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -51,6 +51,7 @@
'netlink',
'network_manager',
#'rtnetlink',
+ 'types',
'util',
'vlan_interface',
]
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index f8254b0..ace2b1d 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -117,7 +117,7 @@
"/xyz/openbmc_test/network"sv, config::Parser());
EXPECT_EQ(mtu, intf.mtu());
- EXPECT_EQ(mac_address::toString(mac), intf.macAddress());
+ EXPECT_EQ(std::to_string(mac), intf.macAddress());
EXPECT_TRUE(intf.linkUp());
}
diff --git a/test/test_ipaddress.cpp b/test/test_ipaddress.cpp
index f5a6147..9747cf9 100644
--- a/test/test_ipaddress.cpp
+++ b/test/test_ipaddress.cpp
@@ -7,7 +7,6 @@
#include <cstring>
#include <stdexcept>
-#include <stdplus/raw.hpp>
#include <string>
#include <vector>
@@ -126,8 +125,7 @@
EXPECT_EQ(msg.ifa_index, addresses[0].interface);
EXPECT_EQ(msg.ifa_scope, addresses[0].scope);
EXPECT_EQ(msg.ifa_prefixlen, addresses[0].prefix);
- EXPECT_TRUE(
- stdplus::raw::equal(addr, std::get<in_addr>(addresses[0].address)));
+ EXPECT_EQ(addr, addresses[0].address);
}
TEST(ParseNeighbor, FilterScope)
@@ -164,8 +162,7 @@
EXPECT_EQ(msg.ifa_index, addresses[0].interface);
EXPECT_EQ(msg.ifa_scope, addresses[0].scope);
EXPECT_EQ(msg.ifa_prefixlen, addresses[0].prefix);
- EXPECT_TRUE(
- stdplus::raw::equal(addr, std::get<in_addr>(addresses[0].address)));
+ EXPECT_EQ(addr, addresses[0].address);
}
TEST(ParseNeighbor, NoFilter)
@@ -198,8 +195,7 @@
EXPECT_EQ(msg.ifa_index, addresses[0].interface);
EXPECT_EQ(msg.ifa_scope, addresses[0].scope);
EXPECT_EQ(msg.ifa_prefixlen, addresses[0].prefix);
- EXPECT_TRUE(
- stdplus::raw::equal(addr, std::get<in6_addr>(addresses[0].address)));
+ EXPECT_EQ(addr, addresses[0].address);
}
} // namespace detail
diff --git a/test/test_neighbor.cpp b/test/test_neighbor.cpp
index 0f0abc4..3cdb209 100644
--- a/test/test_neighbor.cpp
+++ b/test/test_neighbor.cpp
@@ -8,7 +8,6 @@
#include <cstring>
#include <stdexcept>
-#include <stdplus/raw.hpp>
#include <string>
#include <vector>
@@ -118,8 +117,7 @@
EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
EXPECT_EQ(msg.ndm_state, neighbors[0].state);
EXPECT_FALSE(neighbors[0].mac);
- EXPECT_TRUE(
- stdplus::raw::equal(addr, std::get<in_addr>(neighbors[0].address)));
+ EXPECT_EQ(addr, neighbors[0].address);
}
TEST(ParseNeighbor, FilterInterface)
@@ -155,8 +153,7 @@
EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
EXPECT_EQ(msg.ndm_state, neighbors[0].state);
EXPECT_FALSE(neighbors[0].mac);
- EXPECT_TRUE(
- stdplus::raw::equal(addr, std::get<in_addr>(neighbors[0].address)));
+ EXPECT_EQ(addr, neighbors[0].address);
}
TEST(ParseNeighbor, FilterState)
@@ -192,8 +189,7 @@
EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
EXPECT_EQ(msg.ndm_state, neighbors[0].state);
EXPECT_FALSE(neighbors[0].mac);
- EXPECT_TRUE(
- stdplus::raw::equal(addr, std::get<in_addr>(neighbors[0].address)));
+ EXPECT_EQ(addr, neighbors[0].address);
}
TEST(ParseNeighbor, Full)
@@ -234,10 +230,8 @@
EXPECT_EQ(1, neighbors.size());
EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
EXPECT_EQ(msg.ndm_state, neighbors[0].state);
- EXPECT_TRUE(neighbors[0].mac);
- EXPECT_TRUE(stdplus::raw::equal(mac, *neighbors[0].mac));
- EXPECT_TRUE(
- stdplus::raw::equal(addr, std::get<in6_addr>(neighbors[0].address)));
+ EXPECT_EQ(mac, neighbors[0].mac);
+ EXPECT_EQ(addr, neighbors[0].address);
}
} // namespace detail
diff --git a/test/test_types.cpp b/test/test_types.cpp
new file mode 100644
index 0000000..1f34144
--- /dev/null
+++ b/test/test_types.cpp
@@ -0,0 +1,133 @@
+#include "types.hpp"
+
+#include <arpa/inet.h>
+#include <fmt/chrono.h>
+#include <fmt/format.h>
+
+#include <sstream>
+#include <string_view>
+
+#include <gtest/gtest.h>
+
+using std::literals::string_view_literals::operator""sv;
+
+TEST(EqualOperator, EthAddr)
+{
+ EXPECT_EQ((ether_addr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}),
+ (ether_addr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}));
+ EXPECT_EQ((ether_addr{}), (ether_addr{}));
+ EXPECT_NE((ether_addr{1}), (ether_addr{}));
+}
+
+TEST(EqualOperator, InAddr)
+{
+ EXPECT_EQ((in_addr{0xff00ff00}), (in_addr{0xff00ff00}));
+ EXPECT_EQ((in_addr{}), (in_addr{}));
+ EXPECT_NE((in_addr{1}), (in_addr{}));
+}
+
+TEST(EqualOperator, In6Addr)
+{
+ EXPECT_EQ((in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}),
+ (in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
+ EXPECT_EQ((in6_addr{}), (in6_addr{}));
+ EXPECT_NE((in6_addr{1}), (in6_addr{}));
+}
+
+namespace phosphor::network
+{
+
+TEST(EqualOperator, InAddrAny)
+{
+ EXPECT_EQ(InAddrAny(in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0xff}),
+ (in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
+ EXPECT_NE(InAddrAny(in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0xff}),
+ (in_addr{}));
+ EXPECT_EQ((in6_addr{}), InAddrAny(in6_addr{}));
+ EXPECT_NE((in_addr{}), InAddrAny(in6_addr{}));
+ EXPECT_NE(InAddrAny(in6_addr{1}), InAddrAny(in6_addr{}));
+}
+
+namespace detail
+{
+
+TEST(BufMaker, EthAddr)
+{
+ AddrBufMaker<ether_addr> abm;
+ EXPECT_EQ("11:22:33:44:55:66"sv,
+ abm(ether_addr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}));
+ EXPECT_EQ("01:02:03:04:05:67"sv,
+ abm(ether_addr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
+ EXPECT_EQ("00:00:00:00:00:00"sv,
+ abm(ether_addr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
+}
+
+TEST(BufMaker, InAddr)
+{
+ AddrBufMaker<in_addr> abm;
+ EXPECT_EQ("255.255.255.255"sv, abm(in_addr{0xffffffff}));
+ EXPECT_EQ("1.15.3.4"sv, abm(in_addr{htonl(0x010f0304)}));
+ EXPECT_EQ("0.0.0.0"sv, abm(in_addr{}));
+}
+
+TEST(BufMaker, In6Addr)
+{
+ AddrBufMaker<in6_addr> abm;
+ EXPECT_EQ("::"sv, abm(in6_addr{}));
+ EXPECT_EQ("ff::"sv, abm(in6_addr{0, 0xff}));
+ EXPECT_EQ("::ff"sv,
+ abm(in6_addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
+ EXPECT_EQ("0:0:ff::ff"sv, abm(in6_addr{0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0xff}));
+ EXPECT_EQ("::100:0:ff"sv,
+ abm(in6_addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0xff}));
+ EXPECT_EQ("ff00::"sv, abm(in6_addr{0xff}));
+ EXPECT_EQ("1:2:3:4:5:6:7:8"sv,
+ abm(in6_addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8}));
+}
+
+TEST(BasicOps, AllAddrs)
+{
+ EXPECT_NE(InAddrAny{in6_addr{}}, InAddrAny{in_addr{}});
+
+ EXPECT_EQ("a 01:00:00:00:00:00", fmt::format("a {}", ether_addr{1}));
+ EXPECT_EQ("a 0.0.0.1", fmt::format("a {}", in_addr{htonl(1)}));
+ EXPECT_EQ("a 0.0.0.1", fmt::format("a {}", InAddrAny{in_addr{htonl(1)}}));
+ EXPECT_EQ("a 100::", fmt::format("a {}", in6_addr{1}));
+ EXPECT_EQ("a 100::", fmt::format("a {}", InAddrAny{in6_addr{1}}));
+
+ EXPECT_EQ("01:00:00:00:00:00", std::to_string(ether_addr{1}));
+ EXPECT_EQ("0.0.0.1", std::to_string(in_addr{htonl(1)}));
+ EXPECT_EQ("0.0.0.1", std::to_string(InAddrAny{in_addr{htonl(1)}}));
+ EXPECT_EQ("100::", std::to_string(in6_addr{1}));
+ EXPECT_EQ("100::", std::to_string(InAddrAny{in6_addr{1}}));
+
+ EXPECT_EQ("a01:00:00:00:00:00",
+ (std::stringstream{} << "a" << ether_addr{1}).str());
+ EXPECT_EQ("a0.0.0.1",
+ (std::stringstream{} << "a" << in_addr{htonl(1)}).str());
+ EXPECT_EQ(
+ "a0.0.0.1",
+ (std::stringstream{} << "a" << InAddrAny{in_addr{htonl(1)}}).str());
+ EXPECT_EQ("a100::", (std::stringstream{} << "a" << in6_addr{1}).str());
+ EXPECT_EQ("a100::",
+ (std::stringstream{} << "a" << InAddrAny{in6_addr{1}}).str());
+}
+
+TEST(Perf, In6Addr)
+{
+ GTEST_SKIP();
+ auto start = std::chrono::steady_clock::now();
+ for (size_t i = 0; i < 10000000; ++i)
+ {
+ AddrBufMaker<in6_addr>{}(in6_addr{1});
+ }
+ fmt::print("Duration: {}\n", std::chrono::steady_clock::now() - start);
+ // Make sure this test isn't enabled
+ EXPECT_FALSE(true);
+}
+
+} // namespace detail
+} // namespace phosphor::network
diff --git a/test/test_util.cpp b/test/test_util.cpp
index cfbab77..39f5c39 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -1,12 +1,13 @@
#include "util.hpp"
#include <arpa/inet.h>
+#include <fmt/chrono.h>
#include <netinet/in.h>
+#include <charconv>
#include <cstddef>
#include <cstring>
#include <stdexcept>
-#include <stdplus/raw.hpp>
#include <string>
#include <string_view>
#include <xyz/openbmc_project/Common/error.hpp>
@@ -55,19 +56,6 @@
EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument);
}
-TEST_F(TestUtil, IpToString)
-{
- struct in_addr ip1;
- EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
- EXPECT_EQ("192.168.10.1", toString(ip1));
- EXPECT_EQ("192.168.10.1", toString(InAddrAny(ip1)));
-
- struct in6_addr ip2;
- EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
- EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(ip2));
- EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(InAddrAny(ip2)));
-}
-
TEST_F(TestUtil, IpValidation)
{
EXPECT_TRUE(isValidIP(AF_INET, "0.0.0.0"));
@@ -152,30 +140,15 @@
TEST(MacFromString, Valid)
{
- EXPECT_TRUE(
- stdplus::raw::equal(ether_addr{}, fromString("00:00:00:00:00:00")));
- EXPECT_TRUE(
- stdplus::raw::equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
- fromString("FF:EE:DD:cc:bb:aa")));
- EXPECT_TRUE(
- stdplus::raw::equal(ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05},
- fromString("0:1:2:3:4:5")));
- EXPECT_TRUE(
- stdplus::raw::equal(ether_addr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab},
- fromString("0123456789AB")));
- EXPECT_TRUE(
- stdplus::raw::equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
- fromString("FFEEDDccbbaa")));
-}
-
-TEST(MacToString, Valid)
-{
- EXPECT_EQ("11:22:33:44:55:66",
- toString({0x11, 0x22, 0x33, 0x44, 0x55, 0x66}));
- EXPECT_EQ("01:02:03:04:05:67",
- toString({0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
- EXPECT_EQ("00:00:00:00:00:00",
- toString({0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
+ EXPECT_EQ((ether_addr{}), fromString("00:00:00:00:00:00"));
+ EXPECT_EQ((ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
+ fromString("FF:EE:DD:cc:bb:aa"));
+ EXPECT_EQ((ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}),
+ fromString("0:1:2:3:4:5"));
+ EXPECT_EQ((ether_addr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab}),
+ fromString("0123456789AB"));
+ EXPECT_EQ((ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
+ fromString("FFEEDDccbbaa"));
}
TEST(MacIsEmpty, True)