Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 1 | #include "mock_syscall.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 2 | #include "network_manager.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 <phosphor-logging/elog-errors.hpp> |
| 13 | #include <sdbusplus/bus.hpp> |
| 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 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 22 | namespace fs = std::experimental::filesystem; |
| 23 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 24 | class TestNetworkManager : public testing::Test |
| 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
| 27 | sdbusplus::bus::bus bus; |
| 28 | Manager manager; |
| 29 | std::string confDir; |
| 30 | TestNetworkManager() : |
| 31 | bus(sdbusplus::bus::new_default()), |
| 32 | manager(bus, "/xyz/openbmc_test/abc", "/tmp") |
| 33 | { |
| 34 | setConfDir(); |
| 35 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 36 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | ~TestNetworkManager() |
| 38 | { |
| 39 | if (confDir != "") |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 40 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 41 | fs::remove_all(confDir); |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 42 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 44 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 45 | void setConfDir() |
| 46 | { |
| 47 | char tmp[] = "/tmp/NetworkManager.XXXXXX"; |
| 48 | confDir = mkdtemp(tmp); |
| 49 | manager.setConfDir(confDir); |
| 50 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 51 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 52 | void createInterfaces() |
| 53 | { |
| 54 | manager.createInterfaces(); |
| 55 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 56 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 57 | int getSize() |
| 58 | { |
| 59 | return manager.interfaces.size(); |
| 60 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 61 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 62 | bool isInterfaceAdded(std::string intf) |
| 63 | { |
| 64 | return manager.interfaces.find(intf) != manager.interfaces.end() |
| 65 | ? true |
| 66 | : false; |
| 67 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // getifaddrs will not return any interface |
| 71 | TEST_F(TestNetworkManager, NoInterface) |
| 72 | { |
| 73 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 74 | bool caughtException = false; |
| 75 | try |
| 76 | { |
| 77 | createInterfaces(); |
| 78 | } |
| 79 | catch (InternalFailure& e) |
| 80 | { |
| 81 | caughtException = true; |
| 82 | } |
| 83 | |
| 84 | EXPECT_EQ(true, caughtException); |
| 85 | } |
| 86 | |
| 87 | // getifaddrs returns single interface. |
| 88 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 89 | { |
| 90 | bool caughtException = false; |
| 91 | try |
| 92 | { |
| 93 | // Adds the following ip in the getifaddrs list. |
| 94 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 95 | IFF_UP | IFF_RUNNING); |
| 96 | |
| 97 | // Now create the interfaces which will call the mocked getifaddrs |
| 98 | // which returns the above interface detail. |
| 99 | createInterfaces(); |
| 100 | EXPECT_EQ(1, getSize()); |
| 101 | EXPECT_EQ(true, isInterfaceAdded("igb1")); |
| 102 | } |
| 103 | catch (std::exception& e) |
| 104 | { |
| 105 | caughtException = true; |
| 106 | } |
| 107 | EXPECT_EQ(false, caughtException); |
| 108 | } |
| 109 | |
| 110 | // getifaddrs returns two interfaces. |
| 111 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 112 | { |
| 113 | try |
| 114 | { |
| 115 | mock_addIP("igb0", "192.0.2.2", "255.255.255.128", |
| 116 | IFF_UP | IFF_RUNNING); |
| 117 | |
| 118 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 119 | IFF_UP | IFF_RUNNING); |
| 120 | |
| 121 | createInterfaces(); |
| 122 | EXPECT_EQ(2, getSize()); |
| 123 | EXPECT_EQ(true, isInterfaceAdded("igb0")); |
| 124 | } |
| 125 | catch (std::exception& e) |
| 126 | { |
| 127 | } |
| 128 | } |
| 129 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 130 | } // namespace network |
| 131 | } // namespace phosphor |