William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 1 | #include "test_network_manager.hpp" |
| 2 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 3 | #include "config_parser.hpp" |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 4 | |
William A. Kennington III | 9c441fd | 2023-02-24 13:40:01 -0800 | [diff] [blame] | 5 | #include <net/if_arp.h> |
| 6 | |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 8 | #include <stdplus/gtest/tmp.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 9 | |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 10 | #include <filesystem> |
| 11 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 12 | #include <gtest/gtest.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 19 | using ::testing::Key; |
| 20 | using ::testing::UnorderedElementsAre; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 21 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 22 | class TestNetworkManager : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 23 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 24 | protected: |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 25 | stdplus::Pinned<sdbusplus::bus_t> bus; |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 26 | TestManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 27 | TestNetworkManager() : |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 28 | bus(sdbusplus::bus::new_default()), |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 29 | manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir()) |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 30 | {} |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 31 | |
| 32 | void deleteVLAN(std::string_view ifname) |
| 33 | { |
| 34 | manager.interfaces.find(ifname)->second->vlan->delete_(); |
| 35 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 36 | }; |
| 37 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 38 | TEST_F(TestNetworkManager, NoInterface) |
| 39 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 40 | EXPECT_TRUE(manager.interfaces.empty()); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 41 | } |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 42 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 43 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 44 | { |
William A. Kennington III | 9c441fd | 2023-02-24 13:40:01 -0800 | [diff] [blame] | 45 | manager.addInterface( |
| 46 | {.type = ARPHRD_ETHER, .idx = 2, .flags = 0, .name = "igb1"}); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 47 | manager.handleAdminState("managed", 2); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 48 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 49 | // Now create the interfaces which will call the mocked getifaddrs |
| 50 | // which returns the above interface detail. |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 51 | EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1"))); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // getifaddrs returns two interfaces. |
| 55 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 56 | { |
William A. Kennington III | 9c441fd | 2023-02-24 13:40:01 -0800 | [diff] [blame] | 57 | manager.addInterface( |
| 58 | {.type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "igb0"}); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 59 | manager.handleAdminState("managed", 1); |
| 60 | manager.handleAdminState("unmanaged", 2); |
William A. Kennington III | 9c441fd | 2023-02-24 13:40:01 -0800 | [diff] [blame] | 61 | manager.addInterface( |
| 62 | {.type = ARPHRD_ETHER, .idx = 2, .flags = 0, .name = "igb1"}); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 63 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 64 | EXPECT_THAT(manager.interfaces, |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 65 | UnorderedElementsAre(Key("igb0"), Key("igb1"))); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 66 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 67 | |
| 68 | TEST_F(TestNetworkManager, WithVLAN) |
| 69 | { |
| 70 | EXPECT_THROW(manager.vlan("", 8000), std::exception); |
| 71 | EXPECT_THROW(manager.vlan("", 0), std::exception); |
| 72 | EXPECT_THROW(manager.vlan("eth0", 2), std::exception); |
| 73 | |
William A. Kennington III | 9c441fd | 2023-02-24 13:40:01 -0800 | [diff] [blame] | 74 | manager.addInterface( |
| 75 | {.type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "eth0"}); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 76 | manager.handleAdminState("managed", 1); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 77 | EXPECT_NO_THROW(manager.vlan("eth0", 2)); |
| 78 | EXPECT_NO_THROW(manager.vlan("eth0", 4094)); |
| 79 | EXPECT_THAT( |
| 80 | manager.interfaces, |
| 81 | UnorderedElementsAre(Key("eth0"), Key("eth0.2"), Key("eth0.4094"))); |
| 82 | auto netdev1 = config::pathForIntfDev(CaseTmpDir(), "eth0.2"); |
| 83 | auto netdev2 = config::pathForIntfDev(CaseTmpDir(), "eth0.4094"); |
| 84 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev1)); |
| 85 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); |
| 86 | |
| 87 | deleteVLAN("eth0.2"); |
| 88 | EXPECT_THAT(manager.interfaces, |
| 89 | UnorderedElementsAre(Key("eth0"), Key("eth0.4094"))); |
| 90 | EXPECT_FALSE(std::filesystem::is_regular_file(netdev1)); |
| 91 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); |
| 92 | } |
| 93 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 94 | } // namespace network |
| 95 | } // namespace phosphor |