google-ipmi-sys: Refactor to use new version of OEM IPMI Handler

Using the new version of ipmi handler provide a higher level wrapper
over the same functionalities. It helps us parse the input and output to
have more control of the input/output we see.

The input and output will be
`std::uint8_t, std::optional<std::vector<uint8_t>>`.

This represents `subcommand` and any input data.

Changes to note,
- all subcommand in the request/response struct are removed. It will be
  managed by the wrapper directly.
    - Unit tests checking for input with only the subcommand are
      removed.
- Move all reply struct to header files to be accessible in unit test.

Tested:
All IPMI OEM command still works the same as before this change.

Change-Id: I4230ab84a497a867248fe82224e32cc69b314b64
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/test/cable_unittest.cpp b/test/cable_unittest.cpp
index 06b4706..27124ad 100644
--- a/test/cable_unittest.cpp
+++ b/test/cable_unittest.cpp
@@ -15,15 +15,15 @@
 #include "cable.hpp"
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
+#include <tuple>
 #include <vector>
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 using ::testing::StrEq;
 
@@ -34,69 +34,49 @@
 
 TEST(CableCommandTest, RequestTooSmall)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     HandlerMock hMock;
 
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cableCheck(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
 }
 
 TEST(CableCommandTest, FailsLengthSanityCheck)
 {
     // Minimum is three bytes, but a length of zero for the string is invalid.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x00,
-                                         'a'};
-
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {0x00, 'a'};
     HandlerMock hMock;
 
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cableCheck(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
 }
 
 TEST(CableCommandTest, LengthTooLongForPacket)
 {
     // The length of a the string, as specified is longer than string provided.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x02,
-                                         'a'};
-
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {0x02, 'a'};
     HandlerMock hMock;
 
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cableCheck(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
 }
 
 TEST(CableCommandTest, ValidRequestValidReturn)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x01,
-                                         'a'};
-
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {0x01, 'a'};
 
     HandlerMock hMock;
 
     EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0));
-    EXPECT_EQ(IPMI_CC_OK, cableCheck(request.data(), reply, &dataLen, &hMock));
 
     // Check results.
-    struct CableReply expectedReply, actualReply;
-    expectedReply.subcommand = SysOEMCommands::SysCableCheck;
+    struct CableReply expectedReply;
     expectedReply.value = 0;
 
-    EXPECT_EQ(sizeof(expectedReply), dataLen);
-    std::memcpy(&actualReply, reply, dataLen);
+    auto reply = cableCheck(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
 
-    EXPECT_EQ(expectedReply.subcommand, actualReply.subcommand);
-    EXPECT_EQ(expectedReply.value, actualReply.value);
+    EXPECT_EQ(sizeof(struct CableReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysCableCheck, result.first);
+    EXPECT_EQ(expectedReply.value, data[0]);
 }
 
 } // namespace ipmi
diff --git a/test/cpld_unittest.cpp b/test/cpld_unittest.cpp
index f9ab539..5747ce5 100644
--- a/test/cpld_unittest.cpp
+++ b/test/cpld_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "cpld.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <tuple>
@@ -22,8 +23,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -33,20 +32,17 @@
 
 TEST(CpldCommandTest, RequestTooSmall)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCpldVersion};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cpldVersion(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              cpldVersion(request, &hMock));
 }
 
 TEST(CpldCommandTest, ValidRequestReturnsHappy)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCpldVersion, 0x04};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {0x04};
+
     std::uint8_t expectedMaj = 0x5;
     std::uint8_t expectedMin = 0x3;
     std::uint8_t expectedPt = 0x7;
@@ -57,11 +53,18 @@
         .WillOnce(Return(std::make_tuple(expectedMaj, expectedMin, expectedPt,
                                          expectedSbPtr)));
 
