blob: df27b5bac42ade8e29560af8696936cc851691c2 [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);
Ratan Gupta212f53e2018-04-30 17:28:05 +053044
Ratan Guptaa7ff3852018-11-16 14:05:57 +053045 Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23);
46
47 auto path = serialize(1, client, manager.dbusPersistentLocation);
48 Client restoreClient(bus, objPath.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053049
50 deserialize(path, restoreClient);
51
52 EXPECT_EQ("1.1.1.1", restoreClient.address());
53 EXPECT_EQ(23, restoreClient.port());
54}
55
56TEST_F(TestSerialize, deserialize_non_existent_file)
57{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053058 std::string objPath = clientObjPath;
59 objPath += "/" + std::to_string(1);
60
61 Client client(bus, objPath.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053062 fs::path path = manager.dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053063 path /= "1";
Ratan Gupta212f53e2018-04-30 17:28:05 +053064
65 auto ret = deserialize(path, client);
66
67 EXPECT_EQ(false, ret);
68}
69
70TEST_F(TestSerialize, deserialize_empty_file)
71{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053072 std::string objPath = clientObjPath;
73 objPath += "/" + std::to_string(1);
74
75 Client restoreClient(bus, objPath.c_str(), manager);
Ratan Gupta212f53e2018-04-30 17:28:05 +053076
77 std::fstream file;
78
79 fs::path path = manager.dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053080 path /= "1";
Ratan Gupta212f53e2018-04-30 17:28:05 +053081
82 file.open(path.string(), std::ofstream::out);
83 file.close();
84 // deserialize the object with empty file
85 auto ret = deserialize(path, restoreClient);
86 EXPECT_EQ(false, ret);
87}
88
89} // namespace snmp
90} // namespace network
91} // namespace phosphor