blob: 9e1b4d0929956ee13e859308b4cc6ee39733532f [file] [log] [blame]
Ratan Gupta594123d2017-05-16 22:48:35 +05301#include "util.hpp"
2
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -07003#include <stdexcept>
William A. Kennington III1b594202022-11-17 18:16:46 -08004#include <stdplus/raw.hpp>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -07005#include <string>
6#include <string_view>
Ratan Gupta35297172018-11-28 18:40:16 +05307
Lei YU307554e2021-03-18 14:56:50 +08008#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -05009#include <gtest/gtest.h>
10
Ratan Gupta594123d2017-05-16 22:48:35 +053011namespace phosphor
12{
13namespace network
14{
15
William A. Kennington III1b594202022-11-17 18:16:46 -080016TEST(TestUtil, AddrFromBuf)
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080017{
18 std::string tooSmall(1, 'a');
19 std::string tooLarge(24, 'a');
20
William A. Kennington III1b594202022-11-17 18:16:46 -080021 struct in_addr ip1 = {0x01020304};
22 auto buf1 = stdplus::raw::asView<char>(ip1);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080023 InAddrAny res1 = addrFromBuf(AF_INET, buf1);
William A. Kennington III1b594202022-11-17 18:16:46 -080024 EXPECT_EQ(ip1, res1);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080025 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error);
26 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070027 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080028
William A. Kennington III1b594202022-11-17 18:16:46 -080029 struct in6_addr ip2 = {0xfd, 0, 0, 0, 1};
30 auto buf2 = stdplus::raw::asView<char>(ip2);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080031 InAddrAny res2 = addrFromBuf(AF_INET6, buf2);
William A. Kennington III1b594202022-11-17 18:16:46 -080032 EXPECT_EQ(ip2, res2);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080033 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error);
34 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070035 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080036}
37
William A. Kennington III1b594202022-11-17 18:16:46 -080038TEST(TestUtil, InterfaceToUbootEthAddr)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070039{
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
William A. Kennington III1137a972019-04-20 20:49:58 -070051namespace mac_address
52{
53
William A. Kennington III1137a972019-04-20 20:49:58 -070054TEST(MacIsEmpty, True)
55{
56 EXPECT_TRUE(isEmpty({}));
57}
58
59TEST(MacIsEmpty, False)
60{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070061 EXPECT_FALSE(isEmpty({1}));
62 EXPECT_FALSE(isEmpty({0, 0, 0, 1}));
63 EXPECT_FALSE(isEmpty({0, 0, 0, 0, 0, 1}));
William A. Kennington III1137a972019-04-20 20:49:58 -070064}
65
66TEST(MacIsMulticast, True)
67{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070068 EXPECT_TRUE(isMulticast({255, 255, 255, 255, 255, 255}));
69 EXPECT_TRUE(isMulticast({1}));
William A. Kennington III1137a972019-04-20 20:49:58 -070070}
71
72TEST(MacIsMulticast, False)
73{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070074 EXPECT_FALSE(isMulticast({0, 1, 2, 3, 4, 5}));
75 EXPECT_FALSE(isMulticast({0xfe, 255, 255, 255, 255, 255}));
William A. Kennington III1137a972019-04-20 20:49:58 -070076}
77
78TEST(MacIsUnicast, True)
79{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070080 EXPECT_TRUE(isUnicast({0, 1, 2, 3, 4, 5}));
81 EXPECT_TRUE(isUnicast({0xfe, 255, 255, 255, 255, 255}));
William A. Kennington III1137a972019-04-20 20:49:58 -070082}
83
84TEST(MacIsUnicast, False)
85{
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -070086 EXPECT_FALSE(isUnicast({}));
87 EXPECT_FALSE(isUnicast({1}));
88 EXPECT_FALSE(isUnicast({255, 255, 255, 255, 255, 255}));
William A. Kennington III1137a972019-04-20 20:49:58 -070089}
90
Lei YU307554e2021-03-18 14:56:50 +080091TEST(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
100TEST(IgnoredInterfaces, NotEmpty)
101{
102 using ::testing::ContainerEq;
William A. Kennington III95530ec2022-08-19 01:44:39 -0700103 std::unordered_set<std::string_view> expected = {"eth0"};
Lei YU307554e2021-03-18 14:56:50 +0800104 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
William A. Kennington III1137a972019-04-20 20:49:58 -0700112} // namespace mac_address
Gunnar Mills57d9c502018-09-14 14:42:34 -0500113} // namespace network
114} // namespace phosphor