Add unit tests for policy::find()

Test that everything works when passing in a property
map that contains an error message and the additional
data.  This exercises the code that finds a search
modifier.

Change-Id: If3abcea41aa1f98b97262f1e8a0483e1772e9842
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index bbb71b5..a699696 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,5 +12,6 @@
 test_policy_SOURCES = test_policy.cpp
 test_policy_LDADD = \
 	$(top_builddir)/policy_table.o \
+	$(top_builddir)/policy_find.o \
 	-lstdc++fs \
 	$(SDBUSPLUS_LIBS)
diff --git a/test/test_policy.cpp b/test/test_policy.cpp
index 9e28d08..0993e8f 100644
--- a/test/test_policy.cpp
+++ b/test/test_policy.cpp
@@ -17,6 +17,7 @@
 #include <gtest/gtest.h>
 #include <experimental/filesystem>
 #include "policy_table.hpp"
+#include "policy_find.hpp"
 
 using namespace ibm::logging;
 namespace fs = std::experimental::filesystem;
@@ -186,3 +187,152 @@
         ASSERT_EQ((*details).get().msg, "Error CCCCCC");
     }
 }
+
+/**
+ * Test policy::find() that uses the data from a property
+ * map to find entries in the policy table.
+ */
+TEST_F(PolicyTableTest, TestFinder)
+{
+    using namespace std::literals::string_literals;
+
+    policy::Table policy{jsonFile};
+    ASSERT_EQ(policy.isLoaded(), true);
+
+    //A basic search with no modifier
+    {
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test1"s}}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), "ABCD1234");
+        ASSERT_EQ(std::get<policy::MsgField>(values), "Error ABCD1234");
+    }
+
+    //Use CALLOUT_INVENTORY_PATH from the AdditionalData property
+    {
+        std::vector<std::string> ad{
+                "FOO=BAR"s,
+                "CALLOUT_INVENTORY_PATH=mod2"s};
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test3"s}},
+            {"AdditionalData"s, ad}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), "BBBBBB");
+        ASSERT_EQ(std::get<policy::MsgField>(values), "Error BBBBBB");
+    }
+
+    //Use an I2C DEVICE_PATH from the AdditionalData property
+    {
+        std::vector<std::string> ad{
+                "FOO=BAR"s,
+                "CALLOUT_DEVICE_PATH=/some/i2c/path"s};
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}},
+            {"AdditionalData"s, ad}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), "DDDDDDDD");
+        ASSERT_EQ(std::get<policy::MsgField>(values), "Error DDDDDDDD");
+    }
+
+    //Use an FSI DEVICE_PATH from the AdditionalData property
+    {
+        std::vector<std::string> ad{
+                "FOO=BAR"s,
+                "CALLOUT_DEVICE_PATH=/some/fsi/path"s};
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}},
+            {"AdditionalData"s, ad}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), "EEEEEEEE");
+        ASSERT_EQ(std::get<policy::MsgField>(values), "Error EEEEEEEE");
+    }
+
+    //Use PROCEDURE from the AdditionalData property
+    {
+        std::vector<std::string> ad{
+                "FOO=BAR"s,
+                "PROCEDURE=109"s};
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test5"s}},
+            {"AdditionalData"s, ad}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), "FFFFFFFF");
+        ASSERT_EQ(std::get<policy::MsgField>(values), "Error FFFFFFFF");
+    }
+
+    //Use RAIL_NAME from the AdditionalData property
+    {
+        std::vector<std::string> ad{
+                "FOO=BAR"s,
+                "RAIL_NAME=RAIL_5"s};
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test6"s}},
+            {"AdditionalData"s, ad}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), "GGGGGGGG");
+        ASSERT_EQ(std::get<policy::MsgField>(values), "Error GGGGGGGG");
+    }
+
+    //Use INPUT_NAME from the AdditionalData property
+    {
+        std::vector<std::string> ad{
+                "FOO=BAR"s,
+                "INPUT_NAME=INPUT_42"s};
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}},
+            {"AdditionalData"s, ad}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), "HHHHHHHH");
+        ASSERT_EQ(std::get<policy::MsgField>(values), "Error HHHHHHHH");
+    }
+
+    //Test not finding an entry.
+    {
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"hello world"s}}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), policy.defaultEID());
+        ASSERT_EQ(std::get<policy::MsgField>(values), policy.defaultMsg());
+    }
+
+    // Test that strange AdditionalData values don't break anything
+    {
+        std::vector<std::string> ad{
+                "FOO"s,
+                "INPUT_NAME="s};
+        DbusPropertyMap testProperties
+        {
+            {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}},
+            {"AdditionalData"s, ad}
+        };
+
+        auto values = policy::find(policy, testProperties);
+        ASSERT_EQ(std::get<policy::EIDField>(values), policy.defaultEID());
+        ASSERT_EQ(std::get<policy::MsgField>(values), policy.defaultMsg());
+    }
+}
+