blob: 1b0928eb30f4c4f4552cc0951fc094c8856df31a [file] [log] [blame]
Ratan Gupta212f53e2018-04-30 17:28:05 +05301#include "snmp_client.hpp"
2#include "snmp_conf_manager.hpp"
3#include "snmp_serialize.hpp"
4
Patrick Williams1334b7b2021-02-22 17:15:12 -06005#include <netinet/in.h>
6
7#include <experimental/filesystem>
8#include <fstream>
9
10#include <gtest/gtest.h>
11
Ratan Gupta212f53e2018-04-30 17:28:05 +053012namespace phosphor
13{
14namespace network
15{
16namespace snmp
17{
18
19constexpr auto clientObjPath = "/xyz/openbmc_test/snmp/client";
20namespace fs = std::experimental::filesystem;
21
22class TestSerialize : public testing::Test
23{
24 public:
25 sdbusplus::bus::bus bus;
26 ConfManager manager;
27 TestSerialize() :
28 bus(sdbusplus::bus::new_default()),
29 manager(bus, "/xyz/openbmc_test/snmp/manager")
30 {
31 char tmp[] = "/tmp/snmpManager.XXXXXX";
32 std::string confDir = mkdtemp(tmp);
33 manager.dbusPersistentLocation = confDir;
34 }
35 ~TestSerialize()
36 {
37 std::error_code ec;
38 fs::remove_all(manager.dbusPersistentLocation, ec);
39 }
40};
41
42TEST_F(TestSerialize, serialize)
43{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053044 std::string objPath = clientObjPath;
45 objPath += "/" + std::to_string(1);
Patrick Williams2cbf7232021-02-24 06:47:06 -060046 std::string objPath2 = clientObjPath;
47 objPath2 += "/" + std::to_string(2);
Ratan Gupta212f53e2018-04-30 17:28:05 +053048
Ratan Guptaa7ff3852018-11-16 14:05:57 +053049 Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23);
50
51 auto path = serialize(1, client, manager.dbusPersistentLocation);
Patrick Williams2cbf7232021-02-24 06:47:06 -060052 Client restoreClient(bus, objPath2.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053053
54 deserialize(path, restoreClient);
55
56 EXPECT_EQ("1.1.1.1", restoreClient.address());
57 EXPECT_EQ(23, restoreClient.port());
58}
59
60TEST_F(TestSerialize, deserialize_non_existent_file)
61{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053062 std::string objPath = clientObjPath;
63 objPath += "/" + std::to_string(1);
64
65 Client client(bus, objPath.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053066 fs::path path = manager.dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053067 path /= "1";
Ratan Gupta212f53e2018-04-30 17:28:05 +053068
69 auto ret = deserialize(path, client);
70
71 EXPECT_EQ(false, ret);
72}
73
74TEST_F(TestSerialize, deserialize_empty_file)
75{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053076 std::string objPath = clientObjPath;
77 objPath += "/" + std::to_string(1);
78
79 Client restoreClient(bus, objPath.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053080
81 std::fstream file;
82
83 fs::path path = manager.dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053084 path /= "1";
Ratan Gupta212f53e2018-04-30 17:28:05 +053085
86 file.open(path.string(), std::ofstream::out);
87 file.close();
88 // deserialize the object with empty file
89 auto ret = deserialize(path, restoreClient);
90 EXPECT_EQ(false, ret);
91}
92
93} // namespace snmp
94} // namespace network
95} // namespace phosphor