Add unit tests for the policy table

Change-Id: I60ec83dfb3c4a65d20502d4680501a55ed6f0278
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/.gitignore b/.gitignore
index 020c1fe..90e7bd9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,6 +35,7 @@
 
 # Repo Specific Items
 ibm-log-manager
+test_policy
 .*.swp
 *.o
 *.trs
diff --git a/Makefile.am b/Makefile.am
index 67d142c..9a086b5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -18,3 +18,5 @@
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 	$(SDBUSPLUS_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS)
+
+SUBDIRS = . test
diff --git a/configure.ac b/configure.ac
index 9bbe9fb..b58aebe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -26,6 +26,27 @@
 
 LT_INIT
 
+# Check/set gtest specific functions.
+AX_PTHREAD([GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=1"],[GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=0"])
+AC_SUBST(GTEST_CPPFLAGS)
+AC_ARG_ENABLE([oe-sdk],
+    AS_HELP_STRING([--enable-oe-sdk], [Link testcases absolutely against OE SDK so they can be ran within it.])
+)
+AC_ARG_VAR(OECORE_TARGET_SYSROOT,
+    [Path to the OE SDK SYSROOT])
+AS_IF([test "x$enable_oe_sdk" == "xyes"],
+    AS_IF([test "x$OECORE_TARGET_SYSROOT" == "x"],
+          AC_MSG_ERROR([OECORE_TARGET_SYSROOT must be set with --enable-oe-sdk])
+    )
+    AC_MSG_NOTICE([Enabling OE-SDK at $OECORE_TARGET_SYSROOT])
+    [
+        testcase_flags="-Wl,-rpath,\${OECORE_TARGET_SYSROOT}/lib"
+        testcase_flags="${testcase_flags} -Wl,-rpath,\${OECORE_TARGET_SYSROOT}/usr/lib"
+        testcase_flags="${testcase_flags} -Wl,-dynamic-linker,`find \${OECORE_TARGET_SYSROOT}/lib/ld-*.so | sort -r -n | head -n1`"
+    ]
+    AC_SUBST([OESDK_TESTCASE_FLAGS], [$testcase_flags])
+)
+
 AC_DEFINE(LOGGING_PATH, "/xyz/openbmc_project/logging",
           [The xyz log manager DBus object path])
 AC_DEFINE(LOGGING_IFACE, "xyz.openbmc_project.Logging.Entry",
@@ -35,5 +56,5 @@
 AC_DEFINE(IBM_LOGGING_BUSNAME, "com.ibm.Logging",
           [The IBM log manager DBus busname to own])
 
-AC_CONFIG_FILES([Makefile])
+AC_CONFIG_FILES([Makefile test/Makefile])
 AC_OUTPUT
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..bbb71b5
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,16 @@
+AM_CPPFLAGS = -I$(top_srcdir)
+
+TESTS = $(check_PROGRAMS)
+
+check_PROGRAMS = test_policy
+test_policy_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS)
+
+test_policy_CXXFLAGS = $(PTHREAD_CFLAGS) $(SDBUSPLUS_CFLAGS)
+test_policy_LDFLAGS = -lgtest_main -lgtest \
+	$(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS)
+
+test_policy_SOURCES = test_policy.cpp
+test_policy_LDADD = \
+	$(top_builddir)/policy_table.o \
+	-lstdc++fs \
+	$(SDBUSPLUS_LIBS)
diff --git a/test/test_policy.cpp b/test/test_policy.cpp
new file mode 100644
index 0000000..9e28d08
--- /dev/null
+++ b/test/test_policy.cpp
@@ -0,0 +1,188 @@
+/**
+ * Copyright © 2018 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <fstream>
+#include <gtest/gtest.h>
+#include <experimental/filesystem>
+#include "policy_table.hpp"
+
+using namespace ibm::logging;
+namespace fs = std::experimental::filesystem;
+
+static constexpr auto json = R"(
+[
+    {
+    "dtls":[
+      {
+        "CEID":"ABCD1234",
+        "mod":"",
+        "msg":"Error ABCD1234"
+      }
+    ],
+    "err":"xyz.openbmc_project.Error.Test1"
+    },
+
+    {
+    "dtls":[
+      {
+        "CEID":"XYZ222",
+        "mod":"",
+        "msg":"Error XYZ222"
+      }
+    ],
+    "err":"xyz.openbmc_project.Error.Test2"
+    },
+
+    {
+    "dtls":[
+      {
+        "CEID":"AAAAAA",
+        "mod":"mod1",
+        "msg":"Error AAAAAA"
+      },
+      {
+        "CEID":"BBBBBB",
+        "mod":"mod2",
+        "msg":"Error BBBBBB"
+      },
+      {
+        "CEID":"CCCCCC",
+        "mod":"mod3",
+        "msg":"Error CCCCCC"
+      }
+    ],
+    "err":"xyz.openbmc_project.Error.Test3"
+    },
+
+    {
+    "dtls":[
+      {
+        "CEID":"DDDDDDDD",
+        "mod":"I2C",
+        "msg":"Error DDDDDDDD"
+      },
+      {
+        "CEID":"EEEEEEEE",
+        "mod":"FSI",
+        "msg":"Error EEEEEEEE"
+      }
+    ],
+    "err":"xyz.openbmc_project.Error.Test4"
+    },
+
+    {
+    "dtls":[
+      {
+        "CEID":"FFFFFFFF",
+        "mod":"6D",
+        "msg":"Error FFFFFFFF"
+      }
+    ],
+
+    "err":"xyz.openbmc_project.Error.Test5"
+    },
+    {
+    "dtls":[
+      {
+        "CEID":"GGGGGGGG",
+        "mod":"RAIL_5",
+        "msg":"Error GGGGGGGG"
+      }
+    ],
+
+    "err":"xyz.openbmc_project.Error.Test6"
+    },
+    {
+    "dtls":[
+      {
+        "CEID":"HHHHHHHH",
+        "mod":"INPUT_42",
+        "msg":"Error HHHHHHHH"
+      }
+    ],
+    "err":"xyz.openbmc_project.Error.Test7"
+    }
+])";
+
+
+/**
+ * Helper class to write the above json to a file and then
+ * remove it when the tests are over.
+ */
+class PolicyTableTest : public ::testing::Test
+{
+    protected:
+
+        virtual void SetUp()
+        {
+            char dir[] = {"./jsonTestXXXXXX"};
+
+            jsonDir = mkdtemp(dir);
+            jsonFile = jsonDir / "policy.json";
+
+            std::ofstream f{jsonFile};
+            f << json;
+        }
+
+        virtual void TearDown()
+        {
+            fs::remove_all(jsonDir);
+        }
+
+        fs::path jsonDir;
+        fs::path jsonFile;
+};
+
+
+/**
+ * Test finding entries in the policy table
+ */
+TEST_F(PolicyTableTest, TestTable)
+{
+    policy::Table policy{jsonFile};
+    ASSERT_EQ(policy.isLoaded(), true);
+
+    ////////////////////////////////////
+    //Basic search, no modifier
+    std::string err{"xyz.openbmc_project.Error.Test2"};
+    std::string mod;
+
+    auto details = policy.find(err, mod);
+    ASSERT_EQ(static_cast<bool>(details), true);
+    if (details)
+    {
+        ASSERT_EQ((*details).get().ceid, "XYZ222");
+        ASSERT_EQ((*details).get().msg, "Error XYZ222");
+    }
+
+    /////////////////////////////////////
+    //Not found
+    err = "foo";
+    details = policy.find(err, mod);
+    ASSERT_EQ(static_cast<bool>(details), false);
+
+    /////////////////////////////////////
+    //Test with a modifier
+    err = "xyz.openbmc_project.Error.Test3";
+    mod = "mod3";
+
+    details = policy.find(err, mod);
+    ASSERT_EQ(static_cast<bool>(details), true);
+    if (details)
+    {
+        ASSERT_EQ((*details).get().ceid, "CCCCCC");
+        ASSERT_EQ((*details).get().msg, "Error CCCCCC");
+    }
+}