snoop.h/main.cpp: Add first tests for PostReporter class

Add unit tests for PostReporter DBus object path, reading, and updating.
Move the class definition to the header file such that the
tests can access it.

Tested: unit tests pass.

Change-Id: I4d3571ccd5455297d6b02ed74ed789fe05e40979
Signed-off-by: Kun Yi <kunyi731@gmail.com>
diff --git a/lpcsnoop/snoop.hpp b/lpcsnoop/snoop.hpp
index d1dd847..90729be 100644
--- a/lpcsnoop/snoop.hpp
+++ b/lpcsnoop/snoop.hpp
@@ -1,6 +1,25 @@
 #pragma once
 
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server.hpp>
+
+#include "xyz/openbmc_project/State/Boot/Raw/server.hpp"
+
 /* The LPC snoop on port 80h is mapped to this dbus path. */
 #define SNOOP_OBJECTPATH "/xyz/openbmc_project/state/boot/raw"
 /* The LPC snoop on port 80h is mapped to this dbus service. */
 #define SNOOP_BUSNAME "xyz.openbmc_project.State.Boot.Raw"
+
+template <typename... T>
+using ServerObject = typename sdbusplus::server::object::object<T...>;
+using PostInterface = sdbusplus::xyz::openbmc_project::State::Boot::server::Raw;
+using PostObject = ServerObject<PostInterface>;
+
+class PostReporter : public PostObject
+{
+  public:
+    PostReporter(sdbusplus::bus::bus& bus, const char* objPath, bool defer) :
+        PostObject(bus, objPath, defer)
+    {
+    }
+};
diff --git a/main.cpp b/main.cpp
index 2e79e2d..8037564 100644
--- a/main.cpp
+++ b/main.cpp
@@ -25,26 +25,8 @@
 #include <memory>
 #include <thread>
 
-#include <sdbusplus/bus.hpp>
-#include <sdbusplus/server.hpp>
-#include "xyz/openbmc_project/State/Boot/Raw/server.hpp"
-
 #include "lpcsnoop/snoop.hpp"
 
-template <typename... T>
-using ServerObject = typename sdbusplus::server::object::object<T...>;
-using PostInterface = sdbusplus::xyz::openbmc_project::State::Boot::server::Raw;
-using PostObject = ServerObject<PostInterface>;
-
-class PostReporter : public PostObject
-{
-  public:
-    PostReporter(sdbusplus::bus::bus& bus, const char* objPath, bool defer) :
-        PostObject(bus, objPath, defer)
-    {
-    }
-};
-
 static const char* snoopFilename = "/dev/aspeed-lpc-snoop0";
 
 /*
diff --git a/test/Makefile.am b/test/Makefile.am
index e28604d..cd2665a 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,9 +1,21 @@
 # Common flags for all binaries in this subdirectory.
 # Per-program specific flags can be specified using
 # prg_CPPFLAGS = ....
-AM_CPPFLAGS = -I${top_srcdir} $(GTEST_CFLAGS)
-AM_CXXFLAGS = $(PTHREAD_CFLAGS) $(GTEST_CFLAGS)
-AM_LDFLAGS = $(GTEST_LIBS) $(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS)
+AM_CPPFLAGS = -I${top_srcdir} \
+              $(GTEST_CFLAGS) \
+              $(GMOCK_CFLAGS) \
+              $(SDBUSPLUS_CFLAGS) \
+              $(PHOSPHOR_DBUS_INTERFACES_CFLAGS)
+
+AM_CXXFLAGS = $(PTHREAD_CFLAGS) \
+              $(GTEST_CFLAGS)
+
+AM_LDFLAGS = $(GTEST_LIBS) \
+             $(PTHREAD_LIBS) \
+             $(GMOCK_LIBS) \
+             $(OESDK_TESTCASE_FLAGS) \
+             $(SDBUSPLUS_LIBS) \
+             $(PHOSPHOR_DBUS_INTERFACES_LIBS)
 
 # The list of unit test programs we are building and running.
 check_PROGRAMS = post_reporter_test
diff --git a/test/post_reporter_test.cpp b/test/post_reporter_test.cpp
index 738982d..d1a158e 100644
--- a/test/post_reporter_test.cpp
+++ b/test/post_reporter_test.cpp
@@ -1,10 +1,82 @@
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/test/sdbus_mock.hpp>
 
+#include "lpcsnoop/snoop.hpp"
+
+using ::testing::IsNull;
+using ::testing::NiceMock;
+using ::testing::Return;
+using ::testing::StrEq;
+using ::testing::_;
+
+namespace
+{
+
+// Fixture for testing class PostReporter
 class PostReporterTest : public ::testing::Test
 {
+  protected:
+    PostReporterTest() : bus_mock(), bus(sdbusplus::get_mocked_new(&bus_mock))
+    {
+    }
+
+    ~PostReporterTest()
+    {
+    }
+
+    NiceMock<sdbusplus::SdBusMock> bus_mock;
+    sdbusplus::bus::bus bus;
 };
 
-TEST(PostReporterTest, DummyTest)
+TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath)
 {
-    EXPECT_EQ(1, 1);
+
+    EXPECT_CALL(bus_mock,
+                sd_bus_emit_object_added(IsNull(), StrEq(SNOOP_OBJECTPATH)))
+        .WillOnce(Return(0));
+
+    PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+    testReporter.emit_object_added();
 }
+
+TEST_F(PostReporterTest, AddsObjectWithExpectedName)
+{
+    EXPECT_CALL(bus_mock,
+                sd_bus_add_object_vtable(IsNull(), _, StrEq(SNOOP_OBJECTPATH),
+                                         StrEq(SNOOP_BUSNAME), _, _))
+        .WillOnce(Return(0));
+
+    PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+}
+
+TEST_F(PostReporterTest, ValueReadsDefaultToZero)
+{
+    PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+    EXPECT_EQ(0, testReporter.value());
+}
+
+TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
+{
+    PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+    testReporter.value(65537);
+    EXPECT_EQ(65537, testReporter.value());
+}
+
+TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
+{
+    PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+    testReporter.value(123);
+    EXPECT_EQ(123, testReporter.value());
+    testReporter.value(456);
+    EXPECT_EQ(456, testReporter.value());
+    testReporter.value(0);
+    EXPECT_EQ(0, testReporter.value());
+    testReporter.value(456);
+    EXPECT_EQ(456, testReporter.value());
+    testReporter.value(456);
+    EXPECT_EQ(456, testReporter.value());
+}
+
+} // namespace