blob: f7777427051f3be319ed5b56758291bd3aba7030 [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
14
15class VersionTest : public testing::Test
16{
17 protected:
18
19 virtual void SetUp()
20 {
21 char versionDir[] = "./versionXXXXXX";
22 _directory = mkdtemp(versionDir);
23
24 if (_directory.empty())
25 {
26 throw std::bad_alloc();
27 }
28 }
29
30 virtual void TearDown()
31 {
32 fs::remove_all(_directory);
33 }
34
35 std::string _directory;
36};
37
38/** @brief Make sure we correctly get the version and purpose from getValue()*/
39TEST_F(VersionTest, TestGetValue)
40{
41 auto manifestFilePath = _directory + "/" + "MANIFEST";
42 auto version = "test-version";
43 auto purpose = "BMC";
44
45 std::ofstream file;
46 file.open(manifestFilePath, std::ofstream::out);
47 ASSERT_TRUE(file.is_open());
48
49 file << "version=" << version << std::endl;
50 file << "purpose=" << purpose << std::endl;
51 file.close();
52
53 EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
54 EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
55}
56
57/** @brief Make sure we correctly get the Id from getId()*/
58TEST_F(VersionTest, TestGetId)
59{
Gunnar Mills01d55c32017-04-20 10:52:15 -050060 auto version = "test-id";
Saqib Khan26a960d2017-09-19 14:23:28 -050061 unsigned char digest[SHA512_DIGEST_LENGTH];
62 SHA512_CTX ctx;
63 SHA512_Init(&ctx);
64 SHA512_Update(&ctx, version, strlen(version));
65 SHA512_Final(digest, &ctx);
66 char mdString[SHA512_DIGEST_LENGTH*2+1];
67 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
68 {
69 snprintf(&mdString[i*2], 3, "%02x", (unsigned int)digest[i]);
70 }
71 std::string hexId = std::string(mdString);
72 hexId = hexId.substr(0, 8);
73 EXPECT_EQ(Version::getId(version), hexId);
Gunnar Mills01d55c32017-04-20 10:52:15 -050074}