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 <gtest/gtest.h>
17fb299fa2Sopenharmony_ci#include "image_patch.h"
18fb299fa2Sopenharmony_ci#include "diffpatch.h"
19fb299fa2Sopenharmony_ci#include "unittest_comm.h"
20fb299fa2Sopenharmony_ci
21fb299fa2Sopenharmony_ciusing namespace std;
22fb299fa2Sopenharmony_ciusing namespace Hpackage;
23fb299fa2Sopenharmony_ciusing namespace UpdatePatch;
24fb299fa2Sopenharmony_ciusing namespace testing::ext;
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_cinamespace {
27fb299fa2Sopenharmony_ciclass TestCompressedImagePatch : public CompressedImagePatch {
28fb299fa2Sopenharmony_cipublic:
29fb299fa2Sopenharmony_ci    TestCompressedImagePatch(UpdatePatchWriterPtr writer, const std::vector<uint8_t> &bonusData)
30fb299fa2Sopenharmony_ci        : CompressedImagePatch(writer, bonusData) {}
31fb299fa2Sopenharmony_ci
32fb299fa2Sopenharmony_ci    ~TestCompressedImagePatch() override {}
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_ci    int32_t ApplyImagePatch(const PatchParam &param, size_t &startOffset) override
35fb299fa2Sopenharmony_ci    {
36fb299fa2Sopenharmony_ci        return 0;
37fb299fa2Sopenharmony_ci    }
38fb299fa2Sopenharmony_ci
39fb299fa2Sopenharmony_ci    int32_t TestStartReadHeader(const PatchParam &param, PatchHeader &header, size_t &offset)
40fb299fa2Sopenharmony_ci    {
41fb299fa2Sopenharmony_ci        return StartReadHeader(param, header, offset);
42fb299fa2Sopenharmony_ci    }
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_ci    int32_t TestDecompressData(PkgManager::PkgManagerPtr &pkgManager, PkgBuffer buffer,
45fb299fa2Sopenharmony_ci    PkgManager::StreamPtr &stream, bool memory, size_t expandedLen)
46fb299fa2Sopenharmony_ci    {
47fb299fa2Sopenharmony_ci        return DecompressData(pkgManager, buffer, stream, memory, expandedLen);
48fb299fa2Sopenharmony_ci    }
49fb299fa2Sopenharmony_ciprotected:
50fb299fa2Sopenharmony_ci    int32_t ReadHeader(const PatchParam &param, PatchHeader &header, size_t &offset) override
51fb299fa2Sopenharmony_ci    {
52fb299fa2Sopenharmony_ci        return 0;
53fb299fa2Sopenharmony_ci    }
54fb299fa2Sopenharmony_ci
55fb299fa2Sopenharmony_ci    std::unique_ptr<FileInfo> GetFileInfo() const override
56fb299fa2Sopenharmony_ci    {
57fb299fa2Sopenharmony_ci        ZipFileInfo *fileInfo = new(std::nothrow) ZipFileInfo;
58fb299fa2Sopenharmony_ci        return std::unique_ptr<FileInfo>((FileInfo *)fileInfo);
59fb299fa2Sopenharmony_ci    }
60fb299fa2Sopenharmony_ci};
61fb299fa2Sopenharmony_ci
62fb299fa2Sopenharmony_ciclass CompressedImagePatchUnitTest : public testing::Test {
63fb299fa2Sopenharmony_cipublic:
64fb299fa2Sopenharmony_ci    CompressedImagePatchUnitTest() {}
65fb299fa2Sopenharmony_ci    ~CompressedImagePatchUnitTest() {}
66fb299fa2Sopenharmony_ci
67fb299fa2Sopenharmony_ci    static void SetUpTestCase(void) {}
68fb299fa2Sopenharmony_ci    static void TearDownTestCase(void) {}
69fb299fa2Sopenharmony_ci    void SetUp() {}
70fb299fa2Sopenharmony_ci    void TearDown() {}
71fb299fa2Sopenharmony_ci    void TestBody() {}
72fb299fa2Sopenharmony_ci};
73fb299fa2Sopenharmony_ci
74fb299fa2Sopenharmony_ciHWTEST_F(CompressedImagePatchUnitTest, TestStartReadHeader, TestSize.Level0)
75fb299fa2Sopenharmony_ci{
76fb299fa2Sopenharmony_ci    UpdatePatchWriterPtr writer = nullptr;
77fb299fa2Sopenharmony_ci    const std::vector<uint8_t> bonusData;
78fb299fa2Sopenharmony_ci    TestCompressedImagePatch test(writer, bonusData);
79fb299fa2Sopenharmony_ci    PatchParam patchParam = {
80fb299fa2Sopenharmony_ci        reinterpret_cast<u_char*>(const_cast<char*>("xxx")), sizeof("xxx"),
81fb299fa2Sopenharmony_ci        reinterpret_cast<u_char*>(const_cast<char*>("xxx")), sizeof("xxx")
82fb299fa2Sopenharmony_ci    };
83fb299fa2Sopenharmony_ci    PatchHeader header = {0, 0, 0, 0, 0};
84fb299fa2Sopenharmony_ci    size_t offset = 0;
85fb299fa2Sopenharmony_ci    int32_t ret = test.TestStartReadHeader(patchParam, header, offset);
86fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
87fb299fa2Sopenharmony_ci}
88fb299fa2Sopenharmony_ci
89fb299fa2Sopenharmony_ciHWTEST_F(CompressedImagePatchUnitTest, TestDecompressData, TestSize.Level0)
90fb299fa2Sopenharmony_ci{
91fb299fa2Sopenharmony_ci    UpdatePatchWriterPtr writer = nullptr;
92fb299fa2Sopenharmony_ci    const std::vector<uint8_t> bonusData;
93fb299fa2Sopenharmony_ci    TestCompressedImagePatch test(writer, bonusData);
94fb299fa2Sopenharmony_ci    PkgManager::PkgManagerPtr pkgManager = nullptr;
95fb299fa2Sopenharmony_ci    PkgBuffer buffer;
96fb299fa2Sopenharmony_ci    PkgManager::StreamPtr stream;
97fb299fa2Sopenharmony_ci    bool memory = false;
98fb299fa2Sopenharmony_ci    size_t expandedLen = 0;
99fb299fa2Sopenharmony_ci    int32_t ret = test.TestDecompressData(pkgManager, buffer, stream, memory, expandedLen);
100fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
101fb299fa2Sopenharmony_ci    expandedLen = 1;
102fb299fa2Sopenharmony_ci    ret = test.TestDecompressData(pkgManager, buffer, stream, memory, expandedLen);
103fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, -1);
104fb299fa2Sopenharmony_ci    pkgManager = PkgManager::CreatePackageInstance();
105fb299fa2Sopenharmony_ci    ret = test.TestDecompressData(pkgManager, buffer, stream, memory, expandedLen);
106fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, -1);
107fb299fa2Sopenharmony_ci    PkgManager::ReleasePackageInstance(pkgManager);
108fb299fa2Sopenharmony_ci}
109fb299fa2Sopenharmony_ci
110fb299fa2Sopenharmony_ciHWTEST_F(CompressedImagePatchUnitTest, TestApplyImagePatch, TestSize.Level0)
111fb299fa2Sopenharmony_ci{
112fb299fa2Sopenharmony_ci    const std::vector<uint8_t> bonusData;
113fb299fa2Sopenharmony_ci    PatchParam patchParam = {
114fb299fa2Sopenharmony_ci        reinterpret_cast<u_char*>(const_cast<char*>("xxx")), sizeof("xxx"),
115fb299fa2Sopenharmony_ci        reinterpret_cast<u_char*>(const_cast<char*>("xxx")), sizeof("xxx")
116fb299fa2Sopenharmony_ci    };
117fb299fa2Sopenharmony_ci    UpdatePatchWriterPtr writer { nullptr };
118fb299fa2Sopenharmony_ci    std::unique_ptr<ImagePatch> imagePatch = std::make_unique<ZipImagePatch>(writer, bonusData);
119fb299fa2Sopenharmony_ci    size_t offset = 0;
120fb299fa2Sopenharmony_ci    int32_t ret = imagePatch->ApplyImagePatch(patchParam, offset);
121fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, -1);
122fb299fa2Sopenharmony_ci}
123fb299fa2Sopenharmony_ci}
124