diff --git a/src/linux/libnl-helpers/NetlinkErrorCategory.cxx b/src/linux/libnl-helpers/NetlinkErrorCategory.cxx index d50da058..66ae4871 100644 --- a/src/linux/libnl-helpers/NetlinkErrorCategory.cxx +++ b/src/linux/libnl-helpers/NetlinkErrorCategory.cxx @@ -31,7 +31,7 @@ NetlinkErrorCategory::default_error_condition(int error) const noexcept std::error_code make_netlink_error_code(int error) { - return std::error_code(error, NetlinkErrorCategory()); + return std::error_code(error, GetNetlinkErrorCategory()); } std::error_code @@ -44,4 +44,11 @@ MakeNetlinkErrorCode(int error) return make_netlink_error_code(error); } +const NetlinkErrorCategory& +GetNetlinkErrorCategory() +{ + static NetlinkErrorCategory instance; + return instance; +} + } // namespace Microsoft::Net::Netlink diff --git a/src/linux/libnl-helpers/include/microsoft/net/netlink/NetlinkErrorCategory.hxx b/src/linux/libnl-helpers/include/microsoft/net/netlink/NetlinkErrorCategory.hxx index 1b0852e9..e6408484 100644 --- a/src/linux/libnl-helpers/include/microsoft/net/netlink/NetlinkErrorCategory.hxx +++ b/src/linux/libnl-helpers/include/microsoft/net/netlink/NetlinkErrorCategory.hxx @@ -42,26 +42,34 @@ struct NetlinkErrorCategory : /** * @brief Constructs a std::error_code from a netlink error code. - * + * * This function uses the std naming convention. - * + * * @param error The netlink error code. Must be positive. - * @return std::error_code + * @return std::error_code */ [[nodiscard]] std::error_code make_netlink_error_code(int error); /** * @brief Alias for the make_netlink_error_code function. - * + * * This function uses the project naming convention. - * - * @param error - * @return std::error_code + * + * @param error + * @return std::error_code */ [[nodiscard]] std::error_code MakeNetlinkErrorCode(int error); +/** + * @brief Get the NetlinkErrorCategory object instance. + * + * @return const NetlinkErrorCategory& + */ +[[nodiscard]] const NetlinkErrorCategory& +GetNetlinkErrorCategory(); + } // namespace Microsoft::Net::Netlink #endif // NETLINK_ERROR_CATEGORY_HXX diff --git a/tests/unit/linux/libnl-helpers/CMakeLists.txt b/tests/unit/linux/libnl-helpers/CMakeLists.txt index 78119b5a..07650ae8 100644 --- a/tests/unit/linux/libnl-helpers/CMakeLists.txt +++ b/tests/unit/linux/libnl-helpers/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources(libnl-helpers-test-unit Main.cxx TestNetlink80211Interface.cxx TestNetlink80211ProtocolState.cxx + TestNetlinkException.cxx TestNetlinkRoute.cxx ) diff --git a/tests/unit/linux/libnl-helpers/TestNetlinkException.cxx b/tests/unit/linux/libnl-helpers/TestNetlinkException.cxx new file mode 100644 index 00000000..0f688179 --- /dev/null +++ b/tests/unit/linux/libnl-helpers/TestNetlinkException.cxx @@ -0,0 +1,14 @@ + +#include +#include +#include + +TEST_CASE("NetlinkException", "[linux][libnl-helpers]") +{ + using Microsoft::Net::Netlink::NetlinkException; + + SECTION("Does not crash") + { + REQUIRE_THROWS_AS(throw NetlinkException(NLE_BAD_SOCK, "test"), NetlinkException); + } +}