11334b7b3SPatrick Williams #include "snmp_conf_manager.hpp"
21334b7b3SPatrick Williams #include "snmp_serialize.hpp"
39d18e562SRatan Gupta 
49d18e562SRatan Gupta #include <xyz/openbmc_project/Common/error.hpp>
59d18e562SRatan Gupta 
61334b7b3SPatrick Williams #include <gtest/gtest.h>
79d18e562SRatan Gupta 
89d18e562SRatan Gupta namespace phosphor
99d18e562SRatan Gupta {
109d18e562SRatan Gupta namespace network
119d18e562SRatan Gupta {
129d18e562SRatan Gupta namespace snmp
139d18e562SRatan Gupta {
149d18e562SRatan Gupta 
159d18e562SRatan Gupta using InvalidArgument =
169d18e562SRatan Gupta     sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
179d18e562SRatan Gupta 
189d18e562SRatan Gupta auto mgrObjPath = "/xyz/openbmc_test/snmp/manager";
199d18e562SRatan Gupta 
209d18e562SRatan Gupta class TestSNMPClient : public testing::Test
219d18e562SRatan Gupta {
229d18e562SRatan Gupta   public:
23*87d3edd6SPatrick Williams     sdbusplus::bus_t bus;
249d18e562SRatan Gupta     ConfManager manager;
259d18e562SRatan Gupta     TestSNMPClient() :
269d18e562SRatan Gupta         bus(sdbusplus::bus::new_default()), manager(bus, mgrObjPath)
279d18e562SRatan Gupta     {
289d18e562SRatan Gupta         char tmp[] = "/tmp/snmpClient.XXXXXX";
2934d129a9SRatan Gupta         auto confDir = mkdtemp(tmp);
309d18e562SRatan Gupta         manager.dbusPersistentLocation = confDir;
319d18e562SRatan Gupta     }
329d18e562SRatan Gupta 
339d18e562SRatan Gupta     ~TestSNMPClient()
349d18e562SRatan Gupta     {
359d18e562SRatan Gupta         fs::remove_all(manager.dbusPersistentLocation);
369d18e562SRatan Gupta     }
379d18e562SRatan Gupta };
389d18e562SRatan Gupta 
399d18e562SRatan Gupta TEST_F(TestSNMPClient, UpdateProperty)
409d18e562SRatan Gupta {
419d18e562SRatan Gupta     std::string objPath = mgrObjPath;
429d18e562SRatan Gupta     objPath += "/" + std::to_string(1);
439d18e562SRatan Gupta     Client client(bus, objPath.c_str(), manager, "1.1.1.1", 202);
449d18e562SRatan Gupta     EXPECT_EQ(client.address(), "1.1.1.1");
459d18e562SRatan Gupta     EXPECT_EQ(client.port(), 202);
469d18e562SRatan Gupta     client.address("2.2.2.2");
479d18e562SRatan Gupta     EXPECT_EQ(client.address(), "2.2.2.2");
489d18e562SRatan Gupta     client.port(404);
499d18e562SRatan Gupta     EXPECT_EQ(client.port(), 404);
509d18e562SRatan Gupta }
519d18e562SRatan Gupta 
529d18e562SRatan Gupta TEST_F(TestSNMPClient, AddEmptyAddress)
539d18e562SRatan Gupta {
549d18e562SRatan Gupta     std::string objPath = mgrObjPath;
559d18e562SRatan Gupta     objPath += "/" + std::to_string(1);
569d18e562SRatan Gupta     Client client(bus, objPath.c_str(), manager, "1.1.1.1", 202);
579d18e562SRatan Gupta     EXPECT_EQ(client.address(), "1.1.1.1");
589d18e562SRatan Gupta     EXPECT_EQ(client.port(), 202);
599d18e562SRatan Gupta 
609d18e562SRatan Gupta     EXPECT_THROW(client.address(""), InvalidArgument);
619d18e562SRatan Gupta }
629d18e562SRatan Gupta 
639d18e562SRatan Gupta TEST_F(TestSNMPClient, CheckPersistency)
649d18e562SRatan Gupta {
659d18e562SRatan Gupta     std::string objPath = mgrObjPath;
669d18e562SRatan Gupta     objPath += "/" + std::to_string(1);
672cbf7231SPatrick Williams     std::string objPath2 = mgrObjPath;
682cbf7231SPatrick Williams     objPath2 += "/" + std::to_string(2);
699d18e562SRatan Gupta 
709d18e562SRatan Gupta     Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23);
719d18e562SRatan Gupta     client.address("2.2.2.2");
729d18e562SRatan Gupta 
732cbf7231SPatrick Williams     Client restoreClient(bus, objPath2.c_str(), manager);
749d18e562SRatan Gupta     auto persistentPath = manager.dbusPersistentLocation;
759d18e562SRatan Gupta     persistentPath += "/1";
769d18e562SRatan Gupta     deserialize(persistentPath, restoreClient);
779d18e562SRatan Gupta 
789d18e562SRatan Gupta     EXPECT_EQ("2.2.2.2", restoreClient.address());
799d18e562SRatan Gupta     EXPECT_EQ(23, restoreClient.port());
809d18e562SRatan Gupta }
819d18e562SRatan Gupta 
829d18e562SRatan Gupta } // namespace snmp
839d18e562SRatan Gupta } // namespace network
849d18e562SRatan Gupta } // namespace phosphor
85