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