#include "buffer.hpp"
#include "data_interface_mock.hpp"

#include <boost/endian/arithmetic.hpp>
#include <boost/endian/conversion.hpp>

#include <algorithm>
#include <array>
#include <cstdint>
#include <memory>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace bios_bmc_smm_error_logger
{
namespace
{

using ::testing::_;
using ::testing::ElementsAreArray;
using ::testing::InSequence;
using ::testing::Return;

class BufferTest : public ::testing::Test
{
  protected:
    BufferTest() :
        dataInterfaceMock(std::make_unique<DataInterfaceMock>()),
        dataInterfaceMockPtr(dataInterfaceMock.get())
    {
        bufferImpl = std::make_unique<BufferImpl>(std::move(dataInterfaceMock));
        testInitializationHeader.bmcInterfaceVersion = testBmcInterfaceVersion;
        testInitializationHeader.queueSize = testQueueSize;
        testInitializationHeader.ueRegionSize = testUeRegionSize;
        std::transform(testMagicNumber.begin(), testMagicNumber.end(),
                       testInitializationHeader.magicNumber.begin(),
                       [](uint32_t number) -> little_uint32_t {
                           return boost::endian::native_to_little(number);
                       });
    }
    ~BufferTest() override = default;

    // CircularBufferHeader size is 0x30, ensure the test region is bigger
    static constexpr size_t testRegionSize = 0x200;
    static constexpr uint32_t testBmcInterfaceVersion = 123;
    static constexpr uint32_t testQueueSize = 0x200;
    static constexpr uint16_t testUeRegionSize = 0x50;
    static constexpr std::array<uint32_t, 4> testMagicNumber = {
        0x12345678, 0x22345678, 0x32345678, 0x42345678};
    static constexpr size_t bufferHeaderSize =
        sizeof(struct CircularBufferHeader);

    struct CircularBufferHeader testInitializationHeader{};

    std::unique_ptr<DataInterfaceMock> dataInterfaceMock;
    DataInterfaceMock* dataInterfaceMockPtr;
    std::unique_ptr<BufferImpl> bufferImpl;
};

TEST_F(BufferTest, BufferInitializeEraseFail)
{
    InSequence s;
    EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
        .WillOnce(Return(testRegionSize));
    EXPECT_THROW(
        try {
            // Test too big of a proposed buffer compared to the memori size
            uint16_t bigQueueSize = 0x201;
            uint16_t bigUeRegionSize = 0x50;
            bufferImpl->initialize(testBmcInterfaceVersion, bigQueueSize,
                                   bigUeRegionSize, testMagicNumber);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[initialize] Proposed queue size '513' is bigger than the BMC's allocated MMIO region of '512'");
            throw;
        },
        std::runtime_error);
    EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader);

    EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
        .WillOnce(Return(testRegionSize));
    const std::vector<uint8_t> emptyArray(testQueueSize, 0);
    // Return a smaller write than the intended testRegionSize to test the error
    EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
        .WillOnce(Return(testQueueSize - 1));
    EXPECT_THROW(
        try {
            bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
                                   testUeRegionSize, testMagicNumber);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(), "[initialize] Only erased '511'");
            throw;
        },
        std::runtime_error);
    EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader);

    EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
        .WillOnce(Return(testRegionSize));
    EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
        .WillOnce(Return(testQueueSize));
    // Return a smaller write than the intended initializationHeader to test the
    // error
    EXPECT_CALL(*dataInterfaceMockPtr, write(0, _)).WillOnce(Return(0));
    EXPECT_THROW(
        try {
            bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
                                   testUeRegionSize, testMagicNumber);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(),
                         "[initialize] Only wrote '0' bytes of the header");
            throw;
        },
        std::runtime_error);
    EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader);
}

TEST_F(BufferTest, BufferInitializePass)
{
    InSequence s;
    EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
        .WillOnce(Return(testRegionSize));
    const std::vector<uint8_t> emptyArray(testQueueSize, 0);
    EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
        .WillOnce(Return(testQueueSize));

    uint8_t* testInitializationHeaderPtr =
        reinterpret_cast<uint8_t*>(&testInitializationHeader);
    EXPECT_CALL(*dataInterfaceMockPtr,
                write(0, ElementsAreArray(testInitializationHeaderPtr,
                                          bufferHeaderSize)))
        .WillOnce(Return(bufferHeaderSize));
    EXPECT_NO_THROW(
        bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
                               testUeRegionSize, testMagicNumber));
    EXPECT_EQ(bufferImpl->getCachedBufferHeader(), testInitializationHeader);
}

