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