-    EXPECT_EQ(IPMI_CC_OK, cpldVersion(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(expectedMaj, reply[1]);
-    EXPECT_EQ(expectedMin, reply[2]);
-    EXPECT_EQ(expectedPt, reply[3]);
-    EXPECT_EQ(expectedSbPtr, reply[4]);
+    // Reply is in the form of
+    // std::tuple<ipmi::Cc, std::optional<std::tuple<RetTypes...>>>
+    auto reply = cpldVersion(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(struct CpldReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysCpldVersion, result.first);
+    EXPECT_EQ(expectedMaj, data[0]);
+    EXPECT_EQ(expectedMin, data[1]);
+    EXPECT_EQ(expectedPt, data[2]);
+    EXPECT_EQ(expectedSbPtr, data[3]);
 }
 
 } // namespace ipmi
diff --git a/test/entity_unittest.cpp b/test/entity_unittest.cpp
index a939a8a..7b7e843 100644
--- a/test/entity_unittest.cpp
+++ b/test/entity_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "entity_name.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
@@ -35,32 +36,34 @@
 TEST(EntityNameCommandTest, InvalidCommandLength)
 {
     // GetEntityNameRequest is three bytes, let's send 2.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName, 0x01};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {0x01};
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              getEntityName(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              getEntityName(request, &hMock));
 }
 
 TEST(EntityNameCommandTest, ValidRequest)
 {
     std::uint8_t entityId = 3;
     std::uint8_t entityInstance = 5;
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName,
-                                         entityId, entityInstance};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {entityId, entityInstance};
     std::string entityName = "asdf";
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getEntityName(entityId, entityInstance))
         .WillOnce(Return(entityName));
-    EXPECT_EQ(IPMI_CC_OK,
-              getEntityName(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(reply[1], entityName.length());
-    EXPECT_EQ(0, std::memcmp(&reply[2], entityName.c_str(), reply[1]));
+
+    auto reply = getEntityName(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(GetEntityNameReply) + entityName.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysEntityName, result.first);
+    EXPECT_EQ(entityName.length(), data[0]);
+    EXPECT_EQ(entityName.data(),
+              std::string(data.begin() + sizeof(struct GetEntityNameReply),
+                          data.end()));
 }
 
 } // namespace ipmi
diff --git a/test/eth_unittest.cpp b/test/eth_unittest.cpp
index 2b5192a..14b0875 100644
--- a/test/eth_unittest.cpp
+++ b/test/eth_unittest.cpp
@@ -15,17 +15,16 @@
 #include "commands.hpp"
 #include "eth.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
-#include <string>
+#include <string_view>
 #include <tuple>
 #include <vector>
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -37,54 +36,50 @@
 {
     // This command requests no input, therefore it will just return what it
     // knows.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysGetEthDevice};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-    const std::uint8_t expectedAnswer[4] = {'e', 't', 'h', '0'};
+    std::vector<std::uint8_t> request = {};
+    const std::string_view expectedAnswer = "eth0";
     const std::uint8_t expectedChannel = 14;
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getEthDetails(""))
-        .WillOnce(Return(std::make_tuple(
-            expectedChannel,
-            std::string(expectedAnswer,
-                        expectedAnswer + sizeof(expectedAnswer)))));
+        .WillOnce(
+            Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
 
-    EXPECT_EQ(IPMI_CC_OK,
-              getEthDevice(request.data(), &reply[0], &dataLen, &hMock));
-    struct EthDeviceReply check;
-    std::memcpy(&check, &reply[0], sizeof(check));
-    EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice);
-    EXPECT_EQ(check.channel, expectedChannel);
-    EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer));
-    EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)],
-                             sizeof(expectedAnswer)));
+    auto reply = getEthDevice(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(EthDeviceReply) + expectedAnswer.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysGetEthDevice, result.first);
+    EXPECT_EQ(expectedChannel, data[0]);
+    EXPECT_EQ(expectedAnswer.size(), data[1]);
+    EXPECT_EQ(
+        expectedAnswer.data(),
+        std::string(data.begin() + sizeof(struct EthDeviceReply), data.end()));
 }
 
 TEST(EthCommandTest, ValidPopulatedReturnsSuccess)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysGetEthDevice, 'e'};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-    const std::uint8_t expectedAnswer[1] = {'e'};
+    std::vector<std::uint8_t> request = {'e'};
+    const std::string_view expectedAnswer = "e";
     const std::uint8_t expectedChannel = 11;
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getEthDetails("e"))
-        .WillOnce(Return(std::make_tuple(
-            expectedChannel,
-            std::string(expectedAnswer,
-                        expectedAnswer + sizeof(expectedAnswer)))));
+        .WillOnce(
+            Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
 
-    EXPECT_EQ(IPMI_CC_OK,
-              getEthDevice(request.data(), &reply[0], &dataLen, &hMock));
-    struct EthDeviceReply check;
-    std::memcpy(&check, &reply[0], sizeof(check));
-    EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice);
-    EXPECT_EQ(check.channel, expectedChannel);
-    EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer));
-    EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)],
-                             sizeof(expectedAnswer)));
+    auto reply = getEthDevice(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(EthDeviceReply) + expectedAnswer.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysGetEthDevice, result.first);
+    EXPECT_EQ(expectedChannel, data[0]);
+    EXPECT_EQ(expectedAnswer.size(), data[1]);
+    EXPECT_EQ(
+        expectedAnswer.data(),
+        std::string(data.begin() + sizeof(struct EthDeviceReply), data.end()));
 }
 } // namespace ipmi
 } // namespace google
