1 // SPDX-License-Identifier: Apache-2.0 2 #include <cstdlib> 3 #include <dump_serialize.hpp> 4 #include <exception> 5 #include <filesystem> 6 #include <set> 7 #include <string> 8 9 #include <gtest/gtest.h> 10 11 namespace fs = std::filesystem; 12 13 class TestDumpSerial : public ::testing::Test 14 { 15 public: 16 TestDumpSerial() 17 { 18 } 19 20 void SetUp() 21 { 22 char tmpdir[] = "/tmp/dump.XXXXXX"; 23 auto dirPtr = mkdtemp(tmpdir); 24 if (dirPtr == NULL) 25 { 26 throw std::bad_alloc(); 27 } 28 dumpDir = std::string(dirPtr); 29 fs::create_directories(dumpDir); 30 dumpFile = dumpDir; 31 dumpFile /= "elogid"; 32 } 33 void TearDown() 34 { 35 fs::remove_all(dumpDir); 36 } 37 38 std::string dumpDir; 39 fs::path dumpFile; 40 }; 41 42 TEST_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 54 TEST_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 63 TEST(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 } 72