Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 1 | #include "snmp_conf_manager.hpp" |
| 2 | |
| 3 | #include "xyz/openbmc_project/Common/error.hpp" |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | #include <sdbusplus/bus.hpp> |
| 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace snmp |
| 13 | { |
| 14 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 15 | auto managerObjPath = "/xyz/openbmc_test/snmp/manager"; |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 16 | using InvalidArgument = |
| 17 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 18 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 19 | class TestSNMPConfManager : public testing::Test |
| 20 | { |
| 21 | public: |
| 22 | sdbusplus::bus::bus bus; |
| 23 | ConfManager manager; |
| 24 | std::string confDir; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 25 | TestSNMPConfManager() : |
| 26 | bus(sdbusplus::bus::new_default()), manager(bus, managerObjPath) |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 27 | { |
| 28 | char tmp[] = "/tmp/snmpManager.XXXXXX"; |
| 29 | std::string confDir = mkdtemp(tmp); |
| 30 | manager.dbusPersistentLocation = confDir; |
| 31 | } |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 32 | |
| 33 | ~TestSNMPConfManager() |
| 34 | { |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 35 | fs::remove_all(manager.dbusPersistentLocation); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 36 | } |
| 37 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 38 | std::string createSNMPClient(std::string ipaddress, uint16_t port) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 39 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 40 | return manager.client(ipaddress, port); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ClientList &getSNMPClients() |
| 44 | { |
| 45 | return manager.clients; |
| 46 | } |
| 47 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 48 | bool isClientExist(const std::string &ipaddress) |
| 49 | { |
| 50 | for (const auto &val : manager.clients) |
| 51 | { |
| 52 | if (val.second.get()->address() == ipaddress) |
| 53 | { |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 60 | void deleteSNMPClient(std::string ipaddress) |
| 61 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 62 | for (const auto &val : manager.clients) |
| 63 | { |
| 64 | if (val.second.get()->address() == ipaddress) |
| 65 | { |
| 66 | val.second.get()->delete_(); |
| 67 | } |
| 68 | } |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 69 | } |
| 70 | }; |
| 71 | |
| 72 | // Add single SNMP client |
| 73 | TEST_F(TestSNMPConfManager, AddSNMPClient) |
| 74 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 75 | // check the created object path |
| 76 | auto path = createSNMPClient("192.168.1.1", 24); |
| 77 | std::string expectedPath = managerObjPath; |
| 78 | expectedPath += std::string("/1"); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 79 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 80 | EXPECT_EQ(path, expectedPath); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 81 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 82 | // check whether the client created |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 83 | auto &clients = getSNMPClients(); |
| 84 | EXPECT_EQ(1, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 85 | EXPECT_EQ(true, isClientExist("192.168.1.1")); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Add multiple SNMP client |
| 89 | TEST_F(TestSNMPConfManager, AddMultipleSNMPClient) |
| 90 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 91 | // add multiple clients and check whether the object path is generated |
| 92 | // correctly. |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 93 | createSNMPClient("192.168.1.1", 24); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 94 | auto path = createSNMPClient("192.168.1.2", 24); |
| 95 | std::string expectedPath = managerObjPath; |
| 96 | expectedPath += std::string("/2"); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 97 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 98 | EXPECT_EQ(path, expectedPath); |
| 99 | |
| 100 | // check both the clients get created |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 101 | auto &clients = getSNMPClients(); |
| 102 | EXPECT_EQ(2, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 103 | |
| 104 | EXPECT_EQ(true, isClientExist("192.168.1.1")); |
| 105 | EXPECT_EQ(true, isClientExist("192.168.1.2")); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 106 | } |
| 107 | |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 108 | // Add duplicate SNMP client |
| 109 | TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient) |
| 110 | { |
| 111 | createSNMPClient("192.168.1.1", 24); |
| 112 | EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument); |
| 113 | } |
| 114 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 115 | // Delete SNMP client |
| 116 | TEST_F(TestSNMPConfManager, DeleteSNMPClient) |
| 117 | { |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 118 | createSNMPClient("192.168.1.1", 24); |
| 119 | createSNMPClient("192.168.1.2", 24); |
| 120 | |
| 121 | auto &clients = getSNMPClients(); |
| 122 | EXPECT_EQ(2, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 123 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 124 | deleteSNMPClient("192.168.1.1"); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 125 | |
| 126 | auto path = createSNMPClient("192.168.1.3", 24); |
| 127 | std::string expectedPath = managerObjPath; |
| 128 | expectedPath += std::string("/3"); |
| 129 | EXPECT_EQ(path, expectedPath); |
| 130 | |
| 131 | EXPECT_EQ(2, clients.size()); |
| 132 | EXPECT_EQ(true, isClientExist("192.168.1.2")); |
| 133 | EXPECT_EQ(false, isClientExist("192.168.1.1")); |
| 134 | EXPECT_EQ(true, isClientExist("192.168.1.3")); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } // namespace snmp |
Ratan Gupta | aea53d0 | 2018-09-06 17:56:59 +0530 | [diff] [blame] | 138 | } // namespace network |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 139 | } // namespace phosphor |