1 #include "version.hpp"
2 
3 #include <filesystem>
4 #include <fstream>
5 
6 #include <gtest/gtest.h>
7 
8 using phosphor::software::updater::Version;
9 
10 namespace fs = std::filesystem;
11 
12 namespace
13 {
14 constexpr auto validManifest = R"(
15 purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU
16 version=psu-dummy-test.v0.1
17 extended_version=model=dummy_model,manufacturer=dummy_manufacturer)";
18 }
19 
20 constexpr 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"
24     "extended_version=model=dummy_model,manufacturer=dummy_manufacturer\r\n";
25 
26 class TestVersion : public ::testing::Test
27 {
28   public:
29     TestVersion(const TestVersion&) = delete;
30     TestVersion& operator=(const TestVersion&) = delete;
31     TestVersion(TestVersion&&) = delete;
32     TestVersion& operator=(TestVersion&&) = delete;
33 
TestVersion()34     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     }
~TestVersion()43     ~TestVersion() override
44     {
45         fs::remove_all(tmpDir);
46     }
47 
writeFile(const fs::path & file,const char * data)48     static void writeFile(const fs::path& file, const char* data)
49     {
50         std::ofstream f{file};
51         f << data;
52         f.close();
53     }
54     std::string tmpDir;
55 };
56 
TEST_F(TestVersion,getValuesFileNotExist)57 TEST_F(TestVersion, getValuesFileNotExist)
58 {
59     auto ret = Version::getValues("NotExist.file", {""});
60     EXPECT_TRUE(ret.empty());
61 }
62 
TEST_F(TestVersion,getValuesOK)63 TEST_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"});
69     EXPECT_EQ(3U, ret.size());
70     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);
77     EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion);
78 }
79 
TEST_F(TestVersion,getExtVersionInfo)80 TEST_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);
88     EXPECT_EQ(2U, ret.size());
89     EXPECT_EQ("TestManu", ret["manufacturer"]);
90     EXPECT_EQ("TestModel", ret["model"]);
91 }
92 
TEST_F(TestVersion,getValuesOKonCRLFFormat)93 TEST_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"});
99     EXPECT_EQ(3U, ret.size());
100     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);
107     EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion);
108 }
109