blob: a607bb7bc6c76597c7f651d307348d8189071208 [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
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070022using ::testing::Key;
23using ::testing::UnorderedElementsAre;
Manojkiran Edaa879baa2020-06-13 14:39:08 +053024namespace fs = std::filesystem;
Ratan Gupta255d5142017-08-10 09:02:08 +053025
William A. Kennington III84bfe672022-07-13 14:15:30 -070026class TestNetworkManager : public stdplus::gtest::TestWithTmp
Ratan Gupta8ab17922017-05-25 13:07:05 +053027{
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050029 sdbusplus::bus_t bus;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053030 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 TestNetworkManager() :
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070032 bus(sdbusplus::bus::new_default()),
William A. Kennington III84bfe672022-07-13 14:15:30 -070033 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 }
Ratan Gupta255d5142017-08-10 09:02:08 +053036
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 void createInterfaces()
38 {
39 manager.createInterfaces();
40 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053041};
42
43// getifaddrs will not return any interface
44TEST_F(TestNetworkManager, NoInterface)
45{
46 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Patrick Venture8fe594c2019-01-07 13:24:12 -080047 EXPECT_THROW(createInterfaces(), InternalFailure);
Ratan Gupta8ab17922017-05-25 13:07:05 +053048}
Ratan Gupta8ab17922017-05-25 13:07:05 +053049// getifaddrs returns single interface.
50TEST_F(TestNetworkManager, WithSingleInterface)
51{
William A. Kennington III8cff3802019-04-21 20:17:27 -070052 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070053
William A. Kennington III8cff3802019-04-21 20:17:27 -070054 // Adds the following ip in the getifaddrs list.
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070055 mock_addIF("igb1", /*idx=*/2);
56 mock_addIP("igb1", "192.0.2.3", "255.255.255.128");
Ratan Gupta8ab17922017-05-25 13:07:05 +053057
William A. Kennington III8cff3802019-04-21 20:17:27 -070058 // Now create the interfaces which will call the mocked getifaddrs
59 // which returns the above interface detail.
60 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070061 EXPECT_THAT(manager.getInterfaces(), UnorderedElementsAre(Key("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 III9ecb90e2022-10-14 03:12:43 -070069 mock_addIF("igb0", /*idx=*/1);
70 mock_addIP("igb0", "192.0.2.2", "255.255.255.128");
71 mock_addIF("igb1", /*idx=*/2);
72 mock_addIP("igb1", "192.0.2.3", "255.255.255.128");
Ratan Gupta8ab17922017-05-25 13:07:05 +053073
William A. Kennington III8cff3802019-04-21 20:17:27 -070074 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070075 EXPECT_THAT(manager.getInterfaces(),
76 UnorderedElementsAre(Key("igb0"), Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053077}
Gunnar Mills57d9c502018-09-14 14:42:34 -050078} // namespace network
79} // namespace phosphor