diff --git a/test/flash_unittest.cpp b/test/flash_unittest.cpp
index f4337f7..3d575df 100644
--- a/test/flash_unittest.cpp
+++ b/test/flash_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "flash_size.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
@@ -23,8 +24,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -32,34 +31,24 @@
 namespace ipmi
 {
 
-TEST(FlashSizeCommandTest, InvalidCommandLength)
-{
-    // GetFlashSizeRequest is one byte, let's send 0.
-    std::vector<std::uint8_t> request = {};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
-    HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              getFlashSize(request.data(), reply, &dataLen, &hMock));
-}
-
 TEST(FlashSizeCommandTest, ValidRequest)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysGetFlashSize};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
     uint32_t flashSize = 5422312; // 0x52BCE8
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getFlashSize()).WillOnce(Return(flashSize));
-    EXPECT_EQ(IPMI_CC_OK,
-              getFlashSize(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(dataLen, 5);
-    EXPECT_EQ(reply[4], 0);
-    EXPECT_EQ(reply[3], 0x52);
-    EXPECT_EQ(reply[2], 0xBC);
-    EXPECT_EQ(reply[1], 0xE8);
+
+    auto reply = getFlashSize(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(struct GetFlashSizeReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysGetFlashSize, result.first);
+    EXPECT_EQ(0, data[3]);
+    EXPECT_EQ(0x52, data[2]);
+    EXPECT_EQ(0xBC, data[1]);
+    EXPECT_EQ(0xE8, data[0]);
 }
 
 } // namespace ipmi
diff --git a/test/helper.cpp b/test/helper.cpp
new file mode 100644
index 0000000..6f16b55
--- /dev/null
+++ b/test/helper.cpp
@@ -0,0 +1,32 @@
+#include "helper.hpp"
+
+#include <ipmid/api-types.hpp>
+#include <optional>
+#include <span>
+#include <utility>
+
+namespace google
+{
+namespace ipmi
+{
+
+std::pair<std::uint8_t, std::vector<std::uint8_t>>
+    ValidateReply(::ipmi::RspType<std::uint8_t, std::vector<uint8_t>> reply,
+                  bool hasData)
+{
+    // Reply is in the form of
+    // std::tuple<ipmi::Cc, std::optional<std::tuple<RetTypes...>>>
+    EXPECT_EQ(::ipmi::ccSuccess, std::get<0>(reply));
+
+    auto actualReply = std::get<1>(reply);
+    EXPECT_TRUE(actualReply.has_value());
+
+    auto subcommand = std::get<0>(*actualReply);
+    auto data = std::get<1>(*actualReply);
+    EXPECT_EQ(hasData, !data.empty());
+
+    return std::make_pair(subcommand, hasData ? data : std::vector<uint8_t>{});
+}
+
+} // namespace ipmi
+} // namespace google
diff --git a/test/helper.hpp b/test/helper.hpp
new file mode 100644
index 0000000..f8fb170
--- /dev/null
+++ b/test/helper.hpp
@@ -0,0 +1,23 @@
+#pragma once
+
+#include "handler_mock.hpp"
+
+#include <ipmid/api-types.hpp>
+#include <span>
+#include <utility>
+
+#include <gtest/gtest.h>
+
+namespace google
+{
+namespace ipmi
+{
+
+// Validate the return code and the data for the IPMI reply.
+// Returns the subcommand and the optional informations.
+std::pair<std::uint8_t, std::vector<std::uint8_t>>
+    ValidateReply(::ipmi::RspType<std::uint8_t, std::vector<uint8_t>> reply,
+                  bool hasData = true);
+
+} // namespace ipmi
+} // namespace google
diff --git a/test/machine_unittest.cpp b/test/machine_unittest.cpp
index 924c25a..3dfd3c0 100644
--- a/test/machine_unittest.cpp
+++ b/test/machine_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "errors.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "machine_name.hpp"
 
 #include <cstdint>
@@ -34,53 +35,33 @@
 namespace ipmi
 {
 
-TEST(MachineNameCommandTest, InvalidCommandLength)
-{
-    std::vector<std::uint8_t> request = {};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
-    ::testing::StrictMock<HandlerMock> hMock;
-
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              getMachineName(request.data(), reply, &dataLen, &hMock));
-}
-
 TEST(MachineNameCommandTest, InvalidFile)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     ::testing::StrictMock<HandlerMock> hMock;
     EXPECT_CALL(hMock, getMachineName()).WillOnce(Throw(IpmiException(5)));
 
-    EXPECT_EQ(5, getMachineName(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::response(5), getMachineName(request, &hMock));
 }
 
 TEST(MachineNameCommandTest, CachesValidRequest)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
     const std::string ret = "Machine";
 
     ::testing::StrictMock<HandlerMock> hMock;
     EXPECT_CALL(hMock, getMachineName()).WillOnce(Return(ret));
 
-    EXPECT_EQ(IPMI_CC_OK,
-              getMachineName(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]);
-    EXPECT_EQ(ret.size(), reply[1]);
-    EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size()));
+    auto reply = getMachineName(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
 
-    dataLen = request.size();
-    memset(reply, 0, sizeof(reply));
-    EXPECT_EQ(IPMI_CC_OK,
-              getMachineName(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]);
-    EXPECT_EQ(ret.size(), reply[1]);
-    EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size()));
+    EXPECT_EQ(sizeof(GetMachineNameReply) + ret.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysMachineName, result.first);
+    EXPECT_EQ(ret.length(), data[0]);
+    EXPECT_EQ(ret.data(),
+              std::string(data.begin() + sizeof(struct GetMachineNameReply),
+                          data.end()));
 }
 
 } // namespace ipmi
