Add support for tarball contents signature
Currently only supported to allow optional image files in BMC tarball.
In order to ensure that the contents of the tarball are the expected
ones as a full package, an additional signature file has been created
for all the signature files in the tarball,
(ex: image-full.sig = hash(file1.sig + file2.sig + file3.sig...)).
Need to check the existence of the file and the signature verification
passed.
Also, added unit test case for the mergeFiles method.
Tested:
Enable `WANT_SIGNATURE_FULL_VERIFY` and ran the following command:
curl -k -H "X-Auth-Token: $token" -H "Content-Type: application/octet-stream"
-X POST -T obmc-phosphor-image-fp5280g2.static.mtd.tar
https://${bmc}/redfish/v1/UpdateService
{
"@odata.id": "/redfish/v1/TaskService/Tasks/1",
"@odata.type": "#Task.v1_4_3.Task",
"Id": "1",
"TaskState": "Running",
"TaskStatus": "OK"
}
And Log output:
`Successfully completed Signature vaildation.`
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I0e658b9dd90ea405a9c8292f29183ab516a0fa31
diff --git a/test/utest.cpp b/test/utest.cpp
index 94e7c96..b91a461 100644
--- a/test/utest.cpp
+++ b/test/utest.cpp
@@ -1,4 +1,5 @@
#include "image_verify.hpp"
+#include "utils.hpp"
#include "version.hpp"
#include <openssl/sha.h>
@@ -9,6 +10,7 @@
#include <iostream>
#include <sstream>
#include <string>
+#include <vector>
#include <gtest/gtest.h>
@@ -262,3 +264,72 @@
command("rm -rf " + signedConfOpenBMCPath.string());
EXPECT_FALSE(signature->verify());
}
+
+class FileTest : public testing::Test
+{
+ protected:
+ std::string readFile(fs::path path)
+ {
+ std::ifstream f(path, std::ios::in);
+ const auto sz = fs::file_size(path);
+ std::string result(sz, '\0');
+ f.read(result.data(), sz);
+
+ return result;
+ }
+
+ void command(const std::string& cmd)
+ {
+ auto val = std::system(cmd.c_str());
+ if (val)
+ {
+ std::cout << "COMMAND Error: " << val << std::endl;
+ }
+ }
+
+ virtual void SetUp()
+ {
+ // Create test base directory.
+ tmpDir = fs::temp_directory_path() / "testFileXXXXXX";
+ if (!mkdtemp(tmpDir.data()))
+ {
+ throw "Failed to create tmp dir";
+ }
+
+ std::string file1 = tmpDir + "/file1";
+ std::string file2 = tmpDir + "/file2";
+ command("echo \"File Test1\n\n\" > " + file1);
+ command("echo \"FileTe st2\n\nte st2\" > " + file2);
+
+ srcFiles.push_back(file1);
+ srcFiles.push_back(file2);
+ }
+
+ virtual void TearDown()
+ {
+ fs::remove_all(tmpDir);
+ }
+
+ std::vector<std::string> srcFiles;
+ std::string tmpDir;
+};
+
+TEST_F(FileTest, TestMergeFiles)
+{
+ std::string retFile = tmpDir + "/retFile";
+ for (auto file : srcFiles)
+ {
+ command("cat " + file + " >> " + retFile);
+ }
+
+ std::string dstFile = tmpDir + "/dstFile";
+ utils::mergeFiles(srcFiles, dstFile);
+
+ ASSERT_NE(fs::file_size(retFile), static_cast<uintmax_t>(-1));
+ ASSERT_NE(fs::file_size(dstFile), static_cast<uintmax_t>(-1));
+ ASSERT_EQ(fs::file_size(retFile), fs::file_size(dstFile));
+
+ std::string ssRetFile = readFile(fs::path(retFile));
+ std::string ssDstFile = readFile(fs::path(dstFile));
+ ASSERT_EQ(ssRetFile, ssDstFile);
+}
\ No newline at end of file