blob: e8943a9a45bb1d69dde334cd63d50909ad81e96d [file] [log] [blame]
Brandon Kimfcbc3db2022-06-06 21:26:18 -07001#include "buffer.hpp"
2#include "data_interface_mock.hpp"
3
Brandon Kim17ee1a92022-06-07 21:04:08 -07004#include <boost/endian/arithmetic.hpp>
5#include <boost/endian/conversion.hpp>
6
7#include <algorithm>
Brandon Kimfcbc3db2022-06-06 21:26:18 -07008#include <array>
9#include <cstdint>
10#include <memory>
11
12#include <gmock/gmock.h>
13#include <gtest/gtest.h>
14
15namespace bios_bmc_smm_error_logger
16{
17namespace
18{
19
20using ::testing::_;
21using ::testing::ElementsAreArray;
22using ::testing::InSequence;
23using ::testing::Return;
24
25class BufferTest : public ::testing::Test
26{
27 protected:
28 BufferTest() :
29 dataInterfaceMock(std::make_unique<DataInterfaceMock>()),
30 dataInterfaceMockPtr(dataInterfaceMock.get())
31 {
32 bufferImpl = std::make_unique<BufferImpl>(std::move(dataInterfaceMock));
33 testInitializationHeader.bmcInterfaceVersion = testBmcInterfaceVersion;
34 testInitializationHeader.queueSize = testQueueSize;
35 testInitializationHeader.ueRegionSize = testUeRegionSize;
Brandon Kim17ee1a92022-06-07 21:04:08 -070036 std::transform(testMagicNumber.begin(), testMagicNumber.end(),
37 testInitializationHeader.magicNumber.begin(),
38 [](uint32_t number) -> little_uint32_t {
39 return boost::endian::native_to_little(number);
40 });
Brandon Kimfcbc3db2022-06-06 21:26:18 -070041 }
42 ~BufferTest() override = default;
43
44 // CircularBufferHeader size is 0x30, ensure the test region is bigger
45 static constexpr size_t testRegionSize = 0x200;
46 static constexpr uint32_t testBmcInterfaceVersion = 123;
47 static constexpr uint16_t testQueueSize = 0x100;
48 static constexpr uint16_t testUeRegionSize = 0x50;
49 static constexpr std::array<uint32_t, 4> testMagicNumber = {
50 0x12345678, 0x22345678, 0x32345678, 0x42345678};
Brandon Kim17ee1a92022-06-07 21:04:08 -070051 static constexpr size_t bufferHeaderSize =
52 sizeof(struct CircularBufferHeader);
53
Brandon Kimfcbc3db2022-06-06 21:26:18 -070054 struct CircularBufferHeader testInitializationHeader
55 {};
56
57 std::unique_ptr<DataInterfaceMock> dataInterfaceMock;
58 DataInterfaceMock* dataInterfaceMockPtr;
Brandon Kimfcbc3db2022-06-06 21:26:18 -070059 std::unique_ptr<BufferImpl> bufferImpl;
60};
61
62TEST_F(BufferTest, BufferInitializeEraseFail)
63{
64 InSequence s;
65
66 EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
67 .WillOnce(Return(testRegionSize));
68 const std::vector<uint8_t> emptyArray(testRegionSize, 0);
69 // Return a smaller write than the intended testRegionSize to test the error
70 EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
71 .WillOnce(Return(testRegionSize - 1));
72 EXPECT_THROW(
73 try {
74 bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
75 testUeRegionSize, testMagicNumber);
76 } catch (const std::runtime_error& e) {
77 EXPECT_STREQ(e.what(), "Buffer initialization only erased '511'");
78 throw;
79 },
80 std::runtime_error);
Brandon Kim60cab572022-06-15 14:20:05 -070081 EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader);
Brandon Kimfcbc3db2022-06-06 21:26:18 -070082
83 EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
84 .WillOnce(Return(testRegionSize));
85 EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
86 .WillOnce(Return(testRegionSize));
87 // Return a smaller write than the intended initializationHeader to test the
88 // error
89 EXPECT_CALL(*dataInterfaceMockPtr, write(0, _)).WillOnce(Return(0));
90 EXPECT_THROW(
91 try {
92 bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize,
93 testUeRegionSize, testMagicNumber);
94 } catch (const std::runtime_error& e) {
95 EXPECT_STREQ(
96 e.what(),
97 "Buffer initialization buffer header write only wrote '0'");
98 throw;
99 },
100 std::runtime_error);
Brandon Kim60cab572022-06-15 14:20:05 -0700101 EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader);
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700102}
103
104TEST_F(BufferTest, BufferInitializePass)
105{
106 InSequence s;
107 EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize())
108 .WillOnce(Return(testRegionSize));
109 const std::vector<uint8_t> emptyArray(testRegionSize, 0);
110 EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray)))
111 .WillOnce(Return(testRegionSize));
112
113 uint8_t* testInitializationHeaderPtr =
114 reinterpret_cast<uint8_t*>(&testInitializationHeader);
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700115 EXPECT_CALL(*dataInterfaceMockPtr,
116 write(0, ElementsAreArray(testInitializationHeaderPtr,
Brandon Kim17ee1a92022-06-07 21:04:08 -0700117 bufferHeaderSize)))
118 .WillOnce(Return(bufferHeaderSize));
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700119 EXPECT_NO_THROW(bufferImpl->initialize(testBmcInterfaceVersion,
120 testQueueSize, testUeRegionSize,
121 testMagicNumber));
Brandon Kim60cab572022-06-15 14:20:05 -0700122 EXPECT_EQ(bufferImpl->getCachedBufferHeader(), testInitializationHeader);
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700123}
124
Brandon Kim17ee1a92022-06-07 21:04:08 -0700125TEST_F(BufferTest, BufferHeaderReadFail)
126{
127 std::vector<std::uint8_t> testBytesRead{};
128 EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
129 .WillOnce(Return(testBytesRead));
130 EXPECT_THROW(
131 try {
132 bufferImpl->readBufferHeader();
133 } catch (const std::runtime_error& e) {
134 EXPECT_STREQ(e.what(),
135 "Buffer header read only read '0', expected '48'");
136 throw;
137 },
138 std::runtime_error);
139}
140
141TEST_F(BufferTest, BufferHeaderReadPass)
142{
143 uint8_t* testInitializationHeaderPtr =
144 reinterpret_cast<uint8_t*>(&testInitializationHeader);
145 std::vector<uint8_t> testInitializationHeaderVector(
146 testInitializationHeaderPtr,
147 testInitializationHeaderPtr + bufferHeaderSize);
148
149 EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize))
150 .WillOnce(Return(testInitializationHeaderVector));
151 EXPECT_NO_THROW(bufferImpl->readBufferHeader());
152 EXPECT_EQ(bufferImpl->getCachedBufferHeader(), testInitializationHeader);
153}
154
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700155} // namespace
156} // namespace bios_bmc_smm_error_logger