Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 1 | #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 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace network |
| 13 | { |
| 14 | namespace snmp |
| 15 | { |
| 16 | |
| 17 | constexpr auto clientObjPath = "/xyz/openbmc_test/snmp/client"; |
| 18 | namespace fs = std::experimental::filesystem; |
| 19 | |
| 20 | class 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 | |
| 40 | TEST_F(TestSerialize, serialize) |
| 41 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 42 | std::string objPath = clientObjPath; |
| 43 | objPath += "/" + std::to_string(1); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 44 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 45 | 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 Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 49 | |
| 50 | deserialize(path, restoreClient); |
| 51 | |
| 52 | EXPECT_EQ("1.1.1.1", restoreClient.address()); |
| 53 | EXPECT_EQ(23, restoreClient.port()); |
| 54 | } |
| 55 | |
| 56 | TEST_F(TestSerialize, deserialize_non_existent_file) |
| 57 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 58 | std::string objPath = clientObjPath; |
| 59 | objPath += "/" + std::to_string(1); |
| 60 | |
| 61 | Client client(bus, objPath.c_str(), manager); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 62 | fs::path path = manager.dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 63 | path /= "1"; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 64 | |
| 65 | auto ret = deserialize(path, client); |
| 66 | |
| 67 | EXPECT_EQ(false, ret); |
| 68 | } |
| 69 | |
| 70 | TEST_F(TestSerialize, deserialize_empty_file) |
| 71 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 72 | std::string objPath = clientObjPath; |
| 73 | objPath += "/" + std::to_string(1); |
| 74 | |
| 75 | Client restoreClient(bus, objPath.c_str(), manager); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 76 | |
| 77 | std::fstream file; |
| 78 | |
| 79 | fs::path path = manager.dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 80 | path /= "1"; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 81 | |
| 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 |