add handler logic to handle SysGetEthDevice
Add a handler to handle code logic outside of the actual IPMI
processing.
Tested: Only ran unit-tests (added new ones).
Change-Id: Iadd8c4f2d9b3e2cfba24ae32cda2ef66177b1177
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index a039e01..fea204b 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -15,6 +15,10 @@
check_PROGRAMS =
TESTS = $(check_PROGRAMS)
+check_PROGRAMS += handler_unittest
+handler_unittest_SOURCES = handler_unittest.cpp
+handler_unittest_LDADD = $(top_builddir)/libsyscmds_common.la
+
check_PROGRAMS += eth_unittest
eth_unittest_SOURCES = eth_unittest.cpp
eth_unittest_LDADD = $(top_builddir)/libsyscmds_common.la
diff --git a/test/eth_unittest.cpp b/test/eth_unittest.cpp
index f0e4d23..dd3c73f 100644
--- a/test/eth_unittest.cpp
+++ b/test/eth_unittest.cpp
@@ -1,14 +1,19 @@
#include "eth.hpp"
+#include "handler_mock.hpp"
#include "main.hpp"
#include <cstdint>
#include <cstring>
+#include <string>
+#include <tuple>
#include <vector>
#include <gtest/gtest.h>
#define MAX_IPMI_BUFFER 64
+using ::testing::Return;
+
namespace google
{
namespace ipmi
@@ -22,12 +27,21 @@
size_t dataLen = request.size();
std::uint8_t reply[MAX_IPMI_BUFFER];
const std::uint8_t expectedAnswer[4] = {'e', 't', 'h', '0'};
+ const std::uint8_t expectedChannel = 1;
- EXPECT_EQ(IPMI_CC_OK, GetEthDevice(request.data(), &reply[0], &dataLen));
+ HandlerMock hMock;
+ EXPECT_CALL(hMock, getEthDetails())
+ .WillOnce(Return(std::make_tuple(
+ expectedChannel,
+ std::string(expectedAnswer,
+ expectedAnswer + sizeof(expectedAnswer)))));
+
+ 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, 1);
+ EXPECT_EQ(check.channel, expectedChannel);
EXPECT_EQ(check.if_name_len, sizeof(expectedAnswer));
EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)],
sizeof(expectedAnswer)));
diff --git a/test/handler_mock.hpp b/test/handler_mock.hpp
new file mode 100644
index 0000000..8507f50
--- /dev/null
+++ b/test/handler_mock.hpp
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "handler.hpp"
+
+#include <gmock/gmock.h>
+
+namespace google
+{
+namespace ipmi
+{
+
+class HandlerMock : public HandlerInterface
+{
+
+ public:
+ ~HandlerMock() = default;
+
+ MOCK_CONST_METHOD0(getEthDetails, std::tuple<std::uint8_t, std::string>());
+};
+
+} // namespace ipmi
+} // namespace google
diff --git a/test/handler_unittest.cpp b/test/handler_unittest.cpp
new file mode 100644
index 0000000..05edc0a
--- /dev/null
+++ b/test/handler_unittest.cpp
@@ -0,0 +1,26 @@
+#include "handler.hpp"
+
+#include <string>
+#include <tuple>
+
+#include <gtest/gtest.h>
+
+namespace google
+{
+namespace ipmi
+{
+
+TEST(HandlerTest, EthCheckValidHappy)
+{
+ // The code returns compiled-in information, and therefore cannot really
+ // fail.
+ Handler h;
+ std::tuple<std::uint8_t, std::string> result = h.getEthDetails();
+ EXPECT_EQ(1, std::get<0>(result));
+ EXPECT_STREQ("eth0", std::get<1>(result).c_str());
+}
+
+// TODO: Add checks for other functions of handler.
+
+} // namespace ipmi
+} // namespace google