blob: 60a51aa09c6e569dd430e4f067932022db559e7d [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{
42 Client client(bus, clientObjPath, manager, "1.1.1.1", 23);
43
44 auto path = serialize(client, manager.dbusPersistentLocation);
45 Client restoreClient(bus, clientObjPath, manager);
46
47 deserialize(path, restoreClient);
48
49 EXPECT_EQ("1.1.1.1", restoreClient.address());
50 EXPECT_EQ(23, restoreClient.port());
51}
52
53TEST_F(TestSerialize, deserialize_non_existent_file)
54{
55 Client client(bus, clientObjPath, manager);
56 fs::path path = manager.dbusPersistentLocation;
57 path /= "snmpTest";
58
59 auto ret = deserialize(path, client);
60
61 EXPECT_EQ(false, ret);
62}
63
64TEST_F(TestSerialize, deserialize_empty_file)
65{
66 Client restoreClient(bus, clientObjPath, manager);
67
68 std::fstream file;
69
70 fs::path path = manager.dbusPersistentLocation;
71 path /= "snmpTest";
72
73 file.open(path.string(), std::ofstream::out);
74 file.close();
75 // deserialize the object with empty file
76 auto ret = deserialize(path, restoreClient);
77 EXPECT_EQ(false, ret);
78}
79
80} // namespace snmp
81} // namespace network
82} // namespace phosphor