Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 1 | #include "snmp_conf_manager.hpp" |
| 2 | #include "snmp_serialize.hpp" |
Ratan Gupta | 9d18e56 | 2018-11-16 17:19:34 +0530 | [diff] [blame] | 3 | |
| 4 | #include <xyz/openbmc_project/Common/error.hpp> |
| 5 | |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
Ratan Gupta | 9d18e56 | 2018-11-16 17:19:34 +0530 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace snmp |
| 13 | { |
| 14 | |
| 15 | using InvalidArgument = |
| 16 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 17 | |
| 18 | auto mgrObjPath = "/xyz/openbmc_test/snmp/manager"; |
| 19 | |
| 20 | class TestSNMPClient : public testing::Test |
| 21 | { |
| 22 | public: |
| 23 | sdbusplus::bus::bus bus; |
| 24 | ConfManager manager; |
Ratan Gupta | 9d18e56 | 2018-11-16 17:19:34 +0530 | [diff] [blame] | 25 | TestSNMPClient() : |
| 26 | bus(sdbusplus::bus::new_default()), manager(bus, mgrObjPath) |
| 27 | { |
| 28 | char tmp[] = "/tmp/snmpClient.XXXXXX"; |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 29 | auto confDir = mkdtemp(tmp); |
Ratan Gupta | 9d18e56 | 2018-11-16 17:19:34 +0530 | [diff] [blame] | 30 | manager.dbusPersistentLocation = confDir; |
| 31 | } |
| 32 | |
| 33 | ~TestSNMPClient() |
| 34 | { |
| 35 | fs::remove_all(manager.dbusPersistentLocation); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | TEST_F(TestSNMPClient, UpdateProperty) |
| 40 | { |
| 41 | std::string objPath = mgrObjPath; |
| 42 | objPath += "/" + std::to_string(1); |
| 43 | Client client(bus, objPath.c_str(), manager, "1.1.1.1", 202); |
| 44 | EXPECT_EQ(client.address(), "1.1.1.1"); |
| 45 | EXPECT_EQ(client.port(), 202); |
| 46 | client.address("2.2.2.2"); |
| 47 | EXPECT_EQ(client.address(), "2.2.2.2"); |
| 48 | client.port(404); |
| 49 | EXPECT_EQ(client.port(), 404); |
| 50 | } |
| 51 | |
| 52 | TEST_F(TestSNMPClient, AddEmptyAddress) |
| 53 | { |
| 54 | std::string objPath = mgrObjPath; |
| 55 | objPath += "/" + std::to_string(1); |
| 56 | Client client(bus, objPath.c_str(), manager, "1.1.1.1", 202); |
| 57 | EXPECT_EQ(client.address(), "1.1.1.1"); |
| 58 | EXPECT_EQ(client.port(), 202); |
| 59 | |
| 60 | EXPECT_THROW(client.address(""), InvalidArgument); |
| 61 | } |
| 62 | |
| 63 | TEST_F(TestSNMPClient, CheckPersistency) |
| 64 | { |
| 65 | std::string objPath = mgrObjPath; |
| 66 | objPath += "/" + std::to_string(1); |
Patrick Williams | 2cbf723 | 2021-02-24 06:47:06 -0600 | [diff] [blame] | 67 | std::string objPath2 = mgrObjPath; |
| 68 | objPath2 += "/" + std::to_string(2); |
Ratan Gupta | 9d18e56 | 2018-11-16 17:19:34 +0530 | [diff] [blame] | 69 | |
| 70 | Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23); |
| 71 | client.address("2.2.2.2"); |
| 72 | |
Patrick Williams | 2cbf723 | 2021-02-24 06:47:06 -0600 | [diff] [blame] | 73 | Client restoreClient(bus, objPath2.c_str(), manager); |
Ratan Gupta | 9d18e56 | 2018-11-16 17:19:34 +0530 | [diff] [blame] | 74 | auto persistentPath = manager.dbusPersistentLocation; |
| 75 | persistentPath += "/1"; |
| 76 | deserialize(persistentPath, restoreClient); |
| 77 | |
| 78 | EXPECT_EQ("2.2.2.2", restoreClient.address()); |
| 79 | EXPECT_EQ(23, restoreClient.port()); |
| 80 | } |
| 81 | |
| 82 | } // namespace snmp |
| 83 | } // namespace network |
| 84 | } // namespace phosphor |