TEST_F(BufferTest, BufferHeaderReadFail)
{
    std::vector<std::uint8_t> testBytesRead{};
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(testBytesRead));
    EXPECT_THROW(
        try {
            bufferImpl->readBufferHeader();
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(),
                         "Buffer header read only read '0', expected '48'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferTest, BufferHeaderReadPass)
{
    uint8_t* testInitializationHeaderPtr =
        reinterpret_cast<uint8_t*>(&testInitializationHeader);
    std::vector<uint8_t> testInitializationHeaderVector(
        testInitializationHeaderPtr,
        testInitializationHeaderPtr + bufferHeaderSize);

    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(testInitializationHeaderVector));
    EXPECT_NO_THROW(bufferImpl->readBufferHeader());
    EXPECT_EQ(bufferImpl->getCachedBufferHeader(), testInitializationHeader);
}

TEST_F(BufferTest, BufferUpdateReadPtrFail)
{
    // Return write size that is not 2 which is sizeof(little_uint16_t)
    constexpr size_t wrongWriteSize = 1;
    EXPECT_CALL(*dataInterfaceMockPtr, write(_, _))
        .WillOnce(Return(wrongWriteSize));
    EXPECT_THROW(
        try {
            bufferImpl->updateReadPtr(0);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[updateReadPtr] Wrote '1' bytes, instead of expected '3'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferTest, BufferUpdateReadPtrPass)
{
    constexpr size_t expectedWriteSize = 3;
    constexpr uint8_t expectedBmcReadPtrOffset = 0x21;
    // Check that we truncate the highest 24bits
    const uint32_t testNewReadPtr = 0x99881234;
    const std::vector<uint8_t> expectedReadPtr{0x34, 0x12, 0x88};

    EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset,
                                             ElementsAreArray(expectedReadPtr)))
        .WillOnce(Return(expectedWriteSize));
    EXPECT_NO_THROW(bufferImpl->updateReadPtr(testNewReadPtr));

    auto cachedHeader = bufferImpl->getCachedBufferHeader();
    EXPECT_EQ(boost::endian::little_to_native(cachedHeader.bmcReadPtr),
              0x881234);
}

TEST_F(BufferTest, BufferUpdateBmcFlagsFail)
{
    // Return write size that is not 4 which is sizeof(little_uint32_t)
    constexpr size_t wrongWriteSize = 1;
    EXPECT_CALL(*dataInterfaceMockPtr, write(_, _))
        .WillOnce(Return(wrongWriteSize));
    EXPECT_THROW(
        try {
            bufferImpl->updateBmcFlags(static_cast<uint32_t>(BmcFlags::ready));
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[updateBmcFlags] Wrote '1' bytes, instead of expected '4'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferTest, BufferUpdateBmcFlagsPass)
{
    constexpr size_t expectedWriteSize = 4;
    constexpr uint8_t expectedBmcReadPtrOffset = 0x1d;
    const std::vector<uint8_t> expectedNewBmcFlagsVector{0x04, 0x0, 0x0, 0x00};

    EXPECT_CALL(*dataInterfaceMockPtr,
                write(expectedBmcReadPtrOffset,
                      ElementsAreArray(expectedNewBmcFlagsVector)))
        .WillOnce(Return(expectedWriteSize));
    EXPECT_NO_THROW(
        bufferImpl->updateBmcFlags(static_cast<uint32_t>(BmcFlags::ready)));

    auto cachedHeader = bufferImpl->getCachedBufferHeader();
    EXPECT_EQ(boost::endian::little_to_native(cachedHeader.bmcFlags),
              static_cast<uint32_t>(BmcFlags::ready));
}

TEST_F(BufferTest, GetMaxOffsetQueueSizeFail)
{
    InSequence s;
    static constexpr size_t wrongQueueSize = testQueueSize - 1;
    EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
        .WillOnce(Return(testRegionSize));
    const std::vector<uint8_t> emptyArray(wrongQueueSize, 0);
    EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
        .WillOnce(Return(wrongQueueSize));

    EXPECT_CALL(*dataInterfaceMockPtr, write(0, _))
        .WillOnce(Return(bufferHeaderSize));
    EXPECT_NO_THROW(
        bufferImpl->initialize(testBmcInterfaceVersion, wrongQueueSize,
                               testUeRegionSize, testMagicNumber));
    EXPECT_THROW(
        try {
            bufferImpl->getMaxOffset();
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(),
                         "[getMaxOffset] runtime queueSize '511' did not match "
                         "compile-time queueSize '512'. This indicates that the"
                         " buffer was corrupted");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferTest, GetMaxOffsetUeRegionSizeFail)
{
    InSequence s;
    EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
        .WillOnce(Return(testRegionSize));
    const std::vector<uint8_t> emptyArray(testQueueSize, 0);
    EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
        .WillOnce(Return(testQueueSize));

    EXPECT_CALL(*dataInterfaceMockPtr, write(0, _))
        .WillOnce(Return(bufferHeaderSize));
    EXPECT_NO_THROW(
        bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
                               testUeRegionSize + 1, testMagicNumber));
    EXPECT_THROW(
        try {
            bufferImpl->getMaxOffset();
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[getMaxOffset] runtime ueRegionSize '81' did not match "
                "compile-time ueRegionSize '80'. This indicates that the"
                " buffer was corrupted");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferTest, GetOffsetUeRegionSizeFail)
{
    InSequence s;
    EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
        .WillOnce(Return(testRegionSize));
    const std::vector<uint8_t> emptyArray(testQueueSize, 0);
    EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
        .WillOnce(Return(testQueueSize));

    EXPECT_CALL(*dataInterfaceMockPtr, write(0, _))
        .WillOnce(Return(bufferHeaderSize));
    EXPECT_NO_THROW(
        bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
                               testUeRegionSize - 1, testMagicNumber));
    EXPECT_THROW(
        try {
            bufferImpl->getQueueOffset();
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[getQueueOffset] runtime ueRegionSize '79' did not match "
                "compile-time ueRegionSize '80'. This indicates that the"
                " buffer was corrupted");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferTest, ReadUeLog_NoUeRegionConfigured)
{
    struct CircularBufferHeader header = testInitializationHeader;
    header.ueRegionSize =
        boost::endian::native_to_little<uint16_t>(0); // No UE region

    uint8_t* headerPtr = reinterpret_cast<uint8_t*>(&header);
    std::vector<uint8_t> headerBytes(headerPtr, headerPtr + bufferHeaderSize);
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(headerBytes));

    auto result = bufferImpl->readUeLogFromReservedRegion();
    EXPECT_TRUE(result.empty());
}

TEST_F(BufferTest, ReadUeLog_NotPresentDueToFlags)
{
    struct CircularBufferHeader header = testInitializationHeader;
    header.ueRegionSize = boost::endian::native_to_little<uint16_t>(0x20);
    // Flags are the same, so no new UE log
    header.biosFlags = boost::endian::native_to_little<uint32_t>(
        static_cast<uint32_t>((BufferFlags::ueSwitch)));
    header.bmcFlags = boost::endian::native_to_little<uint32_t>(
        static_cast<uint32_t>(BufferFlags::ueSwitch));

    uint8_t* headerPtr = reinterpret_cast<uint8_t*>(&header);
    std::vector<uint8_t> headerBytes(headerPtr, headerPtr + bufferHeaderSize);
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(headerBytes));

    auto result = bufferImpl->readUeLogFromReservedRegion();
    EXPECT_TRUE(result.empty());
}

TEST_F(BufferTest, ReadUeLog_PresentAndSuccessfullyRead)
{
    struct CircularBufferHeader header = testInitializationHeader;
    uint16_t ueSize = 0x20;
    header.ueRegionSize = boost::endian::native_to_little(ueSize);
    header.biosFlags = boost::endian::native_to_little<uint32_t>(
        static_cast<uint32_t>(BufferFlags::ueSwitch));
    header.bmcFlags =
        boost::endian::native_to_little<uint32_t>(0); // BIOS set, BMC not yet

    uint8_t* headerPtr = reinterpret_cast<uint8_t*>(&header);
    std::vector<uint8_t> headerBytes(headerPtr, headerPtr + bufferHeaderSize);
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(headerBytes));

    size_t ueRegionOffset = bufferHeaderSize;
    std::vector<uint8_t> ueData(ueSize, 0xAA);
    EXPECT_CALL(*dataInterfaceMockPtr, read(ueRegionOffset, ueSize))
        .WillOnce(Return(ueData));

    auto result = bufferImpl->readUeLogFromReservedRegion();
    ASSERT_FALSE(result.empty());
    EXPECT_THAT(result, ElementsAreArray(ueData));

    // The initial bmcFlags (0) should remain unchanged in the cache
    struct CircularBufferHeader cachedHeaderAfterRead =
        bufferImpl->getCachedBufferHeader();
    EXPECT_EQ(boost::endian::little_to_native(cachedHeaderAfterRead.bmcFlags),
              0);
}

TEST_F(BufferTest, ReadUeLog_PresentButReadFails)
{
    struct CircularBufferHeader header = testInitializationHeader;
    uint16_t ueSize = 0x20;
    header.ueRegionSize = boost::endian::native_to_little(ueSize);
    header.biosFlags = boost::endian::native_to_little<uint32_t>(
        static_cast<uint32_t>(BufferFlags::ueSwitch));
    header.bmcFlags = boost::endian::native_to_little<uint32_t>(0);

    uint8_t* headerPtr = reinterpret_cast<uint8_t*>(&header);
    std::vector<uint8_t> headerBytes(headerPtr, headerPtr + bufferHeaderSize);
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(headerBytes));

    size_t ueRegionOffset = bufferHeaderSize;
    std::vector<uint8_t> shortUeData(ueSize - 1, 0xAA); // Short read
    EXPECT_CALL(*dataInterfaceMockPtr, read(ueRegionOffset, ueSize))
        .WillOnce(Return(shortUeData));

    // Expect an exception due to short read, which is treated as corruption for
    // UE log
    EXPECT_THROW(
        try {
            bufferImpl->readUeLogFromReservedRegion();
        } catch (const std::runtime_error& e) {
            EXPECT_THAT(e.what(),
                        ::testing::HasSubstr("Failed to read full UE log"));
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferTest, CheckOverflow_NotPresentDueToFlags)
{
    struct CircularBufferHeader header = testInitializationHeader;
    // Flags are the same, so no new overflow
    header.biosFlags = boost::endian::native_to_little<uint32_t>(
        static_cast<uint32_t>(BufferFlags::overflow));
    header.bmcFlags = boost::endian::native_to_little<uint32_t>(
        static_cast<uint32_t>(BufferFlags::overflow));

    uint8_t* headerPtr = reinterpret_cast<uint8_t*>(&header);
    std::vector<uint8_t> headerBytes(headerPtr, headerPtr + bufferHeaderSize);
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(headerBytes));

    bool overflowDetected = bufferImpl->checkForOverflowAndAcknowledge();
    ASSERT_FALSE(overflowDetected);
}

TEST_F(BufferTest, CheckOverflow_PresentAndAcknowledged)
{
    struct CircularBufferHeader header = testInitializationHeader;
    header.biosFlags = boost::endian::native_to_little<uint32_t>(
        static_cast<uint32_t>(BufferFlags::overflow));
    header.bmcFlags =
        boost::endian::native_to_little<uint32_t>(0); // BIOS set, BMC not yet

    uint8_t* headerPtr = reinterpret_cast<uint8_t*>(&header);
    std::vector<uint8_t> headerBytes(headerPtr, headerPtr + bufferHeaderSize);
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(headerBytes));

    uint32_t expectedNewBmcFlags =
        static_cast<uint32_t>(BufferFlags::overflow); // BMC toggles its bit
    little_uint32_t littleExpectedNewBmcFlags =
        boost::endian::native_to_little(expectedNewBmcFlags);
    uint8_t* flagPtr = reinterpret_cast<uint8_t*>(&littleExpectedNewBmcFlags);
    std::vector<uint8_t> expectedFlagWrite(flagPtr,
                                           flagPtr + sizeof(little_uint32_t));
    constexpr uint8_t bmcFlagsOffset =
        offsetof(struct CircularBufferHeader, bmcFlags);

    EXPECT_CALL(*dataInterfaceMockPtr,
                write(bmcFlagsOffset, ElementsAreArray(expectedFlagWrite)))
        .WillOnce(Return(sizeof(little_uint32_t)));

    bool overflowDetected = bufferImpl->checkForOverflowAndAcknowledge();
    ASSERT_TRUE(overflowDetected);

    struct CircularBufferHeader updatedCachedHeader =
        bufferImpl->getCachedBufferHeader();
    EXPECT_EQ(boost::endian::little_to_native(updatedCachedHeader.bmcFlags),
              expectedNewBmcFlags);
}

