Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 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 |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 17 | extended_version=model=dummy_model,manufacturer=dummy_manufacturer)"; |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 18 | } |
| 19 | |
Lei YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 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" |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 24 | "extended_version=model=dummy_model,manufacturer=dummy_manufacturer\r\n"; |
Lei YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 25 | |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 26 | class TestVersion : public ::testing::Test |
| 27 | { |
| 28 | public: |
George Liu | 66a54ad | 2024-08-23 13:53:39 +0800 | [diff] [blame] | 29 | TestVersion(const TestVersion&) = delete; |
| 30 | TestVersion& operator=(const TestVersion&) = delete; |
| 31 | TestVersion(TestVersion&&) = delete; |
| 32 | TestVersion& operator=(TestVersion&&) = delete; |
| 33 | |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 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 | } |
George Liu | 047d994 | 2024-08-23 13:44:31 +0800 | [diff] [blame] | 43 | ~TestVersion() override |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 44 | { |
| 45 | fs::remove_all(tmpDir); |
| 46 | } |
| 47 | |
George Liu | 80c2daa | 2024-08-23 15:46:12 +0800 | [diff] [blame] | 48 | static void writeFile(const fs::path& file, const char* data) |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 49 | { |
| 50 | std::ofstream f{file}; |
| 51 | f << data; |
| 52 | f.close(); |
| 53 | } |
| 54 | std::string tmpDir; |
| 55 | }; |
| 56 | |
| 57 | TEST_F(TestVersion, getValuesFileNotExist) |
| 58 | { |
| 59 | auto ret = Version::getValues("NotExist.file", {""}); |
| 60 | EXPECT_TRUE(ret.empty()); |
| 61 | } |
| 62 | |
| 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"}); |
George Liu | 22c2fbd | 2024-08-23 15:23:21 +0800 | [diff] [blame] | 69 | EXPECT_EQ(3U, ret.size()); |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 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); |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 77 | EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion); |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 78 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 79 | |
| 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); |
George Liu | 22c2fbd | 2024-08-23 15:23:21 +0800 | [diff] [blame] | 88 | EXPECT_EQ(2U, ret.size()); |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 89 | EXPECT_EQ("TestManu", ret["manufacturer"]); |
| 90 | EXPECT_EQ("TestModel", ret["model"]); |
| 91 | } |
Lei YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 92 | |
| 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"}); |
George Liu | 22c2fbd | 2024-08-23 15:23:21 +0800 | [diff] [blame] | 99 | EXPECT_EQ(3U, ret.size()); |
Lei YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 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); |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 107 | EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion); |
Lei YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 108 | } |