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 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 55 | TEST(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 | |
| 64 | TEST(IgnoredInterfaces, NotEmpty) |
| 65 | { |
| 66 | using ::testing::ContainerEq; |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 67 | std::unordered_set<std::string_view> expected = {"eth0"}; |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 68 | 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 III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 76 | } // namespace mac_address |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 77 | } // namespace network |
| 78 | } // namespace phosphor |