class BufferWraparoundReadTest : public BufferTest
{
  protected:
    BufferWraparoundReadTest()
    {
        initializeFuncMock();
    }
    void initializeFuncMock()
    {
        // Initialize the memory and the cachedBufferHeader
        InSequence s;
        EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
            .WillOnce(Return(testRegionSize));
        const std::vector<uint8_t> emptyArray(testQueueSize, 0);
        EXPECT_CALL(*dataInterfaceMockPtr,
                    write(0, ElementsAreArray(emptyArray)))
            .WillOnce(Return(testQueueSize));

        EXPECT_CALL(*dataInterfaceMockPtr, write(0, _))
            .WillOnce(Return(bufferHeaderSize));
        EXPECT_NO_THROW(
            bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
                                   testUeRegionSize, testMagicNumber));
    }
    static constexpr size_t expectedWriteSize = 3;
    static constexpr uint8_t expectedBmcReadPtrOffset = 0x21;
    static constexpr size_t expectedqueueOffset = 0x30 + testUeRegionSize;

    static constexpr size_t testMaxOffset =
        testQueueSize - testUeRegionSize - sizeof(struct CircularBufferHeader);
    uint8_t* testInitializationHeaderPtr =
        reinterpret_cast<uint8_t*>(&testInitializationHeader);
};

