Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 3 | #include <arpa/inet.h> |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 4 | #include <fmt/chrono.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include <netinet/in.h> |
| 6 | |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 7 | #include <charconv> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 8 | #include <cstddef> |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 9 | #include <cstring> |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 10 | #include <stdexcept> |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <string_view> |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
| 14 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 15 | #include <gmock/gmock.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 16 | #include <gtest/gtest.h> |
| 17 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace network |
| 21 | { |
| 22 | |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 23 | using namespace std::literals; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 24 | using InternalFailure = |
| 25 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 26 | class TestUtil : public testing::Test |
| 27 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 28 | public: |
| 29 | TestUtil() |
| 30 | { |
| 31 | // Empty |
| 32 | } |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 33 | }; |
| 34 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 35 | TEST_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 III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 47 | EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 48 | |
| 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 III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 56 | EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 57 | } |
| 58 | |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 59 | TEST_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 III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 72 | namespace mac_address |
| 73 | { |
| 74 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 75 | TEST(MacIsEmpty, True) |
| 76 | { |
| 77 | EXPECT_TRUE(isEmpty({})); |
| 78 | } |
| 79 | |
| 80 | TEST(MacIsEmpty, False) |
| 81 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 82 | 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 III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST(MacIsMulticast, True) |
| 88 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(isMulticast({255, 255, 255, 255, 255, 255})); |
| 90 | EXPECT_TRUE(isMulticast({1})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST(MacIsMulticast, False) |
| 94 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 95 | EXPECT_FALSE(isMulticast({0, 1, 2, 3, 4, 5})); |
| 96 | EXPECT_FALSE(isMulticast({0xfe, 255, 255, 255, 255, 255})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST(MacIsUnicast, True) |
| 100 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 101 | EXPECT_TRUE(isUnicast({0, 1, 2, 3, 4, 5})); |
| 102 | EXPECT_TRUE(isUnicast({0xfe, 255, 255, 255, 255, 255})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST(MacIsUnicast, False) |
| 106 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 107 | EXPECT_FALSE(isUnicast({})); |
| 108 | EXPECT_FALSE(isUnicast({1})); |
| 109 | EXPECT_FALSE(isUnicast({255, 255, 255, 255, 255, 255})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 112 | TEST(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 | |
| 121 | TEST(IgnoredInterfaces, NotEmpty) |
| 122 | { |
| 123 | using ::testing::ContainerEq; |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 124 | std::unordered_set<std::string_view> expected = {"eth0"}; |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 125 | 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 III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 133 | } // namespace mac_address |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 134 | } // namespace network |
| 135 | } // namespace phosphor |