blob: 3d4cee8e80c3a3fd1a5403cbd395a4c950fdf6d3 [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();
William A. Kennington III96444792022-10-05 15:16:22 -070059 EXPECT_EQ(1, manager.getInterfaces().size());
60 EXPECT_NE(manager.getInterfaces().end(),
61 manager.getInterfaces().find("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053062}
63
64// getifaddrs returns two interfaces.
65TEST_F(TestNetworkManager, WithMultipleInterfaces)
66{
William A. Kennington III8cff3802019-04-21 20:17:27 -070067 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070068
William A. Kennington III8cff3802019-04-21 20:17:27 -070069 mock_addIF("igb0", 1);
70 mock_addIP("igb0", "192.0.2.2", "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 mock_addIF("igb1", 2);
73 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053074
William A. Kennington III8cff3802019-04-21 20:17:27 -070075 createInterfaces();
William A. Kennington III96444792022-10-05 15:16:22 -070076 EXPECT_EQ(2, manager.getInterfaces().size());
77 EXPECT_NE(manager.getInterfaces().end(),
78 manager.getInterfaces().find("igb0"));
79 EXPECT_NE(manager.getInterfaces().end(),
80 manager.getInterfaces().find("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053081}
Gunnar Mills57d9c502018-09-14 14:42:34 -050082} // namespace network
83} // namespace phosphor