blob: 48da2f22e7682a9bb3e7de8830c6102712bb0619 [file] [log] [blame]
Ratan Gupta35297172018-11-28 18:40:16 +05301#include "mock_network_manager.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05302#include "mock_syscall.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05303
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include <arpa/inet.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05305#include <net/if.h>
6#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05307#include <stdlib.h>
8
Ratan Gupta8ab17922017-05-25 13:07:05 +05309#include <exception>
Manojkiran Edaa879baa2020-06-13 14:39:08 +053010#include <filesystem>
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070011#include <sdbusplus/bus.hpp>
Patrick Venturea9733402019-01-07 13:27:01 -080012#include <xyz/openbmc_project/Common/error.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
Manojkiran Edaa879baa2020-06-13 14:39:08 +053021namespace fs = std::filesystem;
Ratan Gupta255d5142017-08-10 09:02:08 +053022
Ratan Gupta8ab17922017-05-25 13:07:05 +053023class TestNetworkManager : public testing::Test
24{
Gunnar Mills57d9c502018-09-14 14:42:34 -050025 public:
26 sdbusplus::bus::bus bus;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053027 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 std::string confDir;
29 TestNetworkManager() :
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070030 bus(sdbusplus::bus::new_default()),
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 manager(bus, "/xyz/openbmc_test/abc", "/tmp")
32 {
33 setConfDir();
34 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 ~TestNetworkManager()
37 {
38 if (confDir != "")
Ratan Gupta8ab17922017-05-25 13:07:05 +053039 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 fs::remove_all(confDir);
Ratan Gupta255d5142017-08-10 09:02:08 +053041 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053043
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 void setConfDir()
45 {
46 char tmp[] = "/tmp/NetworkManager.XXXXXX";
47 confDir = mkdtemp(tmp);
48 manager.setConfDir(confDir);
49 }
Ratan Gupta255d5142017-08-10 09:02:08 +053050
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 void createInterfaces()
52 {
53 manager.createInterfaces();
54 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053055};
56
57// getifaddrs will not return any interface
58TEST_F(TestNetworkManager, NoInterface)
59{
60 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Patrick Venture8fe594c2019-01-07 13:24:12 -080061 EXPECT_THROW(createInterfaces(), InternalFailure);
Ratan Gupta8ab17922017-05-25 13:07:05 +053062}
Ratan Gupta8ab17922017-05-25 13:07:05 +053063// getifaddrs returns single interface.
64TEST_F(TestNetworkManager, WithSingleInterface)
65{
William A. Kennington III8cff3802019-04-21 20:17:27 -070066 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070067
William A. Kennington III8cff3802019-04-21 20:17:27 -070068 // Adds the following ip in the getifaddrs list.
69 mock_addIF("igb1", 2);
70 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053071
William A. Kennington III8cff3802019-04-21 20:17:27 -070072 // Now create the interfaces which will call the mocked getifaddrs
73 // which returns the above interface detail.
74 createInterfaces();
75 EXPECT_EQ(1, manager.getInterfaceCount());
76 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053077}
78
79// getifaddrs returns two interfaces.
80TEST_F(TestNetworkManager, WithMultipleInterfaces)
81{
William A. Kennington III8cff3802019-04-21 20:17:27 -070082 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070083
William A. Kennington III8cff3802019-04-21 20:17:27 -070084 mock_addIF("igb0", 1);
85 mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053086
William A. Kennington III8cff3802019-04-21 20:17:27 -070087 mock_addIF("igb1", 2);
88 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053089
William A. Kennington III8cff3802019-04-21 20:17:27 -070090 createInterfaces();
91 EXPECT_EQ(2, manager.getInterfaceCount());
92 EXPECT_EQ(true, manager.hasInterface("igb0"));
93 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053094}
Gunnar Mills57d9c502018-09-14 14:42:34 -050095} // namespace network
96} // namespace phosphor