blob: 97d2ea4cbac5482adf7a75b0a226b36991497733 [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
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700128TEST_F(VersionTest, TestGetExtendedVersion)
129{
130 auto releasePath = _directory + "/" + "os-release";
131 auto ExtendedVersion = "9.88.1-test-ExtendedVersion";
132
133 std::ofstream file;
134 file.open(releasePath, std::ofstream::out);
135 ASSERT_TRUE(file.is_open());
136
137 file << "EXTENDED_VERSION=" << ExtendedVersion << "\n";
138 file.close();
139
140 EXPECT_EQ(Version::getBMCExtendedVersion(releasePath), ExtendedVersion);
141}
142
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600143class SignatureTest : public testing::Test
144{
145 static constexpr auto opensslCmd = "openssl dgst -sha256 -sign ";
146 static constexpr auto testPath = "/tmp/_testSig";
147
148 protected:
149 void command(const std::string& cmd)
150 {
151 auto val = std::system(cmd.c_str());
152 if (val)
153 {
154 std::cout << "COMMAND Error: " << val << std::endl;
155 }
156 }
157 virtual void SetUp()
158 {
159 // Create test base directory.
160 fs::create_directories(testPath);
161
162 // Create unique temporary path for images
163 std::string tmpDir(testPath);
164 tmpDir += "/extractXXXXXX";
165 std::string imageDir = mkdtemp(const_cast<char*>(tmpDir.c_str()));
166
167 // Create unique temporary configuration path
168 std::string tmpConfDir(testPath);
169 tmpConfDir += "/confXXXXXX";
170 std::string confDir = mkdtemp(const_cast<char*>(tmpConfDir.c_str()));
171
172 extractPath = imageDir;
173 extractPath /= "images";
174
175 signedConfPath = confDir;
176 signedConfPath /= "conf";
177
178 signedConfOpenBMCPath = confDir;
179 signedConfOpenBMCPath /= "conf";
180 signedConfOpenBMCPath /= "OpenBMC";
181
182 std::cout << "SETUP " << std::endl;
183
184 command("mkdir " + extractPath.string());
185 command("mkdir " + signedConfPath.string());
186 command("mkdir " + signedConfOpenBMCPath.string());
187
188 std::string hashFile = signedConfOpenBMCPath.string() + "/hashfunc";
189 command("echo \"HashType=RSA-SHA256\" > " + hashFile);
190
191 std::string manifestFile = extractPath.string() + "/" + "MANIFEST";
192 command("echo \"HashType=RSA-SHA256\" > " + manifestFile);
193 command("echo \"KeyType=OpenBMC\" >> " + manifestFile);
194
195 std::string kernelFile = extractPath.string() + "/" + "image-kernel";
196 command("echo \"image-kernel file \" > " + kernelFile);
197
198 std::string rofsFile = extractPath.string() + "/" + "image-rofs";
199 command("echo \"image-rofs file \" > " + rofsFile);
200
201 std::string rwfsFile = extractPath.string() + "/" + "image-rwfs";
202 command("echo \"image-rwfs file \" > " + rwfsFile);
203
204 std::string ubootFile = extractPath.string() + "/" + "image-u-boot";
205 command("echo \"image-u-boot file \" > " + ubootFile);
206
207 std::string pkeyFile = extractPath.string() + "/" + "private.pem";
208 command("openssl genrsa -out " + pkeyFile + " 2048");
209
210 std::string pubkeyFile = extractPath.string() + "/" + "publickey";
211 command("openssl rsa -in " + pkeyFile + " -outform PEM " +
212 "-pubout -out " + pubkeyFile);
213
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600214 command("cp " + pubkeyFile + " " + signedConfOpenBMCPath.string());
215 command(opensslCmd + pkeyFile + " -out " + kernelFile + ".sig " +
216 kernelFile);
217
218 command(opensslCmd + pkeyFile + " -out " + manifestFile + ".sig " +
219 manifestFile);
220 command(opensslCmd + pkeyFile + " -out " + rofsFile + ".sig " +
221 rofsFile);
222 command(opensslCmd + pkeyFile + " -out " + rwfsFile + ".sig " +
223 rwfsFile);
224 command(opensslCmd + pkeyFile + " -out " + ubootFile + ".sig " +
225 ubootFile);
226 command(opensslCmd + pkeyFile + " -out " + pubkeyFile + ".sig " +
227 pubkeyFile);
228
229 signature = std::make_unique<Signature>(extractPath, signedConfPath);
230 }
231 virtual void TearDown()
232 {
233 command("rm -rf " + std::string(testPath));
234 }
235
236 std::unique_ptr<Signature> signature;
237 fs::path extractPath;
238 fs::path signedConfPath;
239 fs::path signedConfOpenBMCPath;
240};
241
Gunnar Mills2bcba022018-04-08 15:02:04 -0500242/** @brief Test for success scenario*/
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600243TEST_F(SignatureTest, TestSignatureVerify)
244{
245 EXPECT_TRUE(signature->verify());
246}
247
248/** @brief Test failure scenario with corrupted signature file*/
249TEST_F(SignatureTest, TestCorruptSignatureFile)
250{
251 // corrupt the image-kernel.sig file and ensure that verification fails
252 std::string kernelFile = extractPath.string() + "/" + "image-kernel";
253 command("echo \"dummy data\" > " + kernelFile + ".sig ");
254 EXPECT_FALSE(signature->verify());
255}
256
257/** @brief Test failure scenario with no public key in the image*/
258TEST_F(SignatureTest, TestNoPublicKeyInImage)
259{
260 // Remove publickey file from the image and ensure that verify fails
261 std::string pubkeyFile = extractPath.string() + "/" + "publickey";
262 command("rm " + pubkeyFile);
263 EXPECT_FALSE(signature->verify());
264}
265
266/** @brief Test failure scenario with invalid hash function value*/
267TEST_F(SignatureTest, TestInvalidHashValue)
268{
269 // Change the hashfunc value and ensure that verification fails
270 std::string hashFile = signedConfOpenBMCPath.string() + "/hashfunc";
271 command("echo \"HashType=md5\" > " + hashFile);
272 EXPECT_FALSE(signature->verify());
273}
274
275/** @brief Test for failure scenario with no config file in system*/
276TEST_F(SignatureTest, TestNoConfigFileInSystem)
277{
278 // Remove the conf folder in the system and ensure that verify fails
279 command("rm -rf " + signedConfOpenBMCPath.string());
280 EXPECT_FALSE(signature->verify());
281}
George Liu0a06e972020-12-17 09:17:04 +0800282
283class FileTest : public testing::Test
284{
285 protected:
286 std::string readFile(fs::path path)
287 {
288 std::ifstream f(path, std::ios::in);
289 const auto sz = fs::file_size(path);
290 std::string result(sz, '\0');
291 f.read(result.data(), sz);
292
293 return result;
294 }
295
296 void command(const std::string& cmd)
297 {
298 auto val = std::system(cmd.c_str());
299 if (val)
300 {
301 std::cout << "COMMAND Error: " << val << std::endl;
302 }
303 }
304
305 virtual void SetUp()
306 {
307 // Create test base directory.
308 tmpDir = fs::temp_directory_path() / "testFileXXXXXX";
309 if (!mkdtemp(tmpDir.data()))
310 {
311 throw "Failed to create tmp dir";
312 }
313
314 std::string file1 = tmpDir + "/file1";
315 std::string file2 = tmpDir + "/file2";
316 command("echo \"File Test1\n\n\" > " + file1);
317 command("echo \"FileTe st2\n\nte st2\" > " + file2);
318
319 srcFiles.push_back(file1);
320 srcFiles.push_back(file2);
321 }
322
323 virtual void TearDown()
324 {
325 fs::remove_all(tmpDir);
326 }
327
328 std::vector<std::string> srcFiles;
329 std::string tmpDir;
330};
331
332TEST_F(FileTest, TestMergeFiles)
333{
334 std::string retFile = tmpDir + "/retFile";
335 for (auto file : srcFiles)
336 {
337 command("cat " + file + " >> " + retFile);
338 }
339
340 std::string dstFile = tmpDir + "/dstFile";
341 utils::mergeFiles(srcFiles, dstFile);
342
343 ASSERT_NE(fs::file_size(retFile), static_cast<uintmax_t>(-1));
344 ASSERT_NE(fs::file_size(dstFile), static_cast<uintmax_t>(-1));
345 ASSERT_EQ(fs::file_size(retFile), fs::file_size(dstFile));
346
347 std::string ssRetFile = readFile(fs::path(retFile));
348 std::string ssDstFile = readFile(fs::path(dstFile));
349 ASSERT_EQ(ssRetFile, ssDstFile);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600350}
351
352TEST(ExecTest, TestConstructArgv)
353{
354 auto name = "/bin/ls";
355 auto arg1 = "-a";
356 auto arg2 = "-l";
357 auto arg3 = "-t";
358 auto arg4 = "-rS";
359 auto argV = utils::internal::constructArgv(name, arg1, arg2, arg3, arg4);
360 char** charArray = argV.data();
361 EXPECT_EQ(argV.size(), 6);
362 EXPECT_EQ(charArray[0], name);
363 EXPECT_EQ(charArray[1], arg1);
364 EXPECT_EQ(charArray[2], arg2);
365 EXPECT_EQ(charArray[3], arg3);
366 EXPECT_EQ(charArray[4], arg4);
367 EXPECT_EQ(charArray[5], nullptr);
368
369 name = "/usr/bin/du";
370 argV = utils::internal::constructArgv(name);
371 charArray = argV.data();
372 EXPECT_EQ(argV.size(), 2);
373 EXPECT_EQ(charArray[0], name);
374 EXPECT_EQ(charArray[1], nullptr);
375
376 name = "/usr/bin/hexdump";
377 arg1 = "-C";
378 arg2 = "/path/to/filename";
379 argV = utils::internal::constructArgv(name, arg1, arg2);
380 charArray = argV.data();
381 EXPECT_EQ(argV.size(), 4);
382 EXPECT_EQ(charArray[0], name);
383 EXPECT_EQ(charArray[1], arg1);
384 EXPECT_EQ(charArray[2], arg2);
385 EXPECT_EQ(charArray[3], nullptr);
386}