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" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 3 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 4 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include <arpa/inet.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 6 | #include <net/if.h> |
| 7 | #include <netinet/in.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 10 | #include <exception> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 11 | #include <experimental/filesystem> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
| 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; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 30 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | std::string confDir; |
| 32 | TestNetworkManager() : |
| 33 | bus(sdbusplus::bus::new_default()), |
| 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 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 59 | int getSize() |
| 60 | { |
| 61 | return manager.interfaces.size(); |
| 62 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 63 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 64 | bool isInterfaceAdded(std::string intf) |
| 65 | { |
| 66 | return manager.interfaces.find(intf) != manager.interfaces.end() |
| 67 | ? true |
| 68 | : false; |
| 69 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | // getifaddrs will not return any interface |
| 73 | TEST_F(TestNetworkManager, NoInterface) |
| 74 | { |
| 75 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 76 | bool caughtException = false; |
| 77 | try |
| 78 | { |
| 79 | createInterfaces(); |
| 80 | } |
| 81 | catch (InternalFailure& e) |
| 82 | { |
| 83 | caughtException = true; |
| 84 | } |
| 85 | |
| 86 | EXPECT_EQ(true, caughtException); |
| 87 | } |
| 88 | |
| 89 | // getifaddrs returns single interface. |
| 90 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 91 | { |
| 92 | bool caughtException = false; |
| 93 | try |
| 94 | { |
| 95 | // Adds the following ip in the getifaddrs list. |
| 96 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 97 | IFF_UP | IFF_RUNNING); |
| 98 | |
| 99 | // Now create the interfaces which will call the mocked getifaddrs |
| 100 | // which returns the above interface detail. |
| 101 | createInterfaces(); |
| 102 | EXPECT_EQ(1, getSize()); |
| 103 | EXPECT_EQ(true, isInterfaceAdded("igb1")); |
| 104 | } |
| 105 | catch (std::exception& e) |
| 106 | { |
| 107 | caughtException = true; |
| 108 | } |
| 109 | EXPECT_EQ(false, caughtException); |
| 110 | } |
| 111 | |
| 112 | // getifaddrs returns two interfaces. |
| 113 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 114 | { |
| 115 | try |
| 116 | { |
| 117 | mock_addIP("igb0", "192.0.2.2", "255.255.255.128", |
| 118 | IFF_UP | IFF_RUNNING); |
| 119 | |
| 120 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 121 | IFF_UP | IFF_RUNNING); |
| 122 | |
| 123 | createInterfaces(); |
| 124 | EXPECT_EQ(2, getSize()); |
| 125 | EXPECT_EQ(true, isInterfaceAdded("igb0")); |
| 126 | } |
| 127 | catch (std::exception& e) |
| 128 | { |
| 129 | } |
| 130 | } |
| 131 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 132 | } // namespace network |
| 133 | } // namespace phosphor |