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_FUZZ_TEST 17fb299fa2Sopenharmony_ci#define PKG_FUZZ_TEST 18fb299fa2Sopenharmony_ci 19fb299fa2Sopenharmony_ci#include <cstring> 20fb299fa2Sopenharmony_ci#include <fcntl.h> 21fb299fa2Sopenharmony_ci#include <iostream> 22fb299fa2Sopenharmony_ci#include <sys/mman.h> 23fb299fa2Sopenharmony_ci#include <sys/stat.h> 24fb299fa2Sopenharmony_ci#include <unistd.h> 25fb299fa2Sopenharmony_ci#include "pkg_algorithm.h" 26fb299fa2Sopenharmony_ci#include "pkg_manager.h" 27fb299fa2Sopenharmony_ci#include "pkg_manager_impl.h" 28fb299fa2Sopenharmony_ci#include "pkg_utils.h" 29fb299fa2Sopenharmony_ci#include "log.h" 30fb299fa2Sopenharmony_ci#include "utils.h" 31fb299fa2Sopenharmony_ci 32fb299fa2Sopenharmony_ciusing Updater::InitUpdaterLogger; 33fb299fa2Sopenharmony_ciusing Hpackage::PkgManager; 34fb299fa2Sopenharmony_ciusing Hpackage::PkgManagerImpl; 35fb299fa2Sopenharmony_ciusing Hpackage::PkgStream; 36fb299fa2Sopenharmony_ciusing Hpackage::PkgAlgorithmFactory; 37fb299fa2Sopenharmony_ciusing Hpackage::DigestAlgorithm; 38fb299fa2Sopenharmony_ciusing Hpackage::ComponentInfo; 39fb299fa2Sopenharmony_ciusing Hpackage::FileInfo; 40fb299fa2Sopenharmony_ciusing Hpackage::ZipFileInfo; 41fb299fa2Sopenharmony_ciusing Hpackage::PkgInfo; 42fb299fa2Sopenharmony_ci 43fb299fa2Sopenharmony_cinamespace OHOS { 44fb299fa2Sopenharmony_ciconst std::string TEST_PATH_FROM = "/data/updater/src/"; 45fb299fa2Sopenharmony_ciconst std::string TEST_PATH_TO = "/data/updater/dst/"; 46fb299fa2Sopenharmony_ci 47fb299fa2Sopenharmony_ciinline std::string GetFuzzPrivateKeyName(int type = 0) 48fb299fa2Sopenharmony_ci{ 49fb299fa2Sopenharmony_ci std::string name = TEST_PATH_FROM; 50fb299fa2Sopenharmony_ci if (type == PKG_DIGEST_TYPE_SHA384) { 51fb299fa2Sopenharmony_ci name += "rsa_private_key384.pem"; 52fb299fa2Sopenharmony_ci } else { 53fb299fa2Sopenharmony_ci name += "rsa_private_key2048.pem"; 54fb299fa2Sopenharmony_ci } 55fb299fa2Sopenharmony_ci return name; 56fb299fa2Sopenharmony_ci} 57fb299fa2Sopenharmony_ci 58fb299fa2Sopenharmony_ciinline std::string GetFuzzCertName(int type = 0) 59fb299fa2Sopenharmony_ci{ 60fb299fa2Sopenharmony_ci std::string name = TEST_PATH_FROM; 61fb299fa2Sopenharmony_ci if (type == PKG_DIGEST_TYPE_SHA384) { 62fb299fa2Sopenharmony_ci name += "signing_cert384.crt"; 63fb299fa2Sopenharmony_ci } else { 64fb299fa2Sopenharmony_ci name += "signing_cert.crt"; 65fb299fa2Sopenharmony_ci } 66fb299fa2Sopenharmony_ci return name; 67fb299fa2Sopenharmony_ci} 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_ciclass FuzzPkgTest { 70fb299fa2Sopenharmony_cipublic: 71fb299fa2Sopenharmony_ci FuzzPkgTest() 72fb299fa2Sopenharmony_ci { 73fb299fa2Sopenharmony_ci pkgManager_ = static_cast<PkgManagerImpl*>(PkgManager::CreatePackageInstance()); 74fb299fa2Sopenharmony_ci } 75fb299fa2Sopenharmony_ci virtual ~FuzzPkgTest() 76fb299fa2Sopenharmony_ci { 77fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(pkgManager_); 78fb299fa2Sopenharmony_ci pkgManager_ = nullptr; 79fb299fa2Sopenharmony_ci } 80fb299fa2Sopenharmony_ci 81fb299fa2Sopenharmony_ciprotected: 82fb299fa2Sopenharmony_ci void SetUp() 83fb299fa2Sopenharmony_ci { 84fb299fa2Sopenharmony_ci InitUpdaterLogger("UPDATER ", "updater_log.log", "updater_status.log", "error_code.log"); 85fb299fa2Sopenharmony_ci // 先创建目标目录 86fb299fa2Sopenharmony_ci if (access(TEST_PATH_TO.c_str(), R_OK | W_OK) == -1) { 87fb299fa2Sopenharmony_ci mkdir(TEST_PATH_TO.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 88fb299fa2Sopenharmony_ci } 89fb299fa2Sopenharmony_ci } 90fb299fa2Sopenharmony_ci void TestBody() {} 91fb299fa2Sopenharmony_ci void TearDown() {} 92fb299fa2Sopenharmony_ci 93fb299fa2Sopenharmony_ci int32_t BuildFileDigest(uint8_t &digest, size_t size, const std::string &pkgPath) 94fb299fa2Sopenharmony_ci { 95fb299fa2Sopenharmony_ci PkgManager::StreamPtr stream = nullptr; 96fb299fa2Sopenharmony_ci int32_t ret = pkgManager_->CreatePkgStream(stream, pkgPath, 0, PkgStream::PkgStreamType_Read); 97fb299fa2Sopenharmony_ci if (ret != 0) { 98fb299fa2Sopenharmony_ci PKG_LOGE("Create input stream fail %s", pkgPath.c_str()); 99fb299fa2Sopenharmony_ci return ret; 100fb299fa2Sopenharmony_ci } 101fb299fa2Sopenharmony_ci size_t fileLength = stream->GetFileLength(); 102fb299fa2Sopenharmony_ci if (fileLength <= 0 || fileLength > SIZE_MAX) { 103fb299fa2Sopenharmony_ci PKG_LOGE("Invalid file len %zu to load %s", fileLength, stream->GetFileName().c_str()); 104fb299fa2Sopenharmony_ci pkgManager_->ClosePkgStream(stream); 105fb299fa2Sopenharmony_ci return -1; 106fb299fa2Sopenharmony_ci } 107fb299fa2Sopenharmony_ci 108fb299fa2Sopenharmony_ci size_t bufSize = 4096; 109fb299fa2Sopenharmony_ci Hpackage::PkgBuffer buff(bufSize); 110fb299fa2Sopenharmony_ci // 整包检查 111fb299fa2Sopenharmony_ci DigestAlgorithm::DigestAlgorithmPtr alg = 112fb299fa2Sopenharmony_ci PkgAlgorithmFactory::GetDigestAlgorithm(PKG_DIGEST_TYPE_SHA256); 113fb299fa2Sopenharmony_ci if (alg == nullptr) { 114fb299fa2Sopenharmony_ci PKG_LOGE("Invalid file %s", stream->GetFileName().c_str()); 115fb299fa2Sopenharmony_ci pkgManager_->ClosePkgStream(stream); 116fb299fa2Sopenharmony_ci return -1; 117fb299fa2Sopenharmony_ci } 118fb299fa2Sopenharmony_ci alg->Init(); 119fb299fa2Sopenharmony_ci 120fb299fa2Sopenharmony_ci size_t offset = 0; 121fb299fa2Sopenharmony_ci size_t readLen = 0; 122fb299fa2Sopenharmony_ci while (offset < fileLength) { 123fb299fa2Sopenharmony_ci ret = stream->Read(buff, offset, bufSize, readLen); 124fb299fa2Sopenharmony_ci if (ret != 0) { 125fb299fa2Sopenharmony_ci PKG_LOGE("read buffer fail %s", stream->GetFileName().c_str()); 126fb299fa2Sopenharmony_ci pkgManager_->ClosePkgStream(stream); 127fb299fa2Sopenharmony_ci return ret; 128fb299fa2Sopenharmony_ci } 129fb299fa2Sopenharmony_ci alg->Update(buff, readLen); 130fb299fa2Sopenharmony_ci 131fb299fa2Sopenharmony_ci offset += readLen; 132fb299fa2Sopenharmony_ci readLen = 0; 133fb299fa2Sopenharmony_ci } 134fb299fa2Sopenharmony_ci Hpackage::PkgBuffer buffer(&digest, size); 135fb299fa2Sopenharmony_ci alg->Final(buffer); 136fb299fa2Sopenharmony_ci pkgManager_->ClosePkgStream(stream); 137fb299fa2Sopenharmony_ci return 0; 138fb299fa2Sopenharmony_ci } 139fb299fa2Sopenharmony_ci 140fb299fa2Sopenharmony_ci void ExtractFile(PkgManager::PkgManagerPtr manager, std::vector<std::string> components, size_t num) 141fb299fa2Sopenharmony_ci { 142fb299fa2Sopenharmony_ci PkgManager::StreamPtr outStream = nullptr; 143fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] file name: %s \r\n", num, (TEST_PATH_TO + components[num]).c_str()); 144fb299fa2Sopenharmony_ci manager->CreatePkgStream(outStream, TEST_PATH_TO + components[num], 0, PkgStream::PkgStreamType_Write); 145fb299fa2Sopenharmony_ci if (outStream == nullptr) { 146fb299fa2Sopenharmony_ci return; 147fb299fa2Sopenharmony_ci } 148fb299fa2Sopenharmony_ci (void)manager->ExtractFile(components[num], outStream); 149fb299fa2Sopenharmony_ci manager->ClosePkgStream(outStream); 150fb299fa2Sopenharmony_ci const FileInfo *info = manager->GetFileInfo(components[num]); 151fb299fa2Sopenharmony_ci if (info->packMethod == PKG_COMPRESS_METHOD_NONE) { 152fb299fa2Sopenharmony_ci const ComponentInfo* compInfo = (const ComponentInfo*)manager->GetFileInfo(components[num]); 153fb299fa2Sopenharmony_ci if (compInfo != nullptr) { 154fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] componentAddr: %s \n", num, (*compInfo).fileInfo.identity.c_str()); 155fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] version: %s \n", num, (*compInfo).version.c_str()); 156fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] originalSize: %zu \n", num, (*compInfo).originalSize); 157fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] size: %zu \n", num, (*compInfo).fileInfo.unpackedSize); 158fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] id: %d \n", num, (*compInfo).id); 159fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] resType: %d \n", num, (*compInfo).resType); 160fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] flags: %d \n", num, (*compInfo).compFlags); 161fb299fa2Sopenharmony_ci PKG_LOGI("comp [%zu] type: %d \n", num, (*compInfo).type); 162fb299fa2Sopenharmony_ci } 163fb299fa2Sopenharmony_ci } else { 164fb299fa2Sopenharmony_ci PKG_LOGI("FileInfo [%zu] id: %s \n", num, info->identity.c_str()); 165fb299fa2Sopenharmony_ci PKG_LOGI("FileInfo [%zu] unpackedSize: %zu \n", num, info->unpackedSize); 166fb299fa2Sopenharmony_ci PKG_LOGI("FileInfo [%zu] packedSize: %zu \n", num, info->packedSize); 167fb299fa2Sopenharmony_ci PKG_LOGI("FileInfo [%zu] packMethod: %d \n", num, info->packMethod); 168fb299fa2Sopenharmony_ci PKG_LOGI("FileInfo [%zu] digestMethod: %d \n", num, info->digestMethod); 169fb299fa2Sopenharmony_ci PKG_LOGI("FileInfo [%zu] flags: %d \n", num, info->flags); 170fb299fa2Sopenharmony_ci } 171fb299fa2Sopenharmony_ci } 172fb299fa2Sopenharmony_ci 173fb299fa2Sopenharmony_ci int CreateZipPackage(const std::vector<std::string> &testNames, 174fb299fa2Sopenharmony_ci const std::string pkgName, const std::string &base, int digestMethod) 175fb299fa2Sopenharmony_ci { 176fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance(); 177fb299fa2Sopenharmony_ci std::vector<std::pair<std::string, ZipFileInfo>> files; 178fb299fa2Sopenharmony_ci // 构建要打包的zip文件 179fb299fa2Sopenharmony_ci for (auto name : testNames) { 180fb299fa2Sopenharmony_ci ZipFileInfo zipFile; 181fb299fa2Sopenharmony_ci zipFile.fileInfo.identity = name; 182fb299fa2Sopenharmony_ci zipFile.fileInfo.packMethod = PKG_COMPRESS_METHOD_ZIP; 183fb299fa2Sopenharmony_ci zipFile.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC; 184fb299fa2Sopenharmony_ci std::string fileName = base + name; 185fb299fa2Sopenharmony_ci files.push_back(std::pair<std::string, ZipFileInfo>(fileName, zipFile)); 186fb299fa2Sopenharmony_ci } 187fb299fa2Sopenharmony_ci 188fb299fa2Sopenharmony_ci PkgInfo pkgInfo; 189fb299fa2Sopenharmony_ci pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 190fb299fa2Sopenharmony_ci pkgInfo.digestMethod = digestMethod; 191fb299fa2Sopenharmony_ci pkgInfo.pkgType = PKG_PACK_TYPE_ZIP; 192fb299fa2Sopenharmony_ci int32_t ret = pkgManager->CreatePackage(pkgName, GetFuzzPrivateKeyName(digestMethod), &pkgInfo, files); 193fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(pkgManager); 194fb299fa2Sopenharmony_ci return ret; 195fb299fa2Sopenharmony_ci } 196fb299fa2Sopenharmony_ci std::vector<std::string> testFileNames_ = { 197fb299fa2Sopenharmony_ci "test_math.us", 198fb299fa2Sopenharmony_ci "test_native.us", 199fb299fa2Sopenharmony_ci "testscript.us", 200fb299fa2Sopenharmony_ci "Verse-script.us", 201fb299fa2Sopenharmony_ci "libcrypto.a", 202fb299fa2Sopenharmony_ci "ggg.zip", 203fb299fa2Sopenharmony_ci "loadScript.us", 204fb299fa2Sopenharmony_ci "registerCmd.us", 205fb299fa2Sopenharmony_ci "test_function.us", 206fb299fa2Sopenharmony_ci "test_if.us", 207fb299fa2Sopenharmony_ci "test_logic.us", 208fb299fa2Sopenharmony_ci }; 209fb299fa2Sopenharmony_ci PkgManagerImpl* pkgManager_ = nullptr; 210fb299fa2Sopenharmony_ci std::string testCombinePkgName = "test_CombinePackage.zip"; 211fb299fa2Sopenharmony_ci std::string testPackagePath = "/data/updater/package/"; 212fb299fa2Sopenharmony_ci std::string testPackageName = "test_package.bin"; 213fb299fa2Sopenharmony_ci std::string testZipPackageName = "test_package.zip"; 214fb299fa2Sopenharmony_ci std::string testLz4PackageName = "test_package.lz4"; 215fb299fa2Sopenharmony_ci std::string testGZipPackageName = "test_package.gz"; 216fb299fa2Sopenharmony_ci}; 217fb299fa2Sopenharmony_ci} 218fb299fa2Sopenharmony_ci#endif // PKG_FUZZ_TEST 219