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#include <cstring> 17fb299fa2Sopenharmony_ci#include <gtest/gtest.h> 18fb299fa2Sopenharmony_ci#include <iostream> 19fb299fa2Sopenharmony_ci#include <memory> 20fb299fa2Sopenharmony_ci#include "log.h" 21fb299fa2Sopenharmony_ci#include "packages_info.h" 22fb299fa2Sopenharmony_ci#include "pkg_algorithm.h" 23fb299fa2Sopenharmony_ci#include "pkg_gzipfile.h" 24fb299fa2Sopenharmony_ci#include "pkg_lz4file.h" 25fb299fa2Sopenharmony_ci#include "pkg_manager.h" 26fb299fa2Sopenharmony_ci#include "pkg_manager_impl.h" 27fb299fa2Sopenharmony_ci#include "pkg_test.h" 28fb299fa2Sopenharmony_ci#include "pkg_upgradefile.h" 29fb299fa2Sopenharmony_ci#include "pkg_utils.h" 30fb299fa2Sopenharmony_ci#include "pkg_zipfile.h" 31fb299fa2Sopenharmony_ci#include "securec.h" 32fb299fa2Sopenharmony_ci 33fb299fa2Sopenharmony_ciusing namespace std; 34fb299fa2Sopenharmony_ciusing namespace Hpackage; 35fb299fa2Sopenharmony_ciusing namespace Updater; 36fb299fa2Sopenharmony_ciusing namespace testing::ext; 37fb299fa2Sopenharmony_ci 38fb299fa2Sopenharmony_cinamespace UpdaterUt { 39fb299fa2Sopenharmony_ciconstexpr uint32_t MAX_FILE_NAME = 256; 40fb299fa2Sopenharmony_ciconstexpr uint32_t CENTRAL_SIGNATURE = 0x02014b50; 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_ciclass TestFile : public PkgFileImpl { 43fb299fa2Sopenharmony_cipublic: 44fb299fa2Sopenharmony_ci explicit TestFile(PkgManager::PkgManagerPtr pkgManager, PkgStreamPtr stream) 45fb299fa2Sopenharmony_ci : PkgFileImpl(pkgManager, stream, PKG_TYPE_MAX) {} 46fb299fa2Sopenharmony_ci 47fb299fa2Sopenharmony_ci virtual ~TestFile() {} 48fb299fa2Sopenharmony_ci 49fb299fa2Sopenharmony_ci virtual int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr inStream) 50fb299fa2Sopenharmony_ci { 51fb299fa2Sopenharmony_ci PkgFileImpl::GetPkgInfo(); 52fb299fa2Sopenharmony_ci PkgFileImpl::AddPkgEntry(inStream->GetFileName()); 53fb299fa2Sopenharmony_ci return 0; 54fb299fa2Sopenharmony_ci } 55fb299fa2Sopenharmony_ci 56fb299fa2Sopenharmony_ci virtual int32_t SavePackage(size_t &offset) 57fb299fa2Sopenharmony_ci { 58fb299fa2Sopenharmony_ci return 0; 59fb299fa2Sopenharmony_ci } 60fb299fa2Sopenharmony_ci 61fb299fa2Sopenharmony_ci virtual int32_t LoadPackage(std::vector<std::string>& fileNames, VerifyFunction verify = nullptr) 62fb299fa2Sopenharmony_ci { 63fb299fa2Sopenharmony_ci return 0; 64fb299fa2Sopenharmony_ci } 65fb299fa2Sopenharmony_ci}; 66fb299fa2Sopenharmony_ci 67fb299fa2Sopenharmony_ciclass PkgPackageTest : public PkgTest { 68fb299fa2Sopenharmony_cipublic: 69fb299fa2Sopenharmony_ci PkgPackageTest() {} 70fb299fa2Sopenharmony_ci ~PkgPackageTest() override {} 71fb299fa2Sopenharmony_ci 72fb299fa2Sopenharmony_ci int TestPkgFile() 73fb299fa2Sopenharmony_ci { 74fb299fa2Sopenharmony_ci if (pkgManager_ == nullptr) { 75fb299fa2Sopenharmony_ci return PKG_SUCCESS; 76fb299fa2Sopenharmony_ci } 77fb299fa2Sopenharmony_ci PkgManager::StreamPtr stream = nullptr; 78fb299fa2Sopenharmony_ci std::string packagePath = TEST_PATH_TO; 79fb299fa2Sopenharmony_ci packagePath += testPackageName; 80fb299fa2Sopenharmony_ci int ret = pkgManager_->CreatePkgStream(stream, packagePath, 0, PkgStream::PkgStreamType_Read); 81fb299fa2Sopenharmony_ci auto file = std::make_unique<Lz4PkgFile>(pkgManager_, PkgStreamImpl::ConvertPkgStream(stream)); 82fb299fa2Sopenharmony_ci EXPECT_NE(file, nullptr); 83fb299fa2Sopenharmony_ci constexpr uint32_t lz4NodeId = 100; 84fb299fa2Sopenharmony_ci auto entry = std::make_unique<Lz4FileEntry>(file.get(), lz4NodeId); 85fb299fa2Sopenharmony_ci EXPECT_NE(entry, nullptr); 86fb299fa2Sopenharmony_ci 87fb299fa2Sopenharmony_ci EXPECT_NE(((PkgEntryPtr)entry.get())->GetPkgFile(), nullptr); 88fb299fa2Sopenharmony_ci Lz4FileInfo fileInfo {}; 89fb299fa2Sopenharmony_ci ret = entry->Init(&fileInfo.fileInfo, PkgStreamImpl::ConvertPkgStream(stream)); 90fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 91fb299fa2Sopenharmony_ci return 0; 92fb299fa2Sopenharmony_ci } 93fb299fa2Sopenharmony_ci 94fb299fa2Sopenharmony_ci int TestPkgFileInvalid() 95fb299fa2Sopenharmony_ci { 96fb299fa2Sopenharmony_ci if (pkgManager_ == nullptr) { 97fb299fa2Sopenharmony_ci return PKG_SUCCESS; 98fb299fa2Sopenharmony_ci } 99fb299fa2Sopenharmony_ci PkgManager::StreamPtr stream = nullptr; 100fb299fa2Sopenharmony_ci std::string packagePath = TEST_PATH_TO; 101fb299fa2Sopenharmony_ci packagePath += testPackageName; 102fb299fa2Sopenharmony_ci int ret = pkgManager_->CreatePkgStream(stream, packagePath, 0, PkgStream::PkgStreamType_Read); 103fb299fa2Sopenharmony_ci FileInfo fileInfo; 104fb299fa2Sopenharmony_ci std::unique_ptr<TestFile> file = std::make_unique<TestFile>(pkgManager_, 105fb299fa2Sopenharmony_ci PkgStreamImpl::ConvertPkgStream(stream)); 106fb299fa2Sopenharmony_ci EXPECT_NE(file, nullptr); 107fb299fa2Sopenharmony_ci ret = file->AddEntry(&fileInfo, PkgStreamImpl::ConvertPkgStream(stream)); 108fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 109fb299fa2Sopenharmony_ci size_t offset = 0; 110fb299fa2Sopenharmony_ci ret = file->SavePackage(offset); 111fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 112fb299fa2Sopenharmony_ci return 0; 113fb299fa2Sopenharmony_ci } 114fb299fa2Sopenharmony_ci 115fb299fa2Sopenharmony_ci int TestBigZipEntry() 116fb299fa2Sopenharmony_ci { 117fb299fa2Sopenharmony_ci EXPECT_NE(pkgManager_, nullptr); 118fb299fa2Sopenharmony_ci PkgManager::StreamPtr stream = nullptr; 119fb299fa2Sopenharmony_ci std::string packagePath = TEST_PATH_TO; 120fb299fa2Sopenharmony_ci uint32_t zipNodeId = 100; 121fb299fa2Sopenharmony_ci packagePath += testPackageName; 122fb299fa2Sopenharmony_ci pkgManager_->CreatePkgStream(stream, packagePath, 0, PkgStream::PkgStreamType_Read); 123fb299fa2Sopenharmony_ci EXPECT_NE(stream, nullptr); 124fb299fa2Sopenharmony_ci std::unique_ptr<TestFile> file = std::make_unique<TestFile>(pkgManager_, 125fb299fa2Sopenharmony_ci PkgStreamImpl::ConvertPkgStream(stream)); 126fb299fa2Sopenharmony_ci EXPECT_NE(file, nullptr); 127fb299fa2Sopenharmony_ci std::unique_ptr<ZipFileEntry> entry = std::make_unique<ZipFileEntry>(file.get(), zipNodeId); 128fb299fa2Sopenharmony_ci EXPECT_NE(entry, nullptr); 129fb299fa2Sopenharmony_ci 130fb299fa2Sopenharmony_ci string name = "TestBigZip"; 131fb299fa2Sopenharmony_ci uint16_t extraSize = 20; 132fb299fa2Sopenharmony_ci size_t offsetHalfWord = 2; 133fb299fa2Sopenharmony_ci size_t offsetWord = 4; 134fb299fa2Sopenharmony_ci size_t offset4Words = 16; 135fb299fa2Sopenharmony_ci size_t offset3Words = 12; 136fb299fa2Sopenharmony_ci int32_t buffLen = MAX_FILE_NAME + sizeof(LocalFileHeader) + sizeof(DataDescriptor) + 137fb299fa2Sopenharmony_ci sizeof(CentralDirEntry) + offsetWord + offset4Words; 138fb299fa2Sopenharmony_ci std::vector<uint8_t> buff(buffLen); 139fb299fa2Sopenharmony_ci CentralDirEntry* centralDir = (CentralDirEntry *)buff.data(); 140fb299fa2Sopenharmony_ci (void)memset_s(centralDir, sizeof(CentralDirEntry), 0, sizeof(CentralDirEntry)); 141fb299fa2Sopenharmony_ci centralDir->signature = CENTRAL_SIGNATURE; 142fb299fa2Sopenharmony_ci centralDir->compressionMethod = PKG_COMPRESS_METHOD_ZIP; 143fb299fa2Sopenharmony_ci centralDir->compressedSize = UINT_MAX; 144fb299fa2Sopenharmony_ci centralDir->uncompressedSize = UINT_MAX; 145fb299fa2Sopenharmony_ci centralDir->nameSize = name.length(); 146fb299fa2Sopenharmony_ci centralDir->extraSize = extraSize; 147fb299fa2Sopenharmony_ci int ret = memcpy_s(buff.data() + sizeof(CentralDirEntry), name.length(), name.c_str(), name.length()); 148fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 149fb299fa2Sopenharmony_ci WriteLE16(buff.data() + sizeof(CentralDirEntry) + name.length(), 1); 150fb299fa2Sopenharmony_ci WriteLE16(buff.data() + sizeof(CentralDirEntry) + name.length() + offsetHalfWord, offset4Words); 151fb299fa2Sopenharmony_ci size_t giantNumber = 100000; 152fb299fa2Sopenharmony_ci size_t size = UINT_MAX + giantNumber; 153fb299fa2Sopenharmony_ci WriteLE64(buff.data() + sizeof(CentralDirEntry) + name.length() + offsetWord, size); 154fb299fa2Sopenharmony_ci WriteLE64(buff.data() + sizeof(CentralDirEntry) + name.length() + offset3Words, size); 155fb299fa2Sopenharmony_ci size_t decodeLen = 0; 156fb299fa2Sopenharmony_ci PkgBuffer buffer(buff); 157fb299fa2Sopenharmony_ci entry->DecodeCentralDirEntry(nullptr, buffer, 0, decodeLen); 158fb299fa2Sopenharmony_ci return 0; 159fb299fa2Sopenharmony_ci } 160fb299fa2Sopenharmony_ci 161fb299fa2Sopenharmony_ci void WriteLE64(uint8_t *buff, size_t size) const 162fb299fa2Sopenharmony_ci { 163fb299fa2Sopenharmony_ci *reinterpret_cast<size_t *>(buff) = size; 164fb299fa2Sopenharmony_ci } 165fb299fa2Sopenharmony_ci 166fb299fa2Sopenharmony_ci int TestPackageInfoFail() 167fb299fa2Sopenharmony_ci { 168fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr manager = PkgManager::CreatePackageInstance(); 169fb299fa2Sopenharmony_ci PackagesInfoPtr pkginfomanager = PackagesInfo::GetPackagesInfoInstance(); 170fb299fa2Sopenharmony_ci std::vector<std::string> target; 171fb299fa2Sopenharmony_ci std::vector<std::string> tmp; 172fb299fa2Sopenharmony_ci 173fb299fa2Sopenharmony_ci target = pkginfomanager->GetOTAVersion(nullptr, "", ""); 174fb299fa2Sopenharmony_ci EXPECT_EQ(target, tmp); 175fb299fa2Sopenharmony_ci target = pkginfomanager->GetOTAVersion(manager, "", ""); 176fb299fa2Sopenharmony_ci EXPECT_EQ(target, tmp); 177fb299fa2Sopenharmony_ci target = pkginfomanager->GetBoardID(nullptr, "", ""); 178fb299fa2Sopenharmony_ci EXPECT_EQ(target, tmp); 179fb299fa2Sopenharmony_ci target = pkginfomanager->GetBoardID(manager, "", ""); 180fb299fa2Sopenharmony_ci EXPECT_EQ(target, tmp); 181fb299fa2Sopenharmony_ci 182fb299fa2Sopenharmony_ci bool ret = pkginfomanager->IsAllowRollback(); 183fb299fa2Sopenharmony_ci EXPECT_EQ(ret, false); 184fb299fa2Sopenharmony_ci PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager); 185fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(manager); 186fb299fa2Sopenharmony_ci return 0; 187fb299fa2Sopenharmony_ci } 188fb299fa2Sopenharmony_ci 189fb299fa2Sopenharmony_ci int TestUpdaterPreProcess() 190fb299fa2Sopenharmony_ci { 191fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance(); 192fb299fa2Sopenharmony_ci std::string packagePath = testPackagePath + "test_package.zip"; 193fb299fa2Sopenharmony_ci std::vector<std::string> components; 194fb299fa2Sopenharmony_ci int32_t ret = pkgManager->LoadPackage(packagePath, Utils::GetCertName(), components); 195fb299fa2Sopenharmony_ci EXPECT_EQ(ret, PKG_SUCCESS); 196fb299fa2Sopenharmony_ci 197fb299fa2Sopenharmony_ci PackagesInfoPtr pkginfomanager = PackagesInfo::GetPackagesInfoInstance(); 198fb299fa2Sopenharmony_ci std::vector<std::string> result; 199fb299fa2Sopenharmony_ci std::vector<std::string> targetVersions = pkginfomanager->GetOTAVersion( 200fb299fa2Sopenharmony_ci pkgManager, "/version_list", testPackagePath); 201fb299fa2Sopenharmony_ci EXPECT_NE(targetVersions, result); 202fb299fa2Sopenharmony_ci 203fb299fa2Sopenharmony_ci std::vector<std::string> boardIdList = pkginfomanager->GetBoardID(pkgManager, "/board_list", ""); 204fb299fa2Sopenharmony_ci EXPECT_NE(boardIdList, result); 205fb299fa2Sopenharmony_ci PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager); 206fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(pkgManager); 207fb299fa2Sopenharmony_ci return 0; 208fb299fa2Sopenharmony_ci } 209fb299fa2Sopenharmony_ci}; 210fb299fa2Sopenharmony_ci 211fb299fa2Sopenharmony_ciHWTEST_F(PkgPackageTest, TestUpdaterPreProcess, TestSize.Level1) 212fb299fa2Sopenharmony_ci{ 213fb299fa2Sopenharmony_ci PkgPackageTest test; 214fb299fa2Sopenharmony_ci EXPECT_EQ(0, test.TestUpdaterPreProcess()); 215fb299fa2Sopenharmony_ci} 216fb299fa2Sopenharmony_ci 217fb299fa2Sopenharmony_ciHWTEST_F(PkgPackageTest, TestPackageInfoFail, TestSize.Level1) 218fb299fa2Sopenharmony_ci{ 219fb299fa2Sopenharmony_ci PkgPackageTest test; 220fb299fa2Sopenharmony_ci EXPECT_EQ(0, test.TestPackageInfoFail()); 221fb299fa2Sopenharmony_ci} 222fb299fa2Sopenharmony_ci 223fb299fa2Sopenharmony_ciHWTEST_F(PkgPackageTest, TestPkgFile, TestSize.Level1) 224fb299fa2Sopenharmony_ci{ 225fb299fa2Sopenharmony_ci PkgPackageTest test; 226fb299fa2Sopenharmony_ci EXPECT_EQ(0, test.TestPkgFile()); 227fb299fa2Sopenharmony_ci} 228fb299fa2Sopenharmony_ci 229fb299fa2Sopenharmony_ciHWTEST_F(PkgPackageTest, TestPkgFileInvalid, TestSize.Level1) 230fb299fa2Sopenharmony_ci{ 231fb299fa2Sopenharmony_ci PkgPackageTest test; 232fb299fa2Sopenharmony_ci EXPECT_EQ(0, test.TestPkgFileInvalid()); 233fb299fa2Sopenharmony_ci} 234fb299fa2Sopenharmony_ci 235fb299fa2Sopenharmony_ciHWTEST_F(PkgPackageTest, TestBigZip, TestSize.Level1) 236fb299fa2Sopenharmony_ci{ 237fb299fa2Sopenharmony_ci PkgPackageTest test; 238fb299fa2Sopenharmony_ci EXPECT_EQ(0, test.TestBigZipEntry()); 239fb299fa2Sopenharmony_ci} 240fb299fa2Sopenharmony_ci} 241