serialization: add tests

Resolves openbmc/openbmc#1684.

Change-Id: Ia554147001e51b05fe8692ae0b39e3efaf481130
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index ee0881b..c9946d7 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,12 +1,57 @@
-# gtest unit tests which run during a 'make check'
-check_PROGRAMS =
+AM_CPPFLAGS = -I${top_srcdir}
 
-# Run all 'check' test programs
 TESTS = $(check_PROGRAMS)
 
-# Basic test suite for elog interfaces
-check_PROGRAMS += elog_unittest
-elog_unittest_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS)
-elog_unittest_CXXFLAGS = $(PTHREAD_CFLAGS)
-elog_unittest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS)
-elog_unittest_SOURCES = elog_unittest.cpp
\ No newline at end of file
+check_PROGRAMS = \
+	elog_unittest \
+	serialization_test_path \
+	serialization_test_properties
+
+test_cppflags = \
+	-Igtest \
+	$(GTEST_CPPFLAGS) \
+	$(AM_CPPFLAGS) \
+	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
+	$(SDBUSPLUS_CFLAGS)
+
+test_cxxflags = \
+	$(PTHREAD_CFLAGS)
+
+test_ldflags = \
+	-lgtest_main -lgtest \
+	$(PTHREAD_LIBS) \
+	$(OESDK_TESTCASE_FLAGS) \
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
+	$(SDBUSPLUS_LIBS)
+
+test_ldadd = \
+	$(top_builddir)/elog_serialize.o \
+	$(top_builddir)/elog_entry.o \
+	$(top_builddir)/log_manager.o \
+	$(top_builddir)/org.openbmc.Associations.o \
+	$(top_builddir)/xyz/openbmc_project/Logging/Internal/Manager/server.o \
+	$(top_builddir)/elog_meta.o \
+	$(top_builddir)/elog-lookup.o \
+	$(top_builddir)/elog-process-metadata.o
+
+
+elog_unittest_CPPFLAGS = $(test_cppflags)
+elog_unittest_CXXFLAGS = $(test_cxxflags)
+elog_unittest_LDFLAGS = $(test_ldflags)
+elog_unittest_SOURCES = elog_unittest.cpp
+
+serialization_test_path_CPPFLAGS = $(test_cppflags)
+serialization_test_path_CXXFLAGS = $(test_cxxflags)
+serialization_test_path_SOURCES = serialization_test_path.cpp
+serialization_test_path_LDADD = $(test_ldadd)
+serialization_test_path_LDFLAGS = \
+	$(test_ldflags) \
+	-lstdc++fs
+
+serialization_test_properties_CPPFLAGS = $(test_cppflags)
+serialization_test_properties_CXXFLAGS = $(test_cxxflags)
+serialization_test_properties_SOURCES = serialization_test_properties.cpp
+serialization_test_properties_LDADD = $(test_ldadd)
+serialization_test_properties_LDFLAGS = \
+	$(test_ldflags) \
+	-lstdc++fs
diff --git a/test/serialization_test_path.cpp b/test/serialization_test_path.cpp
new file mode 100644
index 0000000..50e7d08
--- /dev/null
+++ b/test/serialization_test_path.cpp
@@ -0,0 +1,28 @@
+#include "serialization_tests.hpp"
+#include "elog_entry.hpp"
+#include "elog_serialize.hpp"
+
+namespace phosphor
+{
+namespace logging
+{
+namespace test
+{
+
+TEST_F(TestSerialization, testPath)
+{
+    auto id = 99;
+    auto e = std::make_unique<Entry>(
+                 bus,
+                 std::string(OBJ_ENTRY) + '/' + std::to_string(id),
+                 id,
+                 manager);
+    auto path = serialize(*e, TestSerialization::dir);
+    EXPECT_EQ(path.c_str(), TestSerialization::dir / std::to_string(id));
+}
+
+} // namespace test
+} // namespace logging
+} // namespace phosphor
+
+
diff --git a/test/serialization_test_properties.cpp b/test/serialization_test_properties.cpp
new file mode 100644
index 0000000..6c8ddf3
--- /dev/null
+++ b/test/serialization_test_properties.cpp
@@ -0,0 +1,53 @@
+#include "serialization_tests.hpp"
+#include "elog_entry.hpp"
+#include "elog_serialize.hpp"
+
+namespace phosphor
+{
+namespace logging
+{
+namespace test
+{
+
+TEST_F(TestSerialization, testProperties)
+{
+    auto id = 99;
+    phosphor::logging::AssociationList assocations{};
+    std::vector<std::string> testData{"additional", "data"};
+    uint64_t timestamp{100};
+    std::string message{"test error"};
+    auto input = std::make_unique<Entry>(
+                     bus,
+                     std::string(OBJ_ENTRY) + '/' + std::to_string(id),
+                     id,
+                     timestamp,
+                     Entry::Level::Informational,
+                     std::move(message),
+                     std::move(testData),
+                     std::move(assocations),
+                     manager);
+    auto path = serialize(*input, TestSerialization::dir);
+
+    auto idStr = path.filename().c_str();
+    id = std::stol(idStr);
+    auto output = std::make_unique<Entry>(
+                      bus,
+                      std::string(OBJ_ENTRY) + '/' + idStr,
+                      id,
+                      manager);
+    deserialize(path, *output);
+
+    EXPECT_EQ(input->id(), output->id());
+    EXPECT_EQ(input->severity(), output->severity());
+    EXPECT_EQ(input->timestamp(), output->timestamp());
+    EXPECT_EQ(input->message(), output->message());
+    EXPECT_EQ(input->additionalData(), output->additionalData());
+    EXPECT_EQ(input->resolved(), output->resolved());
+    EXPECT_EQ(input->associations(), output->associations());
+}
+
+} // namespace test
+} // namespace logging
+} // namespace phosphor
+
+
diff --git a/test/serialization_tests.hpp b/test/serialization_tests.hpp
new file mode 100644
index 0000000..111c7f7
--- /dev/null
+++ b/test/serialization_tests.hpp
@@ -0,0 +1,41 @@
+#include <gtest/gtest.h>
+#include <experimental/filesystem>
+#include <stdlib.h>
+#include <sdbusplus/bus.hpp>
+#include "log_manager.hpp"
+#include "config.h"
+
+namespace phosphor
+{
+namespace logging
+{
+namespace test
+{
+
+namespace fs = std::experimental::filesystem;
+
+char tmplt[] = "/tmp/logging_test.XXXXXX";
+auto bus = sdbusplus::bus::new_default();
+phosphor::logging::Manager manager(bus, OBJ_INTERNAL);
+
+class TestSerialization : public testing::Test
+{
+    public:
+        TestSerialization() :
+            dir(fs::path(mkdtemp(tmplt)))
+        {
+        }
+
+        ~TestSerialization()
+        {
+            fs::remove_all(dir);
+        }
+
+        fs::path dir;
+};
+
+} // namespace test
+} // namespace logging
+} // namespace phosphor
+
+