1 #include "util.hpp" 2 3 #include <arpa/inet.h> 4 #include <fmt/chrono.h> 5 #include <netinet/in.h> 6 7 #include <charconv> 8 #include <cstddef> 9 #include <cstring> 10 #include <stdexcept> 11 #include <string> 12 #include <string_view> 13 #include <xyz/openbmc_project/Common/error.hpp> 14 15 #include <gmock/gmock.h> 16 #include <gtest/gtest.h> 17 18 namespace phosphor 19 { 20 namespace network 21 { 22 23 using namespace std::literals; 24 using InternalFailure = 25 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 26 class TestUtil : public testing::Test 27 { 28 public: 29 TestUtil() 30 { 31 // Empty 32 } 33 }; 34 35 TEST_F(TestUtil, AddrFromBuf) 36 { 37 std::string tooSmall(1, 'a'); 38 std::string tooLarge(24, 'a'); 39 40 struct in_addr ip1; 41 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1)); 42 std::string_view buf1(reinterpret_cast<char*>(&ip1), sizeof(ip1)); 43 InAddrAny res1 = addrFromBuf(AF_INET, buf1); 44 EXPECT_EQ(0, memcmp(&ip1, &std::get<struct in_addr>(res1), sizeof(ip1))); 45 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error); 46 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error); 47 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument); 48 49 struct in6_addr ip2; 50 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2)); 51 std::string_view buf2(reinterpret_cast<char*>(&ip2), sizeof(ip2)); 52 InAddrAny res2 = addrFromBuf(AF_INET6, buf2); 53 EXPECT_EQ(0, memcmp(&ip2, &std::get<struct in6_addr>(res2), sizeof(ip2))); 54 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error); 55 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error); 56 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument); 57 } 58 59 TEST_F(TestUtil, InterfaceToUbootEthAddr) 60 { 61 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("et")); 62 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth")); 63 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("sit0")); 64 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("ethh0")); 65 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth0h")); 66 EXPECT_EQ("ethaddr", interfaceToUbootEthAddr("eth0")); 67 EXPECT_EQ("eth1addr", interfaceToUbootEthAddr("eth1")); 68 EXPECT_EQ("eth5addr", interfaceToUbootEthAddr("eth5")); 69 EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28")); 70 } 71 72 namespace mac_address 73 { 74 75 TEST(MacIsEmpty, True) 76 { 77 EXPECT_TRUE(isEmpty({})); 78 } 79 80 TEST(MacIsEmpty, False) 81 { 82 EXPECT_FALSE(isEmpty({1})); 83 EXPECT_FALSE(isEmpty({0, 0, 0, 1})); 84 EXPECT_FALSE(isEmpty({0, 0, 0, 0, 0, 1})); 85 } 86 87 TEST(MacIsMulticast, True) 88 { 89 EXPECT_TRUE(isMulticast({255, 255, 255, 255, 255, 255})); 90 EXPECT_TRUE(isMulticast({1})); 91 } 92 93 TEST(MacIsMulticast, False) 94 { 95 EXPECT_FALSE(isMulticast({0, 1, 2, 3, 4, 5})); 96 EXPECT_FALSE(isMulticast({0xfe, 255, 255, 255, 255, 255})); 97 } 98 99 TEST(MacIsUnicast, True) 100 { 101 EXPECT_TRUE(isUnicast({0, 1, 2, 3, 4, 5})); 102 EXPECT_TRUE(isUnicast({0xfe, 255, 255, 255, 255, 255})); 103 } 104 105 TEST(MacIsUnicast, False) 106 { 107 EXPECT_FALSE(isUnicast({})); 108 EXPECT_FALSE(isUnicast({1})); 109 EXPECT_FALSE(isUnicast({255, 255, 255, 255, 255, 255})); 110 } 111 112 TEST(IgnoredInterfaces, Empty) 113 { 114 auto ret = internal::parseInterfaces({}); 115 EXPECT_TRUE(ret.empty()); 116 117 ret = internal::parseInterfaces(" , ,, "); 118 EXPECT_TRUE(ret.empty()); 119 } 120 121 TEST(IgnoredInterfaces, NotEmpty) 122 { 123 using ::testing::ContainerEq; 124 std::unordered_set<std::string_view> expected = {"eth0"}; 125 auto ret = internal::parseInterfaces("eth0"); 126 EXPECT_THAT(ret, ContainerEq(expected)); 127 128 expected = {"eth0", "eth1", "bond1", "usb0"}; 129 ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,"); 130 EXPECT_THAT(ret, ContainerEq(expected)); 131 } 132 133 } // namespace mac_address 134 } // namespace network 135 } // namespace phosphor 136