blob: 58688999de4ece1804022777e5ca2ce58d097f65 [file] [log] [blame]
Patrick Williams1334b7b2021-02-22 17:15:12 -06001#include "snmp_conf_manager.hpp"
2#include "snmp_serialize.hpp"
Ratan Gupta9d18e562018-11-16 17:19:34 +05303
4#include <xyz/openbmc_project/Common/error.hpp>
5
Patrick Williams1334b7b2021-02-22 17:15:12 -06006#include <gtest/gtest.h>
Ratan Gupta9d18e562018-11-16 17:19:34 +05307
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:
Patrick Williams87d3edd2022-07-22 19:26:53 -050023 sdbusplus::bus_t bus;
Ratan Gupta9d18e562018-11-16 17:19:34 +053024 ConfManager manager;
Ratan Gupta9d18e562018-11-16 17:19:34 +053025 TestSNMPClient() :
26 bus(sdbusplus::bus::new_default()), manager(bus, mgrObjPath)
27 {
28 char tmp[] = "/tmp/snmpClient.XXXXXX";
Ratan Gupta34d129a2021-12-04 21:04:51 +053029 auto confDir = mkdtemp(tmp);
Ratan Gupta9d18e562018-11-16 17:19:34 +053030 manager.dbusPersistentLocation = confDir;
31 }
32
33 ~TestSNMPClient()
34 {
35 fs::remove_all(manager.dbusPersistentLocation);
36 }
37};
38
39TEST_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);
Ivan Mikhaylov40f769b2023-06-20 19:18:56 +030044 EXPECT_EQ(client.transportProtocol(),
45 sdbusplus::server::xyz::openbmc_project::network::Client::
46 TransportProtocol::UDP);
Ratan Gupta9d18e562018-11-16 17:19:34 +053047 EXPECT_EQ(client.address(), "1.1.1.1");
48 EXPECT_EQ(client.port(), 202);
49 client.address("2.2.2.2");
50 EXPECT_EQ(client.address(), "2.2.2.2");
51 client.port(404);
52 EXPECT_EQ(client.port(), 404);
53}
54
55TEST_F(TestSNMPClient, AddEmptyAddress)
56{
57 std::string objPath = mgrObjPath;
58 objPath += "/" + std::to_string(1);
59 Client client(bus, objPath.c_str(), manager, "1.1.1.1", 202);
60 EXPECT_EQ(client.address(), "1.1.1.1");
61 EXPECT_EQ(client.port(), 202);
62
63 EXPECT_THROW(client.address(""), InvalidArgument);
64}
65
66TEST_F(TestSNMPClient, CheckPersistency)
67{
68 std::string objPath = mgrObjPath;
69 objPath += "/" + std::to_string(1);
Patrick Williams2cbf7232021-02-24 06:47:06 -060070 std::string objPath2 = mgrObjPath;
71 objPath2 += "/" + std::to_string(2);
Ratan Gupta9d18e562018-11-16 17:19:34 +053072
73 Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23);
74 client.address("2.2.2.2");
75
Patrick Williams2cbf7232021-02-24 06:47:06 -060076 Client restoreClient(bus, objPath2.c_str(), manager);
Ratan Gupta9d18e562018-11-16 17:19:34 +053077 auto persistentPath = manager.dbusPersistentLocation;
78 persistentPath += "/1";
79 deserialize(persistentPath, restoreClient);
80
81 EXPECT_EQ("2.2.2.2", restoreClient.address());
82 EXPECT_EQ(23, restoreClient.port());
83}
84
85} // namespace snmp
86} // namespace network
87} // namespace phosphor