154aa6d63Sopenharmony_ci/* 254aa6d63Sopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 354aa6d63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 454aa6d63Sopenharmony_ci * you may not use this file except in compliance with the License. 554aa6d63Sopenharmony_ci * You may obtain a copy of the License at 654aa6d63Sopenharmony_ci * 754aa6d63Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 854aa6d63Sopenharmony_ci * 954aa6d63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1054aa6d63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1154aa6d63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1254aa6d63Sopenharmony_ci * See the License for the specific language governing permissions and 1354aa6d63Sopenharmony_ci * limitations under the License. 1454aa6d63Sopenharmony_ci */ 1554aa6d63Sopenharmony_ci 1654aa6d63Sopenharmony_ci#include <sys/types.h> 1754aa6d63Sopenharmony_ci#include <sys/stat.h> 1854aa6d63Sopenharmony_ci#include <unistd.h> 1954aa6d63Sopenharmony_ci#include <cstdio> 2054aa6d63Sopenharmony_ci#include <fstream> 2154aa6d63Sopenharmony_ci#include <memory> 2254aa6d63Sopenharmony_ci#include <string> 2354aa6d63Sopenharmony_ci#include <filesystem> 2454aa6d63Sopenharmony_ci#include <gtest/gtest.h> 2554aa6d63Sopenharmony_ci 2654aa6d63Sopenharmony_ci#include "file_utils.h" 2754aa6d63Sopenharmony_ci#include "hap_signer_block_utils.h" 2854aa6d63Sopenharmony_ci#include "zip_entry.h" 2954aa6d63Sopenharmony_ci#include "zip_signer.h" 3054aa6d63Sopenharmony_ci#include "zip_utils.h" 3154aa6d63Sopenharmony_ci 3254aa6d63Sopenharmony_cinamespace OHOS { 3354aa6d63Sopenharmony_cinamespace SignatureTools { 3454aa6d63Sopenharmony_ciconst std::string UNSIGNED_HAP_PATH = "./zip/unsigned.hap"; 3554aa6d63Sopenharmony_ciconst std::string SIGNED_HAP_PATH = "./zip/signed.hap"; 3654aa6d63Sopenharmony_ciconst std::string EMPTY_HAP_PATH = "./zip/empty.hap"; 3754aa6d63Sopenharmony_ciconst std::string DATA_DESCRIPTOR_HAP_PATH = "./zip/data_descriptor_hap.hap"; 3854aa6d63Sopenharmony_ciconst std::string EOCD_ONLY_HAP_PATH = "./zip/eocd_only.hap"; 3954aa6d63Sopenharmony_ciconst std::string DUMMY_HAP_PATH = "./zip/dummy.hap"; 4054aa6d63Sopenharmony_ciconst std::string ZIP_ENTRIES_WRONG_HAP_V1_PATH = "./zip/zip_entries_wrong_v1.hap"; 4154aa6d63Sopenharmony_ciconst std::string ZIP_ENTRIES_WRONG_HAP_V2_PATH = "./zip/zip_entries_wrong_v2.hap"; 4254aa6d63Sopenharmony_ciconst std::string ZIP_ENTRIES_WRONG_HAP_V3_PATH = "./zip/zip_entries_wrong_v3.hap"; 4354aa6d63Sopenharmony_ciconst std::string ZIP_ENTRIES_WRONG_HAP_V4_PATH = "./zip/zip_entries_wrong_v4.hap"; 4454aa6d63Sopenharmony_ciconst std::string ZIP_ENTRY_DATA_WRONG_HAP_PATH = "./zip/zip_entry_data_wrong.hap"; 4554aa6d63Sopenharmony_ciconst std::string CD_ONLY_HAP_V1_PATH = "./zip/cd_only_v1.hap"; 4654aa6d63Sopenharmony_ciconst std::string CD_ONLY_HAP_V2_PATH = "./zip/cd_only_v2.hap"; 4754aa6d63Sopenharmony_ciconst std::string OUTPUT_HAP_PATH = "./zip/output.hap"; 4854aa6d63Sopenharmony_ciconst std::string ZERO_HAP_PATH = "./zip/zero.hap"; 4954aa6d63Sopenharmony_ciconst static int ALIGNMENT = 4; 5054aa6d63Sopenharmony_ciclass ZipSignerTest : public testing::Test { 5154aa6d63Sopenharmony_cipublic: 5254aa6d63Sopenharmony_ci static void SetUpTestCase(void); 5354aa6d63Sopenharmony_ci static void TearDownTestCase(void); 5454aa6d63Sopenharmony_ci void SetUp(); 5554aa6d63Sopenharmony_ci void TearDown(); 5654aa6d63Sopenharmony_ci}; 5754aa6d63Sopenharmony_ci 5854aa6d63Sopenharmony_civoid ZipSignerTest::SetUpTestCase(void) 5954aa6d63Sopenharmony_ci{ 6054aa6d63Sopenharmony_ci (void)rename("./zip/cd_only_v1.txt", CD_ONLY_HAP_V1_PATH.c_str()); 6154aa6d63Sopenharmony_ci (void)rename("./zip/cd_only_v2.txt", CD_ONLY_HAP_V2_PATH.c_str()); 6254aa6d63Sopenharmony_ci (void)rename("./zip/data_descriptor_hap.txt", DATA_DESCRIPTOR_HAP_PATH.c_str()); 6354aa6d63Sopenharmony_ci (void)rename("./zip/unsigned.txt", UNSIGNED_HAP_PATH.c_str()); 6454aa6d63Sopenharmony_ci (void)rename("./zip/signed.txt", SIGNED_HAP_PATH.c_str()); 6554aa6d63Sopenharmony_ci (void)rename("./zip/zip_entries_wrong_v1.txt", ZIP_ENTRIES_WRONG_HAP_V1_PATH.c_str()); 6654aa6d63Sopenharmony_ci (void)rename("./zip/zip_entries_wrong_v2.txt", ZIP_ENTRIES_WRONG_HAP_V2_PATH.c_str()); 6754aa6d63Sopenharmony_ci (void)rename("./zip/zip_entries_wrong_v3.txt", ZIP_ENTRIES_WRONG_HAP_V3_PATH.c_str()); 6854aa6d63Sopenharmony_ci (void)rename("./zip/zip_entries_wrong_v4.txt", ZIP_ENTRIES_WRONG_HAP_V4_PATH.c_str()); 6954aa6d63Sopenharmony_ci (void)rename("./zip/zip_entry_data_wrong.txt", ZIP_ENTRY_DATA_WRONG_HAP_PATH.c_str()); 7054aa6d63Sopenharmony_ci sync(); 7154aa6d63Sopenharmony_ci} 7254aa6d63Sopenharmony_ci 7354aa6d63Sopenharmony_civoid ZipSignerTest::TearDownTestCase(void) 7454aa6d63Sopenharmony_ci{ 7554aa6d63Sopenharmony_ci} 7654aa6d63Sopenharmony_ci 7754aa6d63Sopenharmony_civoid ZipSignerTest::SetUp() 7854aa6d63Sopenharmony_ci{ 7954aa6d63Sopenharmony_ci} 8054aa6d63Sopenharmony_ci 8154aa6d63Sopenharmony_civoid ZipSignerTest::TearDown() 8254aa6d63Sopenharmony_ci{ 8354aa6d63Sopenharmony_ci} 8454aa6d63Sopenharmony_ci 8554aa6d63Sopenharmony_ci/** 8654aa6d63Sopenharmony_ci * @tc.name: Test ZipSigner Full process 8754aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner interface for SUCCESS. 8854aa6d63Sopenharmony_ci * @tc.type: FUNC 8954aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 9054aa6d63Sopenharmony_ci */ 9154aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, FullProcessTest001, testing::ext::TestSize.Level1) 9254aa6d63Sopenharmony_ci{ 9354aa6d63Sopenharmony_ci /* 9454aa6d63Sopenharmony_ci * @tc.steps: step1. test ZipSigner full process function 9554aa6d63Sopenharmony_ci * @tc.expected: step1. use the unsigned hap file, the return will be true. 9654aa6d63Sopenharmony_ci */ 9754aa6d63Sopenharmony_ci std::ifstream rawInput(UNSIGNED_HAP_PATH, std::ios::binary); 9854aa6d63Sopenharmony_ci std::ofstream rawOutput(OUTPUT_HAP_PATH, std::ios::binary | std::ios::trunc | std::ios::out); 9954aa6d63Sopenharmony_ci auto zip1 = std::make_shared<ZipSigner>(); 10054aa6d63Sopenharmony_ci ASSERT_TRUE(zip1->Init(rawInput)); 10154aa6d63Sopenharmony_ci zip1->Alignment(ALIGNMENT); 10254aa6d63Sopenharmony_ci zip1->RemoveSignBlock(); 10354aa6d63Sopenharmony_ci ASSERT_TRUE(zip1->ToFile(rawInput, rawOutput)); 10454aa6d63Sopenharmony_ci rawOutput.close(); 10554aa6d63Sopenharmony_ci rawInput.close(); 10654aa6d63Sopenharmony_ci 10754aa6d63Sopenharmony_ci /* 10854aa6d63Sopenharmony_ci * @tc.steps: step2. test ZipSigner full process function 10954aa6d63Sopenharmony_ci * @tc.expected: step2. use the signed hap file, the return will be true. 11054aa6d63Sopenharmony_ci */ 11154aa6d63Sopenharmony_ci std::ifstream wholeInput(SIGNED_HAP_PATH, std::ios::binary); 11254aa6d63Sopenharmony_ci std::ofstream wholeOutput(OUTPUT_HAP_PATH, std::ios::binary | std::ios::trunc | std::ios::out); 11354aa6d63Sopenharmony_ci auto zip2 = std::make_shared<ZipSigner>(); 11454aa6d63Sopenharmony_ci ASSERT_TRUE(zip2->Init(wholeInput)); 11554aa6d63Sopenharmony_ci zip2->Alignment(ALIGNMENT); 11654aa6d63Sopenharmony_ci zip2->RemoveSignBlock(); 11754aa6d63Sopenharmony_ci ASSERT_TRUE(zip2->ToFile(wholeInput, wholeOutput)); 11854aa6d63Sopenharmony_ci wholeOutput.close(); 11954aa6d63Sopenharmony_ci wholeInput.close(); 12054aa6d63Sopenharmony_ci 12154aa6d63Sopenharmony_ci /* 12254aa6d63Sopenharmony_ci * @tc.steps: step3. test ZipSigner full process function 12354aa6d63Sopenharmony_ci * @tc.expected: step3. use the hap file with data descriptor, the return will be true. 12454aa6d63Sopenharmony_ci */ 12554aa6d63Sopenharmony_ci std::ifstream dataDescInput(DATA_DESCRIPTOR_HAP_PATH, std::ios::binary); 12654aa6d63Sopenharmony_ci std::ofstream dataDescOutput(OUTPUT_HAP_PATH, std::ios::binary | std::ios::trunc | std::ios::out); 12754aa6d63Sopenharmony_ci auto zip3 = std::make_shared<ZipSigner>(); 12854aa6d63Sopenharmony_ci ASSERT_TRUE(zip3->Init(dataDescInput)); 12954aa6d63Sopenharmony_ci zip3->Alignment(ALIGNMENT); 13054aa6d63Sopenharmony_ci zip3->RemoveSignBlock(); 13154aa6d63Sopenharmony_ci ASSERT_TRUE(zip3->ToFile(dataDescInput, dataDescOutput)); 13254aa6d63Sopenharmony_ci dataDescOutput.close(); 13354aa6d63Sopenharmony_ci dataDescInput.close(); 13454aa6d63Sopenharmony_ci} 13554aa6d63Sopenharmony_ci 13654aa6d63Sopenharmony_ci/** 13754aa6d63Sopenharmony_ci * @tc.name: Test ZipSigner Init Function 13854aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner interface for SUCCESS and FAIL 13954aa6d63Sopenharmony_ci * @tc.type: FUNC 14054aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 14154aa6d63Sopenharmony_ci */ 14254aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipSignerInitTest001, testing::ext::TestSize.Level1) 14354aa6d63Sopenharmony_ci{ 14454aa6d63Sopenharmony_ci /* 14554aa6d63Sopenharmony_ci * @tc.steps: step1. test Init function 14654aa6d63Sopenharmony_ci * @tc.expected: step1. make the hap file is not exist, the return will be false. 14754aa6d63Sopenharmony_ci */ 14854aa6d63Sopenharmony_ci auto zip1 = std::make_shared<ZipSigner>(); 14954aa6d63Sopenharmony_ci std::ifstream dummyInput(DUMMY_HAP_PATH, std::ios::binary); 15054aa6d63Sopenharmony_ci ASSERT_FALSE(zip1->Init(dummyInput)); 15154aa6d63Sopenharmony_ci /* 15254aa6d63Sopenharmony_ci * @tc.steps: step2. test Init function 15354aa6d63Sopenharmony_ci * @tc.expected: step2. make the hap file is empty, the return will be false. 15454aa6d63Sopenharmony_ci */ 15554aa6d63Sopenharmony_ci std::ofstream emptyOuptut(EMPTY_HAP_PATH, std::ios::binary | std::ios::trunc | std::ios::out); 15654aa6d63Sopenharmony_ci emptyOuptut << ""; 15754aa6d63Sopenharmony_ci emptyOuptut.close(); 15854aa6d63Sopenharmony_ci auto zip2 = std::make_shared<ZipSigner>(); 15954aa6d63Sopenharmony_ci std::ifstream emptyInput(EMPTY_HAP_PATH, std::ios::binary); 16054aa6d63Sopenharmony_ci ASSERT_FALSE(zip2->Init(emptyInput)); 16154aa6d63Sopenharmony_ci emptyInput.close(); 16254aa6d63Sopenharmony_ci 16354aa6d63Sopenharmony_ci /* 16454aa6d63Sopenharmony_ci * @tc.steps: step3. test Init function 16554aa6d63Sopenharmony_ci * @tc.expected: step3. make the hap file is ONLY have the eocd block, the return will be false. 16654aa6d63Sopenharmony_ci */ 16754aa6d63Sopenharmony_ci std::ofstream eocdOutput(EOCD_ONLY_HAP_PATH, std::ios::binary | std::ios::trunc | std::ios::out); 16854aa6d63Sopenharmony_ci char eocd[] = {0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 16954aa6d63Sopenharmony_ci 0x46, 0x02, 0x00, 0x00, 0x00, 0x00}; 17054aa6d63Sopenharmony_ci eocdOutput.write(eocd, sizeof(eocd) / sizeof(eocd[0])); 17154aa6d63Sopenharmony_ci eocdOutput.close(); 17254aa6d63Sopenharmony_ci auto zip3 = std::make_shared<ZipSigner>(); 17354aa6d63Sopenharmony_ci std::ifstream eocdInput(EOCD_ONLY_HAP_PATH, std::ios::binary); 17454aa6d63Sopenharmony_ci ASSERT_FALSE(zip3->Init(eocdInput)); 17554aa6d63Sopenharmony_ci eocdInput.close(); 17654aa6d63Sopenharmony_ci} 17754aa6d63Sopenharmony_ci 17854aa6d63Sopenharmony_ci/** 17954aa6d63Sopenharmony_ci * @tc.name: Test GetZipEntries Function 18054aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner::GetZipEntries() interface for FAIL. 18154aa6d63Sopenharmony_ci * @tc.type: FUNC 18254aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 18354aa6d63Sopenharmony_ci */ 18454aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, GetZipEntriesTest001, testing::ext::TestSize.Level1) 18554aa6d63Sopenharmony_ci{ 18654aa6d63Sopenharmony_ci /* 18754aa6d63Sopenharmony_ci * @tc.steps: step1. test GetZipEntries function 18854aa6d63Sopenharmony_ci * @tc.expected: step1. make the cd offset int front of entry end, return will be false. 18954aa6d63Sopenharmony_ci */ 19054aa6d63Sopenharmony_ci std::ifstream inputFile(ZIP_ENTRIES_WRONG_HAP_V3_PATH, std::ios::binary); 19154aa6d63Sopenharmony_ci auto zip = std::make_shared<ZipSigner>(); 19254aa6d63Sopenharmony_ci ASSERT_FALSE(zip->Init(inputFile)); 19354aa6d63Sopenharmony_ci} 19454aa6d63Sopenharmony_ci 19554aa6d63Sopenharmony_ci/** 19654aa6d63Sopenharmony_ci * @tc.name: Test Alignment Function 19754aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipEntry::Alignment() interface. 19854aa6d63Sopenharmony_ci * @tc.type: FUNC 19954aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 20054aa6d63Sopenharmony_ci */ 20154aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, AlignmentTest001, testing::ext::TestSize.Level1) 20254aa6d63Sopenharmony_ci{ 20354aa6d63Sopenharmony_ci /* 20454aa6d63Sopenharmony_ci * @tc.steps: step1. test Alignment function 20554aa6d63Sopenharmony_ci * @tc.expected: step1. make the alignment is equal to 0, return will be equal to -1. 20654aa6d63Sopenharmony_ci */ 20754aa6d63Sopenharmony_ci std::ifstream inputFile(SIGNED_HAP_PATH, std::ios::binary); 20854aa6d63Sopenharmony_ci auto zip1 = std::make_shared<ZipSigner>(); 20954aa6d63Sopenharmony_ci ASSERT_TRUE(zip1->Init(inputFile)); 21054aa6d63Sopenharmony_ci auto zipEntries1 = zip1->GetZipEntries(); 21154aa6d63Sopenharmony_ci int zeroAlignment = 0; 21254aa6d63Sopenharmony_ci for (const auto& zipEntry : zipEntries1) { 21354aa6d63Sopenharmony_ci ASSERT_EQ(zipEntry->Alignment(zeroAlignment), -1); 21454aa6d63Sopenharmony_ci } 21554aa6d63Sopenharmony_ci 21654aa6d63Sopenharmony_ci /* 21754aa6d63Sopenharmony_ci * @tc.steps: step2. test Alignment function 21854aa6d63Sopenharmony_ci * @tc.expected: step2. make the alignment is equal to 4, return will be equal to 0. 21954aa6d63Sopenharmony_ci */ 22054aa6d63Sopenharmony_ci auto zip2 = std::make_shared<ZipSigner>(); 22154aa6d63Sopenharmony_ci ASSERT_TRUE(zip2->Init(inputFile)); 22254aa6d63Sopenharmony_ci auto zipEntries2 = zip2->GetZipEntries(); 22354aa6d63Sopenharmony_ci for (const auto& zipEntry : zipEntries2) { 22454aa6d63Sopenharmony_ci ASSERT_EQ(zipEntry->Alignment(ALIGNMENT), 0); 22554aa6d63Sopenharmony_ci } 22654aa6d63Sopenharmony_ci 22754aa6d63Sopenharmony_ci /* 22854aa6d63Sopenharmony_ci * @tc.steps: step3. test Alignment function 22954aa6d63Sopenharmony_ci * @tc.expected: step3. make the alignment is equal to 4, return will be equal to 1. 23054aa6d63Sopenharmony_ci */ 23154aa6d63Sopenharmony_ci std::ifstream zipEntriesInput(ZIP_ENTRIES_WRONG_HAP_V4_PATH, std::ios::binary); 23254aa6d63Sopenharmony_ci auto zip3 = std::make_shared<ZipSigner>(); 23354aa6d63Sopenharmony_ci ASSERT_TRUE(zip3->Init(zipEntriesInput)); 23454aa6d63Sopenharmony_ci auto zipEntries3 = zip3->GetZipEntries(); 23554aa6d63Sopenharmony_ci for (const auto& zipEntry : zipEntries3) { 23654aa6d63Sopenharmony_ci ASSERT_EQ(zipEntry->Alignment(ALIGNMENT), 1); 23754aa6d63Sopenharmony_ci } 23854aa6d63Sopenharmony_ci 23954aa6d63Sopenharmony_ci zipEntriesInput.close(); 24054aa6d63Sopenharmony_ci inputFile.close(); 24154aa6d63Sopenharmony_ci} 24254aa6d63Sopenharmony_ci 24354aa6d63Sopenharmony_ci/** 24454aa6d63Sopenharmony_ci * @tc.name: Test GetCDOffset Function 24554aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner::GetCDOffset() interface for SUCCESS. 24654aa6d63Sopenharmony_ci * @tc.type: FUNC 24754aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 24854aa6d63Sopenharmony_ci */ 24954aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, GetCDOffsetTest001, testing::ext::TestSize.Level1) 25054aa6d63Sopenharmony_ci{ 25154aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 25254aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 25354aa6d63Sopenharmony_ci bool res = zip->Init(inputFile); 25454aa6d63Sopenharmony_ci uint64_t cdOffset = zip->GetCDOffset(); 25554aa6d63Sopenharmony_ci EXPECT_EQ(res && cdOffset != 0, true); 25654aa6d63Sopenharmony_ci} 25754aa6d63Sopenharmony_ci 25854aa6d63Sopenharmony_ci/** 25954aa6d63Sopenharmony_ci * @tc.name: Test GetEOCDOffset Function 26054aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner::GetEOCDOffset() interface for SUCCESS. 26154aa6d63Sopenharmony_ci * @tc.type: FUNC 26254aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 26354aa6d63Sopenharmony_ci */ 26454aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, GetEOCDOffsetTest001, testing::ext::TestSize.Level1) 26554aa6d63Sopenharmony_ci{ 26654aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 26754aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 26854aa6d63Sopenharmony_ci bool res = zip->Init(inputFile); 26954aa6d63Sopenharmony_ci uint64_t eocdOffset = zip->GetEOCDOffset(); 27054aa6d63Sopenharmony_ci EXPECT_EQ(res && eocdOffset != 0, true); 27154aa6d63Sopenharmony_ci} 27254aa6d63Sopenharmony_ci 27354aa6d63Sopenharmony_ci/** 27454aa6d63Sopenharmony_ci * @tc.name: Test ToFile Function 27554aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner::ToFile() interface for SUCCESS. 27654aa6d63Sopenharmony_ci * @tc.type: FUNC 27754aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 27854aa6d63Sopenharmony_ci */ 27954aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ToFileTest001, testing::ext::TestSize.Level1) 28054aa6d63Sopenharmony_ci{ 28154aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 28254aa6d63Sopenharmony_ci std::ofstream outputFile(OUTPUT_HAP_PATH, std::ios::binary | std::ios::trunc); 28354aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 28454aa6d63Sopenharmony_ci bool initRes = zip->Init(inputFile); 28554aa6d63Sopenharmony_ci bool toFileRes = zip->ToFile(inputFile, outputFile); 28654aa6d63Sopenharmony_ci EXPECT_EQ(initRes && toFileRes, true); 28754aa6d63Sopenharmony_ci} 28854aa6d63Sopenharmony_ci 28954aa6d63Sopenharmony_ci/** 29054aa6d63Sopenharmony_ci * @tc.name: Test ToFile Function 29154aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner::ToFile() interface for FAIL. 29254aa6d63Sopenharmony_ci * @tc.type: FUNC 29354aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 29454aa6d63Sopenharmony_ci */ 29554aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ToFileTest002, testing::ext::TestSize.Level1) 29654aa6d63Sopenharmony_ci{ 29754aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 29854aa6d63Sopenharmony_ci /* 29954aa6d63Sopenharmony_ci * @tc.steps: step1. test ToFile function 30054aa6d63Sopenharmony_ci * @tc.expected: step1. make the output file stream is bad, return will be false. 30154aa6d63Sopenharmony_ci */ 30254aa6d63Sopenharmony_ci std::ofstream outputFile("", std::ios::binary | std::ios::trunc); 30354aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 30454aa6d63Sopenharmony_ci bool initRes = zip->Init(inputFile); 30554aa6d63Sopenharmony_ci bool toFileRes = zip->ToFile(inputFile, outputFile); 30654aa6d63Sopenharmony_ci EXPECT_EQ(initRes && !toFileRes, true); 30754aa6d63Sopenharmony_ci} 30854aa6d63Sopenharmony_ci 30954aa6d63Sopenharmony_ci/** 31054aa6d63Sopenharmony_ci * @tc.name: Test ToFile Function 31154aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner::ToFile() interface for FAIL. 31254aa6d63Sopenharmony_ci * @tc.type: FUNC 31354aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 31454aa6d63Sopenharmony_ci */ 31554aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ToFileTest003, testing::ext::TestSize.Level1) 31654aa6d63Sopenharmony_ci{ 31754aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 31854aa6d63Sopenharmony_ci std::ofstream outputFile("", std::ios::binary | std::ios::trunc); 31954aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 32054aa6d63Sopenharmony_ci bool initRes = zip->Init(inputFile); 32154aa6d63Sopenharmony_ci /* 32254aa6d63Sopenharmony_ci * @tc.steps: step1. test ToFile function 32354aa6d63Sopenharmony_ci * @tc.expected: step1. make the input file stream is bad, return will be false. 32454aa6d63Sopenharmony_ci */ 32554aa6d63Sopenharmony_ci std::ifstream bad("", std::ios::binary); 32654aa6d63Sopenharmony_ci 32754aa6d63Sopenharmony_ci bool toFileRes = zip->ToFile(bad, outputFile); 32854aa6d63Sopenharmony_ci EXPECT_EQ(initRes && !toFileRes, true); 32954aa6d63Sopenharmony_ci} 33054aa6d63Sopenharmony_ci 33154aa6d63Sopenharmony_ci/** 33254aa6d63Sopenharmony_ci * @tc.name: Test ToFile Function 33354aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipSigner::ToFile() interface for SUCCESS. 33454aa6d63Sopenharmony_ci * @tc.type: FUNC 33554aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 33654aa6d63Sopenharmony_ci */ 33754aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ToFileTest004, testing::ext::TestSize.Level1) 33854aa6d63Sopenharmony_ci{ 33954aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 34054aa6d63Sopenharmony_ci std::ofstream outputFile(OUTPUT_HAP_PATH, std::ios::binary | std::ios::trunc); 34154aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 34254aa6d63Sopenharmony_ci ASSERT_TRUE(zip->Init(inputFile)); 34354aa6d63Sopenharmony_ci ASSERT_TRUE(zip->ToFile(inputFile, outputFile)); 34454aa6d63Sopenharmony_ci} 34554aa6d63Sopenharmony_ci 34654aa6d63Sopenharmony_ci/* 34754aa6d63Sopenharmony_ci * @tc.name: Alignment_Test_001 34854aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipEntry::Alignment() interface for SUCCESS. 34954aa6d63Sopenharmony_ci * @tc.type: FUNC 35054aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 35154aa6d63Sopenharmony_ci */ 35254aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipEntryAlignmentTest001, testing::ext::TestSize.Level1) 35354aa6d63Sopenharmony_ci{ 35454aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 35554aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 35654aa6d63Sopenharmony_ci bool res = zip->Init(inputFile); 35754aa6d63Sopenharmony_ci std::vector<ZipEntry*> zipEntries = zip->GetZipEntries(); 35854aa6d63Sopenharmony_ci int alignment = 4; 35954aa6d63Sopenharmony_ci for (auto& zipEntry : zipEntries) { 36054aa6d63Sopenharmony_ci int add = zipEntry->Alignment(alignment); 36154aa6d63Sopenharmony_ci EXPECT_EQ(res && add > 0, true); 36254aa6d63Sopenharmony_ci } 36354aa6d63Sopenharmony_ci} 36454aa6d63Sopenharmony_ci 36554aa6d63Sopenharmony_ci/** 36654aa6d63Sopenharmony_ci * @tc.name: Test GetEOCDByBytes Function 36754aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory::GetEOCDByBytes() interface for SUCCESS. 36854aa6d63Sopenharmony_ci * @tc.type: FUNC 36954aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 37054aa6d63Sopenharmony_ci */ 37154aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectoryGetEOCDByBytesTest001, testing::ext::TestSize.Level1) 37254aa6d63Sopenharmony_ci{ 37354aa6d63Sopenharmony_ci std::ifstream input(UNSIGNED_HAP_PATH, std::ios::binary); 37454aa6d63Sopenharmony_ci input.seekg(0, std::ios::end); 37554aa6d63Sopenharmony_ci uint64_t fileSize = input.tellg(); 37654aa6d63Sopenharmony_ci input.seekg(0, std::ios::beg); 37754aa6d63Sopenharmony_ci 37854aa6d63Sopenharmony_ci int eocdLength = EndOfCentralDirectory::EOCD_LENGTH; 37954aa6d63Sopenharmony_ci uint64_t eocdOffset = reinterpret_cast<uint64_t>(fileSize - eocdLength); 38054aa6d63Sopenharmony_ci 38154aa6d63Sopenharmony_ci std::string retStr; 38254aa6d63Sopenharmony_ci int res = FileUtils::ReadFileByOffsetAndLength(input, eocdOffset, eocdLength, retStr); 38354aa6d63Sopenharmony_ci std::optional<EndOfCentralDirectory*> eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(retStr); 38454aa6d63Sopenharmony_ci EXPECT_EQ(res == 0 && eocdByBytes != std::nullopt, true); 38554aa6d63Sopenharmony_ci} 38654aa6d63Sopenharmony_ci 38754aa6d63Sopenharmony_ci/** 38854aa6d63Sopenharmony_ci * @tc.name: Test GetEOCDByBytes Function 38954aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory::GetEOCDByBytes() interface for FAIL. 39054aa6d63Sopenharmony_ci * @tc.type: FUNC 39154aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 39254aa6d63Sopenharmony_ci */ 39354aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectoryGetEOCDByBytesTest002, testing::ext::TestSize.Level1) 39454aa6d63Sopenharmony_ci{ 39554aa6d63Sopenharmony_ci /* 39654aa6d63Sopenharmony_ci * @tc.steps: step1. test GetEOCDByBytes function 39754aa6d63Sopenharmony_ci * @tc.expected: step1. make the eocd bytes is empty, the return will be nullopt. 39854aa6d63Sopenharmony_ci */ 39954aa6d63Sopenharmony_ci std::string str; 40054aa6d63Sopenharmony_ci std::optional<EndOfCentralDirectory*> eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(str); 40154aa6d63Sopenharmony_ci EXPECT_EQ(eocdByBytes == std::nullopt, true); 40254aa6d63Sopenharmony_ci} 40354aa6d63Sopenharmony_ci 40454aa6d63Sopenharmony_ci/** 40554aa6d63Sopenharmony_ci * @tc.name: Test GetEOCDByBytes Function 40654aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory::GetEOCDByBytes() interface for FAIL. 40754aa6d63Sopenharmony_ci * @tc.type: FUNC 40854aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 40954aa6d63Sopenharmony_ci */ 41054aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectoryGetEOCDByBytesTest003, testing::ext::TestSize.Level1) 41154aa6d63Sopenharmony_ci{ 41254aa6d63Sopenharmony_ci /* 41354aa6d63Sopenharmony_ci * @tc.steps: step1. test GetEOCDByBytes function 41454aa6d63Sopenharmony_ci * @tc.expected: step1. make the eocd bytes is all of zero, the return will be nullopt. 41554aa6d63Sopenharmony_ci */ 41654aa6d63Sopenharmony_ci std::string bytes(22, 0); 41754aa6d63Sopenharmony_ci std::optional<EndOfCentralDirectory*> eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(bytes); 41854aa6d63Sopenharmony_ci EXPECT_EQ(eocdByBytes == std::nullopt, true); 41954aa6d63Sopenharmony_ci} 42054aa6d63Sopenharmony_ci 42154aa6d63Sopenharmony_ci/** 42254aa6d63Sopenharmony_ci * @tc.name: Test GetEOCDByBytes Function 42354aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory::GetEOCDByBytes() interface for FAIL with eocd length is wrong 42454aa6d63Sopenharmony_ci * @tc.type: FUNC 42554aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 42654aa6d63Sopenharmony_ci */ 42754aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectoryGetEOCDByBytesTest004, testing::ext::TestSize.Level1) 42854aa6d63Sopenharmony_ci{ 42954aa6d63Sopenharmony_ci std::string bytes{ 43054aa6d63Sopenharmony_ci 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 43154aa6d63Sopenharmony_ci }; 43254aa6d63Sopenharmony_ci std::optional<EndOfCentralDirectory*> eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(bytes); 43354aa6d63Sopenharmony_ci EXPECT_EQ(eocdByBytes == std::nullopt, true); 43454aa6d63Sopenharmony_ci} 43554aa6d63Sopenharmony_ci 43654aa6d63Sopenharmony_ci/** 43754aa6d63Sopenharmony_ci * @tc.name: Test GetEOCDByBytes Function 43854aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory::GetEOCDByBytes() interface for SUCCESS with comment. 43954aa6d63Sopenharmony_ci * @tc.type: FUNC 44054aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 44154aa6d63Sopenharmony_ci */ 44254aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectoryGetEOCDByBytesTest005, testing::ext::TestSize.Level1) 44354aa6d63Sopenharmony_ci{ 44454aa6d63Sopenharmony_ci std::string bytes{ 44554aa6d63Sopenharmony_ci 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1 44654aa6d63Sopenharmony_ci }; 44754aa6d63Sopenharmony_ci std::optional<EndOfCentralDirectory*> eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(bytes); 44854aa6d63Sopenharmony_ci EXPECT_EQ(eocdByBytes != std::nullopt, true); 44954aa6d63Sopenharmony_ci std::string eocdBytes = eocdByBytes.value()->ToBytes(); 45054aa6d63Sopenharmony_ci EXPECT_EQ(eocdBytes.size() > 0, true); 45154aa6d63Sopenharmony_ci} 45254aa6d63Sopenharmony_ci 45354aa6d63Sopenharmony_ci/** 45454aa6d63Sopenharmony_ci * @tc.name: Test GetEOCDByBytes Function 45554aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory::SetComment() interface. 45654aa6d63Sopenharmony_ci * @tc.type: FUNC 45754aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 45854aa6d63Sopenharmony_ci */ 45954aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectorySetCommentTest001, testing::ext::TestSize.Level1) 46054aa6d63Sopenharmony_ci{ 46154aa6d63Sopenharmony_ci std::string bytes{ 46254aa6d63Sopenharmony_ci 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 46354aa6d63Sopenharmony_ci }; 46454aa6d63Sopenharmony_ci std::optional<EndOfCentralDirectory*> eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(bytes); 46554aa6d63Sopenharmony_ci std::string comment{1}; 46654aa6d63Sopenharmony_ci eocdByBytes.value()->SetComment(comment); 46754aa6d63Sopenharmony_ci EXPECT_EQ(eocdByBytes != std::nullopt, true); 46854aa6d63Sopenharmony_ci} 46954aa6d63Sopenharmony_ci 47054aa6d63Sopenharmony_ci/** 47154aa6d63Sopenharmony_ci * @tc.name: Test ToBytes Function 47254aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory::ToBytes() interface for SUCCESS. 47354aa6d63Sopenharmony_ci * @tc.type: FUNC 47454aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 47554aa6d63Sopenharmony_ci */ 47654aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectoryToBytesTest001, testing::ext::TestSize.Level1) 47754aa6d63Sopenharmony_ci{ 47854aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 47954aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 48054aa6d63Sopenharmony_ci bool initRes = zip->Init(inputFile); 48154aa6d63Sopenharmony_ci EndOfCentralDirectory* eocd = zip->GetEndOfCentralDirectory(); 48254aa6d63Sopenharmony_ci std::string eocdBytes = eocd->ToBytes(); 48354aa6d63Sopenharmony_ci int signature = eocd->GetSIGNATURE(); 48454aa6d63Sopenharmony_ci int diskNum = eocd->GetDiskNum(); 48554aa6d63Sopenharmony_ci std::string comment = eocd->GetComment(); 48654aa6d63Sopenharmony_ci EXPECT_EQ(initRes && eocd && eocdBytes.size() > 0 && signature != -1 && comment.size() == 0 && diskNum != -1, 48754aa6d63Sopenharmony_ci true); 48854aa6d63Sopenharmony_ci} 48954aa6d63Sopenharmony_ci 49054aa6d63Sopenharmony_ci/** 49154aa6d63Sopenharmony_ci * @tc.name: Test EndOfCentralDirectory Class 49254aa6d63Sopenharmony_ci * @tc.desc: Test function of EndOfCentralDirectory interface for SUCCESS. 49354aa6d63Sopenharmony_ci * @tc.type: FUNC 49454aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 49554aa6d63Sopenharmony_ci */ 49654aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, EndOfCentralDirectoryTest001, testing::ext::TestSize.Level1) 49754aa6d63Sopenharmony_ci{ 49854aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 49954aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 50054aa6d63Sopenharmony_ci bool initRes = zip->Init(inputFile); 50154aa6d63Sopenharmony_ci EndOfCentralDirectory* eocd = zip->GetEndOfCentralDirectory(); 50254aa6d63Sopenharmony_ci int eocdLength = eocd->GetEocdLength(); 50354aa6d63Sopenharmony_ci int cdStartDiskNum = eocd->GetcDStartDiskNum(); 50454aa6d63Sopenharmony_ci int diskCDNum = eocd->GetThisDiskCDNum(); 50554aa6d63Sopenharmony_ci int eocdLen = eocd->GetLength(); 50654aa6d63Sopenharmony_ci EXPECT_EQ(initRes && eocd && eocdLength > 0 && cdStartDiskNum != -1 && diskCDNum != -1 && eocdLen != -1, 50754aa6d63Sopenharmony_ci true); 50854aa6d63Sopenharmony_ci} 50954aa6d63Sopenharmony_ci 51054aa6d63Sopenharmony_ci/** 51154aa6d63Sopenharmony_ci * @tc.name: Test SetCentralDirectoryOffset Function 51254aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipUtils::SetCentralDirectoryOffset() interface for SUCCESS. 51354aa6d63Sopenharmony_ci * @tc.type: FUNC 51454aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 51554aa6d63Sopenharmony_ci */ 51654aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipUtilsSetCentralDirectoryOffsetTest001, testing::ext::TestSize.Level1) 51754aa6d63Sopenharmony_ci{ 51854aa6d63Sopenharmony_ci std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>(); 51954aa6d63Sopenharmony_ci EXPECT_EQ(outputHap->Init(UNSIGNED_HAP_PATH), true); 52054aa6d63Sopenharmony_ci std::pair<ByteBuffer, int64_t> eocdPair; 52154aa6d63Sopenharmony_ci EXPECT_EQ(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair), true); 52254aa6d63Sopenharmony_ci int64_t centralDirectoryOffset; 52354aa6d63Sopenharmony_ci EXPECT_EQ(HapSignerBlockUtils::GetCentralDirectoryOffset(eocdPair.first, eocdPair.second, 52454aa6d63Sopenharmony_ci centralDirectoryOffset), 52554aa6d63Sopenharmony_ci true); 52654aa6d63Sopenharmony_ci int64_t newCentralDirOffset = centralDirectoryOffset + 10; 52754aa6d63Sopenharmony_ci eocdPair.first.SetPosition(0); 52854aa6d63Sopenharmony_ci EXPECT_EQ(ZipUtils::SetCentralDirectoryOffset(eocdPair.first, newCentralDirOffset), true); 52954aa6d63Sopenharmony_ci} 53054aa6d63Sopenharmony_ci 53154aa6d63Sopenharmony_ci/** 53254aa6d63Sopenharmony_ci * @tc.name: Test SetCentralDirectoryOffset Function 53354aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipUtils::SetCentralDirectoryOffset() interface. 53454aa6d63Sopenharmony_ci * @tc.type: FUNC 53554aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 53654aa6d63Sopenharmony_ci */ 53754aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipUtilsSetCentralDirectoryOffsetTest002, testing::ext::TestSize.Level1) 53854aa6d63Sopenharmony_ci{ 53954aa6d63Sopenharmony_ci std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>(); 54054aa6d63Sopenharmony_ci EXPECT_EQ(outputHap->Init(UNSIGNED_HAP_PATH), true); 54154aa6d63Sopenharmony_ci std::pair<ByteBuffer, int64_t> eocdPair; 54254aa6d63Sopenharmony_ci EXPECT_EQ(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair), true); 54354aa6d63Sopenharmony_ci eocdPair.first.SetPosition(0); 54454aa6d63Sopenharmony_ci /* 54554aa6d63Sopenharmony_ci * @tc.steps: step1. test SetCentralDirectoryOffset function 54654aa6d63Sopenharmony_ci * @tc.expected: step1. make the central directory offset is -1, the return will be false. 54754aa6d63Sopenharmony_ci */ 54854aa6d63Sopenharmony_ci EXPECT_EQ(ZipUtils::SetCentralDirectoryOffset(eocdPair.first, -1), false); 54954aa6d63Sopenharmony_ci} 55054aa6d63Sopenharmony_ci 55154aa6d63Sopenharmony_ci/** 55254aa6d63Sopenharmony_ci * @tc.name: Test SetCentralDirectoryOffset Function 55354aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipUtils::SetCentralDirectoryOffset() interface for FAIL. 55454aa6d63Sopenharmony_ci * @tc.type: FUNC 55554aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 55654aa6d63Sopenharmony_ci */ 55754aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipUtilsSetCentralDirectoryOffsetTest003, testing::ext::TestSize.Level1) 55854aa6d63Sopenharmony_ci{ 55954aa6d63Sopenharmony_ci std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>(); 56054aa6d63Sopenharmony_ci ASSERT_TRUE(outputHap->Init(UNSIGNED_HAP_PATH)); 56154aa6d63Sopenharmony_ci std::pair<ByteBuffer, int64_t> eocdPair; 56254aa6d63Sopenharmony_ci ASSERT_TRUE(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair)); 56354aa6d63Sopenharmony_ci eocdPair.first.SetPosition(0); 56454aa6d63Sopenharmony_ci /* 56554aa6d63Sopenharmony_ci * @tc.steps: step1. test SetCentralDirectoryOffset function 56654aa6d63Sopenharmony_ci * @tc.expected: step1. make the central directory offset is 0x100000000LL, it's greater than 0xffffffffLL, 56754aa6d63Sopenharmony_ci * the return will be false. 56854aa6d63Sopenharmony_ci */ 56954aa6d63Sopenharmony_ci ASSERT_FALSE(ZipUtils::SetCentralDirectoryOffset(eocdPair.first, 0x100000000LL)); 57054aa6d63Sopenharmony_ci} 57154aa6d63Sopenharmony_ci 57254aa6d63Sopenharmony_ci/** 57354aa6d63Sopenharmony_ci * @tc.name: Test ZipEntryHeader Class 57454aa6d63Sopenharmony_ci * @tc.desc: Test function of ZipEntryHeader for SUCCESS. 57554aa6d63Sopenharmony_ci * @tc.type: FUNC 57654aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 57754aa6d63Sopenharmony_ci */ 57854aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipEntryHeaderTest001, testing::ext::TestSize.Level1) 57954aa6d63Sopenharmony_ci{ 58054aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 58154aa6d63Sopenharmony_ci auto zip = std::make_shared<ZipSigner>(); 58254aa6d63Sopenharmony_ci ASSERT_TRUE(zip->Init(inputFile)); 58354aa6d63Sopenharmony_ci 58454aa6d63Sopenharmony_ci auto zipEntries = zip->GetZipEntries(); 58554aa6d63Sopenharmony_ci ASSERT_NE(zipEntries.size(), 0); 58654aa6d63Sopenharmony_ci 58754aa6d63Sopenharmony_ci for (const auto& zipEntry : zipEntries) { 58854aa6d63Sopenharmony_ci auto zipEntryData = zipEntry->GetZipEntryData(); 58954aa6d63Sopenharmony_ci ASSERT_NE(zipEntryData, nullptr); 59054aa6d63Sopenharmony_ci 59154aa6d63Sopenharmony_ci int crc32 = zipEntryData->GetZipEntryHeader()->GetCrc32(); 59254aa6d63Sopenharmony_ci ASSERT_NE(crc32, -1); 59354aa6d63Sopenharmony_ci short lastTime = zipEntryData->GetZipEntryHeader()->GetLastTime(); 59454aa6d63Sopenharmony_ci ASSERT_NE(lastTime, -1); 59554aa6d63Sopenharmony_ci short lastData = zipEntryData->GetZipEntryHeader()->GetLastDate(); 59654aa6d63Sopenharmony_ci ASSERT_NE(lastData, -1); 59754aa6d63Sopenharmony_ci int64_t compressedSize = zipEntryData->GetZipEntryHeader()->GetCompressedSize(); 59854aa6d63Sopenharmony_ci ASSERT_NE(compressedSize, -1); 59954aa6d63Sopenharmony_ci int64_t unCompressedSize = zipEntryData->GetZipEntryHeader()->GetUnCompressedSize(); 60054aa6d63Sopenharmony_ci ASSERT_NE(unCompressedSize, -1); 60154aa6d63Sopenharmony_ci int headerLength = zipEntryData->GetZipEntryHeader()->GetHeaderLength(); 60254aa6d63Sopenharmony_ci ASSERT_NE(headerLength, -1); 60354aa6d63Sopenharmony_ci int signature = zipEntryData->GetZipEntryHeader()->GetSIGNATURE(); 60454aa6d63Sopenharmony_ci ASSERT_NE(signature, -1); 60554aa6d63Sopenharmony_ci short version = zipEntryData->GetZipEntryHeader()->GetVersion(); 60654aa6d63Sopenharmony_ci ASSERT_NE(version, -1); 60754aa6d63Sopenharmony_ci zipEntryData->GetZipEntryHeader()->SetFileName(""); 60854aa6d63Sopenharmony_ci } 60954aa6d63Sopenharmony_ci} 61054aa6d63Sopenharmony_ci 61154aa6d63Sopenharmony_ci/** 61254aa6d63Sopenharmony_ci * @tc.name: Test ZipEntryHeader Class 61354aa6d63Sopenharmony_ci * @tc.desc: Test function ToBytes of ZipEntryHeader for FAIL. 61454aa6d63Sopenharmony_ci * @tc.type: FUNC 61554aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 61654aa6d63Sopenharmony_ci */ 61754aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipEntryHeaderTest002, testing::ext::TestSize.Level1) 61854aa6d63Sopenharmony_ci{ 61954aa6d63Sopenharmony_ci /* 62054aa6d63Sopenharmony_ci * @tc.steps: step1. test ReadFileName and ReadExtra function 62154aa6d63Sopenharmony_ci * @tc.expected: step1. make the file name and extra is empty, the return will be 0. 62254aa6d63Sopenharmony_ci */ 62354aa6d63Sopenharmony_ci ZipEntryHeader zipEntryHeader; 62454aa6d63Sopenharmony_ci zipEntryHeader.ReadFileName(""); 62554aa6d63Sopenharmony_ci zipEntryHeader.ReadExtra(""); 62654aa6d63Sopenharmony_ci std::string bytes = zipEntryHeader.ToBytes(); 62754aa6d63Sopenharmony_ci ASSERT_EQ(bytes.size(), 0); 62854aa6d63Sopenharmony_ci} 62954aa6d63Sopenharmony_ci 63054aa6d63Sopenharmony_ci/** 63154aa6d63Sopenharmony_ci * @tc.name: Test DataDescriptor Class 63254aa6d63Sopenharmony_ci * @tc.desc: Test function of DataDescriptor for SUCCESS. 63354aa6d63Sopenharmony_ci * @tc.type: FUNC 63454aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 63554aa6d63Sopenharmony_ci */ 63654aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, DataDescriptorTest001, testing::ext::TestSize.Level1) 63754aa6d63Sopenharmony_ci{ 63854aa6d63Sopenharmony_ci std::ifstream inputFile(DATA_DESCRIPTOR_HAP_PATH, std::ios::binary); 63954aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 64054aa6d63Sopenharmony_ci bool zipRes = zip->Init(inputFile); 64154aa6d63Sopenharmony_ci std::vector<ZipEntry*> zipEntries = zip->GetZipEntries(); 64254aa6d63Sopenharmony_ci EXPECT_EQ(zipRes && zipEntries.size() > 0, true); 64354aa6d63Sopenharmony_ci for (const auto& zipEntry : zipEntries) { 64454aa6d63Sopenharmony_ci ZipEntryData* zipEntryData = zipEntry->GetZipEntryData(); 64554aa6d63Sopenharmony_ci DataDescriptor* dataDescriptor = zipEntryData->GetDataDescriptor(); 64654aa6d63Sopenharmony_ci if (dataDescriptor) { 64754aa6d63Sopenharmony_ci uint64_t compressedSize = dataDescriptor->GetCompressedSize(); 64854aa6d63Sopenharmony_ci uint64_t unCompressedSize = dataDescriptor->GetUnCompressedSize(); 64954aa6d63Sopenharmony_ci int crc32 = dataDescriptor->GetCrc32(); 65054aa6d63Sopenharmony_ci int signature = dataDescriptor->GetSIGNATURE(); 65154aa6d63Sopenharmony_ci int desLength = dataDescriptor->GetDesLength(); 65254aa6d63Sopenharmony_ci EXPECT_EQ(zipEntryData != nullptr && dataDescriptor != nullptr && compressedSize != -1 && 65354aa6d63Sopenharmony_ci unCompressedSize != -1 && crc32 != -1 && signature != -1 && desLength != -1, true); 65454aa6d63Sopenharmony_ci } else { 65554aa6d63Sopenharmony_ci EXPECT_EQ(dataDescriptor == nullptr, true); 65654aa6d63Sopenharmony_ci } 65754aa6d63Sopenharmony_ci } 65854aa6d63Sopenharmony_ci} 65954aa6d63Sopenharmony_ci 66054aa6d63Sopenharmony_ci/** 66154aa6d63Sopenharmony_ci * @tc.name: Test DataDescriptor Class 66254aa6d63Sopenharmony_ci * @tc.desc: Test function of GetDataDescriptor for FAIL. 66354aa6d63Sopenharmony_ci * @tc.type: FUNC 66454aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 66554aa6d63Sopenharmony_ci */ 66654aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, GetDataDescriptorTest001, testing::ext::TestSize.Level1) 66754aa6d63Sopenharmony_ci{ 66854aa6d63Sopenharmony_ci /* 66954aa6d63Sopenharmony_ci * @tc.steps: step1. test GetDataDescriptor and GetDataDescriptor function 67054aa6d63Sopenharmony_ci * @tc.expected: step1. make the data descriptor is error, the return will be nullptr. 67154aa6d63Sopenharmony_ci */ 67254aa6d63Sopenharmony_ci std::string bytes1{1}; 67354aa6d63Sopenharmony_ci ASSERT_EQ(DataDescriptor::GetDataDescriptor(bytes1), nullptr); 67454aa6d63Sopenharmony_ci 67554aa6d63Sopenharmony_ci std::string bytes2(16, 0); 67654aa6d63Sopenharmony_ci ASSERT_EQ(DataDescriptor::GetDataDescriptor(bytes2), nullptr); 67754aa6d63Sopenharmony_ci} 67854aa6d63Sopenharmony_ci 67954aa6d63Sopenharmony_ci/** 68054aa6d63Sopenharmony_ci * @tc.name: Test CentralDirectory Class 68154aa6d63Sopenharmony_ci * @tc.desc: Test function of CentralDirectory for SUCCESS. 68254aa6d63Sopenharmony_ci * @tc.type: FUNC 68354aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 68454aa6d63Sopenharmony_ci */ 68554aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, CentralDirectoryTest001, testing::ext::TestSize.Level1) 68654aa6d63Sopenharmony_ci{ 68754aa6d63Sopenharmony_ci std::ifstream inputFile(UNSIGNED_HAP_PATH, std::ios::binary); 68854aa6d63Sopenharmony_ci std::shared_ptr<ZipSigner> zip = std::make_shared<ZipSigner>(); 68954aa6d63Sopenharmony_ci bool zipRes = zip->Init(inputFile); 69054aa6d63Sopenharmony_ci std::vector<ZipEntry*> zipEntries = zip->GetZipEntries(); 69154aa6d63Sopenharmony_ci EXPECT_EQ(zipRes && zipEntries.size() > 0, true); 69254aa6d63Sopenharmony_ci CentralDirectory* cd = zipEntries[0]->GetCentralDirectory(); 69354aa6d63Sopenharmony_ci int cdLength = cd->GetCdLength(); 69454aa6d63Sopenharmony_ci int signature = cd->GetSIGNATURE(); 69554aa6d63Sopenharmony_ci short flag = cd->GetFlag(); 69654aa6d63Sopenharmony_ci short lastTime = cd->GetLastTime(); 69754aa6d63Sopenharmony_ci short lastDate = cd->GetLastDate(); 69854aa6d63Sopenharmony_ci int crc32 = cd->GetCrc32(); 69954aa6d63Sopenharmony_ci std::string fileName = cd->GetFileName(); 70054aa6d63Sopenharmony_ci short version = cd->GetVersion(); 70154aa6d63Sopenharmony_ci short versionExtra = cd->GetVersionExtra(); 70254aa6d63Sopenharmony_ci int diskNumStart = cd->GetDiskNumStart(); 70354aa6d63Sopenharmony_ci short internalFile = cd->GetInternalFile(); 70454aa6d63Sopenharmony_ci int externalFile = cd->GetExternalFile(); 70554aa6d63Sopenharmony_ci std::string comment = cd->GetComment(); 70654aa6d63Sopenharmony_ci EXPECT_EQ(cd != nullptr && cdLength != -1 && signature != -1 && flag != -1 && lastTime != -1 && lastDate != -1 && 70754aa6d63Sopenharmony_ci crc32 != -1 && fileName.size() > 0 && version != -1 && versionExtra != -1 && diskNumStart != -1 && 70854aa6d63Sopenharmony_ci internalFile != -1 && externalFile != -1 && comment.size() == 0, true); 70954aa6d63Sopenharmony_ci} 71054aa6d63Sopenharmony_ci 71154aa6d63Sopenharmony_ci/** 71254aa6d63Sopenharmony_ci * @tc.name: Test CentralDirectory Class 71354aa6d63Sopenharmony_ci * @tc.desc: Test function of CentralDirectory for FAIL. 71454aa6d63Sopenharmony_ci * @tc.type: FUNC 71554aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 71654aa6d63Sopenharmony_ci */ 71754aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, CentralDirectoryTest002, testing::ext::TestSize.Level1) 71854aa6d63Sopenharmony_ci{ 71954aa6d63Sopenharmony_ci /* 72054aa6d63Sopenharmony_ci * @tc.steps: step1. test GetCentralDirectory function 72154aa6d63Sopenharmony_ci * @tc.expected: step1. make the central directory is error, the return will be nullptr. 72254aa6d63Sopenharmony_ci */ 72354aa6d63Sopenharmony_ci ByteBuffer bf(1); 72454aa6d63Sopenharmony_ci CentralDirectory* cd = new CentralDirectory(); 72554aa6d63Sopenharmony_ci EXPECT_EQ(CentralDirectory::GetCentralDirectory(bf, cd), false); 72654aa6d63Sopenharmony_ci delete cd; 72754aa6d63Sopenharmony_ci} 72854aa6d63Sopenharmony_ci 72954aa6d63Sopenharmony_ci/** 73054aa6d63Sopenharmony_ci * @tc.name: Test CentralDirectory Class 73154aa6d63Sopenharmony_ci * @tc.desc: Test function of CentralDirectory for SUCCESS with comment. 73254aa6d63Sopenharmony_ci * @tc.type: FUNC 73354aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 73454aa6d63Sopenharmony_ci */ 73554aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, CentralDirectoryTest003, testing::ext::TestSize.Level1) 73654aa6d63Sopenharmony_ci{ 73754aa6d63Sopenharmony_ci std::string str; 73854aa6d63Sopenharmony_ci EXPECT_EQ(FileUtils::ReadFile(CD_ONLY_HAP_V1_PATH, str) == 0, true); 73954aa6d63Sopenharmony_ci ByteBuffer bf(str.c_str(), str.size()); 74054aa6d63Sopenharmony_ci CentralDirectory* cd = new CentralDirectory(); 74154aa6d63Sopenharmony_ci EXPECT_EQ(CentralDirectory::GetCentralDirectory(bf, cd), true); 74254aa6d63Sopenharmony_ci std::string cdBytes = cd->ToBytes(); 74354aa6d63Sopenharmony_ci EXPECT_EQ(cdBytes.size() > 0, true); 74454aa6d63Sopenharmony_ci} 74554aa6d63Sopenharmony_ci 74654aa6d63Sopenharmony_ci/** 74754aa6d63Sopenharmony_ci * @tc.name: Test CentralDirectory Class 74854aa6d63Sopenharmony_ci * @tc.desc: Test function of CentralDirectory for SUCCESS without fileNameLength. 74954aa6d63Sopenharmony_ci * @tc.type: FUNC 75054aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 75154aa6d63Sopenharmony_ci */ 75254aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, CentralDirectoryTest004, testing::ext::TestSize.Level1) 75354aa6d63Sopenharmony_ci{ 75454aa6d63Sopenharmony_ci std::string str; 75554aa6d63Sopenharmony_ci EXPECT_EQ(FileUtils::ReadFile(CD_ONLY_HAP_V2_PATH, str) == 0, true); 75654aa6d63Sopenharmony_ci ByteBuffer bf(str.c_str(), str.size()); 75754aa6d63Sopenharmony_ci CentralDirectory* cd = new CentralDirectory(); 75854aa6d63Sopenharmony_ci EXPECT_EQ(CentralDirectory::GetCentralDirectory(bf, cd), true); 75954aa6d63Sopenharmony_ci std::string cdBytes = cd->ToBytes(); 76054aa6d63Sopenharmony_ci EXPECT_EQ(cdBytes.size() > 0, true); 76154aa6d63Sopenharmony_ci} 76254aa6d63Sopenharmony_ci 76354aa6d63Sopenharmony_ci/** 76454aa6d63Sopenharmony_ci * @tc.name: Test ZipEntryHeader Class 76554aa6d63Sopenharmony_ci * @tc.desc: Test function of GetZipEntryHeader for SUCCESS. 76654aa6d63Sopenharmony_ci * @tc.type: FUNC 76754aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 76854aa6d63Sopenharmony_ci */ 76954aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, GetZipEntryHeaderTest001, testing::ext::TestSize.Level1) 77054aa6d63Sopenharmony_ci{ 77154aa6d63Sopenharmony_ci std::string headBytes{1}; 77254aa6d63Sopenharmony_ci ZipEntryHeader* entryHeader = ZipEntryHeader::GetZipEntryHeader(headBytes); 77354aa6d63Sopenharmony_ci EXPECT_EQ(entryHeader == nullptr, true); 77454aa6d63Sopenharmony_ci delete entryHeader; 77554aa6d63Sopenharmony_ci} 77654aa6d63Sopenharmony_ci 77754aa6d63Sopenharmony_ci/** 77854aa6d63Sopenharmony_ci * @tc.name: Test ZipEntryData Class 77954aa6d63Sopenharmony_ci * @tc.desc: Test function of GetZipEntry for FAIL. 78054aa6d63Sopenharmony_ci * @tc.type: FUNC 78154aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 78254aa6d63Sopenharmony_ci */ 78354aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, GetZipEntryTest001, testing::ext::TestSize.Level1) 78454aa6d63Sopenharmony_ci{ 78554aa6d63Sopenharmony_ci std::ofstream zeroOutput(ZERO_HAP_PATH, std::ios::binary | std::ios::trunc | std::ios::out); 78654aa6d63Sopenharmony_ci std::string zeros(1024, 0x00); 78754aa6d63Sopenharmony_ci zeroOutput.write(zeros.c_str(), zeros.size()); 78854aa6d63Sopenharmony_ci zeroOutput.close(); 78954aa6d63Sopenharmony_ci 79054aa6d63Sopenharmony_ci std::ifstream inputFile(ZERO_HAP_PATH, std::ios::binary); 79154aa6d63Sopenharmony_ci ZipEntryData* zipEntryData = ZipEntryData::GetZipEntry(inputFile, 0, 1024); 79254aa6d63Sopenharmony_ci EXPECT_EQ(zipEntryData == nullptr, true); 79354aa6d63Sopenharmony_ci} 79454aa6d63Sopenharmony_ci 79554aa6d63Sopenharmony_ci/** 79654aa6d63Sopenharmony_ci * @tc.name: Test ZipEntryData Class 79754aa6d63Sopenharmony_ci * @tc.desc: Test function of GetZipEntry for success. 79854aa6d63Sopenharmony_ci * @tc.type: FUNC 79954aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 80054aa6d63Sopenharmony_ci */ 80154aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, GetZipEntryTest002, testing::ext::TestSize.Level1) 80254aa6d63Sopenharmony_ci{ 80354aa6d63Sopenharmony_ci /* 80454aa6d63Sopenharmony_ci * @tc.steps: step1. test ZipEntryData::ReadEntryFileNameAndExtraByOffset function 80554aa6d63Sopenharmony_ci * @tc.expected: step1. FileNameLength and ExtraLength is zero 80654aa6d63Sopenharmony_ci */ 80754aa6d63Sopenharmony_ci std::ifstream inputFile(ZIP_ENTRY_DATA_WRONG_HAP_PATH, std::ios::binary); 80854aa6d63Sopenharmony_ci ZipEntryData* zipEntryData = ZipEntryData::GetZipEntry(inputFile, 0, 1024); 80954aa6d63Sopenharmony_ci ASSERT_EQ(zipEntryData != nullptr, true); 81054aa6d63Sopenharmony_ci} 81154aa6d63Sopenharmony_ci 81254aa6d63Sopenharmony_ci/** 81354aa6d63Sopenharmony_ci * @tc.name: Test ZipSigner Class 81454aa6d63Sopenharmony_ci * @tc.desc: Test ZipSigner interfaces for SUCCESS. 81554aa6d63Sopenharmony_ci * @tc.type: FUNC 81654aa6d63Sopenharmony_ci * @tc.require: SR000H63TL 81754aa6d63Sopenharmony_ci */ 81854aa6d63Sopenharmony_ciHWTEST_F(ZipSignerTest, ZipSignerTest001, testing::ext::TestSize.Level1) 81954aa6d63Sopenharmony_ci{ 82054aa6d63Sopenharmony_ci ZipSigner zip; 82154aa6d63Sopenharmony_ci std::ifstream ifs(ZIP_ENTRIES_WRONG_HAP_V2_PATH, std::ios::binary); 82254aa6d63Sopenharmony_ci ASSERT_TRUE(zip.Init(ifs)); 82354aa6d63Sopenharmony_ci std::vector<ZipEntry*> zipEntries{nullptr}; 82454aa6d63Sopenharmony_ci zip.SetZipEntries(zipEntries); 82554aa6d63Sopenharmony_ci zip.SetSigningOffset(1); 82654aa6d63Sopenharmony_ci int64_t offset = zip.GetSigningOffset(); 82754aa6d63Sopenharmony_ci EXPECT_EQ(offset, 1); 82854aa6d63Sopenharmony_ci std::string signingBlock{0x1, 0x1, 0x1, 0x1, 0x1}; 82954aa6d63Sopenharmony_ci zip.SetSigningBlock(signingBlock); 83054aa6d63Sopenharmony_ci signingBlock = zip.GetSigningBlock(); 83154aa6d63Sopenharmony_ci EXPECT_NE(signingBlock.size(), 0); 83254aa6d63Sopenharmony_ci zip.SetCDOffset(1); 83354aa6d63Sopenharmony_ci zip.SetEOCDOffset(1); 83454aa6d63Sopenharmony_ci zip.SetEndOfCentralDirectory(nullptr); 83554aa6d63Sopenharmony_ci} 83654aa6d63Sopenharmony_ci} // namespace SignatureTools 83754aa6d63Sopenharmony_ci} // namespace OHOS