Unit test framework for phosphor-debug-collector

Change:
-Serialize method does not use any of the watch functionalities,
so defining set in serialization header as using watch header
creates unnecessary dependency.

Test:
Added 3 test cases related to dump serialization.
Ran the UTs on docker environment.

Result:
1/1 debug_inif_test OK             1.48s

Ok:                 1
Expected Fail:      0
Fail:               0
Unexpected Pass:    0
Skipped:            0
Timeout:            0

Signed-off-by: Chirag Sharma <chirshar@in.ibm.com>
Change-Id: I6a88559bf407fa70a96dda3b31aaa1acd038c958
diff --git a/README.md b/README.md
index dfdfc31..e5c9d1d 100644
--- a/README.md
+++ b/README.md
@@ -10,3 +10,12 @@
 ```
 To clean the built files run `ninja -C builddir clean`.
 
+## To run unit tests
+Tests can be run in the CI docker container, refer
+[local-ci-build](https://github.com/openbmc/docs/blob/master/testing/local-ci-build.md)
+
+or with an OpenBMC x86 sdk(see below for x86 steps).
+```
+meson -Doe-sdk=enabled -Dtests=enabled build
+ninja -C build test
+```
diff --git a/dump_serialize.hpp b/dump_serialize.hpp
index 9199da7..8ad4a24 100644
--- a/dump_serialize.hpp
+++ b/dump_serialize.hpp
@@ -2,9 +2,8 @@
 
 #include "config.h"
 
-#include "elog_watch.hpp"
-
 #include <experimental/filesystem>
+#include <set>
 
 namespace phosphor
 {
@@ -12,6 +11,8 @@
 {
 namespace elog
 {
+using EId = uint32_t;
+using ElogList = std::set<EId>;
 
 namespace fs = std::experimental::filesystem;
 
diff --git a/meson.build b/meson.build
index 8bbed76..5dfe67d 100644
--- a/meson.build
+++ b/meson.build
@@ -153,3 +153,7 @@
                         install : executable[3]
                        )
 endforeach
+
+if get_option('tests').enabled()
+  subdir('test')
+endif
diff --git a/meson_options.txt b/meson_options.txt
index da8d8e7..75d97a8 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -1,5 +1,9 @@
 # SPDX-License-Identifier: Apache-2.0
 
+option('tests', type: 'feature', description: 'Build tests')
+
+option('oe-sdk', type: 'feature', description: 'Enable OE SDK')
+
 option('ubifs-workaround', type: 'feature',
         description : 'Turn on ubi workaround for core file'
       )
diff --git a/test/debug_inif_test.cpp b/test/debug_inif_test.cpp
new file mode 100644
index 0000000..04fb6cf
--- /dev/null
+++ b/test/debug_inif_test.cpp
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: Apache-2.0
+#include <cstdlib>
+#include <dump_serialize.hpp>
+#include <exception>
+#include <filesystem>
+#include <set>
+#include <string>
+
+#include <gtest/gtest.h>
+
+namespace fs = std::filesystem;
+
+class TestDumpSerial : public ::testing::Test
+{
+  public:
+    TestDumpSerial()
+    {
+    }
+
+    void SetUp()
+    {
+        char tmpdir[] = "/tmp/dump.XXXXXX";
+        auto dirPtr = mkdtemp(tmpdir);
+        if (dirPtr == NULL)
+        {
+            throw std::bad_alloc();
+        }
+        dumpDir = std::string(dirPtr);
+        fs::create_directories(dumpDir);
+        dumpFile = dumpDir;
+        dumpFile /= "elogid";
+    }
+    void TearDown()
+    {
+        fs::remove_all(dumpDir);
+    }
+
+    std::string dumpDir;
+    fs::path dumpFile;
+};
+
+TEST_F(TestDumpSerial, Serialization)
+{
+    using ElogList = std::set<uint32_t>;
+    ElogList e;
+    e.insert(1);
+    e.insert(2);
+    e.insert(3);
+    phosphor::dump::elog::serialize(e, dumpFile.c_str());
+    bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e);
+    EXPECT_EQ(value, true);
+}
+
+TEST_F(TestDumpSerial, DeserializationFalseCase)
+{
+    using ElogList = std::set<uint32_t>;
+    ElogList e;
+    e.insert(1);
+    bool value = phosphor::dump::elog::deserialize(dumpFile.c_str(), e);
+    EXPECT_EQ(value, false);
+}
+
+TEST(DumpDeSerialPath, DeserializationFalsePath)
+{
+    using ElogList = std::set<uint32_t>;
+    ElogList e;
+    e.insert(1);
+    // Providing dummy path
+    bool value = phosphor::dump::elog::deserialize("/tmp/Fake/serial", e);
+    EXPECT_EQ(value, false);
+}
diff --git a/test/meson.build b/test/meson.build
new file mode 100644
index 0000000..d375acc
--- /dev/null
+++ b/test/meson.build
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: Apache-2.0
+if get_option('oe-sdk').enabled()
+    # Setup OE SYSROOT
+    OECORE_TARGET_SYSROOT = run_command('sh', '-c', 'echo $OECORE_TARGET_SYSROOT').stdout().strip()
+    if OECORE_TARGET_SYSROOT == ''
+        error('Unable to get OECORE_TARGET_SYSROOT, check your environment.')
+    endif
+    message('OECORE_TARGET_SYSROOT: ' + OECORE_TARGET_SYSROOT)
+    rpath = ':'.join([OECORE_TARGET_SYSROOT + '/lib', OECORE_TARGET_SYSROOT + '/usr/lib'])
+    ld_so = run_command('sh', '-c', 'find ' + OECORE_TARGET_SYSROOT + '/lib/ld-*.so | sort -r -n | head -n1').stdout().strip()
+    dynamic_linker = ['-Wl,-dynamic-linker,' + ld_so]
+else
+    dynamic_linker = []
+endif
+
+gtest = dependency('gtest', main: true, disabler: true, required: true)
+gmock = dependency('gmock', disabler: true, required: true)
+
+dump = declare_dependency(
+         sources: [
+        '../dump_serialize.cpp'
+    ])
+
+tests = [
+    'debug_inif_test',
+]
+
+foreach t : tests
+  test(t, executable(t.underscorify(), t + '.cpp',
+                     include_directories: ['.', '../'],
+                     implicit_include_directories: false,
+                     link_args: dynamic_linker,
+                     build_rpath: get_option('oe-sdk').enabled() ? rpath : '',
+                     dependencies:[ gtest,
+                                    gmock,
+                                    dump,
+                                    phosphor_logging,
+                                    cppfs]),
+       workdir: meson.current_source_dir())
+endforeach