blob: 3978b1e3db8e70c15255d17994be15c7cfdf8e30 [file] [log] [blame]
Ratan Gupta594123d2017-05-16 22:48:35 +05301#include "util.hpp"
2
William A. Kennington III5058f572019-01-30 17:18:14 -08003#include <arpa/inet.h>
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -07004#include <fmt/chrono.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include <netinet/in.h>
6
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -07007#include <charconv>
William A. Kennington IIId27410f2019-01-30 17:15:43 -08008#include <cstddef>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -07009#include <cstring>
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -070010#include <stdexcept>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070011#include <string>
12#include <string_view>
Ratan Gupta35297172018-11-28 18:40:16 +053013#include <xyz/openbmc_project/Common/error.hpp>
14
Lei YU307554e2021-03-18 14:56:50 +080015#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050016#include <gtest/gtest.h>
17
Ratan Gupta594123d2017-05-16 22:48:35 +053018namespace phosphor
19{
20namespace network
21{
22
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070023using namespace std::literals;
Ratan Gupta35297172018-11-28 18:40:16 +053024using InternalFailure =
25 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Ratan Gupta594123d2017-05-16 22:48:35 +053026class TestUtil : public testing::Test
27{
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 public:
29 TestUtil()
30 {
31 // Empty
32 }
Ratan Gupta594123d2017-05-16 22:48:35 +053033};
34
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080035TEST_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);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070047 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080048
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);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070056 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080057}
58
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070059TEST_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
William A. Kennington III1137a972019-04-20 20:49:58 -070072namespace mac_address
73{
74
William A. Kennington III1137a972019-04-20 20:49:58 -070075TEST(MacIsEmpty, True)
76{
77 EXPECT_TRUE(isEmpty({}));
78}
79
80TEST(MacIsEmpty, False)
81{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070082 EXPECT_FALSE(isEmpty({1}));
83 EXPECT_FALSE(isEmpty({0, 0, 0, 1}));
84 EXPECT_FALSE(isEmpty({0, 0, 0, 0, 0, 1}));
William A. Kennington III1137a972019-04-20 20:49:58 -070085}
86
87TEST(MacIsMulticast, True)
88{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070089 EXPECT_TRUE(isMulticast({255, 255, 255, 255, 255, 255}));
90 EXPECT_TRUE(isMulticast({1}));
William A. Kennington III1137a972019-04-20 20:49:58 -070091}
92
93TEST(MacIsMulticast, False)
94{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070095 EXPECT_FALSE(isMulticast({0, 1, 2, 3, 4, 5}));
96 EXPECT_FALSE(isMulticast({0xfe, 255, 255, 255, 255, 255}));
William A. Kennington III1137a972019-04-20 20:49:58 -070097}
98
99TEST(MacIsUnicast, True)
100{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -0700101 EXPECT_TRUE(isUnicast({0, 1, 2, 3, 4, 5}));
102 EXPECT_TRUE(isUnicast({0xfe, 255, 255, 255, 255, 255}));
William A. Kennington III1137a972019-04-20 20:49:58 -0700103}
104
105TEST(MacIsUnicast, False)
106{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -0700107 EXPECT_FALSE(isUnicast({}));
108 EXPECT_FALSE(isUnicast({1}));
109 EXPECT_FALSE(isUnicast({255, 255, 255, 255, 255, 255}));
William A. Kennington III1137a972019-04-20 20:49:58 -0700110}
111
Lei YU307554e2021-03-18 14:56:50 +0800112TEST(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
121TEST(IgnoredInterfaces, NotEmpty)
122{
123 using ::testing::ContainerEq;
William A. Kennington III95530ec2022-08-19 01:44:39 -0700124 std::unordered_set<std::string_view> expected = {"eth0"};
Lei YU307554e2021-03-18 14:56:50 +0800125 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
William A. Kennington III1137a972019-04-20 20:49:58 -0700133} // namespace mac_address
Gunnar Mills57d9c502018-09-14 14:42:34 -0500134} // namespace network
135} // namespace phosphor