1 // SPDX-License-Identifier: Apache-2.0
2 #include <dump_serialize.hpp>
3 
4 #include <cstdlib>
5 #include <exception>
6 #include <filesystem>
7 #include <set>
8 #include <span>
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:
18     TestDumpSerial() = default;
19 
SetUp()20     void SetUp()
21     {
22         char tmpdir[] = "/tmp/dump.XXXXXX";
23         std::span<char> tmpdirSpan(reinterpret_cast<char*>(tmpdir),
24                                    sizeof(tmpdir));
25         auto dirPtr = mkdtemp(tmpdirSpan.data());
26         if (dirPtr == nullptr)
27         {
28             throw std::bad_alloc();
29         }
30         dumpDir = std::string(dirPtr);
31         fs::create_directories(dumpDir);
32         dumpFile = dumpDir;
33         dumpFile /= "elogid";
34     }
TearDown()35     void TearDown()
36     {
37         fs::remove_all(dumpDir);
38     }
39 
40     std::string dumpDir;
41     fs::path dumpFile;
42 };
43 
TEST_F(TestDumpSerial,Serialization)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 
TEST_F(TestDumpSerial,DeserializationFalseCase)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 
TEST(DumpDeSerialPath,DeserializationFalsePath)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 }
74