| #include "tpm/tpm_event_log.hpp" |
| |
| #include <blobs-ipmid/blobs.hpp> |
| |
| #include <cstdio> |
| #include <fstream> |
| #include <string> |
| #include <vector> |
| |
| #include <gtest/gtest.h> |
| |
| namespace ipmi_hoth |
| { |
| |
| class TpmEventLogTest : public ::testing::Test |
| { |
| protected: |
| void SetUp() override |
| { |
| char tempPath[] = "/tmp/tpm_event_log_testXXXXXX"; |
| int fd = mkstemp(tempPath); |
| if (fd == -1) |
| { |
| throw std::runtime_error("Failed to create temp file"); |
| } |
| testFilePath = tempPath; |
| close(fd); |
| } |
| |
| void TearDown() override |
| { |
| if (!testFilePath.empty()) |
| { |
| std::remove(testFilePath.c_str()); |
| } |
| } |
| |
| void writeTestData(const std::string& data) |
| { |
| std::ofstream ofs(testFilePath, std::ios::binary); |
| ofs << data; |
| } |
| |
| std::string testFilePath; |
| const std::string blobId = "/tpm/eventlog"; |
| }; |
| |
| TEST_F(TpmEventLogTest, CanHandleBlobReturnsTrueForCorrectPath) |
| { |
| TpmEventLogBlobHandler handler(testFilePath); |
| EXPECT_TRUE(handler.canHandleBlob(blobId)); |
| EXPECT_FALSE(handler.canHandleBlob("/other/blob")); |
| } |
| |
| TEST_F(TpmEventLogTest, GetBlobIdsReturnsCorrectPath) |
| { |
| TpmEventLogBlobHandler handler(testFilePath); |
| auto ids = handler.getBlobIds(); |
| EXPECT_EQ(ids.size(), 1); |
| EXPECT_EQ(ids[0], blobId); |
| } |
| |
| TEST_F(TpmEventLogTest, StatReturnsCorrectSize) |
| { |
| std::string data = "test data"; |
| writeTestData(data); |
| |
| TpmEventLogBlobHandler handler(testFilePath); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(handler.stat(blobId, &meta)); |
| EXPECT_EQ(meta.size, data.size()); |
| EXPECT_EQ(meta.blobState, blobs::StateFlags::open_read); |
| } |
| |
| TEST_F(TpmEventLogTest, OpenSucceedsForReadOnly) |
| { |
| writeTestData("some data"); |
| TpmEventLogBlobHandler handler(testFilePath); |
| |
| EXPECT_TRUE(handler.open(1, blobs::OpenFlags::read, blobId)); |
| } |
| |
| TEST_F(TpmEventLogTest, OpenFailsForWrite) |
| { |
| writeTestData("some data"); |
| TpmEventLogBlobHandler handler(testFilePath); |
| |
| EXPECT_FALSE(handler.open(1, blobs::OpenFlags::write, blobId)); |
| EXPECT_FALSE( |
| handler.open(1, blobs::OpenFlags::read | blobs::OpenFlags::write, blobId)); |
| } |
| |
| TEST_F(TpmEventLogTest, OpenFailsForInvalidPath) |
| { |
| TpmEventLogBlobHandler handler(testFilePath); |
| EXPECT_FALSE(handler.open(1, blobs::OpenFlags::read, "/invalid/path")); |
| } |
| |
| TEST_F(TpmEventLogTest, ReadReturnsCorrectData) |
| { |
| std::string data = "0123456789"; |
| writeTestData(data); |
| |
| TpmEventLogBlobHandler handler(testFilePath); |
| uint16_t session = 1; |
| ASSERT_TRUE(handler.open(session, blobs::OpenFlags::read, blobId)); |
| |
| // Read full data |
| auto result = handler.read(session, 0, 10); |
| EXPECT_EQ(std::string(result.begin(), result.end()), data); |
| |
| // Read partial data |
| result = handler.read(session, 2, 3); |
| EXPECT_EQ(std::string(result.begin(), result.end()), "234"); |
| |
| // Read with offset beyond size |
| result = handler.read(session, 10, 1); |
| EXPECT_TRUE(result.empty()); |
| |
| // Read more than available |
| result = handler.read(session, 8, 10); |
| EXPECT_EQ(std::string(result.begin(), result.end()), "89"); |
| } |
| |
| TEST_F(TpmEventLogTest, SessionStatReturnsCorrectData) |
| { |
| std::string data = "hello world"; |
| writeTestData(data); |
| |
| TpmEventLogBlobHandler handler(testFilePath); |
| uint16_t session = 1; |
| ASSERT_TRUE(handler.open(session, blobs::OpenFlags::read, blobId)); |
| |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(handler.stat(session, &meta)); |
| EXPECT_EQ(meta.size, data.size()); |
| EXPECT_EQ(meta.blobState, blobs::StateFlags::open_read); |
| } |
| |
| TEST_F(TpmEventLogTest, CloseInvalidatesSession) |
| { |
| writeTestData("data"); |
| TpmEventLogBlobHandler handler(testFilePath); |
| uint16_t session = 1; |
| |
| ASSERT_TRUE(handler.open(session, blobs::OpenFlags::read, blobId)); |
| EXPECT_TRUE(handler.close(session)); |
| EXPECT_FALSE(handler.close(session)); // Already closed |
| |
| // Read should fail now |
| auto result = handler.read(session, 0, 4); |
| EXPECT_TRUE(result.empty()); |
| } |
| |
| } // namespace ipmi_hoth |