1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PKG_TEST 17 #define PKG_TEST 18 19 #include <cstring> 20 #include <fcntl.h> 21 #include <gtest/gtest.h> 22 #include <iostream> 23 #include <sys/mman.h> 24 #include <sys/stat.h> 25 #include <unistd.h> 26 #include "log.h" 27 #include "pkg_algorithm.h" 28 #include "pkg_manager.h" 29 #include "pkg_manager_impl.h" 30 #include "pkg_utils.h" 31 #include "unittest_comm.h" 32 #include "utils.h" 33 34 using Hpackage::PkgManager; 35 using Hpackage::PkgManagerImpl; 36 using Hpackage::PkgStream; 37 using Hpackage::PkgAlgorithmFactory; 38 using Hpackage::DigestAlgorithm; 39 using Hpackage::ComponentInfo; 40 using Hpackage::FileInfo; 41 using Hpackage::ZipFileInfo; 42 using Hpackage::PkgInfo; 43 using Updater::InitUpdaterLogger; 44 45 namespace UpdaterUt { 46 class PkgTest : public ::testing::Test { 47 public: PkgTest()48 PkgTest() 49 { 50 pkgManager_ = static_cast<PkgManagerImpl*>(PkgManager::GetPackageInstance()); 51 } ~PkgTest()52 virtual ~PkgTest() 53 { 54 pkgManager_ = static_cast<PkgManagerImpl*>(PkgManager::GetPackageInstance()); 55 PkgManager::ReleasePackageInstance(pkgManager_); 56 pkgManager_ = nullptr; 57 } 58 59 protected: SetUp()60 void SetUp() 61 { 62 // 先创建目标目录 63 if (access(TEST_PATH_TO.c_str(), R_OK | W_OK) == -1) { 64 mkdir(TEST_PATH_TO.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 65 } 66 InitUpdaterLogger("UPDATER ", "updater_log.log", "updater_status.log", "error_code.log"); 67 } TearDown()68 void TearDown() {} TestBody()69 void TestBody() {} 70 BuildFileDigest(uint8_t & digest,size_t size,const std::string & packagePath)71 int32_t BuildFileDigest(uint8_t &digest, size_t size, const std::string &packagePath) 72 { 73 PkgManager::StreamPtr stream = nullptr; 74 int32_t ret = pkgManager_->CreatePkgStream(stream, packagePath, 0, PkgStream::PkgStreamType_Read); 75 PKG_CHECK(ret == 0, return ret, "Create input stream fail %s", packagePath.c_str()); 76 size_t fileLen = stream->GetFileLength(); 77 PKG_CHECK(fileLen > 0, pkgManager_->ClosePkgStream(stream); return -1, "invalid file to load"); 78 PKG_CHECK(fileLen <= SIZE_MAX, pkgManager_->ClosePkgStream(stream); return -1, 79 "Invalid file len %zu to load %s", fileLen, stream->GetFileName().c_str()); 80 81 size_t buffSize = 4096; 82 Hpackage::PkgBuffer buff(buffSize); 83 // 整包检查 84 DigestAlgorithm::DigestAlgorithmPtr algorithm = PkgAlgorithmFactory::GetDigestAlgorithm(PKG_DIGEST_TYPE_SHA256); 85 PKG_CHECK(algorithm != nullptr, pkgManager_->ClosePkgStream(stream); return -1, 86 "Invalid file %s", stream->GetFileName().c_str()); 87 algorithm->Init(); 88 89 size_t offset = 0; 90 size_t readLen = 0; 91 while (offset < fileLen) { 92 ret = stream->Read(buff, offset, buffSize, readLen); 93 PKG_CHECK(ret == 0, 94 pkgManager_->ClosePkgStream(stream); return ret, 95 "read buffer fail %s", stream->GetFileName().c_str()); 96 algorithm->Update(buff, readLen); 97 98 offset += readLen; 99 readLen = 0; 100 } 101 Hpackage::PkgBuffer buffer(&digest, size); 102 algorithm->Final(buffer); 103 pkgManager_->ClosePkgStream(stream); 104 return 0; 105 } 106 ExtractFile(PkgManager::PkgManagerPtr manager,std::vector<std::string> components,size_t i)107 void ExtractFile(PkgManager::PkgManagerPtr manager, std::vector<std::string> components, size_t i) 108 { 109 PkgManager::StreamPtr outStream = nullptr; 110 PKG_LOGI("comp [%zu] file name: %s \r\n", i, (TEST_PATH_TO + components[i]).c_str()); 111 manager->CreatePkgStream(outStream, TEST_PATH_TO + components[i], 0, PkgStream::PkgStreamType_Write); 112 EXPECT_NE(nullptr, outStream); 113 if (outStream == nullptr) { 114 return; 115 } 116 int ret = manager->ExtractFile(components[i], outStream); 117 EXPECT_EQ(ret, 0); 118 manager->ClosePkgStream(outStream); 119 const FileInfo *info = manager->GetFileInfo(components[i]); 120 ASSERT_NE(info, nullptr); 121 if (info->packMethod == PKG_COMPRESS_METHOD_NONE) { 122 const ComponentInfo* compInfo = (const ComponentInfo*)manager->GetFileInfo(components[i]); 123 if (compInfo != nullptr) { 124 PKG_LOGI("comp [%zu] componentAddr: %s \n", i, (*compInfo).fileInfo.identity.c_str()); 125 PKG_LOGI("comp [%zu] version: %s \n", i, (*compInfo).version.c_str()); 126 PKG_LOGI("comp [%zu] originalSize: %zu \n", i, (*compInfo).originalSize); 127 PKG_LOGI("comp [%zu] size: %zu \n", i, (*compInfo).fileInfo.unpackedSize); 128 PKG_LOGI("comp [%zu] id: %d \n", i, (*compInfo).id); 129 PKG_LOGI("comp [%zu] resType: %d \n", i, (*compInfo).resType); 130 PKG_LOGI("comp [%zu] flags: %d \n", i, (*compInfo).compFlags); 131 PKG_LOGI("comp [%zu] type: %d \n", i, (*compInfo).type); 132 } 133 } else { 134 PKG_LOGI("FileInfo [%zu] id: %s \n", i, info->identity.c_str()); 135 PKG_LOGI("FileInfo [%zu] unpackedSize: %zu \n", i, info->unpackedSize); 136 PKG_LOGI("FileInfo [%zu] packedSize: %zu \n", i, info->packedSize); 137 PKG_LOGI("FileInfo [%zu] packMethod: %d \n", i, info->packMethod); 138 PKG_LOGI("FileInfo [%zu] digestMethod: %d \n", i, info->digestMethod); 139 PKG_LOGI("FileInfo [%zu] flags: %d \n", i, info->flags); 140 } 141 } 142 CreateZipPackage(const std::vector<std::string> & testFileNames,const std::string packageName,const std::string & base,int digestMethod)143 int CreateZipPackage(const std::vector<std::string> &testFileNames, 144 const std::string packageName, const std::string &base, int digestMethod) 145 { 146 PkgManager::PkgManagerPtr pkgManager = PkgManager::GetPackageInstance(); 147 EXPECT_NE(pkgManager, nullptr); 148 std::vector<std::pair<std::string, ZipFileInfo>> files; 149 // 构建要打包的zip文件 150 for (auto name : testFileNames) { 151 ZipFileInfo file; 152 file.fileInfo.identity = name; 153 file.fileInfo.packMethod = PKG_COMPRESS_METHOD_ZIP; 154 file.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC; 155 std::string fileName = base + name; 156 files.push_back(std::pair<std::string, ZipFileInfo>(fileName, file)); 157 } 158 159 PkgInfo pkgInfo; 160 pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 161 pkgInfo.digestMethod = digestMethod; 162 pkgInfo.pkgType = PKG_PACK_TYPE_ZIP; 163 int32_t ret = pkgManager->CreatePackage(packageName, GetTestPrivateKeyName(digestMethod), &pkgInfo, files); 164 EXPECT_EQ(ret, 0); 165 return ret; 166 } 167 std::vector<std::string> testFileNames_ = { 168 "loadScript.us", 169 "registerCmd.us", 170 "test_function.us", 171 "test_if.us", 172 "test_logic.us", 173 "test_math.us", 174 "test_native.us", 175 "testscript.us", 176 "Verse-script.us", 177 "libcrypto.a", 178 "ggg.zip" 179 }; 180 PkgManagerImpl* pkgManager_ = nullptr; 181 std::string testPackageName = "test_package.bin"; 182 std::string testZipPackageName = "test_package.zip"; 183 std::string testLz4PackageName = "test_package.lz4"; 184 std::string testGZipPackageName = "test_package.gz"; 185 std::string testCombinePkgName = "test_CombinePackage.zip"; 186 }; 187 } 188 #endif // PKG_TEST 189