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 <fcntl.h>
18fb299fa2Sopenharmony_ci#include <functional>
19fb299fa2Sopenharmony_ci#include <gtest/gtest.h>
20fb299fa2Sopenharmony_ci#include <iostream>
21fb299fa2Sopenharmony_ci#include <memory>
22fb299fa2Sopenharmony_ci#include <sys/mman.h>
23fb299fa2Sopenharmony_ci#include <sys/stat.h>
24fb299fa2Sopenharmony_ci#include <unistd.h>
25fb299fa2Sopenharmony_ci#include "log.h"
26fb299fa2Sopenharmony_ci#include "pkg_algorithm.h"
27fb299fa2Sopenharmony_ci#include "pkg_gzipfile.h"
28fb299fa2Sopenharmony_ci#include "pkg_manager.h"
29fb299fa2Sopenharmony_ci#include "pkg_manager_impl.h"
30fb299fa2Sopenharmony_ci#include "pkg_test.h"
31fb299fa2Sopenharmony_ci#include "pkg_utils.h"
32fb299fa2Sopenharmony_ci#include "securec.h"
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_ciusing namespace std;
35fb299fa2Sopenharmony_ciusing namespace Hpackage;
36fb299fa2Sopenharmony_ciusing namespace Updater;
37fb299fa2Sopenharmony_ciusing namespace testing::ext;
38fb299fa2Sopenharmony_ci
39fb299fa2Sopenharmony_cinamespace UpdaterUt {
40fb299fa2Sopenharmony_ciconstexpr auto WINDOWBITS = -15;  // 32kb window; negative to indicate a raw stream.
41fb299fa2Sopenharmony_ciconstexpr auto MEMLEVEL = 8;      // the default value.
42fb299fa2Sopenharmony_ciconstexpr auto STRATEGY = Z_DEFAULT_STRATEGY;
43fb299fa2Sopenharmony_ciconstexpr uint16_t HEADER_CRC = 0x02; /* bit 1 set: CRC16 for the gzip header */
44fb299fa2Sopenharmony_ciconstexpr uint16_t EXTRA_FIELD = 0x04; /* bit 2 set: extra field present */
45fb299fa2Sopenharmony_ciconstexpr uint16_t ORIG_NAME = 0x08; /* bit 3 set: original file name present */
46fb299fa2Sopenharmony_ciconstexpr uint16_t COMMENT = 0x10; /* bit 4 set: file comment present */
47fb299fa2Sopenharmony_ciconstexpr uint32_t DEFAULT_LOCAK_DIGEST = 32;
48fb299fa2Sopenharmony_ciconstexpr uint32_t TEST_FILE_VERSION = 1000;
49fb299fa2Sopenharmony_ciconstexpr uint32_t TEST_DECOMPRESS_GZIP_OFFSET = 2;
50fb299fa2Sopenharmony_ciconstexpr int32_t LZ4F_MAX_BLOCKID = 7;
51fb299fa2Sopenharmony_ciconstexpr int32_t ZIP_MAX_LEVEL = 9;
52fb299fa2Sopenharmony_ci
53fb299fa2Sopenharmony_ciclass TestPkgStream : public PkgStreamImpl {
54fb299fa2Sopenharmony_cipublic:
55fb299fa2Sopenharmony_ci    explicit TestPkgStream(PkgManager::PkgManagerPtr pkgManager, std::string fileName)
56fb299fa2Sopenharmony_ci        : PkgStreamImpl(pkgManager, fileName) {}
57fb299fa2Sopenharmony_ci    virtual ~TestPkgStream() {}
58fb299fa2Sopenharmony_ci
59fb299fa2Sopenharmony_ci    int32_t Read(PkgBuffer &buff, size_t start, size_t size, size_t &readLen) override
60fb299fa2Sopenharmony_ci    {
61fb299fa2Sopenharmony_ci        return PkgStreamImpl::Read(buff, start, size, readLen);
62fb299fa2Sopenharmony_ci    }
63fb299fa2Sopenharmony_ci    int32_t Write(const PkgBuffer &ptr, size_t size, size_t start) override
64fb299fa2Sopenharmony_ci    {
65fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
66fb299fa2Sopenharmony_ci    }
67fb299fa2Sopenharmony_ci    int32_t Seek(long int sizeT, int whence) override
68fb299fa2Sopenharmony_ci    {
69fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
70fb299fa2Sopenharmony_ci    }
71fb299fa2Sopenharmony_ci    int32_t Flush(size_t size) override
72fb299fa2Sopenharmony_ci    {
73fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
74fb299fa2Sopenharmony_ci    }
75fb299fa2Sopenharmony_ci    const std::string GetFileName() const override
76fb299fa2Sopenharmony_ci    {
77fb299fa2Sopenharmony_ci        return "";
78fb299fa2Sopenharmony_ci    }
79fb299fa2Sopenharmony_ci    size_t GetFileLength() override
80fb299fa2Sopenharmony_ci    {
81fb299fa2Sopenharmony_ci        return 0;
82fb299fa2Sopenharmony_ci    }
83fb299fa2Sopenharmony_ci};
84fb299fa2Sopenharmony_ci
85fb299fa2Sopenharmony_ciclass PkgMangerTest : public PkgTest {
86fb299fa2Sopenharmony_cipublic:
87fb299fa2Sopenharmony_ci    PkgMangerTest() {}
88fb299fa2Sopenharmony_ci    ~PkgMangerTest() override {}
89fb299fa2Sopenharmony_ci
90fb299fa2Sopenharmony_ci    static int TestStreamProcess(const PkgBuffer &ptr, size_t size, size_t start, bool isFinish,
91fb299fa2Sopenharmony_ci        const void *context)
92fb299fa2Sopenharmony_ci    {
93fb299fa2Sopenharmony_ci        PKG_LOGI("TestStreamProcess size %zu, start %zu finish %d", size, start, isFinish);
94fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
95fb299fa2Sopenharmony_ci    }
96fb299fa2Sopenharmony_ci
97fb299fa2Sopenharmony_ci    void GetUpgradePkgInfo(UpgradePkgInfo &pkgInfo, std::vector<std::pair<std::string, ComponentInfo>> &files)
98fb299fa2Sopenharmony_ci    {
99fb299fa2Sopenharmony_ci        pkgInfo.softwareVersion = "100.100.100.100";
100fb299fa2Sopenharmony_ci        pkgInfo.date = "2021-02-02";
101fb299fa2Sopenharmony_ci        pkgInfo.time = "21:23:49";
102fb299fa2Sopenharmony_ci        pkgInfo.productUpdateId = "555.555.100.555";
103fb299fa2Sopenharmony_ci        pkgInfo.pkgInfo.entryCount = testFileNames_.size();
104fb299fa2Sopenharmony_ci        pkgInfo.pkgInfo.digestMethod = PKG_DIGEST_TYPE_SHA256;
105fb299fa2Sopenharmony_ci        pkgInfo.pkgInfo.signMethod = PKG_SIGN_METHOD_RSA;
106fb299fa2Sopenharmony_ci        pkgInfo.pkgInfo.pkgType = PKG_PACK_TYPE_UPGRADE;
107fb299fa2Sopenharmony_ci        pkgInfo.updateFileVersion = TEST_FILE_VERSION;
108fb299fa2Sopenharmony_ci        std::string filePath;
109fb299fa2Sopenharmony_ci        uint16_t componentInfoId = 100;
110fb299fa2Sopenharmony_ci        files.resize(testFileNames_.size());
111fb299fa2Sopenharmony_ci        for (uint32_t i = 0; i < testFileNames_.size(); i++) {
112fb299fa2Sopenharmony_ci            filePath = TEST_PATH_FROM;
113fb299fa2Sopenharmony_ci            filePath += testFileNames_[i].c_str();
114fb299fa2Sopenharmony_ci            files[i].first = filePath;
115fb299fa2Sopenharmony_ci
116fb299fa2Sopenharmony_ci            ComponentInfo* info = &files[i].second;
117fb299fa2Sopenharmony_ci            int ret = BuildFileDigest(*info->digest, sizeof(info->digest), filePath);
118fb299fa2Sopenharmony_ci            EXPECT_EQ(ret, PKG_SUCCESS);
119fb299fa2Sopenharmony_ci            info->fileInfo.identity = testFileNames_[i];
120fb299fa2Sopenharmony_ci            info->fileInfo.unpackedSize = GetFileSize(filePath);
121fb299fa2Sopenharmony_ci            info->fileInfo.packedSize = info->fileInfo.unpackedSize;
122fb299fa2Sopenharmony_ci            info->fileInfo.packMethod = PKG_COMPRESS_METHOD_NONE;
123fb299fa2Sopenharmony_ci            info->fileInfo.digestMethod = PKG_DIGEST_TYPE_SHA256;
124fb299fa2Sopenharmony_ci            info->version = "2.2.2.2";
125fb299fa2Sopenharmony_ci            info->id = componentInfoId;
126fb299fa2Sopenharmony_ci            info->resType = 0;
127fb299fa2Sopenharmony_ci            info->type = 0;
128fb299fa2Sopenharmony_ci            info->originalSize = info->fileInfo.unpackedSize;
129fb299fa2Sopenharmony_ci            info->compFlags = 0;
130fb299fa2Sopenharmony_ci        }
131fb299fa2Sopenharmony_ci    }
132fb299fa2Sopenharmony_ci    int TestPackagePack()
133fb299fa2Sopenharmony_ci    {
134fb299fa2Sopenharmony_ci        PKG_LOGI("\n\n ************* TestPackagePack %s \r\n", testPackageName.c_str());
135fb299fa2Sopenharmony_ci        UpgradePkgInfo pkgInfo;
136fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ComponentInfo>> files;
137fb299fa2Sopenharmony_ci        GetUpgradePkgInfo(pkgInfo, files);
138fb299fa2Sopenharmony_ci        std::string packagePath = TEST_PATH_TO;
139fb299fa2Sopenharmony_ci        packagePath += testPackageName;
140fb299fa2Sopenharmony_ci        int32_t ret = pkgManager_->CreatePackage(packagePath, "", &pkgInfo.pkgInfo, files);
141fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
142fb299fa2Sopenharmony_ci        ret = pkgManager_->CreatePackage(packagePath, GetTestPrivateKeyName(0), &pkgInfo.pkgInfo, files);
143fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_SUCCESS);
144fb299fa2Sopenharmony_ci        return 0;
145fb299fa2Sopenharmony_ci    }
146fb299fa2Sopenharmony_ci
147fb299fa2Sopenharmony_ci    int TestPackagePackFileNotExist()
148fb299fa2Sopenharmony_ci    {
149fb299fa2Sopenharmony_ci        PKG_LOGI("\n\n ************* TestPackagePackFileNotExist %s \r\n", testPackageName.c_str());
150fb299fa2Sopenharmony_ci        UpgradePkgInfo pkgInfo;
151fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ComponentInfo>> files;
152fb299fa2Sopenharmony_ci        GetUpgradePkgInfo(pkgInfo, files);
153fb299fa2Sopenharmony_ci        std::string packagePath = TEST_PATH_TO;
154fb299fa2Sopenharmony_ci        packagePath += testPackageName;
155fb299fa2Sopenharmony_ci
156fb299fa2Sopenharmony_ci        // 修改成错误的路径
157fb299fa2Sopenharmony_ci        files[0].first = "sssssssssss";
158fb299fa2Sopenharmony_ci        int32_t ret = pkgManager_->CreatePackage(packagePath, GetTestPrivateKeyName(0), &pkgInfo.pkgInfo, files);
159fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
160fb299fa2Sopenharmony_ci        return 0;
161fb299fa2Sopenharmony_ci    }
162fb299fa2Sopenharmony_ci
163fb299fa2Sopenharmony_ci    int TestPackagePackParamInvalid()
164fb299fa2Sopenharmony_ci    {
165fb299fa2Sopenharmony_ci        PKG_LOGI("\n\n ************* TestPackagePackParamInvalid %s \r\n", testPackageName.c_str());
166fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ComponentInfo>> files;
167fb299fa2Sopenharmony_ci        std::string packagePath = TEST_PATH_TO;
168fb299fa2Sopenharmony_ci        packagePath += testPackageName;
169fb299fa2Sopenharmony_ci        int32_t ret = pkgManager_->CreatePackage(packagePath, GetTestPrivateKeyName(0), nullptr, files);
170fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_PARAM);
171fb299fa2Sopenharmony_ci        return 0;
172fb299fa2Sopenharmony_ci    }
173fb299fa2Sopenharmony_ci
174fb299fa2Sopenharmony_ci    int TestPkgStreamImpl()
175fb299fa2Sopenharmony_ci    {
176fb299fa2Sopenharmony_ci        std::string path = TEST_PATH_TO + testCombinePkgName;
177fb299fa2Sopenharmony_ci        std::unique_ptr<TestPkgStream> stream = std::make_unique<TestPkgStream>(pkgManager_, path);
178fb299fa2Sopenharmony_ci        EXPECT_NE(stream, nullptr);
179fb299fa2Sopenharmony_ci        constexpr size_t buffSize = 10;
180fb299fa2Sopenharmony_ci        uint8_t buff[buffSize];
181fb299fa2Sopenharmony_ci        size_t size = sizeof(buff);
182fb299fa2Sopenharmony_ci        size_t start = 0;
183fb299fa2Sopenharmony_ci        size_t readLen = 0;
184fb299fa2Sopenharmony_ci        PkgBuffer buffer(buff, sizeof(buff));
185fb299fa2Sopenharmony_ci        int ret = ((PkgStreamPtr)(stream.get()))->Read(buffer, start, size, readLen);
186fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
187fb299fa2Sopenharmony_ci        PkgBuffer data = {};
188fb299fa2Sopenharmony_ci        ret = ((PkgStreamPtr)(stream.get()))->GetBuffer(data);
189fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
190fb299fa2Sopenharmony_ci
191fb299fa2Sopenharmony_ci        ret = ((PkgStreamPtr)(stream.get()))->GetStreamType();
192fb299fa2Sopenharmony_ci        EXPECT_EQ(PkgStream::PkgStreamType_Read, ret);
193fb299fa2Sopenharmony_ci
194fb299fa2Sopenharmony_ci        return 0;
195fb299fa2Sopenharmony_ci    }
196fb299fa2Sopenharmony_ci
197fb299fa2Sopenharmony_ci    int TestInvalidStream()
198fb299fa2Sopenharmony_ci    {
199fb299fa2Sopenharmony_ci        std::string packagePath = TEST_PATH_TO;
200fb299fa2Sopenharmony_ci        packagePath += testPackageName;
201fb299fa2Sopenharmony_ci        auto stream = std::make_unique<FileStream>(pkgManager_, testPackageName, nullptr, 0);
202fb299fa2Sopenharmony_ci        size_t start = 0;
203fb299fa2Sopenharmony_ci        size_t readLen = 0;
204fb299fa2Sopenharmony_ci        size_t bufferSize = 10;
205fb299fa2Sopenharmony_ci        PkgBuffer buffer(bufferSize);
206fb299fa2Sopenharmony_ci        int ret = stream->Read(buffer, start, bufferSize, readLen);
207fb299fa2Sopenharmony_ci        EXPECT_EQ(PKG_INVALID_STREAM, ret);
208fb299fa2Sopenharmony_ci        return 0;
209fb299fa2Sopenharmony_ci    }
210fb299fa2Sopenharmony_ci
211fb299fa2Sopenharmony_ci    int TestRead()
212fb299fa2Sopenharmony_ci    {
213fb299fa2Sopenharmony_ci        constexpr size_t buffSize = 8;
214fb299fa2Sopenharmony_ci        int index = 7;
215fb299fa2Sopenharmony_ci        uint8_t buffValue = 100;
216fb299fa2Sopenharmony_ci        uint8_t buff[buffSize] = {0};
217fb299fa2Sopenharmony_ci        buff[index] = buffValue;
218fb299fa2Sopenharmony_ci        ReadLE64(buff);
219fb299fa2Sopenharmony_ci        return 0;
220fb299fa2Sopenharmony_ci    }
221fb299fa2Sopenharmony_ci
222fb299fa2Sopenharmony_ci    int TestCheckFile()
223fb299fa2Sopenharmony_ci    {
224fb299fa2Sopenharmony_ci        std::string filePath = TEST_PATH_TO;
225fb299fa2Sopenharmony_ci        filePath += "/4444/";
226fb299fa2Sopenharmony_ci        int ret = CheckFile(filePath, PkgStream::PkgStreamType_Read);
227fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, 0);
228fb299fa2Sopenharmony_ci        return 0;
229fb299fa2Sopenharmony_ci    }
230fb299fa2Sopenharmony_ci
231fb299fa2Sopenharmony_ci    int TestGetCurrPath()
232fb299fa2Sopenharmony_ci    {
233fb299fa2Sopenharmony_ci        std::string path = GetCurrPath();
234fb299fa2Sopenharmony_ci        if (path == "./") {
235fb299fa2Sopenharmony_ci            EXPECT_EQ(1, 0);
236fb299fa2Sopenharmony_ci        }
237fb299fa2Sopenharmony_ci        return 0;
238fb299fa2Sopenharmony_ci    }
239fb299fa2Sopenharmony_ci
240fb299fa2Sopenharmony_ci    int TestCreatePackageInvalidFile()
241fb299fa2Sopenharmony_ci    {
242fb299fa2Sopenharmony_ci        UpgradePkgInfo pkgInfo;
243fb299fa2Sopenharmony_ci        size_t testSize = 100;
244fb299fa2Sopenharmony_ci        uint16_t componentInfoId = 100;
245fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ComponentInfo>> files;
246fb299fa2Sopenharmony_ci        GetUpgradePkgInfo(pkgInfo, files);
247fb299fa2Sopenharmony_ci        ComponentInfo info;
248fb299fa2Sopenharmony_ci        info.fileInfo.identity = "aaaaaaaa";
249fb299fa2Sopenharmony_ci        info.fileInfo.unpackedSize = testSize;
250fb299fa2Sopenharmony_ci        info.fileInfo.packedSize = testSize;
251fb299fa2Sopenharmony_ci        info.fileInfo.packMethod = PKG_COMPRESS_METHOD_NONE;
252fb299fa2Sopenharmony_ci        info.fileInfo.digestMethod = PKG_DIGEST_TYPE_SHA256;
253fb299fa2Sopenharmony_ci        info.version = "2.2.2.2";
254fb299fa2Sopenharmony_ci        info.id = componentInfoId;
255fb299fa2Sopenharmony_ci        info.resType = 0;
256fb299fa2Sopenharmony_ci        info.type = 0;
257fb299fa2Sopenharmony_ci        info.originalSize = testSize;
258fb299fa2Sopenharmony_ci        info.compFlags = 0;
259fb299fa2Sopenharmony_ci        std::string packagePath = TEST_PATH_TO;
260fb299fa2Sopenharmony_ci        packagePath += testPackageName;
261fb299fa2Sopenharmony_ci        files.push_back(std::pair<std::string, ComponentInfo>("/qqqqqq", info));
262fb299fa2Sopenharmony_ci        int ret = pkgManager_->CreatePackage(packagePath, GetTestPrivateKeyName(0), &pkgInfo.pkgInfo, files);
263fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
264fb299fa2Sopenharmony_ci        return 0;
265fb299fa2Sopenharmony_ci    }
266fb299fa2Sopenharmony_ci
267fb299fa2Sopenharmony_ci    int TestCreatePackageInvalidSignMethod()
268fb299fa2Sopenharmony_ci    {
269fb299fa2Sopenharmony_ci        UpgradePkgInfo pkgInfo;
270fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ComponentInfo>> files;
271fb299fa2Sopenharmony_ci        GetUpgradePkgInfo(pkgInfo, files);
272fb299fa2Sopenharmony_ci        std::string packagePath = TEST_PATH_TO;
273fb299fa2Sopenharmony_ci        packagePath += testPackageName;
274fb299fa2Sopenharmony_ci        uint8_t signMethodIndex = 10;
275fb299fa2Sopenharmony_ci        pkgInfo.pkgInfo.signMethod = PKG_SIGN_METHOD_RSA + signMethodIndex;
276fb299fa2Sopenharmony_ci        int ret = pkgManager_->CreatePackage(packagePath, GetTestPrivateKeyName(0), &pkgInfo.pkgInfo, files);
277fb299fa2Sopenharmony_ci        EXPECT_NE(ret, 0);
278fb299fa2Sopenharmony_ci        return 0;
279fb299fa2Sopenharmony_ci    }
280fb299fa2Sopenharmony_ci
281fb299fa2Sopenharmony_ci    int TestLz4PackageInvalidFile()
282fb299fa2Sopenharmony_ci    {
283fb299fa2Sopenharmony_ci        EXPECT_NE(pkgManager_, nullptr);
284fb299fa2Sopenharmony_ci
285fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, Lz4FileInfo>> files;
286fb299fa2Sopenharmony_ci        Lz4FileInfo file;
287fb299fa2Sopenharmony_ci        file.fileInfo.identity = testPackageName;
288fb299fa2Sopenharmony_ci        file.fileInfo.packMethod = PKG_COMPRESS_METHOD_ZIP;
289fb299fa2Sopenharmony_ci        file.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC;
290fb299fa2Sopenharmony_ci        files.push_back(std::pair<std::string, Lz4FileInfo>("fileName", file));
291fb299fa2Sopenharmony_ci        PkgInfo pkgInfo;
292fb299fa2Sopenharmony_ci        pkgInfo.signMethod = PKG_SIGN_METHOD_RSA;
293fb299fa2Sopenharmony_ci        pkgInfo.digestMethod  = PKG_DIGEST_TYPE_SHA256;
294fb299fa2Sopenharmony_ci        pkgInfo.pkgType = PKG_PACK_TYPE_GZIP;
295fb299fa2Sopenharmony_ci        std::string fileName = TEST_PATH_TO;
296fb299fa2Sopenharmony_ci        fileName += testGZipPackageName;
297fb299fa2Sopenharmony_ci        int ret = pkgManager_->CreatePackage(fileName, "", &pkgInfo, files);
298fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
299fb299fa2Sopenharmony_ci        ret = pkgManager_->CreatePackage(fileName, GetTestPrivateKeyName(0), nullptr, files);
300fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_PARAM);
301fb299fa2Sopenharmony_ci        ret = pkgManager_->CreatePackage(fileName, GetTestPrivateKeyName(0), &pkgInfo, files);
302fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
303fb299fa2Sopenharmony_ci        return 0;
304fb299fa2Sopenharmony_ci    }
305fb299fa2Sopenharmony_ci
306fb299fa2Sopenharmony_ci    int TestLz4PackageInvalidPkgType()
307fb299fa2Sopenharmony_ci    {
308fb299fa2Sopenharmony_ci        EXPECT_NE(pkgManager_, nullptr);
309fb299fa2Sopenharmony_ci
310fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, Lz4FileInfo>> files;
311fb299fa2Sopenharmony_ci        Lz4FileInfo file;
312fb299fa2Sopenharmony_ci        file.fileInfo.identity = testPackageName;
313fb299fa2Sopenharmony_ci        file.fileInfo.packMethod = PKG_COMPRESS_METHOD_ZIP;
314fb299fa2Sopenharmony_ci        file.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC;
315fb299fa2Sopenharmony_ci        files.push_back(std::pair<std::string, Lz4FileInfo>("fileName", file));
316fb299fa2Sopenharmony_ci        PkgInfo pkgInfo;
317fb299fa2Sopenharmony_ci        pkgInfo.signMethod = PKG_SIGN_METHOD_RSA;
318fb299fa2Sopenharmony_ci        pkgInfo.digestMethod  = PKG_DIGEST_TYPE_SHA256;
319fb299fa2Sopenharmony_ci        uint8_t pkgTypeIndex = 100;
320fb299fa2Sopenharmony_ci        pkgInfo.pkgType = PKG_PACK_TYPE_GZIP + pkgTypeIndex;
321fb299fa2Sopenharmony_ci        std::string fileName = TEST_PATH_TO;
322fb299fa2Sopenharmony_ci        fileName += testGZipPackageName;
323fb299fa2Sopenharmony_ci        int ret = pkgManager_->CreatePackage(fileName, GetTestPrivateKeyName(0), &pkgInfo, files);
324fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
325fb299fa2Sopenharmony_ci        return 0;
326fb299fa2Sopenharmony_ci    }
327fb299fa2Sopenharmony_ci
328fb299fa2Sopenharmony_ci    int TestZipPackageInvalidFile()
329fb299fa2Sopenharmony_ci    {
330fb299fa2Sopenharmony_ci        EXPECT_NE(pkgManager_, nullptr);
331fb299fa2Sopenharmony_ci
332fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ZipFileInfo>> files;
333fb299fa2Sopenharmony_ci        for (auto name : testFileNames_) {
334fb299fa2Sopenharmony_ci            ZipFileInfo file;
335fb299fa2Sopenharmony_ci            file.fileInfo.identity = name;
336fb299fa2Sopenharmony_ci            file.fileInfo.packMethod = PKG_COMPRESS_METHOD_ZIP;
337fb299fa2Sopenharmony_ci            file.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC;
338fb299fa2Sopenharmony_ci            files.push_back(std::pair<std::string, ZipFileInfo>("55555555555", file));
339fb299fa2Sopenharmony_ci        }
340fb299fa2Sopenharmony_ci        PkgInfo pkgInfo;
341fb299fa2Sopenharmony_ci        pkgInfo.signMethod = PKG_SIGN_METHOD_RSA;
342fb299fa2Sopenharmony_ci        pkgInfo.digestMethod  = PKG_DIGEST_TYPE_SHA256;
343fb299fa2Sopenharmony_ci        pkgInfo.pkgType  = PKG_PACK_TYPE_ZIP;
344fb299fa2Sopenharmony_ci        std::string fileName = TEST_PATH_TO;
345fb299fa2Sopenharmony_ci        fileName += testZipPackageName;
346fb299fa2Sopenharmony_ci        int ret = pkgManager_->CreatePackage(fileName, "", &pkgInfo, files);
347fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
348fb299fa2Sopenharmony_ci        ret = pkgManager_->CreatePackage(fileName, GetTestPrivateKeyName(0), nullptr, files);
349fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_PARAM);
350fb299fa2Sopenharmony_ci        ret = pkgManager_->CreatePackage(fileName, GetTestPrivateKeyName(0), &pkgInfo, files);
351fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
352fb299fa2Sopenharmony_ci        return 0;
353fb299fa2Sopenharmony_ci    }
354fb299fa2Sopenharmony_ci
355fb299fa2Sopenharmony_ci    int TestLoadPackageFail()
356fb299fa2Sopenharmony_ci    {
357fb299fa2Sopenharmony_ci        EXPECT_NE(pkgManager_, nullptr);
358fb299fa2Sopenharmony_ci        std::vector<std::string> components;
359fb299fa2Sopenharmony_ci        std::string fileName = TEST_PATH_TO;
360fb299fa2Sopenharmony_ci        fileName += "testZipPackageName.aaa";
361fb299fa2Sopenharmony_ci        int32_t ret = pkgManager_->LoadPackage(fileName, GetTestCertName(0), components);
362fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, PKG_INVALID_FILE);
363fb299fa2Sopenharmony_ci        return 0;
364fb299fa2Sopenharmony_ci    }
365fb299fa2Sopenharmony_ci
366fb299fa2Sopenharmony_ci    void TestDecompressLz4plus(Hpackage::Lz4FileInfo &lz4Info)
367fb299fa2Sopenharmony_ci    {
368fb299fa2Sopenharmony_ci        EXPECT_NE(pkgManager_, nullptr);
369fb299fa2Sopenharmony_ci        int8_t compressionLevel = 2;
370fb299fa2Sopenharmony_ci        lz4Info.fileInfo.identity = "Lz4";
371fb299fa2Sopenharmony_ci        lz4Info.fileInfo.packMethod = PKG_COMPRESS_METHOD_LZ4;
372fb299fa2Sopenharmony_ci        lz4Info.fileInfo.digestMethod = PKG_DIGEST_TYPE_NONE;
373fb299fa2Sopenharmony_ci        lz4Info.compressionLevel = compressionLevel;
374fb299fa2Sopenharmony_ci        lz4Info.blockSizeID = 0;
375fb299fa2Sopenharmony_ci        lz4Info.contentChecksumFlag = 0;
376fb299fa2Sopenharmony_ci        lz4Info.blockIndependence = 0;
377fb299fa2Sopenharmony_ci    }
378fb299fa2Sopenharmony_ci
379fb299fa2Sopenharmony_ci    int CheckDataIntegrityLz4(Hpackage::Lz4FileInfo &lz4Info, size_t fileSize,
380fb299fa2Sopenharmony_ci        PkgManager::StreamPtr stream, void *mappedData, std::vector<uint8_t> &digest)
381fb299fa2Sopenharmony_ci    {
382fb299fa2Sopenharmony_ci        size_t addrOffset = 4;
383fb299fa2Sopenharmony_ci        TestDecompressLz4plus(lz4Info);
384fb299fa2Sopenharmony_ci        Hpackage::PkgBuffer buffer(static_cast<uint8_t*>(mappedData) + addrOffset, fileSize);
385fb299fa2Sopenharmony_ci        int32_t ret = pkgManager_->DecompressBuffer(&lz4Info.fileInfo, buffer, stream);
386fb299fa2Sopenharmony_ci
387fb299fa2Sopenharmony_ci        // 生成摘要,检查数据完整
388fb299fa2Sopenharmony_ci        SHA256_CTX sha256Ctx = {};
389fb299fa2Sopenharmony_ci        SHA256_Init(&sha256Ctx);
390fb299fa2Sopenharmony_ci        SHA256_Update(&sha256Ctx, static_cast<const uint8_t*>(mappedData), lz4Info.fileInfo.packedSize + 4);
391fb299fa2Sopenharmony_ci        SHA256_Final(digest.data(), &sha256Ctx);
392fb299fa2Sopenharmony_ci        if (ret != 0) {
393fb299fa2Sopenharmony_ci            PKG_LOGE("Can not decompress buff ");
394fb299fa2Sopenharmony_ci            return -1;
395fb299fa2Sopenharmony_ci        }
396fb299fa2Sopenharmony_ci        PKG_LOGI("GetLz4UncompressedData packedSize:%zu unpackedSize:%zu fileSize: %zu",
397fb299fa2Sopenharmony_ci            lz4Info.fileInfo.packedSize, lz4Info.fileInfo.unpackedSize, fileSize);
398fb299fa2Sopenharmony_ci        return 0;
399fb299fa2Sopenharmony_ci    }
400fb299fa2Sopenharmony_ci
401fb299fa2Sopenharmony_ci    int TestDecompressLz4(Hpackage::Lz4FileInfo &lz4Info,
402fb299fa2Sopenharmony_ci        std::vector<uint8_t> &uncompressedData, std::vector<uint8_t> &digest)
403fb299fa2Sopenharmony_ci    {
404fb299fa2Sopenharmony_ci        std::string testFileName = TEST_PATH_FROM + "../diffpatch/PatchLz4test_new.lz4";
405fb299fa2Sopenharmony_ci        size_t fileSize = GetFileSize(testFileName);
406fb299fa2Sopenharmony_ci        int32_t fd = open(testFileName.c_str(), O_RDWR);
407fb299fa2Sopenharmony_ci        if (fd <= 0) {
408fb299fa2Sopenharmony_ci            PKG_LOGE("Can not open file ");
409fb299fa2Sopenharmony_ci            return -1;
410fb299fa2Sopenharmony_ci        }
411fb299fa2Sopenharmony_ci
412fb299fa2Sopenharmony_ci        size_t uncompressedDataSize = 1024;
413fb299fa2Sopenharmony_ci        uncompressedData.resize(uncompressedDataSize);
414fb299fa2Sopenharmony_ci        PkgManager::StreamPtr stream = nullptr;
415fb299fa2Sopenharmony_ci        pkgManager_->CreatePkgStream(stream, "Lz4",
416fb299fa2Sopenharmony_ci            [&](const PkgBuffer &buffer, size_t size, size_t start, bool isFinish, const void* context) -> int {
417fb299fa2Sopenharmony_ci                (void)isFinish;
418fb299fa2Sopenharmony_ci                (void)context;
419fb299fa2Sopenharmony_ci                (void)size;
420fb299fa2Sopenharmony_ci                (void)start;
421fb299fa2Sopenharmony_ci                (void)buffer;
422fb299fa2Sopenharmony_ci                size_t oldSize = uncompressedData.size();
423fb299fa2Sopenharmony_ci                if ((start + size) > uncompressedData.size()) {
424fb299fa2Sopenharmony_ci                    uncompressedData.resize(oldSize * ((start + size) / oldSize + 1));
425fb299fa2Sopenharmony_ci                }
426fb299fa2Sopenharmony_ci                EXPECT_GE(memcpy_s(uncompressedData.data() + start, size, buffer.buffer, size), 0);
427fb299fa2Sopenharmony_ci                return PKG_SUCCESS;
428fb299fa2Sopenharmony_ci            }, nullptr);
429fb299fa2Sopenharmony_ci
430fb299fa2Sopenharmony_ci        std::unique_ptr<Hpackage::PkgStream, std::function<void(Hpackage::PkgManager::StreamPtr)>> outStream(stream,
431fb299fa2Sopenharmony_ci            [&](Hpackage::PkgManager::StreamPtr stream) {
432fb299fa2Sopenharmony_ci            pkgManager_->ClosePkgStream(stream);
433fb299fa2Sopenharmony_ci        });
434fb299fa2Sopenharmony_ci        if (outStream == nullptr) {
435fb299fa2Sopenharmony_ci            PKG_LOGE("Can not create stream ");
436fb299fa2Sopenharmony_ci            close(fd);
437fb299fa2Sopenharmony_ci            return -1;
438fb299fa2Sopenharmony_ci        }
439fb299fa2Sopenharmony_ci
440fb299fa2Sopenharmony_ci        void* mappedData = mmap(nullptr, fileSize, PROT_READ, MAP_SHARED, fd, 0);
441fb299fa2Sopenharmony_ci        if (mappedData == MAP_FAILED) {
442fb299fa2Sopenharmony_ci            PKG_LOGE("Can not mmap ");
443fb299fa2Sopenharmony_ci            close(fd);
444fb299fa2Sopenharmony_ci            return -2;
445fb299fa2Sopenharmony_ci        }
446fb299fa2Sopenharmony_ci        int ret = CheckDataIntegrityLz4(lz4Info, fileSize, outStream.get(), mappedData, digest);
447fb299fa2Sopenharmony_ci        munmap(mappedData, fileSize);
448fb299fa2Sopenharmony_ci        close(fd);
449fb299fa2Sopenharmony_ci        return ret;
450fb299fa2Sopenharmony_ci    }
451fb299fa2Sopenharmony_ci
452fb299fa2Sopenharmony_ci    void TestDecompressGzipInitFile(Hpackage::ZipFileInfo &zipInfo, size_t &offset,
453fb299fa2Sopenharmony_ci        size_t &fileSize, void *mappedData)
454fb299fa2Sopenharmony_ci    {
455fb299fa2Sopenharmony_ci        int32_t zipMethod = 8;
456fb299fa2Sopenharmony_ci        int32_t zipLevel = 4;
457fb299fa2Sopenharmony_ci        zipInfo.fileInfo.identity = "gzip";
458fb299fa2Sopenharmony_ci        zipInfo.fileInfo.packMethod = PKG_COMPRESS_METHOD_GZIP;
459fb299fa2Sopenharmony_ci        zipInfo.fileInfo.digestMethod = PKG_DIGEST_TYPE_NONE;
460fb299fa2Sopenharmony_ci        zipInfo.method = zipMethod;
461fb299fa2Sopenharmony_ci        zipInfo.level = zipLevel;
462fb299fa2Sopenharmony_ci        zipInfo.memLevel = MEMLEVEL;
463fb299fa2Sopenharmony_ci        zipInfo.windowBits = WINDOWBITS;
464fb299fa2Sopenharmony_ci        zipInfo.strategy = STRATEGY;
465fb299fa2Sopenharmony_ci
466fb299fa2Sopenharmony_ci        auto buffer = reinterpret_cast<uint8_t*>(mappedData);
467fb299fa2Sopenharmony_ci        auto header = reinterpret_cast<GZipHeader*>(mappedData);
468fb299fa2Sopenharmony_ci        // 有扩展头信息
469fb299fa2Sopenharmony_ci        if (header->flags & EXTRA_FIELD) {
470fb299fa2Sopenharmony_ci            uint16_t extLen = ReadLE16(buffer + offset);
471fb299fa2Sopenharmony_ci            offset += sizeof(uint16_t) + extLen;
472fb299fa2Sopenharmony_ci        }
473fb299fa2Sopenharmony_ci        if (header->flags & ORIG_NAME) {
474fb299fa2Sopenharmony_ci            std::string fileName;
475fb299fa2Sopenharmony_ci            PkgFileImpl::ConvertBufferToString(fileName, {buffer + offset, fileSize - offset});
476fb299fa2Sopenharmony_ci            offset += fileName.size() + 1;
477fb299fa2Sopenharmony_ci        }
478fb299fa2Sopenharmony_ci        if (header->flags & COMMENT) {
479fb299fa2Sopenharmony_ci            std::string comment;
480fb299fa2Sopenharmony_ci            PkgFileImpl::ConvertBufferToString(comment, {buffer + offset, fileSize - offset});
481fb299fa2Sopenharmony_ci            offset += comment.size() + 1;
482fb299fa2Sopenharmony_ci        }
483fb299fa2Sopenharmony_ci        if (header->flags & HEADER_CRC) { // 暂不校验
484fb299fa2Sopenharmony_ci            offset += TEST_DECOMPRESS_GZIP_OFFSET;
485fb299fa2Sopenharmony_ci        }
486fb299fa2Sopenharmony_ci        return;
487fb299fa2Sopenharmony_ci    }
488fb299fa2Sopenharmony_ci
489fb299fa2Sopenharmony_ci    int CheckDataIntegrityGzip(Hpackage::ZipFileInfo &zipInfo, size_t fileSize,
490fb299fa2Sopenharmony_ci        PkgManager::StreamPtr stream, void *mappedData, std::vector<uint8_t> &digest)
491fb299fa2Sopenharmony_ci    {
492fb299fa2Sopenharmony_ci        EXPECT_NE(pkgManager_, nullptr);
493fb299fa2Sopenharmony_ci        size_t offset = 10;
494fb299fa2Sopenharmony_ci        TestDecompressGzipInitFile(zipInfo, offset, fileSize, mappedData);
495fb299fa2Sopenharmony_ci
496fb299fa2Sopenharmony_ci        Hpackage::PkgBuffer data(reinterpret_cast<uint8_t*>(mappedData) + offset, fileSize);
497fb299fa2Sopenharmony_ci        int32_t ret = pkgManager_->DecompressBuffer(&zipInfo.fileInfo, data, stream);
498fb299fa2Sopenharmony_ci
499fb299fa2Sopenharmony_ci        // 生成摘要,检查数据完整
500fb299fa2Sopenharmony_ci        SHA256_CTX sha256Ctx = {};
501fb299fa2Sopenharmony_ci        SHA256_Init(&sha256Ctx);
502fb299fa2Sopenharmony_ci        SHA256_Update(&sha256Ctx, reinterpret_cast<const uint8_t*>(mappedData) + offset, zipInfo.fileInfo.packedSize);
503fb299fa2Sopenharmony_ci        SHA256_Final(digest.data(), &sha256Ctx);
504fb299fa2Sopenharmony_ci        if (ret != 0) {
505fb299fa2Sopenharmony_ci            PKG_LOGE("Can not decompress buff ");
506fb299fa2Sopenharmony_ci            return -1;
507fb299fa2Sopenharmony_ci        }
508fb299fa2Sopenharmony_ci        PKG_LOGI("GetGZipUncompressedData packedSize:%zu unpackedSize:%zu",
509fb299fa2Sopenharmony_ci            zipInfo.fileInfo.packedSize, zipInfo.fileInfo.unpackedSize);
510fb299fa2Sopenharmony_ci        return 0;
511fb299fa2Sopenharmony_ci    }
512fb299fa2Sopenharmony_ci
513fb299fa2Sopenharmony_ci    int TestDecompressGzip(Hpackage::ZipFileInfo &zipInfo, std::vector<uint8_t> &uncompressedData,
514fb299fa2Sopenharmony_ci        std::vector<uint8_t> &digest)
515fb299fa2Sopenharmony_ci    {
516fb299fa2Sopenharmony_ci        std::string testFileName = TEST_PATH_FROM + "../applypatch/TestDecompressGzip.new.gz";
517fb299fa2Sopenharmony_ci        size_t fileSize = GetFileSize(testFileName);
518fb299fa2Sopenharmony_ci        size_t uncompressedDataSize = 1024;
519fb299fa2Sopenharmony_ci        int fd = open(testFileName.c_str(), O_RDWR);
520fb299fa2Sopenharmony_ci        if (fd < 0) {
521fb299fa2Sopenharmony_ci            return -1;
522fb299fa2Sopenharmony_ci        }
523fb299fa2Sopenharmony_ci        uncompressedData.resize(uncompressedDataSize);
524fb299fa2Sopenharmony_ci        PkgManager::StreamPtr stream = nullptr;
525fb299fa2Sopenharmony_ci        pkgManager_->CreatePkgStream(stream, "Gzip",
526fb299fa2Sopenharmony_ci            [&](const PkgBuffer &buffer, size_t size, size_t start, bool isFinish, const void* context) -> int {
527fb299fa2Sopenharmony_ci                (void)isFinish;
528fb299fa2Sopenharmony_ci                (void)context;
529fb299fa2Sopenharmony_ci                (void)size;
530fb299fa2Sopenharmony_ci                (void)start;
531fb299fa2Sopenharmony_ci                (void)buffer;
532fb299fa2Sopenharmony_ci                size_t oldSize = uncompressedData.size();
533fb299fa2Sopenharmony_ci                if ((start + size) > uncompressedData.size()) {
534fb299fa2Sopenharmony_ci                    uncompressedData.resize(oldSize * ((start + size) / oldSize + 1));
535fb299fa2Sopenharmony_ci                }
536fb299fa2Sopenharmony_ci                EXPECT_GE(memcpy_s(uncompressedData.data() + start, size, buffer.buffer, size), 0);
537fb299fa2Sopenharmony_ci                return PKG_SUCCESS;
538fb299fa2Sopenharmony_ci            }, nullptr);
539fb299fa2Sopenharmony_ci
540fb299fa2Sopenharmony_ci        std::unique_ptr<Hpackage::PkgStream, std::function<void(Hpackage::PkgManager::StreamPtr)>> outStream(stream,
541fb299fa2Sopenharmony_ci            [&](Hpackage::PkgManager::StreamPtr stream) {
542fb299fa2Sopenharmony_ci            pkgManager_->ClosePkgStream(stream);
543fb299fa2Sopenharmony_ci        });
544fb299fa2Sopenharmony_ci        if (outStream == nullptr) {
545fb299fa2Sopenharmony_ci            PKG_LOGE("Can not create stream ");
546fb299fa2Sopenharmony_ci            close(fd);
547fb299fa2Sopenharmony_ci            return -1;
548fb299fa2Sopenharmony_ci        }
549fb299fa2Sopenharmony_ci
550fb299fa2Sopenharmony_ci        void* mappedData = mmap(nullptr, fileSize, PROT_READ, MAP_SHARED, fd, 0);
551fb299fa2Sopenharmony_ci        if (mappedData == MAP_FAILED) {
552fb299fa2Sopenharmony_ci            PKG_LOGE("Can not mmap ");
553fb299fa2Sopenharmony_ci            close(fd);
554fb299fa2Sopenharmony_ci            return -2;
555fb299fa2Sopenharmony_ci        }
556fb299fa2Sopenharmony_ci        int ret = CheckDataIntegrityGzip(zipInfo, fileSize, outStream.get(), mappedData, digest);
557fb299fa2Sopenharmony_ci        munmap(mappedData, fileSize);
558fb299fa2Sopenharmony_ci        close(fd);
559fb299fa2Sopenharmony_ci        return ret;
560fb299fa2Sopenharmony_ci    }
561fb299fa2Sopenharmony_ci
562fb299fa2Sopenharmony_ci    int TestCompressBuffer(Hpackage::FileInfo &info, std::vector<uint8_t> uncompressedData,
563fb299fa2Sopenharmony_ci        std::vector<uint8_t> digest)
564fb299fa2Sopenharmony_ci    {
565fb299fa2Sopenharmony_ci        EXPECT_NE(pkgManager_, nullptr);
566fb299fa2Sopenharmony_ci        // 生成摘要,检查数据完整
567fb299fa2Sopenharmony_ci        SHA256_CTX sha256Ctx = {};
568fb299fa2Sopenharmony_ci        SHA256_Init(&sha256Ctx);
569fb299fa2Sopenharmony_ci        PkgManager::StreamPtr stream = nullptr;
570fb299fa2Sopenharmony_ci        pkgManager_->CreatePkgStream(stream, "Gzip",
571fb299fa2Sopenharmony_ci            [&](const PkgBuffer &ptr, size_t size, size_t start, bool isFinish, const void* context) -> int {
572fb299fa2Sopenharmony_ci                (void)isFinish;
573fb299fa2Sopenharmony_ci                (void)context;
574fb299fa2Sopenharmony_ci                (void)size;
575fb299fa2Sopenharmony_ci                (void)start;
576fb299fa2Sopenharmony_ci                (void)ptr;
577fb299fa2Sopenharmony_ci                SHA256_Update(&sha256Ctx, ptr.buffer, size);
578fb299fa2Sopenharmony_ci                return PKG_SUCCESS;
579fb299fa2Sopenharmony_ci            }, nullptr);
580fb299fa2Sopenharmony_ci
581fb299fa2Sopenharmony_ci        std::unique_ptr<Hpackage::PkgStream, std::function<void(Hpackage::PkgManager::StreamPtr)>> outStream(stream,
582fb299fa2Sopenharmony_ci            [&](Hpackage::PkgManager::StreamPtr stream) {
583fb299fa2Sopenharmony_ci            pkgManager_->ClosePkgStream(stream);
584fb299fa2Sopenharmony_ci        });
585fb299fa2Sopenharmony_ci        if (outStream == nullptr) {
586fb299fa2Sopenharmony_ci            PKG_LOGE("Can not create stream ");
587fb299fa2Sopenharmony_ci            return -1;
588fb299fa2Sopenharmony_ci        }
589fb299fa2Sopenharmony_ci        Hpackage::PkgBuffer buffer(uncompressedData.data(), info.unpackedSize);
590fb299fa2Sopenharmony_ci        int32_t ret = pkgManager_->CompressBuffer(&info, buffer, outStream.get());
591fb299fa2Sopenharmony_ci        PKG_LOGE("GetGZipUncompressedData packedSize:%zu unpackedSize:%zu",
592fb299fa2Sopenharmony_ci            info.packedSize, info.unpackedSize);
593fb299fa2Sopenharmony_ci        if (ret != 0) {
594fb299fa2Sopenharmony_ci            PKG_LOGE("Fail to CompressBuffer");
595fb299fa2Sopenharmony_ci            return -1;
596fb299fa2Sopenharmony_ci        }
597fb299fa2Sopenharmony_ci        std::vector<uint8_t> localDigest(DEFAULT_LOCAK_DIGEST);
598fb299fa2Sopenharmony_ci        SHA256_Final(localDigest.data(), &sha256Ctx);
599fb299fa2Sopenharmony_ci        ret = memcmp(localDigest.data(), digest.data(), localDigest.size());
600fb299fa2Sopenharmony_ci        PKG_LOGE("digest cmp result %d", ret);
601fb299fa2Sopenharmony_ci        return ret;
602fb299fa2Sopenharmony_ci    }
603fb299fa2Sopenharmony_ci
604fb299fa2Sopenharmony_ci    void TestReadWriteLENull()
605fb299fa2Sopenharmony_ci    {
606fb299fa2Sopenharmony_ci        uint8_t *buff = nullptr;
607fb299fa2Sopenharmony_ci        WriteLE16(buff, 0);
608fb299fa2Sopenharmony_ci        uint16_t ret16 = ReadLE16(buff);
609fb299fa2Sopenharmony_ci        EXPECT_EQ(ret16, 0);
610fb299fa2Sopenharmony_ci        WriteLE32(buff, 0);
611fb299fa2Sopenharmony_ci        uint32_t ret32 = ReadLE32(buff);
612fb299fa2Sopenharmony_ci        EXPECT_EQ(ret32, 0);
613fb299fa2Sopenharmony_ci        uint64_t ret64 = ReadLE64(buff);
614fb299fa2Sopenharmony_ci        EXPECT_EQ(ret64, 0);
615fb299fa2Sopenharmony_ci    }
616fb299fa2Sopenharmony_ci};
617fb299fa2Sopenharmony_ci
618fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestGZipBuffer, TestSize.Level1)
619fb299fa2Sopenharmony_ci{
620fb299fa2Sopenharmony_ci    PkgMangerTest test;
621fb299fa2Sopenharmony_ci    Hpackage::ZipFileInfo zipInfo;
622fb299fa2Sopenharmony_ci    std::vector<uint8_t> digest(32);
623fb299fa2Sopenharmony_ci    std::vector<uint8_t> uncompressedData;
624fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestDecompressGzip(zipInfo, uncompressedData, digest));
625fb299fa2Sopenharmony_ci    int32_t ret = 0;
626fb299fa2Sopenharmony_ci    for (int32_t i = 0; i < ZIP_MAX_LEVEL; i++) {
627fb299fa2Sopenharmony_ci        zipInfo.level = i;
628fb299fa2Sopenharmony_ci        ret = test.TestCompressBuffer(zipInfo.fileInfo, uncompressedData, digest);
629fb299fa2Sopenharmony_ci        if (ret == 0) {
630fb299fa2Sopenharmony_ci            break;
631fb299fa2Sopenharmony_ci        }
632fb299fa2Sopenharmony_ci    }
633fb299fa2Sopenharmony_ci    EXPECT_EQ(0, ret);
634fb299fa2Sopenharmony_ci    uncompressedData.clear();
635fb299fa2Sopenharmony_ci}
636fb299fa2Sopenharmony_ci
637fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestLz4Buffer, TestSize.Level1)
638fb299fa2Sopenharmony_ci{
639fb299fa2Sopenharmony_ci    PkgMangerTest test;
640fb299fa2Sopenharmony_ci    Hpackage::Lz4FileInfo lz4Info;
641fb299fa2Sopenharmony_ci    std::vector<uint8_t> digest(32);
642fb299fa2Sopenharmony_ci    std::vector<uint8_t> uncompressedData;
643fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestDecompressLz4(lz4Info, uncompressedData, digest));
644fb299fa2Sopenharmony_ci    int32_t ret = 0;
645fb299fa2Sopenharmony_ci    for (int32_t i = 0; i < LZ4F_MAX_BLOCKID; i++) {
646fb299fa2Sopenharmony_ci        lz4Info.compressionLevel = 2;
647fb299fa2Sopenharmony_ci        lz4Info.blockSizeID = i;
648fb299fa2Sopenharmony_ci        ret = test.TestCompressBuffer(lz4Info.fileInfo, uncompressedData, digest);
649fb299fa2Sopenharmony_ci        if (ret == 0) {
650fb299fa2Sopenharmony_ci            break;
651fb299fa2Sopenharmony_ci        }
652fb299fa2Sopenharmony_ci    }
653fb299fa2Sopenharmony_ci    EXPECT_EQ(0, ret);
654fb299fa2Sopenharmony_ci    uncompressedData.clear();
655fb299fa2Sopenharmony_ci}
656fb299fa2Sopenharmony_ci
657fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestInvalidCreatePackage, TestSize.Level1)
658fb299fa2Sopenharmony_ci{
659fb299fa2Sopenharmony_ci    PkgMangerTest test;
660fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestPackagePack());
661fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestPackagePackFileNotExist());
662fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestPackagePackParamInvalid());
663fb299fa2Sopenharmony_ci}
664fb299fa2Sopenharmony_ci
665fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestPkgStreamImpl, TestSize.Level1)
666fb299fa2Sopenharmony_ci{
667fb299fa2Sopenharmony_ci    PkgMangerTest test;
668fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestPkgStreamImpl());
669fb299fa2Sopenharmony_ci}
670fb299fa2Sopenharmony_ci
671fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestInvalidStream, TestSize.Level1)
672fb299fa2Sopenharmony_ci{
673fb299fa2Sopenharmony_ci    PkgMangerTest test;
674fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestInvalidStream());
675fb299fa2Sopenharmony_ci}
676fb299fa2Sopenharmony_ci
677fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestRead, TestSize.Level1)
678fb299fa2Sopenharmony_ci{
679fb299fa2Sopenharmony_ci    PkgMangerTest test;
680fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestRead());
681fb299fa2Sopenharmony_ci}
682fb299fa2Sopenharmony_ci
683fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestCheckFile, TestSize.Level1)
684fb299fa2Sopenharmony_ci{
685fb299fa2Sopenharmony_ci    PkgMangerTest test;
686fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestCheckFile());
687fb299fa2Sopenharmony_ci}
688fb299fa2Sopenharmony_ci
689fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestCreatePackageFail, TestSize.Level1)
690fb299fa2Sopenharmony_ci{
691fb299fa2Sopenharmony_ci    PkgMangerTest test;
692fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestCreatePackageInvalidFile());
693fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestCreatePackageInvalidSignMethod());
694fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestLz4PackageInvalidFile());
695fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestLz4PackageInvalidPkgType());
696fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestZipPackageInvalidFile());
697fb299fa2Sopenharmony_ci}
698fb299fa2Sopenharmony_ci
699fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestLoadPackageFail, TestSize.Level1)
700fb299fa2Sopenharmony_ci{
701fb299fa2Sopenharmony_ci    PkgMangerTest test;
702fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestLoadPackageFail());
703fb299fa2Sopenharmony_ci}
704fb299fa2Sopenharmony_ci
705fb299fa2Sopenharmony_ciHWTEST_F(PkgMangerTest, TestReadWriteLENull, TestSize.Level1)
706fb299fa2Sopenharmony_ci{
707fb299fa2Sopenharmony_ci    PkgMangerTest test;
708fb299fa2Sopenharmony_ci    test.TestReadWriteLENull();
709fb299fa2Sopenharmony_ci}
710fb299fa2Sopenharmony_ci}
711