blob: 4e81d481dfe883d231b67b3d9f072be1940ac200 [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>
William A. Kennington III84bfe672022-07-13 14:15:30 -070012#include <stdplus/gtest/tmp.hpp>
Patrick Venturea9733402019-01-07 13:27:01 -080013#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050014
15#include <gtest/gtest.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +053016
17namespace phosphor
18{
19namespace network
20{
21
Manojkiran Edaa879baa2020-06-13 14:39:08 +053022namespace fs = std::filesystem;
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{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
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 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 }
Ratan Gupta255d5142017-08-10 09:02:08 +053034
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 void createInterfaces()
36 {
37 manager.createInterfaces();
38 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053039};
40
41// getifaddrs will not return any interface
42TEST_F(TestNetworkManager, NoInterface)
43{
44 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Patrick Venture8fe594c2019-01-07 13:24:12 -080045 EXPECT_THROW(createInterfaces(), InternalFailure);
Ratan Gupta8ab17922017-05-25 13:07:05 +053046}
Ratan Gupta8ab17922017-05-25 13:07:05 +053047// getifaddrs returns single interface.
48TEST_F(TestNetworkManager, WithSingleInterface)
49{
William A. Kennington III8cff3802019-04-21 20:17:27 -070050 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070051
William A. Kennington III8cff3802019-04-21 20:17:27 -070052 // Adds the following ip in the getifaddrs list.
53 mock_addIF("igb1", 2);
54 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053055
William A. Kennington III8cff3802019-04-21 20:17:27 -070056 // Now create the interfaces which will call the mocked getifaddrs
57 // which returns the above interface detail.
58 createInterfaces();
59 EXPECT_EQ(1, manager.getInterfaceCount());
60 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053061}
62
63// getifaddrs returns two interfaces.
64TEST_F(TestNetworkManager, WithMultipleInterfaces)
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 mock_addIF("igb0", 1);
69 mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053070
William A. Kennington III8cff3802019-04-21 20:17:27 -070071 mock_addIF("igb1", 2);
72 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053073
William A. Kennington III8cff3802019-04-21 20:17:27 -070074 createInterfaces();
75 EXPECT_EQ(2, manager.getInterfaceCount());
76 EXPECT_EQ(true, manager.hasInterface("igb0"));
77 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053078}
Gunnar Mills57d9c502018-09-14 14:42:34 -050079} // namespace network
80} // namespace phosphor