1fda15a33SLei YU #include "version.hpp"
2fda15a33SLei YU
3fda15a33SLei YU #include <filesystem>
4fda15a33SLei YU #include <fstream>
5fda15a33SLei YU
6fda15a33SLei YU #include <gtest/gtest.h>
7fda15a33SLei YU
8fda15a33SLei YU using phosphor::software::updater::Version;
9fda15a33SLei YU
10fda15a33SLei YU namespace fs = std::filesystem;
11fda15a33SLei YU
12fda15a33SLei YU namespace
13fda15a33SLei YU {
14fda15a33SLei YU constexpr auto validManifest = R"(
15fda15a33SLei YU purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU
16fda15a33SLei YU version=psu-dummy-test.v0.1
174ba2edccSChicago Duan extended_version=model=dummy_model,manufacturer=dummy_manufacturer)";
18fda15a33SLei YU }
19fda15a33SLei YU
20d0f8bbb3SLei YU constexpr auto validManifestWithCRLF =
21d0f8bbb3SLei YU "\r\n"
22d0f8bbb3SLei YU "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU\r\n"
23d0f8bbb3SLei YU "version=psu-dummy-test.v0.1\r\n"
244ba2edccSChicago Duan "extended_version=model=dummy_model,manufacturer=dummy_manufacturer\r\n";
25d0f8bbb3SLei YU
26fda15a33SLei YU class TestVersion : public ::testing::Test
27fda15a33SLei YU {
28fda15a33SLei YU public:
2966a54ad4SGeorge Liu TestVersion(const TestVersion&) = delete;
3066a54ad4SGeorge Liu TestVersion& operator=(const TestVersion&) = delete;
3166a54ad4SGeorge Liu TestVersion(TestVersion&&) = delete;
3266a54ad4SGeorge Liu TestVersion& operator=(TestVersion&&) = delete;
3366a54ad4SGeorge Liu
TestVersion()34fda15a33SLei YU TestVersion()
35fda15a33SLei YU {
36fda15a33SLei YU auto tmpPath = fs::temp_directory_path();
37fda15a33SLei YU tmpDir = (tmpPath / "test_XXXXXX");
38fda15a33SLei YU if (!mkdtemp(tmpDir.data()))
39fda15a33SLei YU {
40fda15a33SLei YU throw "Failed to create temp dir";
41fda15a33SLei YU }
42fda15a33SLei YU }
~TestVersion()43047d9944SGeorge Liu ~TestVersion() override
44fda15a33SLei YU {
45fda15a33SLei YU fs::remove_all(tmpDir);
46fda15a33SLei YU }
47fda15a33SLei YU
writeFile(const fs::path & file,const char * data)48*80c2daaeSGeorge Liu static void writeFile(const fs::path& file, const char* data)
49fda15a33SLei YU {
50fda15a33SLei YU std::ofstream f{file};
51fda15a33SLei YU f << data;
52fda15a33SLei YU f.close();
53fda15a33SLei YU }
54fda15a33SLei YU std::string tmpDir;
55fda15a33SLei YU };
56fda15a33SLei YU
TEST_F(TestVersion,getValuesFileNotExist)57fda15a33SLei YU TEST_F(TestVersion, getValuesFileNotExist)
58fda15a33SLei YU {
59fda15a33SLei YU auto ret = Version::getValues("NotExist.file", {""});
60fda15a33SLei YU EXPECT_TRUE(ret.empty());
61fda15a33SLei YU }
62fda15a33SLei YU
TEST_F(TestVersion,getValuesOK)63fda15a33SLei YU TEST_F(TestVersion, getValuesOK)
64fda15a33SLei YU {
65fda15a33SLei YU auto manifestFilePath = fs::path(tmpDir) / "MANIFEST";
66fda15a33SLei YU writeFile(manifestFilePath, validManifest);
67fda15a33SLei YU auto ret = Version::getValues(manifestFilePath.string(),
68fda15a33SLei YU {"purpose", "version", "extended_version"});
6922c2fbd8SGeorge Liu EXPECT_EQ(3U, ret.size());
70fda15a33SLei YU auto purpose = ret["purpose"];
71fda15a33SLei YU auto version = ret["version"];
72fda15a33SLei YU auto extVersion = ret["extended_version"];
73fda15a33SLei YU
74fda15a33SLei YU EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU",
75fda15a33SLei YU purpose);
76fda15a33SLei YU EXPECT_EQ("psu-dummy-test.v0.1", version);
774ba2edccSChicago Duan EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion);
78fda15a33SLei YU }
799edb7330SLei YU
TEST_F(TestVersion,getExtVersionInfo)809edb7330SLei YU TEST_F(TestVersion, getExtVersionInfo)
819edb7330SLei YU {
829edb7330SLei YU std::string extVersion = "";
839edb7330SLei YU auto ret = Version::getExtVersionInfo(extVersion);
849edb7330SLei YU EXPECT_TRUE(ret.empty());
859edb7330SLei YU
869edb7330SLei YU extVersion = "manufacturer=TestManu,model=TestModel";
879edb7330SLei YU ret = Version::getExtVersionInfo(extVersion);
8822c2fbd8SGeorge Liu EXPECT_EQ(2U, ret.size());
899edb7330SLei YU EXPECT_EQ("TestManu", ret["manufacturer"]);
909edb7330SLei YU EXPECT_EQ("TestModel", ret["model"]);
919edb7330SLei YU }
92d0f8bbb3SLei YU
TEST_F(TestVersion,getValuesOKonCRLFFormat)93d0f8bbb3SLei YU TEST_F(TestVersion, getValuesOKonCRLFFormat)
94d0f8bbb3SLei YU {
95d0f8bbb3SLei YU auto manifestFilePath = fs::path(tmpDir) / "MANIFEST";
96d0f8bbb3SLei YU writeFile(manifestFilePath, validManifestWithCRLF);
97d0f8bbb3SLei YU auto ret = Version::getValues(manifestFilePath.string(),
98d0f8bbb3SLei YU {"purpose", "version", "extended_version"});
9922c2fbd8SGeorge Liu EXPECT_EQ(3U, ret.size());
100d0f8bbb3SLei YU auto purpose = ret["purpose"];
101d0f8bbb3SLei YU auto version = ret["version"];
102d0f8bbb3SLei YU auto extVersion = ret["extended_version"];
103d0f8bbb3SLei YU
104d0f8bbb3SLei YU EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU",
105d0f8bbb3SLei YU purpose);
106d0f8bbb3SLei YU EXPECT_EQ("psu-dummy-test.v0.1", version);
1074ba2edccSChicago Duan EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion);
108d0f8bbb3SLei YU }
109