Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 1 | #include "buffer.hpp" |
| 2 | #include "data_interface_mock.hpp" |
| 3 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 4 | #include <boost/endian/arithmetic.hpp> |
| 5 | #include <boost/endian/conversion.hpp> |
| 6 | |
| 7 | #include <algorithm> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 8 | #include <array> |
| 9 | #include <cstdint> |
| 10 | #include <memory> |
| 11 | |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | namespace bios_bmc_smm_error_logger |
| 16 | { |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | using ::testing::_; |
| 21 | using ::testing::ElementsAreArray; |
| 22 | using ::testing::InSequence; |
| 23 | using ::testing::Return; |
| 24 | |
| 25 | class 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 Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 36 | 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 Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 41 | } |
| 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; |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 47 | static constexpr uint32_t testQueueSize = 0x100; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 48 | static constexpr uint16_t testUeRegionSize = 0x50; |
| 49 | static constexpr std::array<uint32_t, 4> testMagicNumber = { |
| 50 | 0x12345678, 0x22345678, 0x32345678, 0x42345678}; |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 51 | static constexpr size_t bufferHeaderSize = |
| 52 | sizeof(struct CircularBufferHeader); |
| 53 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 54 | struct CircularBufferHeader testInitializationHeader |
| 55 | {}; |
| 56 | |
| 57 | std::unique_ptr<DataInterfaceMock> dataInterfaceMock; |
| 58 | DataInterfaceMock* dataInterfaceMockPtr; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 59 | std::unique_ptr<BufferImpl> bufferImpl; |
| 60 | }; |
| 61 | |
| 62 | TEST_F(BufferTest, BufferInitializeEraseFail) |
| 63 | { |
| 64 | InSequence s; |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 65 | EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize()) |
| 66 | .WillOnce(Return(testRegionSize)); |
| 67 | EXPECT_THROW( |
| 68 | try { |
| 69 | // Test too big of a proposed buffer compared to the memori size |
| 70 | uint16_t bigQueueSize = 0x181; |
| 71 | uint16_t bigUeRegionSize = 0x50; |
| 72 | bufferImpl->initialize(testBmcInterfaceVersion, bigQueueSize, |
| 73 | bigUeRegionSize, testMagicNumber); |
| 74 | } catch (const std::runtime_error& e) { |
| 75 | EXPECT_STREQ( |
| 76 | e.what(), |
| 77 | "[initialize] Proposed region size '513' " |
| 78 | "is bigger than the BMC's allocated MMIO region of '512'"); |
| 79 | throw; |
| 80 | }, |
| 81 | std::runtime_error); |
| 82 | EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 83 | |
| 84 | EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize()) |
| 85 | .WillOnce(Return(testRegionSize)); |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 86 | size_t testProposedBufferSize = |
| 87 | sizeof(struct CircularBufferHeader) + testUeRegionSize + testQueueSize; |
| 88 | const std::vector<uint8_t> emptyArray(testProposedBufferSize, 0); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 89 | // Return a smaller write than the intended testRegionSize to test the error |
| 90 | EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray))) |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 91 | .WillOnce(Return(testProposedBufferSize - 1)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 92 | EXPECT_THROW( |
| 93 | try { |
| 94 | bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize, |
| 95 | testUeRegionSize, testMagicNumber); |
| 96 | } catch (const std::runtime_error& e) { |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 97 | EXPECT_STREQ(e.what(), "[initialize] Only erased '383'"); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 98 | throw; |
| 99 | }, |
| 100 | std::runtime_error); |
Brandon Kim | 60cab57 | 2022-06-15 14:20:05 -0700 | [diff] [blame] | 101 | EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 102 | |
| 103 | EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize()) |
| 104 | .WillOnce(Return(testRegionSize)); |
| 105 | EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray))) |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 106 | .WillOnce(Return(testProposedBufferSize)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 107 | // Return a smaller write than the intended initializationHeader to test the |
| 108 | // error |
| 109 | EXPECT_CALL(*dataInterfaceMockPtr, write(0, _)).WillOnce(Return(0)); |
| 110 | EXPECT_THROW( |
| 111 | try { |
| 112 | bufferImpl->initialize(testBmcInterfaceVersion, testQueueSize, |
| 113 | testUeRegionSize, testMagicNumber); |
| 114 | } catch (const std::runtime_error& e) { |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 115 | EXPECT_STREQ(e.what(), |
| 116 | "[initialize] Only wrote '0' bytes of the header"); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 117 | throw; |
| 118 | }, |
| 119 | std::runtime_error); |
Brandon Kim | 60cab57 | 2022-06-15 14:20:05 -0700 | [diff] [blame] | 120 | EXPECT_NE(bufferImpl->getCachedBufferHeader(), testInitializationHeader); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(BufferTest, BufferInitializePass) |
| 124 | { |
| 125 | InSequence s; |
| 126 | EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize()) |
| 127 | .WillOnce(Return(testRegionSize)); |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 128 | size_t testProposedBufferSize = |
| 129 | sizeof(struct CircularBufferHeader) + testUeRegionSize + testQueueSize; |
| 130 | const std::vector<uint8_t> emptyArray(testProposedBufferSize, 0); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 131 | EXPECT_CALL(*dataInterfaceMockPtr, write(0, ElementsAreArray(emptyArray))) |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 132 | .WillOnce(Return(testProposedBufferSize)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 133 | |
| 134 | uint8_t* testInitializationHeaderPtr = |
| 135 | reinterpret_cast<uint8_t*>(&testInitializationHeader); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 136 | EXPECT_CALL(*dataInterfaceMockPtr, |
| 137 | write(0, ElementsAreArray(testInitializationHeaderPtr, |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 138 | bufferHeaderSize))) |
| 139 | .WillOnce(Return(bufferHeaderSize)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 140 | EXPECT_NO_THROW(bufferImpl->initialize(testBmcInterfaceVersion, |
| 141 | testQueueSize, testUeRegionSize, |
| 142 | testMagicNumber)); |
Brandon Kim | 60cab57 | 2022-06-15 14:20:05 -0700 | [diff] [blame] | 143 | EXPECT_EQ(bufferImpl->getCachedBufferHeader(), testInitializationHeader); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 146 | TEST_F(BufferTest, BufferHeaderReadFail) |
| 147 | { |
| 148 | std::vector<std::uint8_t> testBytesRead{}; |
| 149 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 150 | .WillOnce(Return(testBytesRead)); |
| 151 | EXPECT_THROW( |
| 152 | try { |
| 153 | bufferImpl->readBufferHeader(); |
| 154 | } catch (const std::runtime_error& e) { |
| 155 | EXPECT_STREQ(e.what(), |
| 156 | "Buffer header read only read '0', expected '48'"); |
| 157 | throw; |
| 158 | }, |
| 159 | std::runtime_error); |
| 160 | } |
| 161 | |
| 162 | TEST_F(BufferTest, BufferHeaderReadPass) |
| 163 | { |
| 164 | uint8_t* testInitializationHeaderPtr = |
| 165 | reinterpret_cast<uint8_t*>(&testInitializationHeader); |
| 166 | std::vector<uint8_t> testInitializationHeaderVector( |
| 167 | testInitializationHeaderPtr, |
| 168 | testInitializationHeaderPtr + bufferHeaderSize); |
| 169 | |
| 170 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 171 | .WillOnce(Return(testInitializationHeaderVector)); |
| 172 | EXPECT_NO_THROW(bufferImpl->readBufferHeader()); |
| 173 | EXPECT_EQ(bufferImpl->getCachedBufferHeader(), testInitializationHeader); |
| 174 | } |
| 175 | |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 176 | TEST_F(BufferTest, BufferUpdateReadPtrFail) |
| 177 | { |
| 178 | // Return write size that is not 2 which is sizeof(little_uint16_t) |
| 179 | constexpr size_t wrongWriteSize = 1; |
| 180 | EXPECT_CALL(*dataInterfaceMockPtr, write(_, _)) |
| 181 | .WillOnce(Return(wrongWriteSize)); |
| 182 | EXPECT_THROW( |
| 183 | try { |
| 184 | bufferImpl->updateReadPtr(0); |
| 185 | } catch (const std::runtime_error& e) { |
| 186 | EXPECT_STREQ( |
| 187 | e.what(), |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 188 | "[updateReadPtr] Wrote '1' bytes, instead of expected '3'"); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 189 | throw; |
| 190 | }, |
| 191 | std::runtime_error); |
| 192 | } |
| 193 | |
| 194 | TEST_F(BufferTest, BufferUpdateReadPtrPass) |
| 195 | { |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 196 | constexpr size_t expectedWriteSize = 3; |
| 197 | constexpr uint8_t expectedBmcReadPtrOffset = 0x21; |
| 198 | // Check that we truncate the highest 24bits |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 199 | const uint32_t testNewReadPtr = 0x99881234; |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 200 | const std::vector<uint8_t> expectedReadPtr{0x34, 0x12, 0x88}; |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 201 | |
| 202 | EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset, |
| 203 | ElementsAreArray(expectedReadPtr))) |
| 204 | .WillOnce(Return(expectedWriteSize)); |
| 205 | EXPECT_NO_THROW(bufferImpl->updateReadPtr(testNewReadPtr)); |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 206 | |
| 207 | auto cachedHeader = bufferImpl->getCachedBufferHeader(); |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 208 | EXPECT_EQ(boost::endian::little_to_native(cachedHeader.bmcReadPtr), |
| 209 | 0x881234); |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST_F(BufferTest, BufferUpdateBmcFlagsFail) |
| 213 | { |
| 214 | // Return write size that is not 4 which is sizeof(little_uint32_t) |
| 215 | constexpr size_t wrongWriteSize = 1; |
| 216 | EXPECT_CALL(*dataInterfaceMockPtr, write(_, _)) |
| 217 | .WillOnce(Return(wrongWriteSize)); |
| 218 | EXPECT_THROW( |
| 219 | try { |
| 220 | bufferImpl->updateBmcFlags(static_cast<uint32_t>(BmcFlags::ready)); |
| 221 | } catch (const std::runtime_error& e) { |
| 222 | EXPECT_STREQ( |
| 223 | e.what(), |
| 224 | "[updateBmcFlags] Wrote '1' bytes, instead of expected '4'"); |
| 225 | throw; |
| 226 | }, |
| 227 | std::runtime_error); |
| 228 | } |
| 229 | |
| 230 | TEST_F(BufferTest, BufferUpdateBmcFlagsPass) |
| 231 | { |
| 232 | constexpr size_t expectedWriteSize = 4; |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 233 | constexpr uint8_t expectedBmcReadPtrOffset = 0x1d; |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 234 | const std::vector<uint8_t> expectedNewBmcFlagsVector{0x04, 0x0, 0x0, 0x00}; |
| 235 | |
| 236 | EXPECT_CALL(*dataInterfaceMockPtr, |
| 237 | write(expectedBmcReadPtrOffset, |
| 238 | ElementsAreArray(expectedNewBmcFlagsVector))) |
| 239 | .WillOnce(Return(expectedWriteSize)); |
| 240 | EXPECT_NO_THROW( |
| 241 | bufferImpl->updateBmcFlags(static_cast<uint32_t>(BmcFlags::ready))); |
| 242 | |
| 243 | auto cachedHeader = bufferImpl->getCachedBufferHeader(); |
| 244 | EXPECT_EQ(boost::endian::little_to_native(cachedHeader.bmcFlags), |
| 245 | static_cast<uint32_t>(BmcFlags::ready)); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 248 | class BufferWraparoundReadTest : public BufferTest |
| 249 | { |
| 250 | protected: |
| 251 | BufferWraparoundReadTest() |
| 252 | { |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 253 | initializeFuncMock(); |
| 254 | } |
| 255 | void initializeFuncMock() |
| 256 | { |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 257 | // Initialize the memory and the cachedBufferHeader |
| 258 | InSequence s; |
| 259 | EXPECT_CALL(*dataInterfaceMockPtr, getMemoryRegionSize()) |
| 260 | .WillOnce(Return(testRegionSize)); |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 261 | size_t testProposedBufferSize = sizeof(struct CircularBufferHeader) + |
| 262 | testUeRegionSize + testQueueSize; |
| 263 | const std::vector<uint8_t> emptyArray(testProposedBufferSize, 0); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 264 | EXPECT_CALL(*dataInterfaceMockPtr, |
| 265 | write(0, ElementsAreArray(emptyArray))) |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 266 | .WillOnce(Return(testProposedBufferSize)); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 267 | |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 268 | EXPECT_CALL(*dataInterfaceMockPtr, write(0, _)) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 269 | .WillOnce(Return(bufferHeaderSize)); |
| 270 | EXPECT_NO_THROW(bufferImpl->initialize(testBmcInterfaceVersion, |
| 271 | testQueueSize, testUeRegionSize, |
| 272 | testMagicNumber)); |
| 273 | } |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 274 | static constexpr size_t expectedWriteSize = 3; |
| 275 | static constexpr uint8_t expectedBmcReadPtrOffset = 0x21; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 276 | static constexpr size_t expectedqueueOffset = 0x30 + testUeRegionSize; |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 277 | |
| 278 | uint8_t* testInitializationHeaderPtr = |
| 279 | reinterpret_cast<uint8_t*>(&testInitializationHeader); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 280 | }; |
| 281 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 282 | TEST_F(BufferWraparoundReadTest, ParamsTooBigFail) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 283 | { |
| 284 | InSequence s; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 285 | |
| 286 | size_t tooBigOffset = testQueueSize + 1; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 287 | EXPECT_THROW( |
| 288 | try { |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 289 | bufferImpl->wraparoundRead(tooBigOffset, /* length */ 1); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 290 | } catch (const std::runtime_error& e) { |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 291 | EXPECT_STREQ( |
| 292 | e.what(), |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 293 | "[wraparoundRead] relativeOffset '257' was bigger than queueSize '256'"); |
| 294 | throw; |
| 295 | }, |
| 296 | std::runtime_error); |
| 297 | |
| 298 | size_t tooBigLength = testQueueSize + 1; |
| 299 | EXPECT_THROW( |
| 300 | try { |
| 301 | bufferImpl->wraparoundRead(/* relativeOffset */ 0, tooBigLength); |
| 302 | } catch (const std::runtime_error& e) { |
| 303 | EXPECT_STREQ(e.what(), "[wraparoundRead] length '257' was bigger " |
| 304 | "than queueSize '256'"); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 305 | throw; |
| 306 | }, |
| 307 | std::runtime_error); |
| 308 | } |
| 309 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 310 | TEST_F(BufferWraparoundReadTest, NoWrapAroundReadFails) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 311 | { |
| 312 | InSequence s; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 313 | size_t testLength = 0x10; |
| 314 | size_t testOffset = 0x20; |
| 315 | |
| 316 | // Fail the first read |
| 317 | std::vector<std::uint8_t> shortTestBytesRead(testLength - 1); |
| 318 | EXPECT_CALL(*dataInterfaceMockPtr, |
| 319 | read(testOffset + expectedqueueOffset, testLength)) |
| 320 | .WillOnce(Return(shortTestBytesRead)); |
| 321 | |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 322 | EXPECT_THROW( |
| 323 | try { |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 324 | bufferImpl->wraparoundRead(testOffset, testLength); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 325 | } catch (const std::runtime_error& e) { |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 326 | EXPECT_STREQ(e.what(), |
| 327 | "[wraparoundRead] Read '15' which was not the " |
| 328 | "requested length of '16'"); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 329 | throw; |
| 330 | }, |
| 331 | std::runtime_error); |
| 332 | } |
| 333 | |
| 334 | TEST_F(BufferWraparoundReadTest, NoWrapAroundReadPass) |
| 335 | { |
| 336 | InSequence s; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 337 | size_t testLength = 0x10; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 338 | size_t testOffset = 0x20; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 339 | |
| 340 | // Successfully read all the requested length without a wrap around |
| 341 | std::vector<std::uint8_t> testBytesRead(testLength); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 342 | EXPECT_CALL(*dataInterfaceMockPtr, |
| 343 | read(testOffset + expectedqueueOffset, testLength)) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 344 | .WillOnce(Return(testBytesRead)); |
| 345 | |
| 346 | // Call to updateReadPtr is triggered |
| 347 | const std::vector<uint8_t> expectedReadPtr{ |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 348 | static_cast<uint8_t>(testOffset + testLength), 0x0, 0x0}; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 349 | EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset, |
| 350 | ElementsAreArray(expectedReadPtr))) |
| 351 | .WillOnce(Return(expectedWriteSize)); |
| 352 | |
| 353 | EXPECT_THAT(bufferImpl->wraparoundRead(testOffset, testLength), |
| 354 | ElementsAreArray(testBytesRead)); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 355 | struct CircularBufferHeader cachedBufferHeader = |
| 356 | bufferImpl->getCachedBufferHeader(); |
| 357 | // The bmcReadPtr should have been updated |
| 358 | EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 359 | testOffset + testLength); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | TEST_F(BufferWraparoundReadTest, WrapAroundReadFails) |
| 363 | { |
| 364 | InSequence s; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 365 | size_t testBytesLeft = 3; |
| 366 | size_t testLength = 0x10; |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 367 | size_t testOffset = testQueueSize - (testLength - testBytesLeft); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 368 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 369 | // Read until the end of the queue |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 370 | std::vector<std::uint8_t> testBytesReadShort(testLength - testBytesLeft); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 371 | EXPECT_CALL(*dataInterfaceMockPtr, read(testOffset + expectedqueueOffset, |
| 372 | testLength - testBytesLeft)) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 373 | .WillOnce(Return(testBytesReadShort)); |
| 374 | |
| 375 | // Read 1 byte short after wraparound |
| 376 | std::vector<std::uint8_t> testBytesLeftReadShort(testBytesLeft - 1); |
| 377 | EXPECT_CALL(*dataInterfaceMockPtr, read(expectedqueueOffset, testBytesLeft)) |
| 378 | .WillOnce(Return(testBytesLeftReadShort)); |
| 379 | |
| 380 | EXPECT_THROW( |
| 381 | try { |
| 382 | bufferImpl->wraparoundRead(testOffset, testLength); |
| 383 | } catch (const std::runtime_error& e) { |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 384 | EXPECT_STREQ( |
| 385 | e.what(), |
| 386 | "[wraparoundRead] Buffer wrapped around but read '2' which was " |
| 387 | "not the requested lenght of '3'"); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 388 | throw; |
| 389 | }, |
| 390 | std::runtime_error); |
| 391 | } |
| 392 | |
| 393 | TEST_F(BufferWraparoundReadTest, WrapAroundReadPasses) |
| 394 | { |
| 395 | InSequence s; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 396 | size_t testBytesLeft = 3; |
| 397 | size_t testLength = 0x10; |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 398 | size_t testOffset = testQueueSize - (testLength - testBytesLeft); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 399 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 400 | // Read to the end of the queue |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 401 | std::vector<std::uint8_t> testBytesReadFirst{16, 15, 14, 13, 12, 11, 10, |
| 402 | 9, 8, 7, 6, 5, 4}; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 403 | EXPECT_CALL(*dataInterfaceMockPtr, read(testOffset + expectedqueueOffset, |
| 404 | testLength - testBytesLeft)) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 405 | .WillOnce(Return(testBytesReadFirst)); |
| 406 | |
| 407 | std::vector<std::uint8_t> testBytesReadSecond{3, 2, 1}; |
| 408 | EXPECT_CALL(*dataInterfaceMockPtr, read(expectedqueueOffset, testBytesLeft)) |
| 409 | .WillOnce(Return(testBytesReadSecond)); |
| 410 | |
| 411 | // Call to updateReadPtr is triggered |
| 412 | const std::vector<uint8_t> expectedReadPtr{ |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 413 | static_cast<uint8_t>(testBytesLeft), 0x0, 0x0}; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 414 | EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset, |
| 415 | ElementsAreArray(expectedReadPtr))) |
| 416 | .WillOnce(Return(expectedWriteSize)); |
| 417 | |
| 418 | std::vector<std::uint8_t> expectedBytes = {16, 15, 14, 13, 12, 11, 10, 9, |
| 419 | 8, 7, 6, 5, 4, 3, 2, 1}; |
| 420 | EXPECT_THAT(bufferImpl->wraparoundRead(testOffset, testLength), |
| 421 | ElementsAreArray(expectedBytes)); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 422 | struct CircularBufferHeader cachedBufferHeader = |
| 423 | bufferImpl->getCachedBufferHeader(); |
| 424 | // The bmcReadPtr should have been updated to reflect the wraparound |
| 425 | EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 426 | testBytesLeft); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 429 | TEST_F(BufferWraparoundReadTest, WrapAroundCornerCasePass) |
| 430 | { |
| 431 | InSequence s; |
| 432 | size_t testBytesLeft = 0; |
| 433 | size_t testLength = 4; |
| 434 | size_t testOffset = testQueueSize - (testLength - testBytesLeft); |
| 435 | |
| 436 | // Read to the very end of the queue |
| 437 | std::vector<std::uint8_t> testBytes{4, 3, 2, 1}; |
| 438 | EXPECT_CALL(*dataInterfaceMockPtr, |
| 439 | read(testOffset + expectedqueueOffset, testLength)) |
| 440 | .WillOnce(Return(testBytes)); |
| 441 | |
| 442 | // Call to updateReadPtr is triggered, since we read to the very end of the |
| 443 | // buffer, update the readPtr up around to 0 |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 444 | const std::vector<uint8_t> expectedReadPtr{0x0, 0x0, 0x0}; |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 445 | EXPECT_CALL(*dataInterfaceMockPtr, write(expectedBmcReadPtrOffset, |
| 446 | ElementsAreArray(expectedReadPtr))) |
| 447 | .WillOnce(Return(expectedWriteSize)); |
| 448 | |
| 449 | EXPECT_THAT(bufferImpl->wraparoundRead(testOffset, testLength), |
| 450 | ElementsAreArray(testBytes)); |
| 451 | struct CircularBufferHeader cachedBufferHeader = |
| 452 | bufferImpl->getCachedBufferHeader(); |
| 453 | // The bmcReadPtr should have been updated to reflect the wraparound |
| 454 | EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 455 | 0); |
| 456 | } |
| 457 | |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 458 | class BufferEntryTest : public BufferWraparoundReadTest |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 459 | { |
| 460 | protected: |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 461 | BufferEntryTest() |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 462 | { |
| 463 | testEntryHeader.sequenceId = testSequenceId; |
| 464 | testEntryHeader.entrySize = testEntrySize; |
| 465 | testEntryHeader.checksum = testChecksum; |
| 466 | testEntryHeader.rdeCommandType = testRdeCommandType; |
| 467 | } |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 468 | ~BufferEntryTest() override = default; |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 469 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 470 | void wraparoundReadMock(const uint32_t relativeOffset, |
| 471 | std::span<std::uint8_t> expetedBytesOutput) |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 472 | { |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 473 | InSequence s; |
| 474 | const uint32_t queueSizeToQueueEnd = testQueueSize - relativeOffset; |
| 475 | |
| 476 | // This will wrap, split the read mocks in 2 |
| 477 | if (expetedBytesOutput.size() > queueSizeToQueueEnd) |
| 478 | { |
| 479 | EXPECT_CALL(*dataInterfaceMockPtr, read(_, _)) |
| 480 | .WillOnce(Return(std::vector<std::uint8_t>( |
| 481 | expetedBytesOutput.begin(), |
| 482 | expetedBytesOutput.begin() + queueSizeToQueueEnd))); |
| 483 | EXPECT_CALL(*dataInterfaceMockPtr, read(_, _)) |
| 484 | .WillOnce(Return(std::vector<std::uint8_t>( |
| 485 | expetedBytesOutput.begin() + queueSizeToQueueEnd, |
| 486 | expetedBytesOutput.end()))); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | EXPECT_CALL(*dataInterfaceMockPtr, read(_, _)) |
| 491 | .WillOnce(Return(std::vector<std::uint8_t>( |
| 492 | expetedBytesOutput.begin(), expetedBytesOutput.end()))); |
| 493 | } |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 494 | |
| 495 | EXPECT_CALL(*dataInterfaceMockPtr, write(_, _)) |
| 496 | .WillOnce(Return(expectedWriteSize)); |
| 497 | } |
| 498 | |
| 499 | static constexpr size_t entryHeaderSize = sizeof(struct QueueEntryHeader); |
| 500 | static constexpr uint16_t testSequenceId = 0; |
| 501 | static constexpr uint16_t testEntrySize = 0x20; |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 502 | static constexpr uint8_t testRdeCommandType = 0x01; |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 503 | // Calculated checksum for the header |
| 504 | static constexpr uint8_t testChecksum = |
| 505 | (testSequenceId ^ testEntrySize ^ testRdeCommandType); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 506 | size_t testOffset = 0x20; |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 507 | |
| 508 | struct QueueEntryHeader testEntryHeader |
| 509 | {}; |
| 510 | }; |
| 511 | |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 512 | TEST_F(BufferEntryTest, ReadEntryHeaderPass) |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 513 | { |
| 514 | uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader); |
| 515 | std::vector<uint8_t> testEntryHeaderVector( |
| 516 | testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 517 | wraparoundReadMock(testOffset, testEntryHeaderVector); |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 518 | EXPECT_EQ(bufferImpl->readEntryHeader(testOffset), testEntryHeader); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 519 | // Check the bmcReadPtr |
| 520 | struct CircularBufferHeader cachedBufferHeader = |
| 521 | bufferImpl->getCachedBufferHeader(); |
| 522 | EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 523 | testOffset + testEntryHeaderVector.size()); |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 526 | TEST_F(BufferEntryTest, ReadEntryChecksumFail) |
| 527 | { |
| 528 | InSequence s; |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 529 | std::vector<uint8_t> testEntryVector(testEntrySize); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 530 | // Offset the checksum by 1 |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 531 | testEntryHeader.checksum += 1; |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 532 | uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader); |
| 533 | std::vector<uint8_t> testEntryHeaderVector( |
| 534 | testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 535 | wraparoundReadMock(testOffset, testEntryHeaderVector); |
| 536 | wraparoundReadMock(testOffset + entryHeaderSize, testEntryVector); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 537 | EXPECT_THROW( |
| 538 | try { |
| 539 | bufferImpl->readEntry(testOffset); |
| 540 | } catch (const std::runtime_error& e) { |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 541 | // Calculation: testChecksum (0x21) XOR (0x22) = 3 |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 542 | EXPECT_STREQ(e.what(), |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 543 | "[readEntry] Checksum was '3', expected '0'"); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 544 | throw; |
| 545 | }, |
| 546 | std::runtime_error); |
| 547 | } |
| 548 | |
| 549 | TEST_F(BufferEntryTest, ReadEntryPass) |
| 550 | { |
| 551 | InSequence s; |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 552 | // We expect this will bump checksum up by "testEntrySize" = 0xff ^ 0xff ... |
| 553 | // (20 times) = 0 therefore leave the checksum as is |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 554 | std::vector<uint8_t> testEntryVector(testEntrySize, 0xff); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 555 | uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader); |
| 556 | std::vector<uint8_t> testEntryHeaderVector( |
| 557 | testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 558 | // Set testOffset so that we can test the wraparound here as well on our |
| 559 | // second read for the entry (by 1 byte) |
| 560 | testOffset = testQueueSize - entryHeaderSize - 1; |
| 561 | wraparoundReadMock(testOffset, testEntryHeaderVector); |
| 562 | wraparoundReadMock(testOffset + entryHeaderSize, testEntryVector); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 563 | |
| 564 | EntryPair testedEntryPair; |
| 565 | EXPECT_NO_THROW(testedEntryPair = bufferImpl->readEntry(testOffset)); |
| 566 | EXPECT_EQ(testedEntryPair.first, testEntryHeader); |
| 567 | EXPECT_THAT(testedEntryPair.second, ElementsAreArray(testEntryVector)); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 568 | struct CircularBufferHeader cachedBufferHeader = |
| 569 | bufferImpl->getCachedBufferHeader(); |
| 570 | // The bmcReadPtr should have been updated to reflect the wraparound |
| 571 | EXPECT_EQ(boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 572 | testEntrySize - 1); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 575 | class BufferReadErrorLogsTest : public BufferEntryTest |
| 576 | { |
| 577 | protected: |
| 578 | BufferReadErrorLogsTest() = default; |
| 579 | |
| 580 | uint8_t* testEntryHeaderPtr = reinterpret_cast<uint8_t*>(&testEntryHeader); |
| 581 | size_t entryAndHeaderSize = entryHeaderSize + testEntrySize; |
| 582 | }; |
| 583 | |
| 584 | TEST_F(BufferReadErrorLogsTest, PtrsTooBigFail) |
| 585 | { |
| 586 | InSequence s; |
| 587 | // Set the biosWritePtr too big |
| 588 | testInitializationHeader.biosWritePtr = |
| 589 | boost::endian::native_to_little((testQueueSize + 1)); |
| 590 | initializeFuncMock(); |
| 591 | |
| 592 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 593 | .WillOnce(Return(std::vector<uint8_t>(testInitializationHeaderPtr, |
| 594 | testInitializationHeaderPtr + |
| 595 | bufferHeaderSize))); |
| 596 | EXPECT_THROW( |
| 597 | try { |
| 598 | bufferImpl->readErrorLogs(); |
| 599 | } catch (const std::runtime_error& e) { |
| 600 | EXPECT_STREQ(e.what(), |
| 601 | "[readErrorLogs] currentBiosWritePtr was '257' " |
| 602 | "which was bigger than queueSize '256'"); |
| 603 | throw; |
| 604 | }, |
| 605 | std::runtime_error); |
| 606 | |
| 607 | // Reset the biosWritePtr and set the bmcReadPtr too big |
| 608 | testInitializationHeader.biosWritePtr = 0; |
| 609 | initializeFuncMock(); |
| 610 | testInitializationHeader.bmcReadPtr = |
| 611 | boost::endian::native_to_little((testQueueSize + 1)); |
| 612 | initializeFuncMock(); |
| 613 | |
| 614 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 615 | .WillOnce(Return(std::vector<uint8_t>(testInitializationHeaderPtr, |
| 616 | testInitializationHeaderPtr + |
| 617 | bufferHeaderSize))); |
| 618 | EXPECT_THROW( |
| 619 | try { |
| 620 | bufferImpl->readErrorLogs(); |
| 621 | } catch (const std::runtime_error& e) { |
| 622 | EXPECT_STREQ(e.what(), "[readErrorLogs] currentReadPtr was '257' " |
| 623 | "which was bigger than queueSize '256'"); |
| 624 | throw; |
| 625 | }, |
| 626 | std::runtime_error); |
| 627 | } |
| 628 | |
| 629 | TEST_F(BufferReadErrorLogsTest, IdenticalPtrsPass) |
| 630 | { |
| 631 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 632 | .WillOnce(Return(std::vector<uint8_t>(testInitializationHeaderPtr, |
| 633 | testInitializationHeaderPtr + |
| 634 | bufferHeaderSize))); |
| 635 | EXPECT_NO_THROW(bufferImpl->readErrorLogs()); |
| 636 | } |
| 637 | |
| 638 | TEST_F(BufferReadErrorLogsTest, NoWraparoundPass) |
| 639 | { |
| 640 | InSequence s; |
| 641 | // Set the biosWritePtr to 1 entryHeader + entry size |
| 642 | testInitializationHeader.biosWritePtr = |
| 643 | boost::endian::native_to_little((entryAndHeaderSize)); |
| 644 | initializeFuncMock(); |
| 645 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 646 | .WillOnce(Return(std::vector<uint8_t>(testInitializationHeaderPtr, |
| 647 | testInitializationHeaderPtr + |
| 648 | bufferHeaderSize))); |
| 649 | std::vector<uint8_t> testEntryHeaderVector( |
| 650 | testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize); |
| 651 | std::vector<uint8_t> testEntryVector(testEntrySize); |
| 652 | wraparoundReadMock(/*relativeOffset=*/0, testEntryHeaderVector); |
| 653 | wraparoundReadMock(/*relativeOffset=*/0 + entryHeaderSize, testEntryVector); |
| 654 | |
| 655 | std::vector<EntryPair> entryPairs; |
| 656 | EXPECT_NO_THROW(entryPairs = bufferImpl->readErrorLogs()); |
| 657 | |
| 658 | // Check that we only read one entryPair and that the content is correct |
Brandon Kim | 1a3dc60 | 2022-06-17 11:34:33 -0700 | [diff] [blame] | 659 | EXPECT_EQ(entryPairs.size(), 1U); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 660 | EXPECT_EQ(entryPairs[0].first, testEntryHeader); |
| 661 | EXPECT_THAT(entryPairs[0].second, ElementsAreArray(testEntryVector)); |
| 662 | } |
| 663 | |
| 664 | TEST_F(BufferReadErrorLogsTest, WraparoundMultiplEntryPass) |
| 665 | { |
| 666 | InSequence s; |
| 667 | // Set the bmcReadPtr to 1 entryHeader + entry size from the "end" exactly |
| 668 | uint32_t entryAndHeaderSizeAwayFromEnd = testQueueSize - entryAndHeaderSize; |
| 669 | testInitializationHeader.bmcReadPtr = |
| 670 | boost::endian::native_to_little(entryAndHeaderSizeAwayFromEnd); |
| 671 | // Set the biosWritePtr to 1 entryHeader + entry size from the "beginning" |
| 672 | testInitializationHeader.biosWritePtr = entryAndHeaderSize; |
| 673 | initializeFuncMock(); |
| 674 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 675 | .WillOnce(Return(std::vector<uint8_t>(testInitializationHeaderPtr, |
| 676 | testInitializationHeaderPtr + |
| 677 | bufferHeaderSize))); |
| 678 | |
| 679 | std::vector<uint8_t> testEntryHeaderVector( |
| 680 | testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize); |
| 681 | std::vector<uint8_t> testEntryVector(testEntrySize); |
| 682 | wraparoundReadMock(/*relativeOffset=*/entryAndHeaderSizeAwayFromEnd, |
| 683 | testEntryHeaderVector); |
| 684 | wraparoundReadMock(/*relativeOffset=*/entryAndHeaderSizeAwayFromEnd + |
| 685 | entryHeaderSize, |
| 686 | testEntryVector); |
| 687 | wraparoundReadMock(/*relativeOffset=*/0 + entryAndHeaderSize, |
| 688 | testEntryHeaderVector); |
| 689 | wraparoundReadMock(/*relativeOffset=*/0 + entryAndHeaderSize + |
| 690 | entryHeaderSize, |
| 691 | testEntryVector); |
| 692 | |
| 693 | std::vector<EntryPair> entryPairs; |
| 694 | EXPECT_NO_THROW(entryPairs = bufferImpl->readErrorLogs()); |
| 695 | |
| 696 | // Check that we only read one entryPair and that the content is correct |
| 697 | EXPECT_EQ(entryPairs.size(), 2); |
| 698 | EXPECT_EQ(entryPairs[0].first, testEntryHeader); |
| 699 | EXPECT_EQ(entryPairs[1].first, testEntryHeader); |
| 700 | EXPECT_THAT(entryPairs[0].second, ElementsAreArray(testEntryVector)); |
| 701 | EXPECT_THAT(entryPairs[1].second, ElementsAreArray(testEntryVector)); |
| 702 | } |
| 703 | |
| 704 | TEST_F(BufferReadErrorLogsTest, WraparoundMismatchingPtrsFail) |
| 705 | { |
| 706 | InSequence s; |
| 707 | testInitializationHeader.bmcReadPtr = boost::endian::native_to_little(0); |
| 708 | // Make the biosWritePtr intentially 1 smaller than expected |
| 709 | testInitializationHeader.biosWritePtr = |
| 710 | boost::endian::native_to_little(entryAndHeaderSize - 1); |
| 711 | initializeFuncMock(); |
| 712 | EXPECT_CALL(*dataInterfaceMockPtr, read(0, bufferHeaderSize)) |
| 713 | .WillOnce(Return(std::vector<uint8_t>(testInitializationHeaderPtr, |
| 714 | testInitializationHeaderPtr + |
| 715 | bufferHeaderSize))); |
| 716 | |
| 717 | std::vector<uint8_t> testEntryHeaderVector( |
| 718 | testEntryHeaderPtr, testEntryHeaderPtr + entryHeaderSize); |
| 719 | std::vector<uint8_t> testEntryVector(testEntrySize); |
| 720 | wraparoundReadMock(/*relativeOffset=*/0, testEntryHeaderVector); |
| 721 | wraparoundReadMock(/*relativeOffset=*/0 + entryHeaderSize, testEntryVector); |
| 722 | |
| 723 | EXPECT_THROW( |
| 724 | try { |
| 725 | bufferImpl->readErrorLogs(); |
| 726 | } catch (const std::runtime_error& e) { |
| 727 | EXPECT_STREQ( |
| 728 | e.what(), |
| 729 | "[readErrorLogs] biosWritePtr '37' and bmcReaddPtr '38' " |
| 730 | "are not identical after reading through all the logs"); |
| 731 | throw; |
| 732 | }, |
| 733 | std::runtime_error); |
| 734 | } |
| 735 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 736 | } // namespace |
| 737 | } // namespace bios_bmc_smm_error_logger |