Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
William A. Kennington III | 1b59420 | 2022-11-17 18:16:46 -0800 | [diff] [blame] | 3 | #include <stdplus/raw.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 4 | |
| 5 | #include <stdexcept> |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <string_view> |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 8 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace network |
| 15 | { |
| 16 | |
William A. Kennington III | 1b59420 | 2022-11-17 18:16:46 -0800 | [diff] [blame] | 17 | TEST(TestUtil, AddrFromBuf) |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 18 | { |
| 19 | std::string tooSmall(1, 'a'); |
| 20 | std::string tooLarge(24, 'a'); |
| 21 | |
William A. Kennington III | 1b59420 | 2022-11-17 18:16:46 -0800 | [diff] [blame] | 22 | struct in_addr ip1 = {0x01020304}; |
| 23 | auto buf1 = stdplus::raw::asView<char>(ip1); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 24 | InAddrAny res1 = addrFromBuf(AF_INET, buf1); |
William A. Kennington III | 1b59420 | 2022-11-17 18:16:46 -0800 | [diff] [blame] | 25 | EXPECT_EQ(ip1, res1); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 26 | EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error); |
| 27 | EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error); |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 28 | EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 29 | |
William A. Kennington III | 1b59420 | 2022-11-17 18:16:46 -0800 | [diff] [blame] | 30 | struct in6_addr ip2 = {0xfd, 0, 0, 0, 1}; |
| 31 | auto buf2 = stdplus::raw::asView<char>(ip2); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 32 | InAddrAny res2 = addrFromBuf(AF_INET6, buf2); |
William A. Kennington III | 1b59420 | 2022-11-17 18:16:46 -0800 | [diff] [blame] | 33 | EXPECT_EQ(ip2, res2); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 34 | EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error); |
| 35 | EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error); |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 36 | EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 37 | } |
| 38 | |
William A. Kennington III | 1b59420 | 2022-11-17 18:16:46 -0800 | [diff] [blame] | 39 | TEST(TestUtil, InterfaceToUbootEthAddr) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 40 | { |
| 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 III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 52 | namespace mac_address |
| 53 | { |
| 54 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 55 | TEST(MacIsEmpty, True) |
| 56 | { |
| 57 | EXPECT_TRUE(isEmpty({})); |
| 58 | } |
| 59 | |
| 60 | TEST(MacIsEmpty, False) |
| 61 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 62 | EXPECT_FALSE(isEmpty({1})); |
| 63 | EXPECT_FALSE(isEmpty({0, 0, 0, 1})); |
| 64 | EXPECT_FALSE(isEmpty({0, 0, 0, 0, 0, 1})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST(MacIsMulticast, True) |
| 68 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(isMulticast({255, 255, 255, 255, 255, 255})); |
| 70 | EXPECT_TRUE(isMulticast({1})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(MacIsMulticast, False) |
| 74 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 75 | EXPECT_FALSE(isMulticast({0, 1, 2, 3, 4, 5})); |
| 76 | EXPECT_FALSE(isMulticast({0xfe, 255, 255, 255, 255, 255})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(MacIsUnicast, True) |
| 80 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 81 | EXPECT_TRUE(isUnicast({0, 1, 2, 3, 4, 5})); |
| 82 | EXPECT_TRUE(isUnicast({0xfe, 255, 255, 255, 255, 255})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(MacIsUnicast, False) |
| 86 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 87 | EXPECT_FALSE(isUnicast({})); |
| 88 | EXPECT_FALSE(isUnicast({1})); |
| 89 | EXPECT_FALSE(isUnicast({255, 255, 255, 255, 255, 255})); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 92 | TEST(IgnoredInterfaces, Empty) |
| 93 | { |
| 94 | auto ret = internal::parseInterfaces({}); |
| 95 | EXPECT_TRUE(ret.empty()); |
| 96 | |
| 97 | ret = internal::parseInterfaces(" , ,, "); |
| 98 | EXPECT_TRUE(ret.empty()); |
| 99 | } |
| 100 | |
| 101 | TEST(IgnoredInterfaces, NotEmpty) |
| 102 | { |
| 103 | using ::testing::ContainerEq; |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 104 | std::unordered_set<std::string_view> expected = {"eth0"}; |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 105 | auto ret = internal::parseInterfaces("eth0"); |
| 106 | EXPECT_THAT(ret, ContainerEq(expected)); |
| 107 | |
| 108 | expected = {"eth0", "eth1", "bond1", "usb0"}; |
| 109 | ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,"); |
| 110 | EXPECT_THAT(ret, ContainerEq(expected)); |
| 111 | } |
| 112 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 113 | } // namespace mac_address |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 114 | } // namespace network |
| 115 | } // namespace phosphor |