1 #include "snmp_conf_manager.hpp" 2 #include "xyz/openbmc_project/Common/error.hpp" 3 4 #include <sdbusplus/bus.hpp> 5 6 #include <gtest/gtest.h> 7 8 namespace phosphor 9 { 10 namespace network 11 { 12 namespace snmp 13 { 14 15 auto managerObjPath = "/xyz/openbmc_test/snmp/manager"; 16 using InvalidArgument = 17 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; 18 19 class TestSNMPConfManager : public testing::Test 20 { 21 public: 22 sdbusplus::bus_t bus; 23 ConfManager manager; 24 // confDir could have been created locally in the 25 // TestSNMPConfManager but somehow that is leading 26 // to segmentation fault while running the unit test. 27 // TODO: https://github.com/openbmc/phosphor-snmp/issues/5 28 std::string confDir; 29 TestSNMPConfManager() : 30 bus(sdbusplus::bus::new_default()), manager(bus, managerObjPath) 31 { 32 char tmp[] = "/tmp/snmpManager.XXXXXX"; 33 confDir = mkdtemp(tmp); 34 manager.dbusPersistentLocation = confDir; 35 } 36 37 ~TestSNMPConfManager() 38 { 39 fs::remove_all(manager.dbusPersistentLocation); 40 } 41 42 std::string createSNMPClient(std::string ipaddress, uint16_t port) 43 { 44 return manager.client(ipaddress, port); 45 } 46 47 ClientList& getSNMPClients() 48 { 49 return manager.clients; 50 } 51 52 bool isClientExist(const std::string& ipaddress) 53 { 54 for (const auto& val : manager.clients) 55 { 56 if (val.second.get()->address() == ipaddress) 57 { 58 return true; 59 } 60 } 61 return false; 62 } 63 64 void deleteSNMPClient(const std::string& ipaddress) 65 { 66 std::vector<size_t> ids{}; 67 for (const auto& val : manager.clients) 68 { 69 if (val.second.get()->address() == ipaddress) 70 { 71 ids.emplace_back(val.second.get()->id); 72 } 73 } 74 75 for (const auto& id : ids) 76 { 77 if (manager.clients.contains(id)) 78 { 79 manager.clients.at(id)->delete_(); 80 } 81 } 82 } 83 }; 84 85 // Add single SNMP client 86 TEST_F(TestSNMPConfManager, AddSNMPClient) 87 { 88 // check the created object path 89 auto path = createSNMPClient("192.168.1.1", 24); 90 std::string expectedPath = managerObjPath; 91 expectedPath += std::string("/1"); 92 93 EXPECT_EQ(path, expectedPath); 94 95 // check whether the client created 96 auto& clients = getSNMPClients(); 97 EXPECT_EQ(1U, clients.size()); 98 EXPECT_EQ(true, isClientExist("192.168.1.1")); 99 } 100 101 // Add multiple SNMP client 102 TEST_F(TestSNMPConfManager, AddMultipleSNMPClient) 103 { 104 // add multiple clients and check whether the object path is generated 105 // correctly. 106 createSNMPClient("192.168.1.1", 24); 107 auto path = createSNMPClient("192.168.1.2", 24); 108 std::string expectedPath = managerObjPath; 109 expectedPath += std::string("/2"); 110 111 EXPECT_EQ(path, expectedPath); 112 113 // check both the clients get created 114 auto& clients = getSNMPClients(); 115 EXPECT_EQ(2U, clients.size()); 116 117 EXPECT_EQ(true, isClientExist("192.168.1.1")); 118 EXPECT_EQ(true, isClientExist("192.168.1.2")); 119 } 120 121 // Add duplicate SNMP client 122 TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient) 123 { 124 createSNMPClient("192.168.1.1", 24); 125 EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument); 126 } 127 128 // Delete SNMP client 129 TEST_F(TestSNMPConfManager, DeleteSNMPClient) 130 { 131 createSNMPClient("192.168.1.1", 24); 132 createSNMPClient("192.168.1.2", 24); 133 createSNMPClient("192.168.1.1", 25); 134 135 auto& clients = getSNMPClients(); 136 EXPECT_EQ(3U, clients.size()); 137 138 deleteSNMPClient("192.168.1.1"); 139 EXPECT_EQ(1U, clients.size()); 140 141 auto path = createSNMPClient("192.168.1.3", 24); 142 std::string expectedPath = managerObjPath; 143 expectedPath += std::string("/4"); 144 EXPECT_EQ(path, expectedPath); 145 146 EXPECT_EQ(2U, clients.size()); 147 EXPECT_EQ(true, isClientExist("192.168.1.2")); 148 EXPECT_EQ(false, isClientExist("192.168.1.1")); 149 EXPECT_EQ(true, isClientExist("192.168.1.3")); 150 } 151 152 } // namespace snmp 153 } // namespace network 154 } // namespace phosphor 155