xref: /openbmc/phosphor-snmp/test/test_snmp_conf_manager.cpp (revision c0d7cd47d4b198f752be3f497bc3c9ae98bb7141)
11dc9178dSRatan Gupta #include "snmp_conf_manager.hpp"
21dc9178dSRatan Gupta #include "xyz/openbmc_project/Common/error.hpp"
31dc9178dSRatan Gupta 
41dc9178dSRatan Gupta #include <sdbusplus/bus.hpp>
51dc9178dSRatan Gupta 
61334b7b3SPatrick Williams #include <gtest/gtest.h>
71334b7b3SPatrick Williams 
81dc9178dSRatan Gupta namespace phosphor
91dc9178dSRatan Gupta {
101dc9178dSRatan Gupta namespace network
111dc9178dSRatan Gupta {
121dc9178dSRatan Gupta namespace snmp
131dc9178dSRatan Gupta {
141dc9178dSRatan Gupta 
15a7ff3850SRatan Gupta auto managerObjPath = "/xyz/openbmc_test/snmp/manager";
169c4fed6dSRatan Gupta using InvalidArgument =
179c4fed6dSRatan Gupta     sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
18a7ff3850SRatan Gupta 
191dc9178dSRatan Gupta class TestSNMPConfManager : public testing::Test
201dc9178dSRatan Gupta {
211dc9178dSRatan Gupta   public:
2287d3edd6SPatrick Williams     sdbusplus::bus_t bus;
231dc9178dSRatan Gupta     ConfManager manager;
2434d129a9SRatan Gupta     // confDir could have been created locally in the
2534d129a9SRatan Gupta     // TestSNMPConfManager but somehow that is leading
2634d129a9SRatan Gupta     // to segmentation fault while running the unit test.
2734d129a9SRatan Gupta     // TODO: https://github.com/openbmc/phosphor-snmp/issues/5
281dc9178dSRatan Gupta     std::string confDir;
TestSNMPConfManager()29a7ff3850SRatan Gupta     TestSNMPConfManager() :
30a7ff3850SRatan Gupta         bus(sdbusplus::bus::new_default()), manager(bus, managerObjPath)
31212f53e8SRatan Gupta     {
32212f53e8SRatan Gupta         char tmp[] = "/tmp/snmpManager.XXXXXX";
3334d129a9SRatan Gupta         confDir = mkdtemp(tmp);
34212f53e8SRatan Gupta         manager.dbusPersistentLocation = confDir;
35212f53e8SRatan Gupta     }
361dc9178dSRatan Gupta 
~TestSNMPConfManager()371dc9178dSRatan Gupta     ~TestSNMPConfManager()
381dc9178dSRatan Gupta     {
39212f53e8SRatan Gupta         fs::remove_all(manager.dbusPersistentLocation);
401dc9178dSRatan Gupta     }
411dc9178dSRatan Gupta 
createSNMPClient(std::string ipaddress,uint16_t port)42a7ff3850SRatan Gupta     std::string createSNMPClient(std::string ipaddress, uint16_t port)
431dc9178dSRatan Gupta     {
44a7ff3850SRatan Gupta         return manager.client(ipaddress, port);
451dc9178dSRatan Gupta     }
461dc9178dSRatan Gupta 
getSNMPClients()471dc9178dSRatan Gupta     ClientList& getSNMPClients()
481dc9178dSRatan Gupta     {
491dc9178dSRatan Gupta         return manager.clients;
501dc9178dSRatan Gupta     }
511dc9178dSRatan Gupta 
isClientExist(const std::string & ipaddress)52a7ff3850SRatan Gupta     bool isClientExist(const std::string& ipaddress)
53a7ff3850SRatan Gupta     {
54a7ff3850SRatan Gupta         for (const auto& val : manager.clients)
55a7ff3850SRatan Gupta         {
56a7ff3850SRatan Gupta             if (val.second.get()->address() == ipaddress)
57a7ff3850SRatan Gupta             {
58a7ff3850SRatan Gupta                 return true;
59a7ff3850SRatan Gupta             }
60a7ff3850SRatan Gupta         }
61a7ff3850SRatan Gupta         return false;
62a7ff3850SRatan Gupta     }
63a7ff3850SRatan Gupta 
deleteSNMPClient(const std::string & ipaddress)64*c0d7cd47SGeorge Liu     void deleteSNMPClient(const std::string& ipaddress)
651dc9178dSRatan Gupta     {
66*c0d7cd47SGeorge Liu         std::vector<size_t> ids{};
67a7ff3850SRatan Gupta         for (const auto& val : manager.clients)
68a7ff3850SRatan Gupta         {
69a7ff3850SRatan Gupta             if (val.second.get()->address() == ipaddress)
70a7ff3850SRatan Gupta             {
71*c0d7cd47SGeorge Liu                 ids.emplace_back(val.second.get()->id);
72*c0d7cd47SGeorge Liu             }
73*c0d7cd47SGeorge Liu         }
74*c0d7cd47SGeorge Liu 
75*c0d7cd47SGeorge Liu         for (const auto& id : ids)
76*c0d7cd47SGeorge Liu         {
77*c0d7cd47SGeorge Liu             if (manager.clients.contains(id))
78*c0d7cd47SGeorge Liu             {
79*c0d7cd47SGeorge Liu                 manager.clients.at(id)->delete_();
80a7ff3850SRatan Gupta             }
81a7ff3850SRatan Gupta         }
821dc9178dSRatan Gupta     }
831dc9178dSRatan Gupta };
841dc9178dSRatan Gupta 
851dc9178dSRatan Gupta // Add single SNMP client
TEST_F(TestSNMPConfManager,AddSNMPClient)861dc9178dSRatan Gupta TEST_F(TestSNMPConfManager, AddSNMPClient)
871dc9178dSRatan Gupta {
88a7ff3850SRatan Gupta     // check the created object path
89a7ff3850SRatan Gupta     auto path = createSNMPClient("192.168.1.1", 24);
90a7ff3850SRatan Gupta     std::string expectedPath = managerObjPath;
91a7ff3850SRatan Gupta     expectedPath += std::string("/1");
921dc9178dSRatan Gupta 
93a7ff3850SRatan Gupta     EXPECT_EQ(path, expectedPath);
941dc9178dSRatan Gupta 
95a7ff3850SRatan Gupta     // check whether the client created
961dc9178dSRatan Gupta     auto& clients = getSNMPClients();
972fddc40cSEd Tanous     EXPECT_EQ(1U, clients.size());
98a7ff3850SRatan Gupta     EXPECT_EQ(true, isClientExist("192.168.1.1"));
991dc9178dSRatan Gupta }
1001dc9178dSRatan Gupta 
1011dc9178dSRatan Gupta // Add multiple SNMP client
TEST_F(TestSNMPConfManager,AddMultipleSNMPClient)1021dc9178dSRatan Gupta TEST_F(TestSNMPConfManager, AddMultipleSNMPClient)
1031dc9178dSRatan Gupta {
104a7ff3850SRatan Gupta     // add multiple clients and check whether the object path is generated
105a7ff3850SRatan Gupta     // correctly.
1061dc9178dSRatan Gupta     createSNMPClient("192.168.1.1", 24);
107a7ff3850SRatan Gupta     auto path = createSNMPClient("192.168.1.2", 24);
108a7ff3850SRatan Gupta     std::string expectedPath = managerObjPath;
109a7ff3850SRatan Gupta     expectedPath += std::string("/2");
1101dc9178dSRatan Gupta 
111a7ff3850SRatan Gupta     EXPECT_EQ(path, expectedPath);
112a7ff3850SRatan Gupta 
113a7ff3850SRatan Gupta     // check both the clients get created
1141dc9178dSRatan Gupta     auto& clients = getSNMPClients();
1152fddc40cSEd Tanous     EXPECT_EQ(2U, clients.size());
116a7ff3850SRatan Gupta 
117a7ff3850SRatan Gupta     EXPECT_EQ(true, isClientExist("192.168.1.1"));
118a7ff3850SRatan Gupta     EXPECT_EQ(true, isClientExist("192.168.1.2"));
1191dc9178dSRatan Gupta }
1201dc9178dSRatan Gupta 
1219c4fed6dSRatan Gupta // Add duplicate SNMP client
TEST_F(TestSNMPConfManager,AddDuplicateSNMPClient)1229c4fed6dSRatan Gupta TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient)
1239c4fed6dSRatan Gupta {
1249c4fed6dSRatan Gupta     createSNMPClient("192.168.1.1", 24);
1259c4fed6dSRatan Gupta     EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument);
1269c4fed6dSRatan Gupta }
1279c4fed6dSRatan Gupta 
1281dc9178dSRatan Gupta // Delete SNMP client
TEST_F(TestSNMPConfManager,DeleteSNMPClient)1291dc9178dSRatan Gupta TEST_F(TestSNMPConfManager, DeleteSNMPClient)
1301dc9178dSRatan Gupta {
1311dc9178dSRatan Gupta     createSNMPClient("192.168.1.1", 24);
1321dc9178dSRatan Gupta     createSNMPClient("192.168.1.2", 24);
133*c0d7cd47SGeorge Liu     createSNMPClient("192.168.1.1", 25);
1341dc9178dSRatan Gupta 
1351dc9178dSRatan Gupta     auto& clients = getSNMPClients();
136*c0d7cd47SGeorge Liu     EXPECT_EQ(3U, clients.size());
137a7ff3850SRatan Gupta 
1381dc9178dSRatan Gupta     deleteSNMPClient("192.168.1.1");
139*c0d7cd47SGeorge Liu     EXPECT_EQ(1U, clients.size());
140a7ff3850SRatan Gupta 
141a7ff3850SRatan Gupta     auto path = createSNMPClient("192.168.1.3", 24);
142a7ff3850SRatan Gupta     std::string expectedPath = managerObjPath;
143*c0d7cd47SGeorge Liu     expectedPath += std::string("/4");
144a7ff3850SRatan Gupta     EXPECT_EQ(path, expectedPath);
145a7ff3850SRatan Gupta 
1462fddc40cSEd Tanous     EXPECT_EQ(2U, clients.size());
147a7ff3850SRatan Gupta     EXPECT_EQ(true, isClientExist("192.168.1.2"));
148a7ff3850SRatan Gupta     EXPECT_EQ(false, isClientExist("192.168.1.1"));
149a7ff3850SRatan Gupta     EXPECT_EQ(true, isClientExist("192.168.1.3"));
1501dc9178dSRatan Gupta }
1511dc9178dSRatan Gupta 
1521dc9178dSRatan Gupta } // namespace snmp
153aea53d07SRatan Gupta } // namespace network
1541dc9178dSRatan Gupta } // namespace phosphor
155