blob: 431afc0cfcb64cafe5e1b07e9206a47cf0642de1 [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
17extended_version=model=dummy_model,manufacture=dummy_manufacture)";
18}
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"
24 "extended_version=model=dummy_model,manufacture=dummy_manufacture\r\n";
25
Lei YUfda15a32019-09-19 14:43:02 +080026class TestVersion : public ::testing::Test
27{
28 public:
29 TestVersion()
30 {
31 auto tmpPath = fs::temp_directory_path();
32 tmpDir = (tmpPath / "test_XXXXXX");
33 if (!mkdtemp(tmpDir.data()))
34 {
35 throw "Failed to create temp dir";
36 }
37 }
38 ~TestVersion()
39 {
40 fs::remove_all(tmpDir);
41 }
42
43 void writeFile(const fs::path& file, const char* data)
44 {
45 std::ofstream f{file};
46 f << data;
47 f.close();
48 }
49 std::string tmpDir;
50};
51
52TEST_F(TestVersion, getValuesFileNotExist)
53{
54 auto ret = Version::getValues("NotExist.file", {""});
55 EXPECT_TRUE(ret.empty());
56}
57
58TEST_F(TestVersion, getValuesOK)
59{
60 auto manifestFilePath = fs::path(tmpDir) / "MANIFEST";
61 writeFile(manifestFilePath, validManifest);
62 auto ret = Version::getValues(manifestFilePath.string(),
63 {"purpose", "version", "extended_version"});
64 EXPECT_EQ(3u, ret.size());
65 auto purpose = ret["purpose"];
66 auto version = ret["version"];
67 auto extVersion = ret["extended_version"];
68
69 EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU",
70 purpose);
71 EXPECT_EQ("psu-dummy-test.v0.1", version);
72 EXPECT_EQ("model=dummy_model,manufacture=dummy_manufacture", extVersion);
73}
Lei YU9edb7332019-09-19 14:46:19 +080074
75TEST_F(TestVersion, getExtVersionInfo)
76{
77 std::string extVersion = "";
78 auto ret = Version::getExtVersionInfo(extVersion);
79 EXPECT_TRUE(ret.empty());
80
81 extVersion = "manufacturer=TestManu,model=TestModel";
82 ret = Version::getExtVersionInfo(extVersion);
83 EXPECT_EQ(2u, ret.size());
84 EXPECT_EQ("TestManu", ret["manufacturer"]);
85 EXPECT_EQ("TestModel", ret["model"]);
86}
Lei YUd0f8bbb2019-10-18 15:49:31 +080087
88TEST_F(TestVersion, getValuesOKonCRLFFormat)
89{
90 auto manifestFilePath = fs::path(tmpDir) / "MANIFEST";
91 writeFile(manifestFilePath, validManifestWithCRLF);
92 auto ret = Version::getValues(manifestFilePath.string(),
93 {"purpose", "version", "extended_version"});
94 EXPECT_EQ(3u, ret.size());
95 auto purpose = ret["purpose"];
96 auto version = ret["version"];
97 auto extVersion = ret["extended_version"];
98
99 EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU",
100 purpose);
101 EXPECT_EQ("psu-dummy-test.v0.1", version);
102 EXPECT_EQ("model=dummy_model,manufacture=dummy_manufacture", extVersion);
103}