Gunnar Mills | 01d55c3 | 2017-04-20 10:52:15 -0500 | [diff] [blame] | 1 | #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 Khan | 26a960d | 2017-09-19 14:23:28 -0500 | [diff] [blame] | 9 | #include <openssl/sha.h> |
Gunnar Mills | 01d55c3 | 2017-04-20 10:52:15 -0500 | [diff] [blame] | 10 | |
| 11 | using namespace phosphor::software::manager; |
| 12 | namespace fs = std::experimental::filesystem; |
| 13 | |
| 14 | |
| 15 | class 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()*/ |
| 39 | TEST_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()*/ |
| 58 | TEST_F(VersionTest, TestGetId) |
| 59 | { |
Gunnar Mills | 01d55c3 | 2017-04-20 10:52:15 -0500 | [diff] [blame] | 60 | auto version = "test-id"; |
Saqib Khan | 26a960d | 2017-09-19 14:23:28 -0500 | [diff] [blame] | 61 | 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 Mills | 01d55c3 | 2017-04-20 10:52:15 -0500 | [diff] [blame] | 74 | } |