TEST_F(BufferWraparoundReadTest, GetMaxOffsetPassTest)
{
    EXPECT_EQ(bufferImpl->getMaxOffset(), testMaxOffset);
}

TEST_F(BufferWraparoundReadTest, GetQueueOffsetPassTest)
{
    EXPECT_EQ(bufferImpl->getQueueOffset(),
              bufferHeaderSize + testUeRegionSize);
}

TEST_F(BufferWraparoundReadTest, ParamsTooBigFail)
{
    InSequence s;
    size_t tooBigOffset = testMaxOffset + 1;
    EXPECT_THROW(
        try {
            bufferImpl->wraparoundRead(tooBigOffset, /* length */ 1);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[wraparoundRead] relativeOffset '385' was bigger than maxOffset '384'");
            throw;
        },
        std::runtime_error);

    size_t tooBigLength = testMaxOffset + 1;
    EXPECT_THROW(
        try {
            bufferImpl->wraparoundRead(/* relativeOffset */ 0, tooBigLength);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(), "[wraparoundRead] length '385' was bigger "
                                   "than maxOffset '384'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferWraparoundReadTest, NoWrapAroundReadFails)
{
    InSequence s;
    size_t testLength = 0x10;
    size_t testOffset = 0x20;

    // Fail the first read
    std::vector<std::uint8_t> shortTestBytesRead(testLength - 1);
    EXPECT_CALL(*dataInterfaceMockPtr,
                read(testOffset + expectedqueueOffset, testLength))
        .WillOnce(Return(shortTestBytesRead));

    EXPECT_THROW(
        try {
            bufferImpl->wraparoundRead(testOffset, testLength);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(),
                         "[wraparoundRead] Read '15' which was not the "
                         "requested length of '16'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferWraparoundReadTest, NoWrapAroundReadPass)
{
    InSequence s;
    size_t testLength = 0x10;
    size_t testOffset = 0x20;

    // Successfully read all the requested length without a wrap around
    std::vector<std::uint8_t> testBytesRead(testLength);
    EXPECT_CALL(*dataInterfaceMockPtr,
                read(testOffset + expectedqueueOffset, testLength))
        .WillOnce(Return(testBytesRead));

    // Call to updateReadPtr is triggered
    const std::vector<uint8_t> expectedReadPtr{
        static_cast<uint8_t>(testOffset + testLength), 0x0, 0x0};
    EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset,
                                             ElementsAreArray(expectedReadPtr)))
        .WillOnce(Return(expectedWriteSize));

    EXPECT_THAT(bufferImpl->wraparoundRead(testOffset, testLength),
                ElementsAreArray(testBytesRead));
    struct CircularBufferHeader cachedBufferHeader =
        bufferImpl->getCachedBufferHeader();
    // The bmcReadPtr should have been updated
    EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
              testOffset + testLength);
}

TEST_F(BufferWraparoundReadTest, WrapAroundReadFails)
{
    InSequence s;
    size_t testBytesLeft = 3;
    size_t testLength = 0x10;
    size_t testOffset = testMaxOffset - (testLength - testBytesLeft);

    // Read until the end of the queue
    std::vector<std::uint8_t> testBytesReadShort(testLength - testBytesLeft);
    EXPECT_CALL(*dataInterfaceMockPtr, read(testOffset + expectedqueueOffset,
                                            testLength - testBytesLeft))
        .WillOnce(Return(testBytesReadShort));

    // Read 1 byte short after wraparound
    std::vector<std::uint8_t> testBytesLeftReadShort(testBytesLeft - 1);
    EXPECT_CALL(*dataInterfaceMockPtr, read(expectedqueueOffset, testBytesLeft))
        .WillOnce(Return(testBytesLeftReadShort));

    EXPECT_THROW(
        try {
            bufferImpl->wraparoundRead(testOffset, testLength);
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[wraparoundRead] Buffer wrapped around but read '2' which was "
                "not the requested lenght of '3'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferWraparoundReadTest, WrapAroundReadPasses)
{
    InSequence s;
    size_t testBytesLeft = 3;
    size_t testLength = 0x10;
    size_t testOffset = testMaxOffset - (testLength - testBytesLeft);

    // Read to the end of the queue
    std::vector<std::uint8_t> testBytesReadFirst{16, 15, 14, 13, 12, 11, 10,
                                                 9,  8,  7,  6,  5,  4};
    EXPECT_CALL(*dataInterfaceMockPtr, read(testOffset + expectedqueueOffset,
                                            testLength - testBytesLeft))
        .WillOnce(Return(testBytesReadFirst));

    std::vector<std::uint8_t> testBytesReadSecond{3, 2, 1};
    EXPECT_CALL(*dataInterfaceMockPtr, read(expectedqueueOffset, testBytesLeft))
        .WillOnce(Return(testBytesReadSecond));

    // Call to updateReadPtr is triggered
    const std::vector<uint8_t> expectedReadPtr{
        static_cast<uint8_t>(testBytesLeft), 0x0, 0x0};
    EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset,
                                             ElementsAreArray(expectedReadPtr)))
        .WillOnce(Return(expectedWriteSize));

    std::vector<std::uint8_t> expectedBytes = {16, 15, 14, 13, 12, 11, 10, 9,
                                               8,  7,  6,  5,  4,  3,  2,  1};
    EXPECT_THAT(bufferImpl->wraparoundRead(testOffset, testLength),
                ElementsAreArray(expectedBytes));
    struct CircularBufferHeader cachedBufferHeader =
        bufferImpl->getCachedBufferHeader();
    // The bmcReadPtr should have been updated to reflect the wraparound
    EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
              testBytesLeft);
}

TEST_F(BufferWraparoundReadTest, WrapAroundCornerCasePass)
{
    InSequence s;
    size_t testBytesLeft = 0;
    size_t testLength = 4;
    size_t testOffset = testMaxOffset - (testLength - testBytesLeft);

    // Read to the very end of the queue
    std::vector<std::uint8_t> testBytes{4, 3, 2, 1};
    EXPECT_CALL(*dataInterfaceMockPtr,
                read(testOffset + expectedqueueOffset, testLength))
        .WillOnce(Return(testBytes));

    // Call to updateReadPtr is triggered, since we read to the very end of the
    // buffer, update the readPtr up around to 0
    const std::vector<uint8_t> expectedReadPtr{0x0, 0x0, 0x0};
    EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset,
                                             ElementsAreArray(expectedReadPtr)))
        .WillOnce(Return(expectedWriteSize));

    EXPECT_THAT(bufferImpl->wraparoundRead(testOffset, testLength),
                ElementsAreArray(testBytes));
    struct CircularBufferHeader cachedBufferHeader =
        bufferImpl->getCachedBufferHeader();
    // The bmcReadPtr should have been updated to reflect the wraparound
    EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
              0);
}

