blob: 3195a2f47da8e70c31933fba175709621f295e8c [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>
9
10using namespace phosphor::software::manager;
11namespace fs = std::experimental::filesystem;
12
13
14class VersionTest : public testing::Test
15{
16 protected:
17
18 virtual void SetUp()
19 {
20 char versionDir[] = "./versionXXXXXX";
21 _directory = mkdtemp(versionDir);
22
23 if (_directory.empty())
24 {
25 throw std::bad_alloc();
26 }
27 }
28
29 virtual void TearDown()
30 {
31 fs::remove_all(_directory);
32 }
33
34 std::string _directory;
35};
36
37/** @brief Make sure we correctly get the version and purpose from getValue()*/
38TEST_F(VersionTest, TestGetValue)
39{
40 auto manifestFilePath = _directory + "/" + "MANIFEST";
41 auto version = "test-version";
42 auto purpose = "BMC";
43
44 std::ofstream file;
45 file.open(manifestFilePath, std::ofstream::out);
46 ASSERT_TRUE(file.is_open());
47
48 file << "version=" << version << std::endl;
49 file << "purpose=" << purpose << std::endl;
50 file.close();
51
52 EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
53 EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
54}
55
56/** @brief Make sure we correctly get the Id from getId()*/
57TEST_F(VersionTest, TestGetId)
58{
59 std::stringstream hexId;
60 auto version = "test-id";
61
62 hexId << std::hex << ((std::hash<std::string> {}(
63 version)) & 0xFFFFFFFF);
64
65 EXPECT_EQ(Version::getId(version), hexId.str());
66
67}