diff --git a/test/meson.build b/test/meson.build
index 6630893..a9da8b9 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -7,6 +7,7 @@
 tests_lib = static_library(
   'common',
   'common.cpp',
+  'helper.cpp',
   implicit_include_directories: false,
   dependencies: tests_pre)
 
diff --git a/test/pcie_unittest.cpp b/test/pcie_unittest.cpp
index 1eb8fd8..26c3725 100644
--- a/test/pcie_unittest.cpp
+++ b/test/pcie_unittest.cpp
@@ -14,6 +14,7 @@
 
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "pcie_i2c.hpp"
 
 #include <cstdint>
@@ -23,8 +24,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -34,67 +33,57 @@
 
 TEST(PcieI2cCommandTest, PcieSlotCountTest)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysPcieSlotCount};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
     size_t expectedSize = 3;
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, buildI2cPcieMapping());
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize));
-    EXPECT_EQ(IPMI_CC_OK,
-              pcieSlotCount(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(expectedSize, reply[1]);
+
+    auto reply = pcieSlotCount(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(struct PcieSlotCountReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysPcieSlotCount, result.first);
+    EXPECT_EQ(expectedSize, data[0]);
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort)
 {
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
 
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              pcieSlotI2cBusMapping(request, &hMock));
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform)
 {
     // If there is no mapping in the device-tree, then the map is of size zero.
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping, 0};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {0};
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0));
-    EXPECT_EQ(IPMI_CC_INVALID_RESERVATION_ID,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseInvalidReservationId(),
+              pcieSlotI2cBusMapping(request, &hMock));
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex)
 {
     // index of 1 is invalid if length is 1.
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping, 1};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {1};
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
-    EXPECT_EQ(IPMI_CC_PARM_OUT_OF_RANGE,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseParmOutOfRange(),
+              pcieSlotI2cBusMapping(request, &hMock));
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex)
 {
     unsigned int index = 0;
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping,
-        static_cast<std::uint8_t>(index)};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {static_cast<std::uint8_t>(index)};
     std::string slotName = "abcd";
     std::uint32_t busNum = 5;
 
@@ -102,11 +91,18 @@
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
     EXPECT_CALL(hMock, getI2cEntry(index))
         .WillOnce(Return(std::make_tuple(busNum, slotName)));
-    EXPECT_EQ(IPMI_CC_OK,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(busNum, reply[1]);
-    EXPECT_EQ(slotName.length(), reply[2]);
-    EXPECT_EQ(0, std::memcmp(slotName.c_str(), &reply[3], reply[2]));
+
+    auto reply = pcieSlotI2cBusMapping(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(SysOEMCommands::SysPcieSlotI2cBusMapping, result.first);
+    EXPECT_EQ(busNum, data[0]);
+    EXPECT_EQ(slotName.length(), data[1]);
+    EXPECT_EQ(
+        slotName,
+        std::string(data.begin() + sizeof(struct PcieSlotI2cBusMappingReply),
+                    data.end()));
 }
 
 } // namespace ipmi
