blob: 1dfcf66c3729abb4fd2a3718220a26584ce73898 [file] [log] [blame]
Chirag Sharma50427252020-08-11 12:11:38 -05001// SPDX-License-Identifier: Apache-2.0
Chirag Sharma50427252020-08-11 12:11:38 -05002#include <dump_serialize.hpp>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -05003
4#include <cstdlib>
Chirag Sharma50427252020-08-11 12:11:38 -05005#include <exception>
6#include <filesystem>
7#include <set>
Jayanth Othayothceb3e762024-11-25 23:19:37 -06008#include <span>
Chirag Sharma50427252020-08-11 12:11:38 -05009#include <string>
10
11#include <gtest/gtest.h>
12
Chirag Sharma50427252020-08-11 12:11:38 -050013class TestDumpSerial : public ::testing::Test
14{
15 public:
Jayanth Othayothf7a5bf72024-11-26 02:46:29 -060016 TestDumpSerial() = default;
Chirag Sharma50427252020-08-11 12:11:38 -050017
18 void SetUp()
19 {
20 char tmpdir[] = "/tmp/dump.XXXXXX";
Jayanth Othayothceb3e762024-11-25 23:19:37 -060021 std::span<char> tmpdirSpan(reinterpret_cast<char*>(tmpdir),
22 sizeof(tmpdir));
23 auto dirPtr = mkdtemp(tmpdirSpan.data());
Jayanth Othayothad6fb6e2024-11-26 01:31:13 -060024 if (dirPtr == nullptr)
Chirag Sharma50427252020-08-11 12:11:38 -050025 {
26 throw std::bad_alloc();
27 }
28 dumpDir = std::string(dirPtr);
Patrick Williamse9ec9522025-02-05 16:49:52 -050029 std::filesystem::create_directories(dumpDir);
Chirag Sharma50427252020-08-11 12:11:38 -050030 dumpFile = dumpDir;
31 dumpFile /= "elogid";
32 }
33 void TearDown()
34 {
Patrick Williamse9ec9522025-02-05 16:49:52 -050035 std::filesystem::remove_all(dumpDir);
Chirag Sharma50427252020-08-11 12:11:38 -050036 }
37
38 std::string dumpDir;
Patrick Williamse9ec9522025-02-05 16:49:52 -050039 std::filesystem::path dumpFile;
Chirag Sharma50427252020-08-11 12:11:38 -050040};
41
42TEST_F(TestDumpSerial, Serialization)
43{
44 using ElogList = std::set<uint32_t>;
45 ElogList e;
46 e.insert(1);
47 e.insert(2);
48 e.insert(3);
49 phosphor::dump::elog::serialize(e, dumpFile.c_str());
50 bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e);
51 EXPECT_EQ(value, true);
52}
53
54TEST_F(TestDumpSerial, DeserializationFalseCase)
55{
56 using ElogList = std::set<uint32_t>;
57 ElogList e;
58 e.insert(1);
59 bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e);
60 EXPECT_EQ(value, false);
61}
62
63TEST(DumpDeSerialPath, DeserializationFalsePath)
64{
65 using ElogList = std::set<uint32_t>;
66 ElogList e;
67 e.insert(1);
68 // Providing dummy path
69 bool value = phosphor::dump::elog::deserialize("/tmp/Fake/serial", e);
70 EXPECT_EQ(value, false);
71}