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/Makefile.am b/test/Makefile.am
index 0243d44..b9ad829 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -38,3 +38,7 @@
 check_PROGRAMS += entity_unittest
 entity_unittest_SOURCES = entity_unittest.cpp
 entity_unittest_LDADD = $(top_builddir)/libsyscmds_common.la
+
+check_PROGRAMS += pcie_unittest
+pcie_unittest_SOURCES = pcie_unittest.cpp
+pcie_unittest_LDADD = $(top_builddir)/libsyscmds_common.la
diff --git a/test/handler_mock.hpp b/test/handler_mock.hpp
index 6394f68..b97cce3 100644
--- a/test/handler_mock.hpp
+++ b/test/handler_mock.hpp
@@ -22,6 +22,10 @@
                                   std::uint8_t>(unsigned int));
     MOCK_CONST_METHOD1(psuResetDelay, void(std::uint32_t));
     MOCK_METHOD2(getEntityName, std::string(std::uint8_t, std::uint8_t));
+    MOCK_METHOD0(buildI2cPcieMapping, void());
+    MOCK_CONST_METHOD0(getI2cPcieMappingSize, size_t());
+    MOCK_CONST_METHOD1(getI2cEntry,
+                       std::tuple<std::uint32_t, std::string>(unsigned int));
 };
 
 } // namespace ipmi
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