diff --git a/test/poweroff_unittest.cpp b/test/poweroff_unittest.cpp
index 4bbd40a..6a90fc6 100644
--- a/test/poweroff_unittest.cpp
+++ b/test/poweroff_unittest.cpp
@@ -14,6 +14,7 @@
 
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "host_power_off.hpp"
 
 #include <cstdint>
@@ -22,8 +23,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 namespace google
 {
 namespace ipmi
@@ -32,12 +31,10 @@
 TEST(PowerOffCommandTest, InvalidRequestLength)
 {
     std::vector<std::uint8_t> request = {SysOEMCommands::SysHostPowerOff};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              hostPowerOff(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              hostPowerOff(request, &hMock));
 }
 
 TEST(PowerOffCommandTest, ValidRequest)
@@ -45,18 +42,17 @@
     // Set the dealy to 15 mins
     std::uint32_t delayValue = 0x384;
     struct HostPowerOffRequest requestContents;
-    requestContents.subcommand = SysOEMCommands::SysHostPowerOff;
     requestContents.delay = delayValue;
 
     std::vector<std::uint8_t> request(sizeof(requestContents));
     std::memcpy(request.data(), &requestContents, sizeof(requestContents));
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, hostPowerOffDelay(delayValue));
-    EXPECT_EQ(IPMI_CC_OK,
-              hostPowerOff(request.data(), reply, &dataLen, &hMock));
+
+    auto reply = hostPowerOff(request, &hMock);
+    auto result = ValidateReply(reply, false);
+    EXPECT_EQ(SysOEMCommands::SysHostPowerOff, result.first);
 }
 
 } // namespace ipmi
diff --git a/test/psu_unittest.cpp b/test/psu_unittest.cpp
index 257baad..a08f040 100644
--- a/test/psu_unittest.cpp
+++ b/test/psu_unittest.cpp
@@ -14,6 +14,7 @@
 
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "psu.hpp"
 
 #include <cstdint>
@@ -22,8 +23,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -33,58 +32,41 @@
 
 TEST(PsuCommandTest, InvalidRequestLength)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysPsuHardReset};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              psuHardReset(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              psuHardReset(request, &hMock));
 }
 
 TEST(PsuCommandTest, ValidRequest)
 {
-    std::uint32_t delayValue = 0x45;
+    std::uint8_t delayValue = 0x45;
     struct PsuResetRequest requestContents;
-    requestContents.subcommand = SysOEMCommands::SysPsuHardReset;
     requestContents.delay = delayValue;
-
     std::vector<std::uint8_t> request(sizeof(requestContents));
     std::memcpy(request.data(), &requestContents, sizeof(requestContents));
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, psuResetDelay(delayValue));
-    EXPECT_EQ(IPMI_CC_OK,
-              psuHardReset(request.data(), reply, &dataLen, &hMock));
-}
 
-TEST(PsuResetOnShutdownCommandTest, InvalidRequestLength)
-{
-    std::vector<std::uint8_t> request;
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    auto reply = psuHardReset(request, &hMock);
+    auto result = ValidateReply(reply, /*hasData=*/false);
 
-    HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              psuHardResetOnShutdown(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(SysOEMCommands::SysPsuHardReset, result.first);
 }
 
 TEST(PsuResetOnShutdownCommandTest, ValidRequest)
 {
-    struct PsuResetOnShutdownRequest requestContents;
-    requestContents.subcommand = SysOEMCommands::SysPsuHardReset;
-
-    std::vector<std::uint8_t> request(sizeof(requestContents));
-    std::memcpy(request.data(), &requestContents, sizeof(requestContents));
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, psuResetOnShutdown());
-    EXPECT_EQ(IPMI_CC_OK,
-              psuHardResetOnShutdown(request.data(), reply, &dataLen, &hMock));
+
+    auto reply = psuHardResetOnShutdown(request, &hMock);
+    auto result = ValidateReply(reply, /*hasData=*/false);
+
+    EXPECT_EQ(SysOEMCommands::SysPsuHardResetOnShutdown, result.first);
 }
 
 } // namespace ipmi