1 #include "util.hpp" 2 3 #include <stdexcept> 4 #include <stdplus/raw.hpp> 5 #include <string> 6 #include <string_view> 7 8 #include <gmock/gmock.h> 9 #include <gtest/gtest.h> 10 11 namespace phosphor 12 { 13 namespace network 14 { 15 16 TEST(TestUtil, AddrFromBuf) 17 { 18 std::string tooSmall(1, 'a'); 19 std::string tooLarge(24, 'a'); 20 21 struct in_addr ip1 = {0x01020304}; 22 auto buf1 = stdplus::raw::asView<char>(ip1); 23 InAddrAny res1 = addrFromBuf(AF_INET, buf1); 24 EXPECT_EQ(ip1, res1); 25 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error); 26 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error); 27 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument); 28 29 struct in6_addr ip2 = {0xfd, 0, 0, 0, 1}; 30 auto buf2 = stdplus::raw::asView<char>(ip2); 31 InAddrAny res2 = addrFromBuf(AF_INET6, buf2); 32 EXPECT_EQ(ip2, res2); 33 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error); 34 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error); 35 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument); 36 } 37 38 TEST(TestUtil, InterfaceToUbootEthAddr) 39 { 40 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("et")); 41 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth")); 42 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("sit0")); 43 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("ethh0")); 44 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth0h")); 45 EXPECT_EQ("ethaddr", interfaceToUbootEthAddr("eth0")); 46 EXPECT_EQ("eth1addr", interfaceToUbootEthAddr("eth1")); 47 EXPECT_EQ("eth5addr", interfaceToUbootEthAddr("eth5")); 48 EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28")); 49 } 50 51 namespace mac_address 52 { 53 54 TEST(MacIsEmpty, True) 55 { 56 EXPECT_TRUE(isEmpty({})); 57 } 58 59 TEST(MacIsEmpty, False) 60 { 61 EXPECT_FALSE(isEmpty({1})); 62 EXPECT_FALSE(isEmpty({0, 0, 0, 1})); 63 EXPECT_FALSE(isEmpty({0, 0, 0, 0, 0, 1})); 64 } 65 66 TEST(MacIsMulticast, True) 67 { 68 EXPECT_TRUE(isMulticast({255, 255, 255, 255, 255, 255})); 69 EXPECT_TRUE(isMulticast({1})); 70 } 71 72 TEST(MacIsMulticast, False) 73 { 74 EXPECT_FALSE(isMulticast({0, 1, 2, 3, 4, 5})); 75 EXPECT_FALSE(isMulticast({0xfe, 255, 255, 255, 255, 255})); 76 } 77 78 TEST(MacIsUnicast, True) 79 { 80 EXPECT_TRUE(isUnicast({0, 1, 2, 3, 4, 5})); 81 EXPECT_TRUE(isUnicast({0xfe, 255, 255, 255, 255, 255})); 82 } 83 84 TEST(MacIsUnicast, False) 85 { 86 EXPECT_FALSE(isUnicast({})); 87 EXPECT_FALSE(isUnicast({1})); 88 EXPECT_FALSE(isUnicast({255, 255, 255, 255, 255, 255})); 89 } 90 91 TEST(IgnoredInterfaces, Empty) 92 { 93 auto ret = internal::parseInterfaces({}); 94 EXPECT_TRUE(ret.empty()); 95 96 ret = internal::parseInterfaces(" , ,, "); 97 EXPECT_TRUE(ret.empty()); 98 } 99 100 TEST(IgnoredInterfaces, NotEmpty) 101 { 102 using ::testing::ContainerEq; 103 std::unordered_set<std::string_view> expected = {"eth0"}; 104 auto ret = internal::parseInterfaces("eth0"); 105 EXPECT_THAT(ret, ContainerEq(expected)); 106 107 expected = {"eth0", "eth1", "bond1", "usb0"}; 108 ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,"); 109 EXPECT_THAT(ret, ContainerEq(expected)); 110 } 111 112 } // namespace mac_address 113 } // namespace network 114 } // namespace phosphor 115