class BufferEntryTest : public BufferWraparoundReadTest
{
  protected:
    BufferEntryTest()
    {
        testEntryHeader.sequenceId = testSequenceId;
        testEntryHeader.entrySize = testEntrySize;
        testEntryHeader.checksum = testChecksum;
        testEntryHeader.rdeCommandType = testRdeCommandType;
    }
    ~BufferEntryTest() override = default;

    void wraparoundReadMock(const uint32_t relativeOffset,
                            std::span<std::uint8_t> expetedBytesOutput)
    {
        InSequence s;
        const uint32_t queueSizeToQueueEnd = testMaxOffset - relativeOffset;

        // This will wrap, split the read mocks in 2
        if (expetedBytesOutput.size() > queueSizeToQueueEnd)
        {
            EXPECT_CALL(*dataInterfaceMockPtr, read(_, _))
                .WillOnce(Return(std::vector<std::uint8_t>(
                    expetedBytesOutput.begin(),
                    expetedBytesOutput.begin() + queueSizeToQueueEnd)));
            EXPECT_CALL(*dataInterfaceMockPtr, read(_, _))
                .WillOnce(Return(std::vector<std::uint8_t>(
                    expetedBytesOutput.begin() + queueSizeToQueueEnd,
                    expetedBytesOutput.end())));
        }
        else
        {
            EXPECT_CALL(*dataInterfaceMockPtr, read(_, _))
                .WillOnce(Return(std::vector<std::uint8_t>(
                    expetedBytesOutput.begin(), expetedBytesOutput.end())));
        }

        EXPECT_CALL(*dataInterfaceMockPtr, write(_, _))
            .WillOnce(Return(expectedWriteSize));
    }

