Refactor Version::getValue()
Rename the function to getValues() and change the parameter and return
value types, and add a test case for it.
It will be used in future commits.
Tested: Verify the unit test case passes.
Signed-off-by: Lei YU <mine260309@gmail.com>
Change-Id: I57ccf857737ef13f4e2f27c5f2fb7400a2170e91
diff --git a/test/meson.build b/test/meson.build
index 0b7b217..2de2b21 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -44,6 +44,7 @@
'../src/version.cpp',
'test_item_updater.cpp',
'test_activation.cpp',
+ 'test_version.cpp',
include_directories: [psu_inc, test_inc],
link_args: dynamic_linker,
build_rpath: oe_sdk.enabled() ? rpath : '',
diff --git a/test/test_version.cpp b/test/test_version.cpp
new file mode 100644
index 0000000..cba599f
--- /dev/null
+++ b/test/test_version.cpp
@@ -0,0 +1,67 @@
+#include "version.hpp"
+
+#include <filesystem>
+#include <fstream>
+
+#include <gtest/gtest.h>
+
+using phosphor::software::updater::Version;
+
+namespace fs = std::filesystem;
+
+namespace
+{
+constexpr auto validManifest = R"(
+purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU
+version=psu-dummy-test.v0.1
+extended_version=model=dummy_model,manufacture=dummy_manufacture)";
+}
+
+class TestVersion : public ::testing::Test
+{
+ public:
+ TestVersion()
+ {
+ auto tmpPath = fs::temp_directory_path();
+ tmpDir = (tmpPath / "test_XXXXXX");
+ if (!mkdtemp(tmpDir.data()))
+ {
+ throw "Failed to create temp dir";
+ }
+ }
+ ~TestVersion()
+ {
+ fs::remove_all(tmpDir);
+ }
+
+ void writeFile(const fs::path& file, const char* data)
+ {
+ std::ofstream f{file};
+ f << data;
+ f.close();
+ }
+ std::string tmpDir;
+};
+
+TEST_F(TestVersion, getValuesFileNotExist)
+{
+ auto ret = Version::getValues("NotExist.file", {""});
+ EXPECT_TRUE(ret.empty());
+}
+
+TEST_F(TestVersion, getValuesOK)
+{
+ auto manifestFilePath = fs::path(tmpDir) / "MANIFEST";
+ writeFile(manifestFilePath, validManifest);
+ auto ret = Version::getValues(manifestFilePath.string(),
+ {"purpose", "version", "extended_version"});
+ EXPECT_EQ(3u, ret.size());
+ auto purpose = ret["purpose"];
+ auto version = ret["version"];
+ auto extVersion = ret["extended_version"];
+
+ EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU",
+ purpose);
+ EXPECT_EQ("psu-dummy-test.v0.1", version);
+ EXPECT_EQ("model=dummy_model,manufacture=dummy_manufacture", extVersion);
+}