1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include <cstring>
17fb299fa2Sopenharmony_ci#include <gtest/gtest.h>
18fb299fa2Sopenharmony_ci#include <iostream>
19fb299fa2Sopenharmony_ci#include <memory>
20fb299fa2Sopenharmony_ci#include "log.h"
21fb299fa2Sopenharmony_ci#include "pkg_algo_deflate.h"
22fb299fa2Sopenharmony_ci#include "pkg_algo_lz4.h"
23fb299fa2Sopenharmony_ci#include "pkg_algorithm.h"
24fb299fa2Sopenharmony_ci#include "pkg_algo_sign.h"
25fb299fa2Sopenharmony_ci#include "pkg_manager.h"
26fb299fa2Sopenharmony_ci#include "pkg_test.h"
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_ciusing namespace std;
29fb299fa2Sopenharmony_ciusing namespace Hpackage;
30fb299fa2Sopenharmony_ciusing namespace Updater;
31fb299fa2Sopenharmony_ciusing namespace testing::ext;
32fb299fa2Sopenharmony_ci
33fb299fa2Sopenharmony_cinamespace UpdaterUt {
34fb299fa2Sopenharmony_ciconstexpr size_t BUFFER_LEN = 10;
35fb299fa2Sopenharmony_ciclass PkgAlgoUnitTest : public PkgTest {
36fb299fa2Sopenharmony_cipublic:
37fb299fa2Sopenharmony_ci    PkgAlgoUnitTest() {}
38fb299fa2Sopenharmony_ci    ~PkgAlgoUnitTest() override {}
39fb299fa2Sopenharmony_ci
40fb299fa2Sopenharmony_ci    int TestCrcDigest() const
41fb299fa2Sopenharmony_ci    {
42fb299fa2Sopenharmony_ci        std::unique_ptr<Crc32Algorithm> algo = std::make_unique<Crc32Algorithm>();
43fb299fa2Sopenharmony_ci        EXPECT_NE(algo, nullptr);
44fb299fa2Sopenharmony_ci        int ret = algo->Init();
45fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
46fb299fa2Sopenharmony_ci        uint8_t buff[BUFFER_LEN] = {1};
47fb299fa2Sopenharmony_ci        PkgBuffer crcBuffer(buff, sizeof(buff));
48fb299fa2Sopenharmony_ci        ret = algo->Update(crcBuffer, sizeof(buff));
49fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
50fb299fa2Sopenharmony_ci        uint32_t crc = 0;
51fb299fa2Sopenharmony_ci        PkgBuffer crcResult(reinterpret_cast<uint8_t *>(&crc), sizeof(crc));
52fb299fa2Sopenharmony_ci        ret = algo->Final(crcResult);
53fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
54fb299fa2Sopenharmony_ci
55fb299fa2Sopenharmony_ci        uint32_t crc2 = 0;
56fb299fa2Sopenharmony_ci        crcResult = {reinterpret_cast<uint8_t *>(&crc2), sizeof(crc)};
57fb299fa2Sopenharmony_ci        ret = algo->Calculate(crcResult, crcBuffer, sizeof(buff));
58fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
59fb299fa2Sopenharmony_ci        EXPECT_EQ(crc, crc2);
60fb299fa2Sopenharmony_ci        return ret;
61fb299fa2Sopenharmony_ci    }
62fb299fa2Sopenharmony_ci
63fb299fa2Sopenharmony_ci    int TestHash256Digest() const
64fb299fa2Sopenharmony_ci    {
65fb299fa2Sopenharmony_ci        std::unique_ptr<Sha256Algorithm> algo = std::make_unique<Sha256Algorithm>();
66fb299fa2Sopenharmony_ci        EXPECT_NE(algo, nullptr);
67fb299fa2Sopenharmony_ci        int ret = algo->Init();
68fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
69fb299fa2Sopenharmony_ci        uint8_t buff[BUFFER_LEN] = {1};
70fb299fa2Sopenharmony_ci        PkgBuffer buffer(buff, sizeof(buff));
71fb299fa2Sopenharmony_ci        ret = algo->Update(buffer, sizeof(buff));
72fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
73fb299fa2Sopenharmony_ci        size_t bufferSize = 32;
74fb299fa2Sopenharmony_ci        PkgBuffer dig(bufferSize);
75fb299fa2Sopenharmony_ci        ret = algo->Final(dig);
76fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
77fb299fa2Sopenharmony_ci        ret = algo->Calculate(dig, buffer, sizeof(buff));
78fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
79fb299fa2Sopenharmony_ci        return ret;
80fb299fa2Sopenharmony_ci    }
81fb299fa2Sopenharmony_ci
82fb299fa2Sopenharmony_ci    int TestHash384Digest() const
83fb299fa2Sopenharmony_ci    {
84fb299fa2Sopenharmony_ci        std::unique_ptr<Sha384Algorithm> algo = std::make_unique<Sha384Algorithm>();
85fb299fa2Sopenharmony_ci        EXPECT_NE(algo, nullptr);
86fb299fa2Sopenharmony_ci        int ret = algo->Init();
87fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
88fb299fa2Sopenharmony_ci        uint8_t buff[BUFFER_LEN] = {1};
89fb299fa2Sopenharmony_ci        PkgBuffer buffer384(buff, sizeof(buff));
90fb299fa2Sopenharmony_ci        ret = algo->Update(buffer384, sizeof(buff));
91fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
92fb299fa2Sopenharmony_ci        size_t bufferSize = 64;
93fb299fa2Sopenharmony_ci        PkgBuffer dig(bufferSize);
94fb299fa2Sopenharmony_ci        ret = algo->Final(dig);
95fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
96fb299fa2Sopenharmony_ci        ret = algo->Calculate(dig, buffer384, sizeof(buff));
97fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
98fb299fa2Sopenharmony_ci        return ret;
99fb299fa2Sopenharmony_ci    }
100fb299fa2Sopenharmony_ci
101fb299fa2Sopenharmony_ci    int TestInvalidParam() const
102fb299fa2Sopenharmony_ci    {
103fb299fa2Sopenharmony_ci        constexpr int8_t invalidType = 100;
104fb299fa2Sopenharmony_ci        constexpr size_t digestLen = 32;
105fb299fa2Sopenharmony_ci        constexpr int16_t magicNumber = 256;
106fb299fa2Sopenharmony_ci        int ret = DigestAlgorithm::GetDigestLen(invalidType);
107fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret);
108fb299fa2Sopenharmony_ci        ret = DigestAlgorithm::GetSignatureLen(invalidType);
109fb299fa2Sopenharmony_ci        EXPECT_EQ(magicNumber, ret);
110fb299fa2Sopenharmony_ci
111fb299fa2Sopenharmony_ci        DigestAlgorithm::DigestAlgorithmPtr algorithm = PkgAlgorithmFactory::GetDigestAlgorithm(invalidType);
112fb299fa2Sopenharmony_ci        EXPECT_NE(nullptr, algorithm);
113fb299fa2Sopenharmony_ci        algorithm->Init();
114fb299fa2Sopenharmony_ci        uint8_t dig2[digestLen];
115fb299fa2Sopenharmony_ci        PkgBuffer buffer(dig2, sizeof(dig2));
116fb299fa2Sopenharmony_ci        algorithm->Update(buffer, sizeof(dig2));
117fb299fa2Sopenharmony_ci        algorithm->Final(buffer);
118fb299fa2Sopenharmony_ci
119fb299fa2Sopenharmony_ci        SignAlgorithm::SignAlgorithmPtr sign = PkgAlgorithmFactory::GetSignAlgorithm(TEST_PATH_FROM, invalidType, 0);
120fb299fa2Sopenharmony_ci        EXPECT_EQ(nullptr, sign);
121fb299fa2Sopenharmony_ci
122fb299fa2Sopenharmony_ci        PkgAlgorithm::PkgAlgorithmPtr algo = PkgAlgorithmFactory::GetAlgorithm(nullptr);
123fb299fa2Sopenharmony_ci        EXPECT_EQ(nullptr, algo);
124fb299fa2Sopenharmony_ci        FileInfo config;
125fb299fa2Sopenharmony_ci        config.packMethod = invalidType;
126fb299fa2Sopenharmony_ci        algo = PkgAlgorithmFactory::GetAlgorithm(nullptr);
127fb299fa2Sopenharmony_ci        EXPECT_EQ(nullptr, algo);
128fb299fa2Sopenharmony_ci        EXPECT_EQ(nullptr, sign);
129fb299fa2Sopenharmony_ci
130fb299fa2Sopenharmony_ci        return 0;
131fb299fa2Sopenharmony_ci    }
132fb299fa2Sopenharmony_ci
133fb299fa2Sopenharmony_ciprivate:
134fb299fa2Sopenharmony_ci    std::string testPackageName = "test_ecc_package.zip";
135fb299fa2Sopenharmony_ci    std::vector<std::string> testFileNames_ = {
136fb299fa2Sopenharmony_ci        "loadScript.us",
137fb299fa2Sopenharmony_ci        "registerCmd.us",
138fb299fa2Sopenharmony_ci        "test_function.us",
139fb299fa2Sopenharmony_ci        "test_if.us",
140fb299fa2Sopenharmony_ci        "test_logic.us",
141fb299fa2Sopenharmony_ci        "test_math.us",
142fb299fa2Sopenharmony_ci        "test_native.us",
143fb299fa2Sopenharmony_ci        "testscript.us",
144fb299fa2Sopenharmony_ci        "Verse-script.us",
145fb299fa2Sopenharmony_ci        "libcrypto.a"
146fb299fa2Sopenharmony_ci    };
147fb299fa2Sopenharmony_ci};
148fb299fa2Sopenharmony_ci
149fb299fa2Sopenharmony_ciHWTEST_F(PkgAlgoUnitTest, TestHash256Digest, TestSize.Level1)
150fb299fa2Sopenharmony_ci{
151fb299fa2Sopenharmony_ci    PkgAlgoUnitTest test;
152fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestCrcDigest());
153fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestHash256Digest());
154fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestHash384Digest());
155fb299fa2Sopenharmony_ci}
156fb299fa2Sopenharmony_ci
157fb299fa2Sopenharmony_ciHWTEST_F(PkgAlgoUnitTest, TestInvalid, TestSize.Level1)
158fb299fa2Sopenharmony_ci{
159fb299fa2Sopenharmony_ci    PkgAlgoUnitTest test;
160fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestInvalidParam());
161fb299fa2Sopenharmony_ci}
162fb299fa2Sopenharmony_ci
163fb299fa2Sopenharmony_ciHWTEST_F(PkgAlgoUnitTest, TestPkgAlgoDeflate, TestSize.Level1)
164fb299fa2Sopenharmony_ci{
165fb299fa2Sopenharmony_ci    ZipFileInfo info {};
166fb299fa2Sopenharmony_ci    PkgAlgoDeflate a1(info);
167fb299fa2Sopenharmony_ci    Lz4FileInfo config {};
168fb299fa2Sopenharmony_ci    PkgAlgorithmLz4 a2(config);
169fb299fa2Sopenharmony_ci    PkgAlgorithmBlockLz4 a3(config);
170fb299fa2Sopenharmony_ci    VerifyAlgorithm a4("aa", 0);
171fb299fa2Sopenharmony_ci    SignAlgorithmRsa a5("bb", 0);
172fb299fa2Sopenharmony_ci    SignAlgorithmEcc a6("cc", 0);
173fb299fa2Sopenharmony_ci    // just for executing these destructor
174fb299fa2Sopenharmony_ci    PkgAlgoDeflate *a7 = new PkgAlgoDeflate(info);
175fb299fa2Sopenharmony_ci    delete a7;
176fb299fa2Sopenharmony_ci    PkgAlgorithmLz4 *a8 = new PkgAlgorithmLz4(config);
177fb299fa2Sopenharmony_ci    delete a8;
178fb299fa2Sopenharmony_ci    PkgAlgorithmBlockLz4 *a9 = new PkgAlgorithmBlockLz4(config);
179fb299fa2Sopenharmony_ci    delete a9;
180fb299fa2Sopenharmony_ci    VerifyAlgorithm *a10 = new VerifyAlgorithm("aa", 0);
181fb299fa2Sopenharmony_ci    delete a10;
182fb299fa2Sopenharmony_ci    SignAlgorithmRsa *a11 = new SignAlgorithmRsa("bb", 0);
183fb299fa2Sopenharmony_ci    delete a11;
184fb299fa2Sopenharmony_ci    SignAlgorithmEcc *a12 = new SignAlgorithmEcc("cc", 0);
185fb299fa2Sopenharmony_ci    std::vector<uint8_t> b1;
186fb299fa2Sopenharmony_ci    std::vector<uint8_t> b2;
187fb299fa2Sopenharmony_ci    int32_t ret = a12->VerifyDigest(b1, b2);
188fb299fa2Sopenharmony_ci    delete a12;
189fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, PKG_INVALID_SIGNATURE);
190fb299fa2Sopenharmony_ci}
191fb299fa2Sopenharmony_ci}
192