use ipmiblob library from ipmi-blob-tool

Drop all code that is now handled by the ipmiblob library provided by
the new ipmi-blob-tool.  This is a library that can be included on the
BMC if necessary, but relies on nothing that is strictly meant for the
BMC.

Change-Id: I2b02ae0d432e84c08e598d27eef85b57c06a70fc
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 86cfad1..de6bea5 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -44,7 +44,6 @@
 	manager_read_unittest \
 	manager_writemeta_unittest \
 	process_unittest \
-	crc_unittest \
 	utils_unittest
 
 TESTS = $(check_PROGRAMS)
@@ -122,11 +121,7 @@
 manager_writemeta_unittest_LDADD = $(top_builddir)/manager.o
 
 process_unittest_SOURCES = process_unittest.cpp
-process_unittest_LDADD = $(top_builddir)/process.o $(top_builddir)/ipmi.o \
-	$(top_builddir)/crc.o
-
-crc_unittest_SOURCES = crc_unittest.cpp
-crc_unittest_LDADD = $(top_builddir)/crc.o
+process_unittest_LDADD = $(top_builddir)/process.o $(top_builddir)/ipmi.o
 
 utils_unittest_SOURCES = utils_unittest.cpp
 utils_unittest_LDADD =  $(top_builddir)/utils.o $(PHOSPHOR_LOGGING_LIBS)
diff --git a/test/crc_mock.hpp b/test/crc_mock.hpp
deleted file mode 100644
index 1562200..0000000
--- a/test/crc_mock.hpp
+++ /dev/null
@@ -1,19 +0,0 @@
-#pragma once
-
-#include "crc.hpp"
-
-#include <gmock/gmock.h>
-
-namespace blobs
-{
-
-class CrcMock : public CrcInterface
-{
-  public:
-    virtual ~CrcMock() = default;
-
-    MOCK_METHOD0(clear, void());
-    MOCK_METHOD2(compute, void(const uint8_t*, uint32_t));
-    MOCK_CONST_METHOD0(get, uint16_t());
-};
-} // namespace blobs
diff --git a/test/crc_unittest.cpp b/test/crc_unittest.cpp
deleted file mode 100644
index fb69cb4..0000000
--- a/test/crc_unittest.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-#include "crc.hpp"
-
-#include <string>
-#include <vector>
-
-#include <gtest/gtest.h>
-
-namespace blobs
-{
-
-TEST(Crc16Test, VerifyCrcValue)
-{
-    // Verify the crc16 is producing the value we expect.
-
-    // Origin: security/crypta/ipmi/portable/ipmi_utils_test.cc
-    struct CrcTestVector
-    {
-        std::string input;
-        uint16_t output;
-    };
-
-    std::string longString =
-        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
-        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
-        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
-        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
-        "AAAAAAAAAAAAAAAA";
-
-    std::vector<CrcTestVector> vectors({{"", 0x1D0F},
-                                        {"A", 0x9479},
-                                        {"123456789", 0xE5CC},
-                                        {longString, 0xE938}});
-
-    Crc16 crc;
-
-    for (const CrcTestVector& testVector : vectors)
-    {
-        crc.clear();
-        auto data = reinterpret_cast<const uint8_t*>(testVector.input.data());
-        crc.compute(data, testVector.input.size());
-        EXPECT_EQ(crc.get(), testVector.output);
-    }
-}
-} // namespace blobs
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
-{
-
-using ::testing::_;
-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
 
+using ::testing::_;
+using ::testing::Eq;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::StrictMock;
+
+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 @@
 
 } // 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 @@
     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 @@
     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 @@
     // 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 @@
     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 @@
     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.
+  protected:
+    void SetUp() override
+    {
+        ipmiblob::crcIntf = &crcMock;
+    }
 
-    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};
+    ipmiblob::CrcMock crcMock;
+};
 
-    // The above request to open a file for reading & writing named:
-    // "/dev/haven/command_passthru"
-
-    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);
-}
-
-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 @@
     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 @@
     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 @@
     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 @@
 
     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};