blob: b91a461325c3a2b12c025d936f2784a361a3aa7e [file] [log] [blame]
Gunnar Millsb0ce9962018-09-07 13:39:10 -05001#include "image_verify.hpp"
George Liu0a06e972020-12-17 09:17:04 +08002#include "utils.hpp"
Gunnar Mills01d55c32017-04-20 10:52:15 -05003#include "version.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05004
5#include <openssl/sha.h>
Gunnar Mills01d55c32017-04-20 10:52:15 -05006#include <stdlib.h>
Gunnar Millsb0ce9962018-09-07 13:39:10 -05007
Adriana Kobylakc98d9122020-05-05 10:36:01 -05008#include <filesystem>
Gunnar Mills01d55c32017-04-20 10:52:15 -05009#include <fstream>
10#include <iostream>
11#include <sstream>
12#include <string>
George Liu0a06e972020-12-17 09:17:04 +080013#include <vector>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050014
15#include <gtest/gtest.h>
Gunnar Mills01d55c32017-04-20 10:52:15 -050016
17using namespace phosphor::software::manager;
Jayanth Othayoth6be275b2018-02-27 08:36:38 -060018using namespace phosphor::software::image;
Gunnar Mills01d55c32017-04-20 10:52:15 -050019
Gunnar Mills01d55c32017-04-20 10:52:15 -050020class VersionTest : public testing::Test
21{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060022 protected:
23 virtual void SetUp()
24 {
25 char versionDir[] = "./versionXXXXXX";
26 _directory = mkdtemp(versionDir);
Gunnar Mills01d55c32017-04-20 10:52:15 -050027
Adriana Kobylak2285fe02018-02-27 15:36:59 -060028 if (_directory.empty())
Gunnar Mills01d55c32017-04-20 10:52:15 -050029 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -060030 throw std::bad_alloc();
Gunnar Mills01d55c32017-04-20 10:52:15 -050031 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060032 }
Gunnar Mills01d55c32017-04-20 10:52:15 -050033
Adriana Kobylak2285fe02018-02-27 15:36:59 -060034 virtual void TearDown()
35 {
36 fs::remove_all(_directory);
37 }
Gunnar Mills01d55c32017-04-20 10:52:15 -050038
Adriana Kobylak2285fe02018-02-27 15:36:59 -060039 std::string _directory;
Gunnar Mills01d55c32017-04-20 10:52:15 -050040};
41
42/** @brief Make sure we correctly get the version and purpose from getValue()*/
43TEST_F(VersionTest, TestGetValue)
44{
45 auto manifestFilePath = _directory + "/" + "MANIFEST";
46 auto version = "test-version";
47 auto purpose = "BMC";
48
49 std::ofstream file;
50 file.open(manifestFilePath, std::ofstream::out);
51 ASSERT_TRUE(file.is_open());
52
Lei YU5a7363b2019-10-18 16:50:59 +080053 file << "version=" << version << "\n";
54 file << "purpose=" << purpose << "\n";
55 file.close();
56
57 EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
58 EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
59}
60
61TEST_F(VersionTest, TestGetValueWithCRLF)
62{
63 auto manifestFilePath = _directory + "/" + "MANIFEST";
64 auto version = "test-version";
65 auto purpose = "BMC";
66
67 std::ofstream file;
68 file.open(manifestFilePath, std::ofstream::out);
69 ASSERT_TRUE(file.is_open());
70
71 file << "version=" << version << "\r\n";
72 file << "purpose=" << purpose << "\r\n";
Gunnar Mills01d55c32017-04-20 10:52:15 -050073 file.close();
74
75 EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
76 EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
77}
78
Adriana Kobylak2a3d9f52020-05-06 10:46:32 -050079TEST_F(VersionTest, TestGetVersionWithQuotes)
80{
81 auto releasePath = _directory + "/" + "os-release";
82 auto version = "1.2.3-test-version";
83
84 std::ofstream file;
85 file.open(releasePath, std::ofstream::out);
86 ASSERT_TRUE(file.is_open());
87
88 file << "VERSION_ID=\"" << version << "\"\n";
89 file.close();
90
91 EXPECT_EQ(Version::getBMCVersion(releasePath), version);
92}
93
94TEST_F(VersionTest, TestGetVersionWithoutQuotes)
95{
96 auto releasePath = _directory + "/" + "os-release";
97 auto version = "9.88.1-test-version";
98
99 std::ofstream file;
100 file.open(releasePath, std::ofstream::out);
101 ASSERT_TRUE(file.is_open());
102
103 file << "VERSION_ID=" << version << "\n";
104 file.close();
105
106 EXPECT_EQ(Version::getBMCVersion(releasePath), version);
107}
108
Gunnar Mills01d55c32017-04-20 10:52:15 -0500109/** @brief Make sure we correctly get the Id from getId()*/
110TEST_F(VersionTest, TestGetId)
111{
Gunnar Mills01d55c32017-04-20 10:52:15 -0500112 auto version = "test-id";
Saqib Khan26a960d2017-09-19 14:23:28 -0500113 unsigned char digest[SHA512_DIGEST_LENGTH];
114 SHA512_CTX ctx;
115 SHA512_Init(&ctx);
116 SHA512_Update(&ctx, version, strlen(version));
117 SHA512_Final(digest, &ctx);
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600118 char mdString[SHA512_DIGEST_LENGTH * 2 + 1];
Saqib Khan26a960d2017-09-19 14:23:28 -0500119 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
120 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600121 snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
Saqib Khan26a960d2017-09-19 14:23:28 -0500122 }
123 std::string hexId = std::string(mdString);
124 hexId = hexId.substr(0, 8);
125 EXPECT_EQ(Version::getId(version), hexId);
Gunnar Mills01d55c32017-04-20 10:52:15 -0500126}
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600127
128class SignatureTest : public testing::Test
129{
130 static constexpr auto opensslCmd = "openssl dgst -sha256 -sign ";
131 static constexpr auto testPath = "/tmp/_testSig";
132
133 protected:
134 void command(const std::string& cmd)
135 {
136 auto val = std::system(cmd.c_str());
137 if (val)
138 {
139 std::cout << "COMMAND Error: " << val << std::endl;
140 }
141 }
142 virtual void SetUp()
143 {
144 // Create test base directory.
145 fs::create_directories(testPath);
146
147 // Create unique temporary path for images
148 std::string tmpDir(testPath);
149 tmpDir += "/extractXXXXXX";
150 std::string imageDir = mkdtemp(const_cast<char*>(tmpDir.c_str()));
151
152 // Create unique temporary configuration path
153 std::string tmpConfDir(testPath);
154 tmpConfDir += "/confXXXXXX";
155 std::string confDir = mkdtemp(const_cast<char*>(tmpConfDir.c_str()));
156
157 extractPath = imageDir;
158 extractPath /= "images";
159
160 signedConfPath = confDir;
161 signedConfPath /= "conf";
162
163 signedConfOpenBMCPath = confDir;
164 signedConfOpenBMCPath /= "conf";
165 signedConfOpenBMCPath /= "OpenBMC";
166
167 std::cout << "SETUP " << std::endl;
168
169 command("mkdir " + extractPath.string());
170 command("mkdir " + signedConfPath.string());
171 command("mkdir " + signedConfOpenBMCPath.string());
172
173 std::string hashFile = signedConfOpenBMCPath.string() + "/hashfunc";
174 command("echo \"HashType=RSA-SHA256\" > " + hashFile);
175
176 std::string manifestFile = extractPath.string() + "/" + "MANIFEST";
177 command("echo \"HashType=RSA-SHA256\" > " + manifestFile);
178 command("echo \"KeyType=OpenBMC\" >> " + manifestFile);
179
180 std::string kernelFile = extractPath.string() + "/" + "image-kernel";
181 command("echo \"image-kernel file \" > " + kernelFile);
182
183 std::string rofsFile = extractPath.string() + "/" + "image-rofs";
184 command("echo \"image-rofs file \" > " + rofsFile);
185
186 std::string rwfsFile = extractPath.string() + "/" + "image-rwfs";
187 command("echo \"image-rwfs file \" > " + rwfsFile);
188
189 std::string ubootFile = extractPath.string() + "/" + "image-u-boot";
190 command("echo \"image-u-boot file \" > " + ubootFile);
191
192 std::string pkeyFile = extractPath.string() + "/" + "private.pem";
193 command("openssl genrsa -out " + pkeyFile + " 2048");
194
195 std::string pubkeyFile = extractPath.string() + "/" + "publickey";
196 command("openssl rsa -in " + pkeyFile + " -outform PEM " +
197 "-pubout -out " + pubkeyFile);
198
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600199 command("cp " + pubkeyFile + " " + signedConfOpenBMCPath.string());
200 command(opensslCmd + pkeyFile + " -out " + kernelFile + ".sig " +
201 kernelFile);
202
203 command(opensslCmd + pkeyFile + " -out " + manifestFile + ".sig " +
204 manifestFile);
205 command(opensslCmd + pkeyFile + " -out " + rofsFile + ".sig " +
206 rofsFile);
207 command(opensslCmd + pkeyFile + " -out " + rwfsFile + ".sig " +
208 rwfsFile);
209 command(opensslCmd + pkeyFile + " -out " + ubootFile + ".sig " +
210 ubootFile);
211 command(opensslCmd + pkeyFile + " -out " + pubkeyFile + ".sig " +
212 pubkeyFile);
213
214 signature = std::make_unique<Signature>(extractPath, signedConfPath);
215 }
216 virtual void TearDown()
217 {
218 command("rm -rf " + std::string(testPath));
219 }
220
221 std::unique_ptr<Signature> signature;
222 fs::path extractPath;
223 fs::path signedConfPath;
224 fs::path signedConfOpenBMCPath;
225};
226
Gunnar Mills2bcba022018-04-08 15:02:04 -0500227/** @brief Test for success scenario*/
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600228TEST_F(SignatureTest, TestSignatureVerify)
229{
230 EXPECT_TRUE(signature->verify());
231}
232
233/** @brief Test failure scenario with corrupted signature file*/
234TEST_F(SignatureTest, TestCorruptSignatureFile)
235{
236 // corrupt the image-kernel.sig file and ensure that verification fails
237 std::string kernelFile = extractPath.string() + "/" + "image-kernel";
238 command("echo \"dummy data\" > " + kernelFile + ".sig ");
239 EXPECT_FALSE(signature->verify());
240}
241
242/** @brief Test failure scenario with no public key in the image*/
243TEST_F(SignatureTest, TestNoPublicKeyInImage)
244{
245 // Remove publickey file from the image and ensure that verify fails
246 std::string pubkeyFile = extractPath.string() + "/" + "publickey";
247 command("rm " + pubkeyFile);
248 EXPECT_FALSE(signature->verify());
249}
250
251/** @brief Test failure scenario with invalid hash function value*/
252TEST_F(SignatureTest, TestInvalidHashValue)
253{
254 // Change the hashfunc value and ensure that verification fails
255 std::string hashFile = signedConfOpenBMCPath.string() + "/hashfunc";
256 command("echo \"HashType=md5\" > " + hashFile);
257 EXPECT_FALSE(signature->verify());
258}
259
260/** @brief Test for failure scenario with no config file in system*/
261TEST_F(SignatureTest, TestNoConfigFileInSystem)
262{
263 // Remove the conf folder in the system and ensure that verify fails
264 command("rm -rf " + signedConfOpenBMCPath.string());
265 EXPECT_FALSE(signature->verify());
266}
George Liu0a06e972020-12-17 09:17:04 +0800267
268class FileTest : public testing::Test
269{
270 protected:
271 std::string readFile(fs::path path)
272 {
273 std::ifstream f(path, std::ios::in);
274 const auto sz = fs::file_size(path);
275 std::string result(sz, '\0');
276 f.read(result.data(), sz);
277
278 return result;
279 }
280
281 void command(const std::string& cmd)
282 {
283 auto val = std::system(cmd.c_str());
284 if (val)
285 {
286 std::cout << "COMMAND Error: " << val << std::endl;
287 }
288 }
289
290 virtual void SetUp()
291 {
292 // Create test base directory.
293 tmpDir = fs::temp_directory_path() / "testFileXXXXXX";
294 if (!mkdtemp(tmpDir.data()))
295 {
296 throw "Failed to create tmp dir";
297 }
298
299 std::string file1 = tmpDir + "/file1";
300 std::string file2 = tmpDir + "/file2";
301 command("echo \"File Test1\n\n\" > " + file1);
302 command("echo \"FileTe st2\n\nte st2\" > " + file2);
303
304 srcFiles.push_back(file1);
305 srcFiles.push_back(file2);
306 }
307
308 virtual void TearDown()
309 {
310 fs::remove_all(tmpDir);
311 }
312
313 std::vector<std::string> srcFiles;
314 std::string tmpDir;
315};
316
317TEST_F(FileTest, TestMergeFiles)
318{
319 std::string retFile = tmpDir + "/retFile";
320 for (auto file : srcFiles)
321 {
322 command("cat " + file + " >> " + retFile);
323 }
324
325 std::string dstFile = tmpDir + "/dstFile";
326 utils::mergeFiles(srcFiles, dstFile);
327
328 ASSERT_NE(fs::file_size(retFile), static_cast<uintmax_t>(-1));
329 ASSERT_NE(fs::file_size(dstFile), static_cast<uintmax_t>(-1));
330 ASSERT_EQ(fs::file_size(retFile), fs::file_size(dstFile));
331
332 std::string ssRetFile = readFile(fs::path(retFile));
333 std::string ssDstFile = readFile(fs::path(dstFile));
334 ASSERT_EQ(ssRetFile, ssDstFile);
335}