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 <string> 9 10 #include <gtest/gtest.h> 11 12 namespace fs = std::filesystem; 13 14 class TestDumpSerial : public ::testing::Test 15 { 16 public: 17 TestDumpSerial() {} 18 19 void SetUp() 20 { 21 char tmpdir[] = "/tmp/dump.XXXXXX"; 22 auto dirPtr = mkdtemp(tmpdir); 23 if (dirPtr == NULL) 24 { 25 throw std::bad_alloc(); 26 } 27 dumpDir = std::string(dirPtr); 28 fs::create_directories(dumpDir); 29 dumpFile = dumpDir; 30 dumpFile /= "elogid"; 31 } 32 void TearDown() 33 { 34 fs::remove_all(dumpDir); 35 } 36 37 std::string dumpDir; 38 fs::path dumpFile; 39 }; 40 41 TEST_F(TestDumpSerial, Serialization) 42 { 43 using ElogList = std::set<uint32_t>; 44 ElogList e; 45 e.insert(1); 46 e.insert(2); 47 e.insert(3); 48 phosphor::dump::elog::serialize(e, dumpFile.c_str()); 49 bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e); 50 EXPECT_EQ(value, true); 51 } 52 53 TEST_F(TestDumpSerial, DeserializationFalseCase) 54 { 55 using ElogList = std::set<uint32_t>; 56 ElogList e; 57 e.insert(1); 58 bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e); 59 EXPECT_EQ(value, false); 60 } 61 62 TEST(DumpDeSerialPath, DeserializationFalsePath) 63 { 64 using ElogList = std::set<uint32_t>; 65 ElogList e; 66 e.insert(1); 67 // Providing dummy path 68 bool value = phosphor::dump::elog::deserialize("/tmp/Fake/serial", e); 69 EXPECT_EQ(value, false); 70 } 71