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