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> |
Patrick Venture | 3f19a51 | 2019-01-07 13:28:57 -0800 | [diff] [blame] | 11 | #include <sdbusplus/test/sdbus_mock.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: |
Patrick Venture | 3f19a51 | 2019-01-07 13:28:57 -0800 | [diff] [blame] | 29 | sdbusplus::SdBusMock sdbus_mock; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 30 | sdbusplus::bus::bus bus; |
Patrick Venture | d94d23e | 2019-01-07 13:05:23 -0800 | [diff] [blame] | 31 | Manager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 32 | std::string confDir; |
| 33 | TestNetworkManager() : |
Patrick Venture | 3f19a51 | 2019-01-07 13:28:57 -0800 | [diff] [blame] | 34 | bus(sdbusplus::get_mocked_new(&sdbus_mock)), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | manager(bus, "/xyz/openbmc_test/abc", "/tmp") |
| 36 | { |
| 37 | setConfDir(); |
| 38 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 39 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 40 | ~TestNetworkManager() |
| 41 | { |
| 42 | if (confDir != "") |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 43 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 44 | fs::remove_all(confDir); |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 45 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 47 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 48 | void setConfDir() |
| 49 | { |
| 50 | char tmp[] = "/tmp/NetworkManager.XXXXXX"; |
| 51 | confDir = mkdtemp(tmp); |
| 52 | manager.setConfDir(confDir); |
| 53 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 54 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 55 | void createInterfaces() |
| 56 | { |
| 57 | manager.createInterfaces(); |
| 58 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | // getifaddrs will not return any interface |
| 62 | TEST_F(TestNetworkManager, NoInterface) |
| 63 | { |
| 64 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Patrick Venture | 8fe594c | 2019-01-07 13:24:12 -0800 | [diff] [blame] | 65 | EXPECT_THROW(createInterfaces(), InternalFailure); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // getifaddrs returns single interface. |
| 69 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 70 | { |
| 71 | bool caughtException = false; |
| 72 | try |
| 73 | { |
| 74 | // Adds the following ip in the getifaddrs list. |
| 75 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 76 | IFF_UP | IFF_RUNNING); |
| 77 | |
| 78 | // Now create the interfaces which will call the mocked getifaddrs |
| 79 | // which returns the above interface detail. |
| 80 | createInterfaces(); |
Patrick Venture | d94d23e | 2019-01-07 13:05:23 -0800 | [diff] [blame] | 81 | EXPECT_EQ(1, manager.getInterfaceCount()); |
| 82 | EXPECT_EQ(true, manager.hasInterface("igb1")); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 83 | } |
| 84 | catch (std::exception& e) |
| 85 | { |
| 86 | caughtException = true; |
| 87 | } |
| 88 | EXPECT_EQ(false, caughtException); |
| 89 | } |
| 90 | |
| 91 | // getifaddrs returns two interfaces. |
| 92 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 93 | { |
| 94 | try |
| 95 | { |
| 96 | mock_addIP("igb0", "192.0.2.2", "255.255.255.128", |
| 97 | IFF_UP | IFF_RUNNING); |
| 98 | |
| 99 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 100 | IFF_UP | IFF_RUNNING); |
| 101 | |
| 102 | createInterfaces(); |
Patrick Venture | d94d23e | 2019-01-07 13:05:23 -0800 | [diff] [blame] | 103 | EXPECT_EQ(2, manager.getInterfaceCount()); |
| 104 | EXPECT_EQ(true, manager.hasInterface("igb0")); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 105 | } |
| 106 | catch (std::exception& e) |
| 107 | { |
| 108 | } |
| 109 | } |
| 110 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 111 | } // namespace network |
| 112 | } // namespace phosphor |