Chirag Sharma | 5042725 | 2020-08-11 12:11:38 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
Chirag Sharma | 5042725 | 2020-08-11 12:11:38 -0500 | [diff] [blame] | 2 | #include <dump_serialize.hpp> |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 3 | |
| 4 | #include <cstdlib> |
Chirag Sharma | 5042725 | 2020-08-11 12:11:38 -0500 | [diff] [blame] | 5 | #include <exception> |
| 6 | #include <filesystem> |
| 7 | #include <set> |
Jayanth Othayoth | ceb3e76 | 2024-11-25 23:19:37 -0600 | [diff] [blame] | 8 | #include <span> |
Chirag Sharma | 5042725 | 2020-08-11 12:11:38 -0500 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | namespace fs = std::filesystem; |
| 14 | |
| 15 | class TestDumpSerial : public ::testing::Test |
| 16 | { |
| 17 | public: |
Jayanth Othayoth | f7a5bf7 | 2024-11-26 02:46:29 -0600 | [diff] [blame^] | 18 | TestDumpSerial() = default; |
Chirag Sharma | 5042725 | 2020-08-11 12:11:38 -0500 | [diff] [blame] | 19 | |
| 20 | void SetUp() |
| 21 | { |
| 22 | char tmpdir[] = "/tmp/dump.XXXXXX"; |
Jayanth Othayoth | ceb3e76 | 2024-11-25 23:19:37 -0600 | [diff] [blame] | 23 | std::span<char> tmpdirSpan(reinterpret_cast<char*>(tmpdir), |
| 24 | sizeof(tmpdir)); |
| 25 | auto dirPtr = mkdtemp(tmpdirSpan.data()); |
Jayanth Othayoth | ad6fb6e | 2024-11-26 01:31:13 -0600 | [diff] [blame] | 26 | if (dirPtr == nullptr) |
Chirag Sharma | 5042725 | 2020-08-11 12:11:38 -0500 | [diff] [blame] | 27 | { |
| 28 | throw std::bad_alloc(); |
| 29 | } |
| 30 | dumpDir = std::string(dirPtr); |
| 31 | fs::create_directories(dumpDir); |
| 32 | dumpFile = dumpDir; |
| 33 | dumpFile /= "elogid"; |
| 34 | } |
| 35 | void TearDown() |
| 36 | { |
| 37 | fs::remove_all(dumpDir); |
| 38 | } |
| 39 | |
| 40 | std::string dumpDir; |
| 41 | fs::path dumpFile; |
| 42 | }; |
| 43 | |
| 44 | TEST_F(TestDumpSerial, Serialization) |
| 45 | { |
| 46 | using ElogList = std::set<uint32_t>; |
| 47 | ElogList e; |
| 48 | e.insert(1); |
| 49 | e.insert(2); |
| 50 | e.insert(3); |
| 51 | phosphor::dump::elog::serialize(e, dumpFile.c_str()); |
| 52 | bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e); |
| 53 | EXPECT_EQ(value, true); |
| 54 | } |
| 55 | |
| 56 | TEST_F(TestDumpSerial, DeserializationFalseCase) |
| 57 | { |
| 58 | using ElogList = std::set<uint32_t>; |
| 59 | ElogList e; |
| 60 | e.insert(1); |
| 61 | bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e); |
| 62 | EXPECT_EQ(value, false); |
| 63 | } |
| 64 | |
| 65 | TEST(DumpDeSerialPath, DeserializationFalsePath) |
| 66 | { |
| 67 | using ElogList = std::set<uint32_t>; |
| 68 | ElogList e; |
| 69 | e.insert(1); |
| 70 | // Providing dummy path |
| 71 | bool value = phosphor::dump::elog::deserialize("/tmp/Fake/serial", e); |
| 72 | EXPECT_EQ(value, false); |
| 73 | } |