Add gtests
Added gtests for getVersion() and getId()
Resolves openbmc/openbmc#1276.
Change-Id: I10a824ae3da426e901defc34fa7f3c6c6a8496d3
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/test/utest.cpp b/test/utest.cpp
new file mode 100644
index 0000000..4da45a5
--- /dev/null
+++ b/test/utest.cpp
@@ -0,0 +1,64 @@
+#include "version_host_software_manager.hpp"
+#include <gtest/gtest.h>
+#include <experimental/filesystem>
+#include <stdlib.h>
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+using namespace openpower::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 from getVersion()*/
+TEST_F(VersionTest, TestGetVersion)
+{
+ auto tocFilePath = _directory + "/" + "pnor.toc";
+ auto version = "test-version";
+
+ std::ofstream file;
+ file.open(tocFilePath, std::ofstream::out);
+ ASSERT_TRUE(file.is_open());
+
+ file << "version=" << version << std::endl;
+ file.close();
+
+ EXPECT_EQ(Version::getVersion(tocFilePath), version);
+}
+
+/** @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());
+
+}