blob: ee41bf1b9256b376af6965fb2a218b866ded1e88 [file] [log] [blame]
Lei YUfda15a32019-09-19 14:43:02 +08001#include "version.hpp"
2
3#include <filesystem>
4#include <fstream>
5
6#include <gtest/gtest.h>
7
8using phosphor::software::updater::Version;
9
10namespace fs = std::filesystem;
11
12namespace
13{
14constexpr auto validManifest = R"(
15purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU
16version=psu-dummy-test.v0.1
Chicago Duan4ba2edc2023-01-06 16:10:59 +080017extended_version=model=dummy_model,manufacturer=dummy_manufacturer)";
Lei YUfda15a32019-09-19 14:43:02 +080018}
19
Lei YUd0f8bbb2019-10-18 15:49:31 +080020constexpr auto validManifestWithCRLF =
21 "\r\n"
22 "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU\r\n"
23 "version=psu-dummy-test.v0.1\r\n"
Chicago Duan4ba2edc2023-01-06 16:10:59 +080024 "extended_version=model=dummy_model,manufacturer=dummy_manufacturer\r\n";
Lei YUd0f8bbb2019-10-18 15:49:31 +080025
Lei YUfda15a32019-09-19 14:43:02 +080026class TestVersion : public ::testing::Test
27{
28 public:
George Liu66a54ad2024-08-23 13:53:39 +080029 TestVersion(const TestVersion&) = delete;
30 TestVersion& operator=(const TestVersion&) = delete;
31 TestVersion(TestVersion&&) = delete;
32 TestVersion& operator=(TestVersion&&) = delete;
33
Lei YUfda15a32019-09-19 14:43:02 +080034 TestVersion()
35 {
36 auto tmpPath = fs::temp_directory_path();
37 tmpDir = (tmpPath / "test_XXXXXX");
38 if (!mkdtemp(tmpDir.data()))
39 {
40 throw "Failed to create temp dir";
41 }
42 }
George Liu047d9942024-08-23 13:44:31 +080043 ~TestVersion() override
Lei YUfda15a32019-09-19 14:43:02 +080044 {
45 fs::remove_all(tmpDir);
46 }
47
George Liu80c2daa2024-08-23 15:46:12 +080048 static void writeFile(const fs::path& file, const char* data)
Lei YUfda15a32019-09-19 14:43:02 +080049 {
50 std::ofstream f{file};
51 f << data;
52 f.close();
53 }
54 std::string tmpDir;
55};
56
57TEST_F(TestVersion, getValuesFileNotExist)
58{
59 auto ret = Version::getValues("NotExist.file", {""});
60 EXPECT_TRUE(ret.empty());
61}
62
63TEST_F(TestVersion, getValuesOK)
64{
65 auto manifestFilePath = fs::path(tmpDir) / "MANIFEST";
66 writeFile(manifestFilePath, validManifest);
67 auto ret = Version::getValues(manifestFilePath.string(),
68 {"purpose", "version", "extended_version"});
George Liu22c2fbd2024-08-23 15:23:21 +080069 EXPECT_EQ(3U, ret.size());
Lei YUfda15a32019-09-19 14:43:02 +080070 auto purpose = ret["purpose"];
71 auto version = ret["version"];
72 auto extVersion = ret["extended_version"];
73
74 EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU",
75 purpose);
76 EXPECT_EQ("psu-dummy-test.v0.1", version);
Chicago Duan4ba2edc2023-01-06 16:10:59 +080077 EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion);
Lei YUfda15a32019-09-19 14:43:02 +080078}
Lei YU9edb7332019-09-19 14:46:19 +080079
80TEST_F(TestVersion, getExtVersionInfo)
81{
82 std::string extVersion = "";
83 auto ret = Version::getExtVersionInfo(extVersion);
84 EXPECT_TRUE(ret.empty());
85
86 extVersion = "manufacturer=TestManu,model=TestModel";
87 ret = Version::getExtVersionInfo(extVersion);
George Liu22c2fbd2024-08-23 15:23:21 +080088 EXPECT_EQ(2U, ret.size());
Lei YU9edb7332019-09-19 14:46:19 +080089 EXPECT_EQ("TestManu", ret["manufacturer"]);
90 EXPECT_EQ("TestModel", ret["model"]);
91}
Lei YUd0f8bbb2019-10-18 15:49:31 +080092
93TEST_F(TestVersion, getValuesOKonCRLFFormat)
94{
95 auto manifestFilePath = fs::path(tmpDir) / "MANIFEST";
96 writeFile(manifestFilePath, validManifestWithCRLF);
97 auto ret = Version::getValues(manifestFilePath.string(),
98 {"purpose", "version", "extended_version"});
George Liu22c2fbd2024-08-23 15:23:21 +080099 EXPECT_EQ(3U, ret.size());
Lei YUd0f8bbb2019-10-18 15:49:31 +0800100 auto purpose = ret["purpose"];
101 auto version = ret["version"];
102 auto extVersion = ret["extended_version"];
103
104 EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU",
105 purpose);
106 EXPECT_EQ("psu-dummy-test.v0.1", version);
Chicago Duan4ba2edc2023-01-06 16:10:59 +0800107 EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion);
Lei YUd0f8bbb2019-10-18 15:49:31 +0800108}