Move version gtests to phosphor-bmc-code-mgmt

Moving the version gtests from openpower-pnor-code-mgmt to
phosphor-bmc-code-mgmt. This code has been reviewed before,
the only changes are renaming to "namespace phosphor",
changes related to renaming the files, and changes related
to renaming function getVersion to getValue.

Change-Id: Id799fcfff8effbb4c63f1f4e7169aa425567dd41
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/.gitignore b/.gitignore
index 6b00353..f521e66 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,8 +13,6 @@
 # http://www.gnu.org/software/autoconf
 
 /autom4te.cache
-/autoscan.log
-/autoscan-*.log
 /aclocal.m4
 /compile
 /config.guess
@@ -36,13 +34,15 @@
 /texinfo.tex
 
 # Repo Specific Items
-/*.o
+*.o
+*.trs
 /config.h
 /config.h.in~
-/config.log
+*.log
 /config.status
 /phosphor-version-software-manager
 /phosphor-download-manager
 Makefile
 .deps
+/test/utest
 *-libtool
diff --git a/Makefile.am b/Makefile.am
index fbc8cbf..c324986 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -39,3 +39,5 @@
 phosphor_version_software_manager_LDFLAGS = $(generic_ldflags)
 phosphor_download_manager_CXXFLAGS = $(generic_cxxflags)
 phosphor_download_manager_LDFLAGS = $(generic_ldflags)
+
+SUBDIRS = test
diff --git a/configure.ac b/configure.ac
index 1928422..6f00959 100755
--- a/configure.ac
+++ b/configure.ac
@@ -22,6 +22,27 @@
 # Checks for library functions
 LT_INIT # Required for systemd linking
 
+# 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])
+)
+
 # Checks for typedefs, structures, and compiler characteristics.
 AX_CXX_COMPILE_STDCXX_14([noext])
 AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
@@ -47,5 +68,5 @@
 AC_CHECK_HEADER(systemd/sd-bus.h, ,[AC_MSG_ERROR([Could not find systemd/sd-bus.h...systemd developement package required])])
 AC_CHECK_HEADER(sdbusplus/server.hpp, ,[AC_MSG_ERROR([Could not find sdbusplus/server.hpp...openbmc/sdbusplus package required])])
 
-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 100755
index 0000000..187aedf
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,16 @@
+AM_CPPFLAGS = -I$(top_srcdir)
+
+# gtest unit tests which run during a 'make check'
+check_PROGRAMS = utest
+
+# Run all 'check' test programs
+TESTS = $(check_PROGRAMS)
+
+# Build/add utest to test suite
+utest_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS)
+utest_CXXFLAGS = $(PTHREAD_CFLAGS) $(PHOSPHOR_LOGGING_CFLAGS)
+utest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS) \
+	$(PHOSPHOR_LOGGING_LIBS) -lstdc++fs
+
+utest_SOURCES = utest.cpp
+utest_LDADD = $(top_builddir)/phosphor_version_software_manager-version.o
diff --git a/test/README.md b/test/README.md
new file mode 100755
index 0000000..6e62971
--- /dev/null
+++ b/test/README.md
@@ -0,0 +1,20 @@
+Instructions on how to execute UTEST.
+
+* When using an SDK - make sure it has been built
+  for an x86 machine.
+
+- First, generate the configure script with a call
+  to ./bootstrap.sh.
+- Once configure script generated, configure with flags
+  "./configure ${CONFIGURE_FLAGS} --enable-oe-sdk"
+- Be sure to include --enable-oe-sdk or the tests
+  will not run properly.
+- Lastly "make check" will build the application
+  source along with unit tests.  Make check also
+  runs the unit tests automatically.
+
+* WHEN RUNNING UTEST remember to take advantage
+  of the gtest capabilities. "./utest --help"
+  - --gtest_repeat=[COUNT]
+  - --gtest_shuffle
+  - --gtest_random_seed=[NUMBER]
diff --git a/test/utest.cpp b/test/utest.cpp
new file mode 100755
index 0000000..3195a2f
--- /dev/null
+++ b/test/utest.cpp
@@ -0,0 +1,67 @@
+#include "version.hpp"
+#include <gtest/gtest.h>
+#include <experimental/filesystem>
+#include <stdlib.h>
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+using namespace phosphor::software::manager;
+namespace fs = std::experimental::filesystem;
+
+
+class VersionTest : public testing::Test
+{
+    protected:
+
+        virtual void SetUp()
+        {
+            char versionDir[] = "./versionXXXXXX";
+            _directory = mkdtemp(versionDir);
+
+            if (_directory.empty())
+            {
+                throw std::bad_alloc();
+            }
+        }
+
+        virtual void TearDown()
+        {
+            fs::remove_all(_directory);
+        }
+
+        std::string _directory;
+};
+
+/** @brief Make sure we correctly get the version and purpose from getValue()*/
+TEST_F(VersionTest, TestGetValue)
+{
+    auto manifestFilePath = _directory + "/" + "MANIFEST";
+    auto version = "test-version";
+    auto purpose = "BMC";
+
+    std::ofstream file;
+    file.open(manifestFilePath, std::ofstream::out);
+    ASSERT_TRUE(file.is_open());
+
+    file << "version=" << version << std::endl;
+    file << "purpose=" << purpose << std::endl;
+    file.close();
+
+    EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
+    EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
+}
+
+/** @brief Make sure we correctly get the Id from getId()*/
+TEST_F(VersionTest, TestGetId)
+{
+    std::stringstream hexId;
+    auto version = "test-id";
+
+    hexId << std::hex << ((std::hash<std::string> {}(
+                               version)) & 0xFFFFFFFF);
+
+    EXPECT_EQ(Version::getId(version), hexId.str());
+
+}