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