1 #include "util.hpp" 2 3 #include <stdplus/raw.hpp> 4 5 #include <stdexcept> 6 #include <string> 7 #include <string_view> 8 9 #include <gmock/gmock.h> 10 #include <gtest/gtest.h> 11 12 namespace phosphor 13 { 14 namespace network 15 { 16 17 TEST(TestUtil, AddrFromBuf) 18 { 19 std::string tooSmall(1, 'a'); 20 std::string tooLarge(24, 'a'); 21 22 stdplus::In4Addr ip1{1, 2, 3, 4}; 23 auto buf1 = stdplus::raw::asView<char>(ip1); 24 auto res1 = addrFromBuf(AF_INET, buf1); 25 EXPECT_EQ(ip1, res1); 26 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error); 27 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error); 28 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument); 29 30 stdplus::In6Addr ip2{0xfd, 0, 0, 0, 1}; 31 auto buf2 = stdplus::raw::asView<char>(ip2); 32 auto res2 = addrFromBuf(AF_INET6, buf2); 33 EXPECT_EQ(ip2, res2); 34 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error); 35 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error); 36 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument); 37 } 38 39 TEST(TestUtil, InterfaceToUbootEthAddr) 40 { 41 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("et")); 42 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth")); 43 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("sit0")); 44 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("ethh0")); 45 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth0h")); 46 EXPECT_EQ("ethaddr", interfaceToUbootEthAddr("eth0")); 47 EXPECT_EQ("eth1addr", interfaceToUbootEthAddr("eth1")); 48 EXPECT_EQ("eth5addr", interfaceToUbootEthAddr("eth5")); 49 EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28")); 50 } 51 52 namespace mac_address 53 { 54 55 TEST(IgnoredInterfaces, Empty) 56 { 57 auto ret = internal::parseInterfaces({}); 58 EXPECT_TRUE(ret.empty()); 59 60 ret = internal::parseInterfaces(" , ,, "); 61 EXPECT_TRUE(ret.empty()); 62 } 63 64 TEST(IgnoredInterfaces, NotEmpty) 65 { 66 using ::testing::ContainerEq; 67 std::unordered_set<std::string_view> expected = {"eth0"}; 68 auto ret = internal::parseInterfaces("eth0"); 69 EXPECT_THAT(ret, ContainerEq(expected)); 70 71 expected = {"eth0", "eth1", "bond1", "usb0"}; 72 ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,"); 73 EXPECT_THAT(ret, ContainerEq(expected)); 74 } 75 76 } // namespace mac_address 77 } // namespace network 78 } // namespace phosphor 79