1*1334b7b3SPatrick Williams #include "snmp_conf_manager.hpp"
2*1334b7b3SPatrick Williams #include "snmp_serialize.hpp"
39d18e562SRatan Gupta 
49d18e562SRatan Gupta #include <xyz/openbmc_project/Common/error.hpp>
59d18e562SRatan Gupta 
6*1334b7b3SPatrick 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:
239d18e562SRatan Gupta     sdbusplus::bus::bus bus;
249d18e562SRatan Gupta     ConfManager manager;
259d18e562SRatan Gupta     std::string confDir;
269d18e562SRatan Gupta     TestSNMPClient() :
279d18e562SRatan Gupta         bus(sdbusplus::bus::new_default()), manager(bus, mgrObjPath)
289d18e562SRatan Gupta     {
299d18e562SRatan Gupta         char tmp[] = "/tmp/snmpClient.XXXXXX";
309d18e562SRatan Gupta         std::string confDir = mkdtemp(tmp);
319d18e562SRatan Gupta         manager.dbusPersistentLocation = confDir;
329d18e562SRatan Gupta     }
339d18e562SRatan Gupta 
349d18e562SRatan Gupta     ~TestSNMPClient()
359d18e562SRatan Gupta     {
369d18e562SRatan Gupta         fs::remove_all(manager.dbusPersistentLocation);
379d18e562SRatan Gupta     }
389d18e562SRatan Gupta };
399d18e562SRatan Gupta 
409d18e562SRatan Gupta TEST_F(TestSNMPClient, UpdateProperty)
419d18e562SRatan Gupta {
429d18e562SRatan Gupta     std::string objPath = mgrObjPath;
439d18e562SRatan Gupta     objPath += "/" + std::to_string(1);
449d18e562SRatan Gupta     Client client(bus, objPath.c_str(), manager, "1.1.1.1", 202);
459d18e562SRatan Gupta     EXPECT_EQ(client.address(), "1.1.1.1");
469d18e562SRatan Gupta     EXPECT_EQ(client.port(), 202);
479d18e562SRatan Gupta     client.address("2.2.2.2");
489d18e562SRatan Gupta     EXPECT_EQ(client.address(), "2.2.2.2");
499d18e562SRatan Gupta     client.port(404);
509d18e562SRatan Gupta     EXPECT_EQ(client.port(), 404);
519d18e562SRatan Gupta }
529d18e562SRatan Gupta 
539d18e562SRatan Gupta TEST_F(TestSNMPClient, AddEmptyAddress)
549d18e562SRatan Gupta {
559d18e562SRatan Gupta     std::string objPath = mgrObjPath;
569d18e562SRatan Gupta     objPath += "/" + std::to_string(1);
579d18e562SRatan Gupta     Client client(bus, objPath.c_str(), manager, "1.1.1.1", 202);
589d18e562SRatan Gupta     EXPECT_EQ(client.address(), "1.1.1.1");
599d18e562SRatan Gupta     EXPECT_EQ(client.port(), 202);
609d18e562SRatan Gupta 
619d18e562SRatan Gupta     EXPECT_THROW(client.address(""), InvalidArgument);
629d18e562SRatan Gupta }
639d18e562SRatan Gupta 
649d18e562SRatan Gupta TEST_F(TestSNMPClient, CheckPersistency)
659d18e562SRatan Gupta {
669d18e562SRatan Gupta     std::string objPath = mgrObjPath;
679d18e562SRatan Gupta     objPath += "/" + std::to_string(1);
682cbf7231SPatrick Williams     std::string objPath2 = mgrObjPath;
692cbf7231SPatrick Williams     objPath2 += "/" + std::to_string(2);
709d18e562SRatan Gupta 
719d18e562SRatan Gupta     Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23);
729d18e562SRatan Gupta     client.address("2.2.2.2");
739d18e562SRatan Gupta 
742cbf7231SPatrick Williams     Client restoreClient(bus, objPath2.c_str(), manager);
759d18e562SRatan Gupta     auto persistentPath = manager.dbusPersistentLocation;
769d18e562SRatan Gupta     persistentPath += "/1";
779d18e562SRatan Gupta     deserialize(persistentPath, restoreClient);
789d18e562SRatan Gupta 
799d18e562SRatan Gupta     EXPECT_EQ("2.2.2.2", restoreClient.address());
809d18e562SRatan Gupta     EXPECT_EQ(23, restoreClient.port());
819d18e562SRatan Gupta }
829d18e562SRatan Gupta 
839d18e562SRatan Gupta } // namespace snmp
849d18e562SRatan Gupta } // namespace network
859d18e562SRatan Gupta } // namespace phosphor
86