    static constexpr size_t entryHeaderSize = sizeof(struct QueueEntryHeader);
    static constexpr uint16_t testSequenceId = 0;
    static constexpr uint16_t testEntrySize = 0x20;
    static constexpr uint8_t testRdeCommandType = 0x01;
    // Calculated checksum for the header
    static constexpr uint8_t testChecksum =
        (testSequenceId ^ testEntrySize ^ testRdeCommandType);
    size_t testOffset = 0x0;

    struct QueueEntryHeader testEntryHeader{};
};

TEST_F(BufferEntryTest, ReadEntryHeaderPass)
{
    uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader);
    std::vector<uint8_t> testEntryHeaderVector(
        testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize);
    wraparoundReadMock(testOffset, testEntryHeaderVector);
    EXPECT_EQ(bufferImpl->readEntryHeader(), testEntryHeader);
    // Check the bmcReadPtr
    struct CircularBufferHeader cachedBufferHeader =
        bufferImpl->getCachedBufferHeader();
    EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
              testOffset + testEntryHeaderVector.size());
}

TEST_F(BufferEntryTest, ReadEntryChecksumFail)
{
    InSequence s;
    std::vector<uint8_t> testEntryVector(testEntrySize);
    // Offset the checksum by 1
    testEntryHeader.checksum += 1;
    uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader);
    std::vector<uint8_t> testEntryHeaderVector(
        testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize);
    wraparoundReadMock(testOffset, testEntryHeaderVector);
    wraparoundReadMock(testOffset + entryHeaderSize, testEntryVector);
    EXPECT_THROW(
        try { bufferImpl->readEntry(); } catch (const std::runtime_error& e) {
            // Calculation: testChecksum (0x21) XOR (0x22) = 3
            EXPECT_STREQ(e.what(),
                         "[readEntry] Checksum was '3', expected '0'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferEntryTest, ReadEntryPassWraparound)
{
    InSequence s;
    // We expect this will bump checksum up by "testEntrySize" = 0xff ^ 0xff ...
    // (20 times) = 0 therefore leave the checksum as is
    std::vector<uint8_t> testEntryVector(testEntrySize, 0xff);
    uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader);
    std::vector<uint8_t> testEntryHeaderVector(
        testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize);
    // Set testOffset so that we can test the wraparound here at the header and
    // update the readPtr
    testOffset = testMaxOffset - 1;
    EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset, _))
        .WillOnce(Return(expectedWriteSize));
    EXPECT_NO_THROW(bufferImpl->updateReadPtr(testOffset));

    wraparoundReadMock(testOffset, testEntryHeaderVector);
    wraparoundReadMock(testOffset + entryHeaderSize, testEntryVector);

    EntryPair testedEntryPair;
    EXPECT_NO_THROW(testedEntryPair = bufferImpl->readEntry());
    EXPECT_EQ(testedEntryPair.first, testEntryHeader);
    EXPECT_THAT(testedEntryPair.second, ElementsAreArray(testEntryVector));
    struct CircularBufferHeader cachedBufferHeader =
        bufferImpl->getCachedBufferHeader();
    // The bmcReadPtr should have been updated to reflect the wraparound
    EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
              entryHeaderSize + testEntrySize - 1);

    // Set testOffset so that we can test the wraparound here as well on our
    // second read for the entry (by 1 byte)
    testOffset = testMaxOffset - entryHeaderSize - 1;
    EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset, _))
        .WillOnce(Return(expectedWriteSize));
    EXPECT_NO_THROW(bufferImpl->updateReadPtr(testOffset));

    wraparoundReadMock(testOffset, testEntryHeaderVector);
    wraparoundReadMock(testOffset + entryHeaderSize, testEntryVector);

    EXPECT_NO_THROW(testedEntryPair = bufferImpl->readEntry());
    EXPECT_EQ(testedEntryPair.first, testEntryHeader);
    EXPECT_THAT(testedEntryPair.second, ElementsAreArray(testEntryVector));
    cachedBufferHeader = bufferImpl->getCachedBufferHeader();
    // The bmcReadPtr should have been updated to reflect the wraparound
    EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr),
              testEntrySize - 1);
}

