Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 1 | #include "snmp_conf_manager.hpp" |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 2 | #include "xyz/openbmc_project/Common/error.hpp" |
| 3 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> |
| 5 | |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
| 7 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace snmp |
| 13 | { |
| 14 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 15 | auto managerObjPath = "/xyz/openbmc_test/snmp/manager"; |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 16 | using InvalidArgument = |
| 17 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 18 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 19 | class TestSNMPConfManager : public testing::Test |
| 20 | { |
| 21 | public: |
Patrick Williams | 87d3edd | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 22 | sdbusplus::bus_t bus; |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 23 | ConfManager manager; |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 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 |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 28 | std::string confDir; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 29 | TestSNMPConfManager() : |
| 30 | bus(sdbusplus::bus::new_default()), manager(bus, managerObjPath) |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 31 | { |
| 32 | char tmp[] = "/tmp/snmpManager.XXXXXX"; |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 33 | confDir = mkdtemp(tmp); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 34 | manager.dbusPersistentLocation = confDir; |
| 35 | } |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 36 | |
| 37 | ~TestSNMPConfManager() |
| 38 | { |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 39 | fs::remove_all(manager.dbusPersistentLocation); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 40 | } |
| 41 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 42 | std::string createSNMPClient(std::string ipaddress, uint16_t port) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 43 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 44 | return manager.client(ipaddress, port); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 45 | } |
| 46 | |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 47 | ClientList& getSNMPClients() |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 48 | { |
| 49 | return manager.clients; |
| 50 | } |
| 51 | |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 52 | bool isClientExist(const std::string& ipaddress) |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 53 | { |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 54 | for (const auto& val : manager.clients) |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 55 | { |
| 56 | if (val.second.get()->address() == ipaddress) |
| 57 | { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
George Liu | c0d7cd4 | 2022-07-28 14:53:01 +0800 | [diff] [blame^] | 64 | void deleteSNMPClient(const std::string& ipaddress) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 65 | { |
George Liu | c0d7cd4 | 2022-07-28 14:53:01 +0800 | [diff] [blame^] | 66 | std::vector<size_t> ids{}; |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 67 | for (const auto& val : manager.clients) |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 68 | { |
| 69 | if (val.second.get()->address() == ipaddress) |
| 70 | { |
George Liu | c0d7cd4 | 2022-07-28 14:53:01 +0800 | [diff] [blame^] | 71 | 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 Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 80 | } |
| 81 | } |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
| 85 | // Add single SNMP client |
| 86 | TEST_F(TestSNMPConfManager, AddSNMPClient) |
| 87 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 88 | // 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 Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 92 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 93 | EXPECT_EQ(path, expectedPath); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 94 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 95 | // check whether the client created |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 96 | auto& clients = getSNMPClients(); |
Ed Tanous | 2fddc40 | 2022-05-23 11:00:04 -0700 | [diff] [blame] | 97 | EXPECT_EQ(1U, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 98 | EXPECT_EQ(true, isClientExist("192.168.1.1")); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Add multiple SNMP client |
| 102 | TEST_F(TestSNMPConfManager, AddMultipleSNMPClient) |
| 103 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 104 | // add multiple clients and check whether the object path is generated |
| 105 | // correctly. |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 106 | createSNMPClient("192.168.1.1", 24); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 107 | auto path = createSNMPClient("192.168.1.2", 24); |
| 108 | std::string expectedPath = managerObjPath; |
| 109 | expectedPath += std::string("/2"); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 110 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 111 | EXPECT_EQ(path, expectedPath); |
| 112 | |
| 113 | // check both the clients get created |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 114 | auto& clients = getSNMPClients(); |
Ed Tanous | 2fddc40 | 2022-05-23 11:00:04 -0700 | [diff] [blame] | 115 | EXPECT_EQ(2U, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 116 | |
| 117 | EXPECT_EQ(true, isClientExist("192.168.1.1")); |
| 118 | EXPECT_EQ(true, isClientExist("192.168.1.2")); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 119 | } |
| 120 | |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 121 | // Add duplicate SNMP client |
| 122 | TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient) |
| 123 | { |
| 124 | createSNMPClient("192.168.1.1", 24); |
| 125 | EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument); |
| 126 | } |
| 127 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 128 | // Delete SNMP client |
| 129 | TEST_F(TestSNMPConfManager, DeleteSNMPClient) |
| 130 | { |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 131 | createSNMPClient("192.168.1.1", 24); |
| 132 | createSNMPClient("192.168.1.2", 24); |
George Liu | c0d7cd4 | 2022-07-28 14:53:01 +0800 | [diff] [blame^] | 133 | createSNMPClient("192.168.1.1", 25); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 134 | |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 135 | auto& clients = getSNMPClients(); |
George Liu | c0d7cd4 | 2022-07-28 14:53:01 +0800 | [diff] [blame^] | 136 | EXPECT_EQ(3U, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 137 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 138 | deleteSNMPClient("192.168.1.1"); |
George Liu | c0d7cd4 | 2022-07-28 14:53:01 +0800 | [diff] [blame^] | 139 | EXPECT_EQ(1U, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 140 | |
| 141 | auto path = createSNMPClient("192.168.1.3", 24); |
| 142 | std::string expectedPath = managerObjPath; |
George Liu | c0d7cd4 | 2022-07-28 14:53:01 +0800 | [diff] [blame^] | 143 | expectedPath += std::string("/4"); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 144 | EXPECT_EQ(path, expectedPath); |
| 145 | |
Ed Tanous | 2fddc40 | 2022-05-23 11:00:04 -0700 | [diff] [blame] | 146 | EXPECT_EQ(2U, clients.size()); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 147 | 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 Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } // namespace snmp |
Ratan Gupta | aea53d0 | 2018-09-06 17:56:59 +0530 | [diff] [blame] | 153 | } // namespace network |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 154 | } // namespace phosphor |