Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 1 | #include "mock_network_manager.hpp" |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 2 | #include "mock_syscall.hpp" |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 3 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | #include <arpa/inet.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 5 | #include <net/if.h> |
| 6 | #include <netinet/in.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 9 | #include <exception> |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 10 | #include <filesystem> |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 12 | #include <stdplus/gtest/tmp.hpp> |
Patrick Venture | a973340 | 2019-01-07 13:27:01 -0800 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 14 | |
| 15 | #include <gtest/gtest.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace network |
| 20 | { |
| 21 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 22 | namespace fs = std::filesystem; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 23 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 24 | class TestNetworkManager : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 27 | sdbusplus::bus_t bus; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 28 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 29 | TestNetworkManager() : |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 30 | bus(sdbusplus::bus::new_default()), |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 31 | manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir()) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 32 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 33 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 34 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | void createInterfaces() |
| 36 | { |
| 37 | manager.createInterfaces(); |
| 38 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | // getifaddrs will not return any interface |
| 42 | TEST_F(TestNetworkManager, NoInterface) |
| 43 | { |
| 44 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Patrick Venture | 8fe594c | 2019-01-07 13:24:12 -0800 | [diff] [blame] | 45 | EXPECT_THROW(createInterfaces(), InternalFailure); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 46 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 47 | // getifaddrs returns single interface. |
| 48 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 49 | { |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 50 | mock_clear(); |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 51 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 52 | // 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 Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 55 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 56 | // Now create the interfaces which will call the mocked getifaddrs |
| 57 | // which returns the above interface detail. |
| 58 | createInterfaces(); |
William A. Kennington III | 9644479 | 2022-10-05 15:16:22 -0700 | [diff] [blame] | 59 | EXPECT_EQ(1, manager.getInterfaces().size()); |
| 60 | EXPECT_NE(manager.getInterfaces().end(), |
| 61 | manager.getInterfaces().find("igb1")); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // getifaddrs returns two interfaces. |
| 65 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 66 | { |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 67 | mock_clear(); |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 68 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 69 | mock_addIF("igb0", 1); |
| 70 | mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 71 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 72 | mock_addIF("igb1", 2); |
| 73 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 74 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 75 | createInterfaces(); |
William A. Kennington III | 9644479 | 2022-10-05 15:16:22 -0700 | [diff] [blame] | 76 | 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 Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 81 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 82 | } // namespace network |
| 83 | } // namespace phosphor |