diff options
Diffstat (limited to 'test/process_unittest.cpp')
-rw-r--r-- | test/process_unittest.cpp | 139 |
1 files changed, 58 insertions, 81 deletions
diff --git a/test/process_unittest.cpp b/test/process_unittest.cpp index bd1f886..d99bd21 100644 --- a/test/process_unittest.cpp +++ b/test/process_unittest.cpp @@ -1,25 +1,34 @@ -#include "crc.hpp" -#include "crc_mock.hpp" #include "ipmi.hpp" #include "manager_mock.hpp" #include "process.hpp" #include <cstring> +#include <ipmiblob/test/crc_mock.hpp> #include <gtest/gtest.h> -namespace blobs -{ +// ipmid.hpp isn't installed where we can grab it and this value is per BMC +// SoC. +#define MAX_IPMI_BUFFER 64 using ::testing::_; +using ::testing::Eq; using ::testing::Invoke; using ::testing::Return; using ::testing::StrictMock; -// ipmid.hpp isn't installed where we can grab it and this value is per BMC -// SoC. -#define MAX_IPMI_BUFFER 64 +namespace ipmiblob +{ +CrcInterface* crcIntf = nullptr; + +std::uint16_t generateCrc(const std::vector<std::uint8_t>& data) +{ + return (crcIntf) ? crcIntf->generateCrc(data) : 0x00; +} +} // namespace ipmiblob +namespace blobs +{ namespace { @@ -46,11 +55,21 @@ void EqualFunctions(IpmiBlobHandler lhs, IpmiBlobHandler rhs) } // namespace -TEST(ValidateBlobCommandTest, InvalidCommandReturnsFailure) +class ValidateBlobCommandTest : public ::testing::Test +{ + protected: + void SetUp() override + { + ipmiblob::crcIntf = &crcMock; + } + + ipmiblob::CrcMock crcMock; +}; + +TEST_F(ValidateBlobCommandTest, InvalidCommandReturnsFailure) { // Verify we handle an invalid command. - StrictMock<CrcMock> crc; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; uint8_t reply[MAX_IPMI_BUFFER] = {0}; @@ -59,16 +78,14 @@ TEST(ValidateBlobCommandTest, InvalidCommandReturnsFailure) dataLen = sizeof(uint8_t); // There is no payload for CRC. ipmi_ret_t rc; - EXPECT_EQ(nullptr, - validateBlobCommand(&crc, request, reply, &dataLen, &rc)); + EXPECT_EQ(nullptr, validateBlobCommand(request, reply, &dataLen, &rc)); EXPECT_EQ(IPMI_CC_INVALID_FIELD_REQUEST, rc); } -TEST(ValidateBlobCommandTest, ValidCommandWithoutPayload) +TEST_F(ValidateBlobCommandTest, ValidCommandWithoutPayload) { // Verify we handle a valid command that doesn't have a payload. - StrictMock<CrcMock> crc; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; uint8_t reply[MAX_IPMI_BUFFER] = {0}; @@ -77,18 +94,16 @@ TEST(ValidateBlobCommandTest, ValidCommandWithoutPayload) dataLen = sizeof(uint8_t); // There is no payload for CRC. ipmi_ret_t rc; - IpmiBlobHandler res = - validateBlobCommand(&crc, request, reply, &dataLen, &rc); + IpmiBlobHandler res = validateBlobCommand(request, reply, &dataLen, &rc); EXPECT_FALSE(res == nullptr); EqualFunctions(getBlobCount, res); } -TEST(ValidateBlobCommandTest, WithPayloadMinimumLengthIs3VerifyChecks) +TEST_F(ValidateBlobCommandTest, WithPayloadMinimumLengthIs3VerifyChecks) { // Verify that if there's a payload, it's at least one command byte and // two bytes for the crc16 and then one data byte. - StrictMock<CrcMock> crc; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; uint8_t reply[MAX_IPMI_BUFFER] = {0}; @@ -98,16 +113,14 @@ TEST(ValidateBlobCommandTest, WithPayloadMinimumLengthIs3VerifyChecks) // There is a payload, but there are insufficient bytes. ipmi_ret_t rc; - EXPECT_EQ(nullptr, - validateBlobCommand(&crc, request, reply, &dataLen, &rc)); + EXPECT_EQ(nullptr, validateBlobCommand(request, reply, &dataLen, &rc)); EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, rc); } -TEST(ValidateBlobCommandTest, WithPayloadAndInvalidCrc) +TEST_F(ValidateBlobCommandTest, WithPayloadAndInvalidCrc) { // Verify that the CRC is checked, and failure is reported. - StrictMock<CrcMock> crc; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; uint8_t reply[MAX_IPMI_BUFFER] = {0}; @@ -124,27 +137,19 @@ TEST(ValidateBlobCommandTest, WithPayloadAndInvalidCrc) dataLen = sizeof(struct BmcBlobWriteTx) + sizeof(expectedBytes); // skip over cmd and crc. - size_t expectedLen = dataLen - 3; - - EXPECT_CALL(crc, clear()); - EXPECT_CALL(crc, compute(_, expectedLen)) - .WillOnce(Invoke([&](const uint8_t* bytes, uint32_t length) { - EXPECT_EQ(0, std::memcmp(&request[3], bytes, length)); - })); - EXPECT_CALL(crc, get()).WillOnce(Return(0x1234)); + std::vector<std::uint8_t> bytes(&request[3], request + dataLen); + EXPECT_CALL(crcMock, generateCrc(Eq(bytes))).WillOnce(Return(0x1234)); ipmi_ret_t rc; - EXPECT_EQ(nullptr, - validateBlobCommand(&crc, request, reply, &dataLen, &rc)); + EXPECT_EQ(nullptr, validateBlobCommand(request, reply, &dataLen, &rc)); EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR, rc); } -TEST(ValidateBlobCommandTest, WithPayloadAndValidCrc) +TEST_F(ValidateBlobCommandTest, WithPayloadAndValidCrc) { // Verify the CRC is checked and if it matches, return the handler. - StrictMock<CrcMock> crc; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; uint8_t reply[MAX_IPMI_BUFFER] = {0}; @@ -161,55 +166,32 @@ TEST(ValidateBlobCommandTest, WithPayloadAndValidCrc) dataLen = sizeof(struct BmcBlobWriteTx) + sizeof(expectedBytes); // skip over cmd and crc. - size_t expectedLen = dataLen - 3; - - EXPECT_CALL(crc, clear()); - EXPECT_CALL(crc, compute(_, expectedLen)) - .WillOnce(Invoke([&](const uint8_t* bytes, uint32_t length) { - EXPECT_EQ(0, std::memcmp(&request[3], bytes, length)); - })); - EXPECT_CALL(crc, get()).WillOnce(Return(0x3412)); + std::vector<std::uint8_t> bytes(&request[3], request + dataLen); + EXPECT_CALL(crcMock, generateCrc(Eq(bytes))).WillOnce(Return(0x3412)); ipmi_ret_t rc; - IpmiBlobHandler res = - validateBlobCommand(&crc, request, reply, &dataLen, &rc); + IpmiBlobHandler res = validateBlobCommand(request, reply, &dataLen, &rc); EXPECT_FALSE(res == nullptr); EqualFunctions(writeBlob, res); } -TEST(ValidateBlobCommandTest, InputIntegrationTest) +class ProcessBlobCommandTest : public ::testing::Test { - // Given a request buffer generated by the host-side utility, verify it is - // properly routed. - - Crc16 crc; - size_t dataLen; - uint8_t request[] = {0x02, 0x88, 0x21, 0x03, 0x00, 0x2f, 0x64, 0x65, 0x76, - 0x2f, 0x68, 0x61, 0x76, 0x65, 0x6e, 0x2f, 0x63, 0x6f, - 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x5f, 0x70, 0x61, 0x73, - 0x73, 0x74, 0x68, 0x72, 0x75, 0x00}; - - // The above request to open a file for reading & writing named: - // "/dev/haven/command_passthru" + protected: + void SetUp() override + { + ipmiblob::crcIntf = &crcMock; + } - uint8_t reply[MAX_IPMI_BUFFER] = {0}; - - dataLen = sizeof(request); - ipmi_ret_t rc; - - IpmiBlobHandler res = - validateBlobCommand(&crc, request, reply, &dataLen, &rc); - EXPECT_FALSE(res == nullptr); - EqualFunctions(openBlob, res); -} + ipmiblob::CrcMock crcMock; +}; -TEST(ProcessBlobCommandTest, CommandReturnsNotOk) +TEST_F(ProcessBlobCommandTest, CommandReturnsNotOk) { // Verify that if the IPMI command handler returns not OK that this is // noticed and returned. - StrictMock<CrcMock> crc; StrictMock<ManagerMock> manager; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; @@ -222,15 +204,14 @@ TEST(ProcessBlobCommandTest, CommandReturnsNotOk) dataLen = sizeof(request); EXPECT_EQ(IPMI_CC_INVALID, - processBlobCommand(h, &manager, &crc, request, reply, &dataLen)); + processBlobCommand(h, &manager, request, reply, &dataLen)); } -TEST(ProcessBlobCommandTest, CommandReturnsOkWithNoPayload) +TEST_F(ProcessBlobCommandTest, CommandReturnsOkWithNoPayload) { // Verify that if the IPMI command handler returns OK but without a payload // it doesn't try to compute a CRC. - StrictMock<CrcMock> crc; StrictMock<ManagerMock> manager; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; @@ -245,15 +226,14 @@ TEST(ProcessBlobCommandTest, CommandReturnsOkWithNoPayload) dataLen = sizeof(request); EXPECT_EQ(IPMI_CC_OK, - processBlobCommand(h, &manager, &crc, request, reply, &dataLen)); + processBlobCommand(h, &manager, request, reply, &dataLen)); } -TEST(ProcessBlobCommandTest, CommandReturnsOkWithInvalidPayloadLength) +TEST_F(ProcessBlobCommandTest, CommandReturnsOkWithInvalidPayloadLength) { // There is a minimum payload length of 2 bytes (the CRC only, no data, for // read), this returns 1. - StrictMock<CrcMock> crc; StrictMock<ManagerMock> manager; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; @@ -268,15 +248,14 @@ TEST(ProcessBlobCommandTest, CommandReturnsOkWithInvalidPayloadLength) dataLen = sizeof(request); EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR, - processBlobCommand(h, &manager, &crc, request, reply, &dataLen)); + processBlobCommand(h, &manager, request, reply, &dataLen)); } -TEST(ProcessBlobCommandTest, CommandReturnsOkWithValidPayloadLength) +TEST_F(ProcessBlobCommandTest, CommandReturnsOkWithValidPayloadLength) { // There is a minimum payload length of 3 bytes, this command returns a // payload of 3 bytes and the crc code is called to process the payload. - StrictMock<CrcMock> crc; StrictMock<ManagerMock> manager; size_t dataLen; uint8_t request[MAX_IPMI_BUFFER] = {0}; @@ -293,12 +272,10 @@ TEST(ProcessBlobCommandTest, CommandReturnsOkWithValidPayloadLength) dataLen = sizeof(request); - EXPECT_CALL(crc, clear()); - EXPECT_CALL(crc, compute(_, payloadLen - sizeof(uint16_t))); - EXPECT_CALL(crc, get()).WillOnce(Return(0x3412)); + EXPECT_CALL(crcMock, generateCrc(_)).WillOnce(Return(0x3412)); EXPECT_EQ(IPMI_CC_OK, - processBlobCommand(h, &manager, &crc, request, reply, &dataLen)); + processBlobCommand(h, &manager, request, reply, &dataLen)); EXPECT_EQ(dataLen, payloadLen); uint8_t expectedBytes[3] = {0x12, 0x34, 0x56}; |