Lei YU | b150134 | 2019-03-01 16:23:29 +0800 | [diff] [blame] | 1 | #include "version.hpp" |
| 2 | |
Adriana Kobylak | 5e0b237 | 2021-12-08 21:59:30 +0000 | [diff] [blame] | 3 | #include <openssl/evp.h> |
Lei YU | b150134 | 2019-03-01 16:23:29 +0800 | [diff] [blame] | 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | using namespace openpower::software::updater; |
| 8 | |
Adriana Kobylak | 5e0b237 | 2021-12-08 21:59:30 +0000 | [diff] [blame] | 9 | using EVP_MD_CTX_Ptr = |
| 10 | std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>; |
| 11 | |
Lei YU | b150134 | 2019-03-01 16:23:29 +0800 | [diff] [blame] | 12 | /** @brief Make sure we correctly get the Id from getId()*/ |
| 13 | TEST(VersionTest, TestGetId) |
| 14 | { |
| 15 | auto version = "test-id"; |
Adriana Kobylak | 5e0b237 | 2021-12-08 21:59:30 +0000 | [diff] [blame] | 16 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 17 | unsigned int digest_count = 0; |
| 18 | |
| 19 | EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free); |
| 20 | |
| 21 | EVP_DigestInit(ctx.get(), EVP_sha512()); |
| 22 | EVP_DigestUpdate(ctx.get(), version, strlen(version)); |
| 23 | EVP_DigestFinal(ctx.get(), digest, &digest_count); |
| 24 | |
| 25 | char mdString[EVP_MAX_MD_SIZE * 2 + 1]; |
| 26 | for (decltype(digest_count) i = 0; i < digest_count; i++) |
Lei YU | b150134 | 2019-03-01 16:23:29 +0800 | [diff] [blame] | 27 | { |
| 28 | snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]); |
| 29 | } |
| 30 | std::string hexId = std::string(mdString); |
| 31 | hexId = hexId.substr(0, 8); |
| 32 | EXPECT_EQ(Version::getId(version), hexId); |
| 33 | } |
| 34 | |
| 35 | TEST(VersionTest, GetVersions) |
| 36 | { |
| 37 | constexpr auto versionString = |
| 38 | "open-power-romulus-v2.2-rc1-48-g268344f-dirty\n" |
| 39 | "\tbuildroot-2018.11.1-7-g5d7cc8c\n" |
| 40 | "\tskiboot-v6.2\n" |
| 41 | "\thostboot-3f1f218-pea87ca7\n" |
| 42 | "\tocc-12c8088\n" |
| 43 | "\tlinux-4.19.13-openpower1-p8031295\n" |
| 44 | "\tpetitboot-1.9.2\n" |
| 45 | "\tmachine-xml-7410460\n" |
| 46 | "\thostboot-binaries-hw121518a.930\n" |
| 47 | "\tcapp-ucode-p9-dd2-v4\n" |
| 48 | "\tsbe-cf61dc3\n" |
| 49 | "\thcode-hw123119a.930"; |
| 50 | |
Patrick Williams | 7fb6c34 | 2023-05-10 07:50:18 -0500 | [diff] [blame] | 51 | const auto& [version, |
| 52 | extendedVersion] = Version::getVersions(versionString); |
Lei YU | b150134 | 2019-03-01 16:23:29 +0800 | [diff] [blame] | 53 | EXPECT_EQ(version, "open-power-romulus-v2.2-rc1-48-g268344f-dirty"); |
| 54 | EXPECT_EQ(extendedVersion, "buildroot-2018.11.1-7-g5d7cc8c," |
| 55 | "skiboot-v6.2," |
| 56 | "hostboot-3f1f218-pea87ca7," |
| 57 | "occ-12c8088," |
| 58 | "linux-4.19.13-openpower1-p8031295," |
| 59 | "petitboot-1.9.2," |
| 60 | "machine-xml-7410460," |
| 61 | "hostboot-binaries-hw121518a.930," |
| 62 | "capp-ucode-p9-dd2-v4," |
| 63 | "sbe-cf61dc3," |
| 64 | "hcode-hw123119a.930"); |
| 65 | } |