blob: 7e3e211985c8c65fa4ec80be69b6a46614d2f729 [file] [log] [blame]
William A. Kennington III09f3a4a2022-10-25 02:59:16 -07001#include "config_parser.hpp"
Ratan Gupta35297172018-11-28 18:40:16 +05302#include "mock_network_manager.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05303#include "mock_syscall.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05304
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include <arpa/inet.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05306#include <net/if.h>
7#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05308#include <stdlib.h>
9
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070010#include <filesystem>
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070011#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -070012#include <stdplus/gtest/tmp.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013
14#include <gtest/gtest.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +053015
16namespace phosphor
17{
18namespace network
19{
20
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070021using ::testing::Key;
22using ::testing::UnorderedElementsAre;
Ratan Gupta255d5142017-08-10 09:02:08 +053023
William A. Kennington III84bfe672022-07-13 14:15:30 -070024class TestNetworkManager : public stdplus::gtest::TestWithTmp
Ratan Gupta8ab17922017-05-25 13:07:05 +053025{
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070026 protected:
Patrick Williamsc38b0712022-07-22 19:26:54 -050027 sdbusplus::bus_t bus;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053028 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050029 TestNetworkManager() :
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070030 bus(sdbusplus::bus::new_default()),
William A. Kennington III84bfe672022-07-13 14:15:30 -070031 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
Gunnar Mills57d9c502018-09-14 14:42:34 -050032 {
William A. Kennington III77848562022-10-26 17:32:57 -070033 system::mock_clear();
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 }
Ratan Gupta255d5142017-08-10 09:02:08 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 void createInterfaces()
37 {
38 manager.createInterfaces();
39 }
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070040
41 void deleteVLAN(std::string_view ifname)
42 {
43 manager.interfaces.find(ifname)->second->vlan->delete_();
44 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053045};
46
47// getifaddrs will not return any interface
48TEST_F(TestNetworkManager, NoInterface)
49{
William A. Kennington III2e09d272022-10-14 17:15:00 -070050 createInterfaces();
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070051 EXPECT_TRUE(manager.interfaces.empty());
Ratan Gupta8ab17922017-05-25 13:07:05 +053052}
Ratan Gupta8ab17922017-05-25 13:07:05 +053053// getifaddrs returns single interface.
54TEST_F(TestNetworkManager, WithSingleInterface)
55{
William A. Kennington III8cff3802019-04-21 20:17:27 -070056 // Adds the following ip in the getifaddrs list.
William A. Kennington III77848562022-10-26 17:32:57 -070057 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"});
Ratan Gupta8ab17922017-05-25 13:07:05 +053058
William A. Kennington III8cff3802019-04-21 20:17:27 -070059 // Now create the interfaces which will call the mocked getifaddrs
60 // which returns the above interface detail.
61 createInterfaces();
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070062 EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053063}
64
65// getifaddrs returns two interfaces.
66TEST_F(TestNetworkManager, WithMultipleInterfaces)
67{
William A. Kennington III77848562022-10-26 17:32:57 -070068 system::mock_addIF({.idx = 1, .flags = 0, .name = "igb0"});
69 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"});
Ratan Gupta8ab17922017-05-25 13:07:05 +053070
William A. Kennington III8cff3802019-04-21 20:17:27 -070071 createInterfaces();
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070072 EXPECT_THAT(manager.interfaces,
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070073 UnorderedElementsAre(Key("igb0"), Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053074}
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070075
76TEST_F(TestNetworkManager, WithVLAN)
77{
78 EXPECT_THROW(manager.vlan("", 8000), std::exception);
79 EXPECT_THROW(manager.vlan("", 0), std::exception);
80 EXPECT_THROW(manager.vlan("eth0", 2), std::exception);
81
82 system::mock_addIF({.idx = 1, .flags = 0, .name = "eth0"});
83 manager.createInterfaces();
84 EXPECT_NO_THROW(manager.vlan("eth0", 2));
85 EXPECT_NO_THROW(manager.vlan("eth0", 4094));
86 EXPECT_THAT(
87 manager.interfaces,
88 UnorderedElementsAre(Key("eth0"), Key("eth0.2"), Key("eth0.4094")));
89 auto netdev1 = config::pathForIntfDev(CaseTmpDir(), "eth0.2");
90 auto netdev2 = config::pathForIntfDev(CaseTmpDir(), "eth0.4094");
91 EXPECT_TRUE(std::filesystem::is_regular_file(netdev1));
92 EXPECT_TRUE(std::filesystem::is_regular_file(netdev2));
93
94 deleteVLAN("eth0.2");
95 EXPECT_THAT(manager.interfaces,
96 UnorderedElementsAre(Key("eth0"), Key("eth0.4094")));
97 EXPECT_FALSE(std::filesystem::is_regular_file(netdev1));
98 EXPECT_TRUE(std::filesystem::is_regular_file(netdev2));
99}
100
Gunnar Mills57d9c502018-09-14 14:42:34 -0500101} // namespace network
102} // namespace phosphor