add handler logic to handle i2c pcie commands

Add handler logic to manage the i2c pcie commands and their
corresponding data structure.

Tested: Only ran unit-tests (added new ones).
Change-Id: Ibd65d6745202dbf6bd67cd2cb480914ca6ae4ed1
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/pcie_unittest.cpp b/test/pcie_unittest.cpp
new file mode 100644
index 0000000..34926ee
--- /dev/null
+++ b/test/pcie_unittest.cpp
@@ -0,0 +1,99 @@
+#include "handler_mock.hpp"
+#include "main.hpp"
+#include "pcie_i2c.hpp"
+
+#include <cstdint>
+#include <cstring>
+#include <tuple>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#define MAX_IPMI_BUFFER 64
+
+using ::testing::Return;
+
+namespace google
+{
+namespace ipmi
+{
+
+TEST(PcieI2cCommandTest, PcieSlotCountTest)
+{
+    std::vector<std::uint8_t> request = {SysOEMCommands::SysPcieSlotCount};
+    size_t dataLen = request.size();
+    std::uint8_t reply[MAX_IPMI_BUFFER];
+    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]);
+}
+
+TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort)
+{
+    std::vector<std::uint8_t> request = {
+        SysOEMCommands::SysPcieSlotI2cBusMapping};
+    size_t dataLen = request.size();
+    std::uint8_t reply[MAX_IPMI_BUFFER];
+
+    HandlerMock hMock;
+    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
+              PcieSlotI2cBusMapping(request.data(), reply, &dataLen, &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];
+
+    HandlerMock hMock;
+    EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0));
+    EXPECT_EQ(IPMI_CC_INVALID_RESERVATION_ID,
+              PcieSlotI2cBusMapping(request.data(), reply, &dataLen, &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];
+
+    HandlerMock hMock;
+    EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
+    EXPECT_EQ(IPMI_CC_PARM_OUT_OF_RANGE,
+              PcieSlotI2cBusMapping(request.data(), reply, &dataLen, &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::string slotName = "abcd";
+    std::uint32_t busNum = 5;
+
+    HandlerMock hMock;
+    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]));
+}
+
+} // namespace ipmi
+} // namespace google