Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 1 | #include "snmp_client.hpp" |
| 2 | #include "snmp_conf_manager.hpp" |
| 3 | #include "snmp_serialize.hpp" |
| 4 | |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 5 | #include <netinet/in.h> |
| 6 | |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 7 | #include <filesystem> |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 8 | #include <fstream> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace network |
| 15 | { |
| 16 | namespace snmp |
| 17 | { |
| 18 | |
| 19 | constexpr auto clientObjPath = "/xyz/openbmc_test/snmp/client"; |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 20 | namespace fs = std::filesystem; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 21 | |
| 22 | class TestSerialize : public testing::Test |
| 23 | { |
| 24 | public: |
Patrick Williams | 87d3edd | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 25 | sdbusplus::bus_t bus; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 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 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 44 | std::string objPath = clientObjPath; |
| 45 | objPath += "/" + std::to_string(1); |
Patrick Williams | 2cbf723 | 2021-02-24 06:47:06 -0600 | [diff] [blame] | 46 | std::string objPath2 = clientObjPath; |
| 47 | objPath2 += "/" + std::to_string(2); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 48 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 49 | Client client(bus, objPath.c_str(), manager, "1.1.1.1", 23); |
| 50 | |
| 51 | auto path = serialize(1, client, manager.dbusPersistentLocation); |
Patrick Williams | 2cbf723 | 2021-02-24 06:47:06 -0600 | [diff] [blame] | 52 | Client restoreClient(bus, objPath2.c_str(), manager); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 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 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 62 | std::string objPath = clientObjPath; |
| 63 | objPath += "/" + std::to_string(1); |
| 64 | |
| 65 | Client client(bus, objPath.c_str(), manager); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 66 | fs::path path = manager.dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 67 | path /= "1"; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 68 | |
| 69 | auto ret = deserialize(path, client); |
| 70 | |
| 71 | EXPECT_EQ(false, ret); |
| 72 | } |
| 73 | |
| 74 | TEST_F(TestSerialize, deserialize_empty_file) |
| 75 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 76 | std::string objPath = clientObjPath; |
| 77 | objPath += "/" + std::to_string(1); |
| 78 | |
| 79 | Client restoreClient(bus, objPath.c_str(), manager); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 80 | |
| 81 | std::fstream file; |
| 82 | |
| 83 | fs::path path = manager.dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 84 | path /= "1"; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 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 |