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