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> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | #include <sdbusplus/bus.hpp> |
| 9 | |
| 10 | #include <net/if.h> |
| 11 | #include <netinet/in.h> |
| 12 | #include <arpa/inet.h> |
| 13 | #include <exception> |
| 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
| 19 | |
| 20 | class TestNetworkManager : public testing::Test |
| 21 | { |
| 22 | public: |
| 23 | |
| 24 | sdbusplus::bus::bus bus; |
| 25 | Manager manager; |
| 26 | |
| 27 | TestNetworkManager() |
| 28 | : bus(sdbusplus::bus::new_default()), |
| 29 | manager(bus, "xyz/openbmc_test/abc") |
| 30 | { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | void createInterfaces() |
| 35 | { |
| 36 | manager.createInterfaces(); |
| 37 | } |
| 38 | |
| 39 | int getSize() |
| 40 | { |
| 41 | return manager.interfaces.size(); |
| 42 | } |
| 43 | |
| 44 | bool isInterfaceAdded(std::string intf) |
| 45 | { |
| 46 | return manager.interfaces.find(intf) != manager.interfaces.end() ? |
| 47 | true : |
| 48 | false; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | // getifaddrs will not return any interface |
| 53 | TEST_F(TestNetworkManager, NoInterface) |
| 54 | { |
| 55 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 56 | bool caughtException = false; |
| 57 | try |
| 58 | { |
| 59 | createInterfaces(); |
| 60 | } |
| 61 | catch (InternalFailure& e) |
| 62 | { |
| 63 | caughtException = true; |
| 64 | } |
| 65 | |
| 66 | EXPECT_EQ(true, caughtException); |
| 67 | } |
| 68 | |
| 69 | // getifaddrs returns single interface. |
| 70 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 71 | { |
| 72 | bool caughtException = false; |
| 73 | try |
| 74 | { |
| 75 | // Adds the following ip in the getifaddrs list. |
| 76 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 77 | IFF_UP | IFF_RUNNING); |
| 78 | |
| 79 | // Now create the interfaces which will call the mocked getifaddrs |
| 80 | // which returns the above interface detail. |
| 81 | createInterfaces(); |
| 82 | EXPECT_EQ(1, getSize()); |
| 83 | EXPECT_EQ(true, isInterfaceAdded("igb1")); |
| 84 | } |
| 85 | catch (std::exception& e) |
| 86 | { |
| 87 | caughtException = true; |
| 88 | } |
| 89 | EXPECT_EQ(false, caughtException); |
| 90 | } |
| 91 | |
| 92 | // getifaddrs returns two interfaces. |
| 93 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 94 | { |
| 95 | try |
| 96 | { |
| 97 | mock_addIP("igb0", "192.0.2.2", "255.255.255.128", |
| 98 | IFF_UP | IFF_RUNNING); |
| 99 | |
| 100 | mock_addIP("igb1", "192.0.2.3", "255.255.255.128", |
| 101 | IFF_UP | IFF_RUNNING); |
| 102 | |
| 103 | createInterfaces(); |
| 104 | EXPECT_EQ(2, getSize()); |
| 105 | EXPECT_EQ(true, isInterfaceAdded("igb0")); |
| 106 | } |
| 107 | catch (std::exception& e) |
| 108 | { |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | }// namespce network |
| 113 | }// namespace phosphor |