class BufferReadErrorLogsTest : public BufferEntryTest
{
  protected:
    BufferReadErrorLogsTest() = default;

    uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader);
    size_t entryAndHeaderSize = entryHeaderSize + testEntrySize;
};

TEST_F(BufferReadErrorLogsTest, PtrsTooBigFail)
{
    InSequence s;
    // Set the biosWritePtr too big
    testInitializationHeader.biosWritePtr =
        boost::endian::native_to_little((testMaxOffset + 1));
    initializeFuncMock();

    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(std::vector<uint8_t>(
            testInitializationHeaderPtr,
            testInitializationHeaderPtr + bufferHeaderSize)));
    EXPECT_THROW(
        try {
            bufferImpl->readErrorLogs();
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(),
                         "[readErrorLogs] currentBiosWritePtr was '385' "
                         "which was bigger than maxOffset '384'");
            throw;
        },
        std::runtime_error);

    // Reset the biosWritePtr and set the bmcReadPtr too big
    testInitializationHeader.biosWritePtr = 0;
    initializeFuncMock();
    testInitializationHeader.bmcReadPtr =
        boost::endian::native_to_little((testMaxOffset + 1));
    initializeFuncMock();

    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(std::vector<uint8_t>(
            testInitializationHeaderPtr,
            testInitializationHeaderPtr + bufferHeaderSize)));
    EXPECT_THROW(
        try {
            bufferImpl->readErrorLogs();
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(e.what(), "[readErrorLogs] currentReadPtr was '385' "
                                   "which was bigger than maxOffset '384'");
            throw;
        },
        std::runtime_error);
}

TEST_F(BufferReadErrorLogsTest, IdenticalPtrsPass)
{
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(std::vector<uint8_t>(
            testInitializationHeaderPtr,
            testInitializationHeaderPtr + bufferHeaderSize)));
    EXPECT_NO_THROW(bufferImpl->readErrorLogs());
}

