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: |
| 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 | |
| 52 | TEST_F(TestVersion, getValuesFileNotExist) |
| 53 | { |
| 54 | auto ret = Version::getValues("NotExist.file", {""}); |
| 55 | EXPECT_TRUE(ret.empty()); |
| 56 | } |
| 57 | |
| 58 | TEST_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); |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 72 | EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion); |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 73 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 74 | |
| 75 | TEST_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 YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 87 | |
| 88 | TEST_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); |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 102 | EXPECT_EQ("model=dummy_model,manufacturer=dummy_manufacturer", extVersion); |
Lei YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 103 | } |