blob: 911f1f12a1e273a7072baa02003ede974fb5896e [file] [log] [blame]
Ratan Gupta1dc91782018-04-19 16:47:12 +05301#include "snmp_conf_manager.hpp"
Ratan Gupta1dc91782018-04-19 16:47:12 +05302#include "xyz/openbmc_project/Common/error.hpp"
3
Ratan Gupta1dc91782018-04-19 16:47:12 +05304#include <sdbusplus/bus.hpp>
5
Patrick Williams1334b7b2021-02-22 17:15:12 -06006#include <gtest/gtest.h>
7
Ratan Gupta1dc91782018-04-19 16:47:12 +05308namespace phosphor
9{
10namespace network
11{
12namespace snmp
13{
14
Ratan Guptaa7ff3852018-11-16 14:05:57 +053015auto managerObjPath = "/xyz/openbmc_test/snmp/manager";
Ratan Gupta9c4fed62018-11-16 17:47:54 +053016using InvalidArgument =
17 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053018
Ratan Gupta1dc91782018-04-19 16:47:12 +053019class TestSNMPConfManager : public testing::Test
20{
21 public:
Patrick Williams87d3edd2022-07-22 19:26:53 -050022 sdbusplus::bus_t bus;
Ratan Gupta1dc91782018-04-19 16:47:12 +053023 ConfManager manager;
Ratan Gupta34d129a2021-12-04 21:04:51 +053024 // 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
Ratan Gupta1dc91782018-04-19 16:47:12 +053028 std::string confDir;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053029 TestSNMPConfManager() :
30 bus(sdbusplus::bus::new_default()), manager(bus, managerObjPath)
Ratan Gupta212f53e2018-04-30 17:28:05 +053031 {
32 char tmp[] = "/tmp/snmpManager.XXXXXX";
Ratan Gupta34d129a2021-12-04 21:04:51 +053033 confDir = mkdtemp(tmp);
Ratan Gupta212f53e2018-04-30 17:28:05 +053034 manager.dbusPersistentLocation = confDir;
35 }
Ratan Gupta1dc91782018-04-19 16:47:12 +053036
37 ~TestSNMPConfManager()
38 {
Ratan Gupta212f53e2018-04-30 17:28:05 +053039 fs::remove_all(manager.dbusPersistentLocation);
Ratan Gupta1dc91782018-04-19 16:47:12 +053040 }
41
Ratan Guptaa7ff3852018-11-16 14:05:57 +053042 std::string createSNMPClient(std::string ipaddress, uint16_t port)
Ratan Gupta1dc91782018-04-19 16:47:12 +053043 {
Ratan Guptaa7ff3852018-11-16 14:05:57 +053044 return manager.client(ipaddress, port);
Ratan Gupta1dc91782018-04-19 16:47:12 +053045 }
46
Patrick Williams1334b7b2021-02-22 17:15:12 -060047 ClientList& getSNMPClients()
Ratan Gupta1dc91782018-04-19 16:47:12 +053048 {
49 return manager.clients;
50 }
51
Patrick Williams1334b7b2021-02-22 17:15:12 -060052 bool isClientExist(const std::string& ipaddress)
Ratan Guptaa7ff3852018-11-16 14:05:57 +053053 {
Patrick Williams1334b7b2021-02-22 17:15:12 -060054 for (const auto& val : manager.clients)
Ratan Guptaa7ff3852018-11-16 14:05:57 +053055 {
56 if (val.second.get()->address() == ipaddress)
57 {
58 return true;
59 }
60 }
61 return false;
62 }
63
George Liuc0d7cd42022-07-28 14:53:01 +080064 void deleteSNMPClient(const std::string& ipaddress)
Ratan Gupta1dc91782018-04-19 16:47:12 +053065 {
George Liuc0d7cd42022-07-28 14:53:01 +080066 std::vector<size_t> ids{};
Patrick Williams1334b7b2021-02-22 17:15:12 -060067 for (const auto& val : manager.clients)
Ratan Guptaa7ff3852018-11-16 14:05:57 +053068 {
69 if (val.second.get()->address() == ipaddress)
70 {
George Liuc0d7cd42022-07-28 14:53:01 +080071 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_();
Ratan Guptaa7ff3852018-11-16 14:05:57 +053080 }
81 }
Ratan Gupta1dc91782018-04-19 16:47:12 +053082 }
83};
84
85// Add single SNMP client
86TEST_F(TestSNMPConfManager, AddSNMPClient)
87{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053088 // check the created object path
89 auto path = createSNMPClient("192.168.1.1", 24);
90 std::string expectedPath = managerObjPath;
91 expectedPath += std::string("/1");
Ratan Gupta1dc91782018-04-19 16:47:12 +053092
Ratan Guptaa7ff3852018-11-16 14:05:57 +053093 EXPECT_EQ(path, expectedPath);
Ratan Gupta1dc91782018-04-19 16:47:12 +053094
Ratan Guptaa7ff3852018-11-16 14:05:57 +053095 // check whether the client created
Patrick Williams1334b7b2021-02-22 17:15:12 -060096 auto& clients = getSNMPClients();
Ed Tanous2fddc402022-05-23 11:00:04 -070097 EXPECT_EQ(1U, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +053098 EXPECT_EQ(true, isClientExist("192.168.1.1"));
Ratan Gupta1dc91782018-04-19 16:47:12 +053099}
100
101// Add multiple SNMP client
102TEST_F(TestSNMPConfManager, AddMultipleSNMPClient)
103{
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530104 // add multiple clients and check whether the object path is generated
105 // correctly.
Ratan Gupta1dc91782018-04-19 16:47:12 +0530106 createSNMPClient("192.168.1.1", 24);
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530107 auto path = createSNMPClient("192.168.1.2", 24);
108 std::string expectedPath = managerObjPath;
109 expectedPath += std::string("/2");
Ratan Gupta1dc91782018-04-19 16:47:12 +0530110
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530111 EXPECT_EQ(path, expectedPath);
112
113 // check both the clients get created
Patrick Williams1334b7b2021-02-22 17:15:12 -0600114 auto& clients = getSNMPClients();
Ed Tanous2fddc402022-05-23 11:00:04 -0700115 EXPECT_EQ(2U, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530116
117 EXPECT_EQ(true, isClientExist("192.168.1.1"));
118 EXPECT_EQ(true, isClientExist("192.168.1.2"));
Ratan Gupta1dc91782018-04-19 16:47:12 +0530119}
120
Ratan Gupta9c4fed62018-11-16 17:47:54 +0530121// Add duplicate SNMP client
122TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient)
123{
124 createSNMPClient("192.168.1.1", 24);
125 EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument);
126}
127
Ratan Gupta1dc91782018-04-19 16:47:12 +0530128// Delete SNMP client
129TEST_F(TestSNMPConfManager, DeleteSNMPClient)
130{
Ratan Gupta1dc91782018-04-19 16:47:12 +0530131 createSNMPClient("192.168.1.1", 24);
132 createSNMPClient("192.168.1.2", 24);
George Liuc0d7cd42022-07-28 14:53:01 +0800133 createSNMPClient("192.168.1.1", 25);
Ratan Gupta1dc91782018-04-19 16:47:12 +0530134
Patrick Williams1334b7b2021-02-22 17:15:12 -0600135 auto& clients = getSNMPClients();
George Liuc0d7cd42022-07-28 14:53:01 +0800136 EXPECT_EQ(3U, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530137
Ratan Gupta1dc91782018-04-19 16:47:12 +0530138 deleteSNMPClient("192.168.1.1");
George Liuc0d7cd42022-07-28 14:53:01 +0800139 EXPECT_EQ(1U, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530140
141 auto path = createSNMPClient("192.168.1.3", 24);
142 std::string expectedPath = managerObjPath;
George Liuc0d7cd42022-07-28 14:53:01 +0800143 expectedPath += std::string("/4");
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530144 EXPECT_EQ(path, expectedPath);
145
Ed Tanous2fddc402022-05-23 11:00:04 -0700146 EXPECT_EQ(2U, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530147 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"));
Ratan Gupta1dc91782018-04-19 16:47:12 +0530150}
151
152} // namespace snmp
Ratan Guptaaea53d02018-09-06 17:56:59 +0530153} // namespace network
Ratan Gupta1dc91782018-04-19 16:47:12 +0530154} // namespace phosphor