TEST_F(BufferReadErrorLogsTest, NoWraparoundPass)
{
    InSequence s;
    // Set the biosWritePtr to 1 entryHeader + entry size
    testInitializationHeader.biosWritePtr =
        boost::endian::native_to_little((entryAndHeaderSize));
    initializeFuncMock();
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(std::vector<uint8_t>(
            testInitializationHeaderPtr,
            testInitializationHeaderPtr + bufferHeaderSize)));
    std::vector<uint8_t> testEntryHeaderVector(
        testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize);
    std::vector<uint8_t> testEntryVector(testEntrySize);
    wraparoundReadMock(/*relativeOffset=*/0, testEntryHeaderVector);
    wraparoundReadMock(/*relativeOffset=*/0 + entryHeaderSize, testEntryVector);

    std::vector<EntryPair> entryPairs;
    EXPECT_NO_THROW(entryPairs = bufferImpl->readErrorLogs());

    // Check that we only read one entryPair and that the content is correct
    EXPECT_EQ(entryPairs.size(), 1U);
    EXPECT_EQ(entryPairs[0].first, testEntryHeader);
    EXPECT_THAT(entryPairs[0].second, ElementsAreArray(testEntryVector));
}

TEST_F(BufferReadErrorLogsTest, WraparoundMultiplEntryPass)
{
    InSequence s;
    // Set the bmcReadPtr to 1 entryHeader + entry size from the "end" exactly
    uint32_t entryAndHeaderSizeAwayFromEnd = testMaxOffset - entryAndHeaderSize;
    testInitializationHeader.bmcReadPtr =
        boost::endian::native_to_little(entryAndHeaderSizeAwayFromEnd);
    // Set the biosWritePtr to 1 entryHeader + entry size from the "beginning"
    testInitializationHeader.biosWritePtr = entryAndHeaderSize;
    initializeFuncMock();
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(std::vector<uint8_t>(
            testInitializationHeaderPtr,
            testInitializationHeaderPtr + bufferHeaderSize)));

    std::vector<uint8_t> testEntryHeaderVector(
        testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize);
    std::vector<uint8_t> testEntryVector(testEntrySize);
    wraparoundReadMock(/*relativeOffset=*/entryAndHeaderSizeAwayFromEnd,
                       testEntryHeaderVector);
    wraparoundReadMock(/*relativeOffset=*/entryAndHeaderSizeAwayFromEnd +
                           entryHeaderSize,
                       testEntryVector);
    wraparoundReadMock(/*relativeOffset=*/0 + entryAndHeaderSize,
                       testEntryHeaderVector);
    wraparoundReadMock(/*relativeOffset=*/0 + entryAndHeaderSize +
                           entryHeaderSize,
                       testEntryVector);

    std::vector<EntryPair> entryPairs;
    EXPECT_NO_THROW(entryPairs = bufferImpl->readErrorLogs());

    // Check that we only read one entryPair and that the content is correct
    EXPECT_EQ(entryPairs.size(), 2);
    EXPECT_EQ(entryPairs[0].first, testEntryHeader);
    EXPECT_EQ(entryPairs[1].first, testEntryHeader);
    EXPECT_THAT(entryPairs[0].second, ElementsAreArray(testEntryVector));
    EXPECT_THAT(entryPairs[1].second, ElementsAreArray(testEntryVector));
}

TEST_F(BufferReadErrorLogsTest, WraparoundMismatchingPtrsFail)
{
    InSequence s;
    testInitializationHeader.bmcReadPtr = boost::endian::native_to_little(0);
    // Make the biosWritePtr intentially 1 smaller than expected
    testInitializationHeader.biosWritePtr =
        boost::endian::native_to_little(entryAndHeaderSize - 1);
    initializeFuncMock();
    EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
        .WillOnce(Return(std::vector<uint8_t>(
            testInitializationHeaderPtr,
            testInitializationHeaderPtr + bufferHeaderSize)));

    std::vector<uint8_t> testEntryHeaderVector(
        testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize);
    std::vector<uint8_t> testEntryVector(testEntrySize);
    wraparoundReadMock(/*relativeOffset=*/0, testEntryHeaderVector);
    wraparoundReadMock(/*relativeOffset=*/0 + entryHeaderSize, testEntryVector);

    EXPECT_THROW(
        try {
            bufferImpl->readErrorLogs();
        } catch (const std::runtime_error& e) {
            EXPECT_STREQ(
                e.what(),
                "[readErrorLogs] biosWritePtr '37' and bmcReaddPtr '38' "
                "are not identical after reading through all the logs");
            throw;
        },
        std::runtime_error);
}

} // namespace
} // namespace bios_bmc_smm_error_logger
