blob: c276cb14d63ce0aded0c25c492f5919027b83c3d [file] [log] [blame]
Ratan Gupta212f53e2018-04-30 17:28:05 +05301#include <experimental/filesystem>
2#include <fstream>
3#include <gtest/gtest.h>
4#include <netinet/in.h>
5
6#include "snmp_client.hpp"
7#include "snmp_conf_manager.hpp"
8#include "snmp_serialize.hpp"
9
10namespace phosphor
11{
12namespace network
13{
14namespace snmp
15{
16
17constexpr auto clientObjPath = "/xyz/openbmc_test/snmp/client";
18namespace fs = std::experimental::filesystem;
19
20class TestSerialize : public testing::Test
21{
22 public:
23 sdbusplus::bus::bus bus;
24 ConfManager manager;
25 TestSerialize() :
26 bus(sdbusplus::bus::new_default()),
27 manager(bus, "/xyz/openbmc_test/snmp/manager")
28 {
29 char tmp[] = "/tmp/snmpManager.XXXXXX";
30 std::string confDir = mkdtemp(tmp);
31 manager.dbusPersistentLocation = confDir;
32 }
33 ~TestSerialize()
34 {
35 std::error_code ec;
36 fs::remove_all(manager.dbusPersistentLocation, ec);
37 }
38};
39
40TEST_F(TestSerialize, serialize)
41{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053042 std::string objPath = clientObjPath;
43 objPath += "/" + std::to_string(1);
Patrick Williams2cbf7232021-02-24 06:47:06 -060044 std::string objPath2 = clientObjPath;
45 objPath2 += "/" + std::to_string(2);
Ratan Gupta212f53e2018-04-30 17:28:05 +053046
Ratan Guptaa7ff3852018-11-16 14:05:57 +053047 Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23);
48
49 auto path = serialize(1, client, manager.dbusPersistentLocation);
Patrick Williams2cbf7232021-02-24 06:47:06 -060050 Client restoreClient(bus, objPath2.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053051
52 deserialize(path, restoreClient);
53
54 EXPECT_EQ("1.1.1.1", restoreClient.address());
55 EXPECT_EQ(23, restoreClient.port());
56}
57
58TEST_F(TestSerialize, deserialize_non_existent_file)
59{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053060 std::string objPath = clientObjPath;
61 objPath += "/" + std::to_string(1);
62
63 Client client(bus, objPath.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053064 fs::path path = manager.dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053065 path /= "1";
Ratan Gupta212f53e2018-04-30 17:28:05 +053066
67 auto ret = deserialize(path, client);
68
69 EXPECT_EQ(false, ret);
70}
71
72TEST_F(TestSerialize, deserialize_empty_file)
73{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053074 std::string objPath = clientObjPath;
75 objPath += "/" + std::to_string(1);
76
77 Client restoreClient(bus, objPath.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053078
79 std::fstream file;
80
81 fs::path path = manager.dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053082 path /= "1";
Ratan Gupta212f53e2018-04-30 17:28:05 +053083
84 file.open(path.string(), std::ofstream::out);
85 file.close();
86 // deserialize the object with empty file
87 auto ret = deserialize(path, restoreClient);
88 EXPECT_EQ(false, ret);
89}
90
91} // namespace snmp
92} // namespace network
93} // namespace phosphor