blob: ac0aa7cdc7ba7308c3297aa592d67b3194735c26 [file] [log] [blame]
Gunnar Mills01d55c32017-04-20 10:52:15 -05001#include "version.hpp"
2#include <gtest/gtest.h>
3#include <experimental/filesystem>
4#include <stdlib.h>
5#include <fstream>
6#include <iostream>
7#include <sstream>
8#include <string>
Saqib Khan26a960d2017-09-19 14:23:28 -05009#include <openssl/sha.h>
Gunnar Mills01d55c32017-04-20 10:52:15 -050010
11using namespace phosphor::software::manager;
12namespace fs = std::experimental::filesystem;
13
Gunnar Mills01d55c32017-04-20 10:52:15 -050014class VersionTest : public testing::Test
15{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060016 protected:
17 virtual void SetUp()
18 {
19 char versionDir[] = "./versionXXXXXX";
20 _directory = mkdtemp(versionDir);
Gunnar Mills01d55c32017-04-20 10:52:15 -050021
Adriana Kobylak2285fe02018-02-27 15:36:59 -060022 if (_directory.empty())
Gunnar Mills01d55c32017-04-20 10:52:15 -050023 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -060024 throw std::bad_alloc();
Gunnar Mills01d55c32017-04-20 10:52:15 -050025 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060026 }
Gunnar Mills01d55c32017-04-20 10:52:15 -050027
Adriana Kobylak2285fe02018-02-27 15:36:59 -060028 virtual void TearDown()
29 {
30 fs::remove_all(_directory);
31 }
Gunnar Mills01d55c32017-04-20 10:52:15 -050032
Adriana Kobylak2285fe02018-02-27 15:36:59 -060033 std::string _directory;
Gunnar Mills01d55c32017-04-20 10:52:15 -050034};
35
36/** @brief Make sure we correctly get the version and purpose from getValue()*/
37TEST_F(VersionTest, TestGetValue)
38{
39 auto manifestFilePath = _directory + "/" + "MANIFEST";
40 auto version = "test-version";
41 auto purpose = "BMC";
42
43 std::ofstream file;
44 file.open(manifestFilePath, std::ofstream::out);
45 ASSERT_TRUE(file.is_open());
46
47 file << "version=" << version << std::endl;
48 file << "purpose=" << purpose << std::endl;
49 file.close();
50
51 EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
52 EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
53}
54
55/** @brief Make sure we correctly get the Id from getId()*/
56TEST_F(VersionTest, TestGetId)
57{
Gunnar Mills01d55c32017-04-20 10:52:15 -050058 auto version = "test-id";
Saqib Khan26a960d2017-09-19 14:23:28 -050059 unsigned char digest[SHA512_DIGEST_LENGTH];
60 SHA512_CTX ctx;
61 SHA512_Init(&ctx);
62 SHA512_Update(&ctx, version, strlen(version));
63 SHA512_Final(digest, &ctx);
Adriana Kobylak2285fe02018-02-27 15:36:59 -060064 char mdString[SHA512_DIGEST_LENGTH * 2 + 1];
Saqib Khan26a960d2017-09-19 14:23:28 -050065 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
66 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -060067 snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
Saqib Khan26a960d2017-09-19 14:23:28 -050068 }
69 std::string hexId = std::string(mdString);
70 hexId = hexId.substr(0, 8);
71 EXPECT_EQ(Version::getId(version), hexId);
Gunnar Mills01d55c32017-04-20 10:52:15 -050072}