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