blob: ab66e28b3397b037d89c6cd65f3a95e3af1b3d20 [file] [log] [blame]
Ratan Gupta594123d2017-05-16 22:48:35 +05301#include "util.hpp"
2
William A. Kennington III1b594202022-11-17 18:16:46 -08003#include <stdplus/raw.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -05004
5#include <stdexcept>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -07006#include <string>
7#include <string_view>
Ratan Gupta35297172018-11-28 18:40:16 +05308
Lei YU307554e2021-03-18 14:56:50 +08009#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050010#include <gtest/gtest.h>
11
Ratan Gupta594123d2017-05-16 22:48:35 +053012namespace phosphor
13{
14namespace network
15{
16
William A. Kennington III1b594202022-11-17 18:16:46 -080017TEST(TestUtil, AddrFromBuf)
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080018{
19 std::string tooSmall(1, 'a');
20 std::string tooLarge(24, 'a');
21
William A. Kennington III1b594202022-11-17 18:16:46 -080022 struct in_addr ip1 = {0x01020304};
23 auto buf1 = stdplus::raw::asView<char>(ip1);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080024 InAddrAny res1 = addrFromBuf(AF_INET, buf1);
William A. Kennington III1b594202022-11-17 18:16:46 -080025 EXPECT_EQ(ip1, res1);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080026 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error);
27 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070028 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080029
William A. Kennington III1b594202022-11-17 18:16:46 -080030 struct in6_addr ip2 = {0xfd, 0, 0, 0, 1};
31 auto buf2 = stdplus::raw::asView<char>(ip2);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080032 InAddrAny res2 = addrFromBuf(AF_INET6, buf2);
William A. Kennington III1b594202022-11-17 18:16:46 -080033 EXPECT_EQ(ip2, res2);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080034 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error);
35 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070036 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080037}
38
William A. Kennington III1b594202022-11-17 18:16:46 -080039TEST(TestUtil, InterfaceToUbootEthAddr)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070040{
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
William A. Kennington III1137a972019-04-20 20:49:58 -070052namespace mac_address
53{
54
Lei YU307554e2021-03-18 14:56:50 +080055TEST(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
64TEST(IgnoredInterfaces, NotEmpty)
65{
66 using ::testing::ContainerEq;
William A. Kennington III95530ec2022-08-19 01:44:39 -070067 std::unordered_set<std::string_view> expected = {"eth0"};
Lei YU307554e2021-03-18 14:56:50 +080068 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
William A. Kennington III1137a972019-04-20 20:49:58 -070076} // namespace mac_address
Gunnar Mills57d9c502018-09-14 14:42:34 -050077} // namespace network
78} // namespace phosphor