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