1 #include "snmp_client.hpp" 2 #include "snmp_conf_manager.hpp" 3 #include "snmp_serialize.hpp" 4 5 #include <netinet/in.h> 6 7 #include <filesystem> 8 #include <fstream> 9 10 #include <gtest/gtest.h> 11 12 namespace phosphor 13 { 14 namespace network 15 { 16 namespace snmp 17 { 18 19 constexpr auto clientObjPath = "/xyz/openbmc_test/snmp/client"; 20 namespace fs = std::filesystem; 21 22 class 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 42 TEST_F(TestSerialize, serialize) 43 { 44 std::string objPath = clientObjPath; 45 objPath += "/" + std::to_string(1); 46 std::string objPath2 = clientObjPath; 47 objPath2 += "/" + std::to_string(2); 48 49 Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23); 50 51 auto path = serialize(1, client, manager.dbusPersistentLocation); 52 Client restoreClient(bus, objPath2.c_str(), manager); 53 54 deserialize(path, restoreClient); 55 56 EXPECT_EQ("1.1.1.1", restoreClient.address()); 57 EXPECT_EQ(23, restoreClient.port()); 58 } 59 60 TEST_F(TestSerialize, deserialize_non_existent_file) 61 { 62 std::string objPath = clientObjPath; 63 objPath += "/" + std::to_string(1); 64 65 Client client(bus, objPath.c_str(), manager); 66 fs::path path = manager.dbusPersistentLocation; 67 path /= "1"; 68 69 auto ret = deserialize(path, client); 70 71 EXPECT_EQ(false, ret); 72 } 73 74 TEST_F(TestSerialize, deserialize_empty_file) 75 { 76 std::string objPath = clientObjPath; 77 objPath += "/" + std::to_string(1); 78 79 Client restoreClient(bus, objPath.c_str(), manager); 80 81 std::fstream file; 82 83 fs::path path = manager.dbusPersistentLocation; 84 path /= "1"; 85 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 96