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::bus 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(std::string ipaddress) 65 { 66 for (const auto& val : manager.clients) 67 { 68 if (val.second.get()->address() == ipaddress) 69 { 70 val.second.get()->delete_(); 71 } 72 } 73 } 74 }; 75 76 // Add single SNMP client 77 TEST_F(TestSNMPConfManager, AddSNMPClient) 78 { 79 // check the created object path 80 auto path = createSNMPClient("192.168.1.1", 24); 81 std::string expectedPath = managerObjPath; 82 expectedPath += std::string("/1"); 83 84 EXPECT_EQ(path, expectedPath); 85 86 // check whether the client created 87 auto& clients = getSNMPClients(); 88 EXPECT_EQ(1, clients.size()); 89 EXPECT_EQ(true, isClientExist("192.168.1.1")); 90 } 91 92 // Add multiple SNMP client 93 TEST_F(TestSNMPConfManager, AddMultipleSNMPClient) 94 { 95 // add multiple clients and check whether the object path is generated 96 // correctly. 97 createSNMPClient("192.168.1.1", 24); 98 auto path = createSNMPClient("192.168.1.2", 24); 99 std::string expectedPath = managerObjPath; 100 expectedPath += std::string("/2"); 101 102 EXPECT_EQ(path, expectedPath); 103 104 // check both the clients get created 105 auto& clients = getSNMPClients(); 106 EXPECT_EQ(2, clients.size()); 107 108 EXPECT_EQ(true, isClientExist("192.168.1.1")); 109 EXPECT_EQ(true, isClientExist("192.168.1.2")); 110 } 111 112 // Add duplicate SNMP client 113 TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient) 114 { 115 createSNMPClient("192.168.1.1", 24); 116 EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument); 117 } 118 119 // Delete SNMP client 120 TEST_F(TestSNMPConfManager, DeleteSNMPClient) 121 { 122 createSNMPClient("192.168.1.1", 24); 123 createSNMPClient("192.168.1.2", 24); 124 125 auto& clients = getSNMPClients(); 126 EXPECT_EQ(2, clients.size()); 127 128 deleteSNMPClient("192.168.1.1"); 129 130 auto path = createSNMPClient("192.168.1.3", 24); 131 std::string expectedPath = managerObjPath; 132 expectedPath += std::string("/3"); 133 EXPECT_EQ(path, expectedPath); 134 135 EXPECT_EQ(2, clients.size()); 136 EXPECT_EQ(true, isClientExist("192.168.1.2")); 137 EXPECT_EQ(false, isClientExist("192.168.1.1")); 138 EXPECT_EQ(true, isClientExist("192.168.1.3")); 139 } 140 141 } // namespace snmp 142 } // namespace network 143 } // namespace phosphor 144