blob: 98a0e1d46b6d432a8691c04565d44f26cb79df45 [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
13namespace fs = std::filesystem;
14
15class TestDumpSerial : public ::testing::Test
16{
17 public:
Jayanth Othayothf7a5bf72024-11-26 02:46:29 -060018 TestDumpSerial() = default;
Chirag Sharma50427252020-08-11 12:11:38 -050019
20 void SetUp()
21 {
22 char tmpdir[] = "/tmp/dump.XXXXXX";
Jayanth Othayothceb3e762024-11-25 23:19:37 -060023 std::span<char> tmpdirSpan(reinterpret_cast<char*>(tmpdir),
24 sizeof(tmpdir));
25 auto dirPtr = mkdtemp(tmpdirSpan.data());
Jayanth Othayothad6fb6e2024-11-26 01:31:13 -060026 if (dirPtr == nullptr)
Chirag Sharma50427252020-08-11 12:11:38 -050027 {
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
44TEST_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
56TEST_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
65TEST(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}