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> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 15 | #include <exception> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 16 | #include <experimental/filesystem> |
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 | { |
| 27 | public: |
| 28 | |
| 29 | sdbusplus::bus::bus bus; |
| 30 | Manager manager; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 31 | std::string confDir; |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 32 | TestNetworkManager() |
| 33 | : bus(sdbusplus::bus::new_default()), |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 34 | manager(bus, "/xyz/openbmc_test/abc", "/tmp") |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 35 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 36 | setConfDir(); |
| 37 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 38 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 39 | ~TestNetworkManager() |
| 40 | { |
| 41 | if(confDir != "") |
| 42 | { |
| 43 | fs::remove_all(confDir); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void setConfDir() |
| 48 | { |
| 49 | char tmp[] = "/tmp/NetworkManager.XXXXXX"; |
| 50 | confDir = mkdtemp(tmp); |
| 51 | manager.setConfDir(confDir); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void createInterfaces() |
| 55 | { |
| 56 | manager.createInterfaces(); |
| 57 | } |
| 58 | |
| 59 | int getSize() |
| 60 | { |
| 61 | return manager.interfaces.size(); |
| 62 | } |
| 63 | |
| 64 | bool isInterfaceAdded(std::string intf) |
| 65 | { |
| 66 | return manager.interfaces.find(intf) != manager.interfaces.end() ? |
| 67 | true : |
| 68 | false; |
| 69 | } |
| 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 | |
| 132 | }// namespce network |
| 133 | }// namespace phosphor |