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> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 10 | #include <experimental/filesystem> |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
Patrick Venture | a973340 | 2019-01-07 13:27:01 -0800 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | |
| 14 | #include <gtest/gtest.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
| 20 | |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 21 | std::unique_ptr<Timer> refreshObjectTimer = nullptr; |
| 22 | std::unique_ptr<Timer> restartTimer = nullptr; |
| 23 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 24 | namespace fs = std::experimental::filesystem; |
| 25 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 26 | class TestNetworkManager : public testing::Test |
| 27 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 28 | public: |
| 29 | sdbusplus::bus::bus bus; |
Patrick Venture | d94d23e | 2019-01-07 13:05:23 -0800 | [diff] [blame] | 30 | Manager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | std::string confDir; |
| 32 | TestNetworkManager() : |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 33 | bus(sdbusplus::bus::new_default()), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | manager(bus, "/xyz/openbmc_test/abc", "/tmp") |
| 35 | { |
| 36 | setConfDir(); |
| 37 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 38 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 39 | ~TestNetworkManager() |
| 40 | { |
| 41 | if (confDir != "") |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 42 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | fs::remove_all(confDir); |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 44 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 45 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 46 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 47 | void setConfDir() |
| 48 | { |
| 49 | char tmp[] = "/tmp/NetworkManager.XXXXXX"; |
| 50 | confDir = mkdtemp(tmp); |
| 51 | manager.setConfDir(confDir); |
| 52 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 53 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 54 | void createInterfaces() |
| 55 | { |
| 56 | manager.createInterfaces(); |
| 57 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // getifaddrs will not return any interface |
| 61 | TEST_F(TestNetworkManager, NoInterface) |
| 62 | { |
| 63 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Patrick Venture | 8fe594c | 2019-01-07 13:24:12 -0800 | [diff] [blame] | 64 | EXPECT_THROW(createInterfaces(), InternalFailure); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // getifaddrs returns single interface. |
| 68 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 69 | { |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 70 | mock_clear(); |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 71 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 72 | // Adds the following ip in the getifaddrs list. |
| 73 | mock_addIF("igb1", 2); |
| 74 | 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] | 75 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 76 | // Now create the interfaces which will call the mocked getifaddrs |
| 77 | // which returns the above interface detail. |
| 78 | createInterfaces(); |
| 79 | EXPECT_EQ(1, manager.getInterfaceCount()); |
| 80 | EXPECT_EQ(true, manager.hasInterface("igb1")); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // getifaddrs returns two interfaces. |
| 84 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 85 | { |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 86 | mock_clear(); |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 87 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 88 | mock_addIF("igb0", 1); |
| 89 | 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] | 90 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 91 | mock_addIF("igb1", 2); |
| 92 | 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] | 93 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 94 | createInterfaces(); |
| 95 | EXPECT_EQ(2, manager.getInterfaceCount()); |
| 96 | EXPECT_EQ(true, manager.hasInterface("igb0")); |
| 97 | EXPECT_EQ(true, manager.hasInterface("igb1")); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 98 | } |
| 99 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 100 | } // namespace network |
| 101 | } // namespace phosphor |