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 <gtest/gtest.h> 18 #include <iostream> 19 #include <memory> 20 #include "log.h" 21 #include "pkg_algo_deflate.h" 22 #include "pkg_algo_lz4.h" 23 #include "pkg_algorithm.h" 24 #include "pkg_algo_sign.h" 25 #include "pkg_manager.h" 26 #include "pkg_test.h" 27 28 using namespace std; 29 using namespace Hpackage; 30 using namespace Updater; 31 using namespace testing::ext; 32 33 namespace UpdaterUt { 34 constexpr size_t BUFFER_LEN = 10; 35 class PkgAlgoUnitTest : public PkgTest { 36 public: PkgAlgoUnitTest()37 PkgAlgoUnitTest() {} ~PkgAlgoUnitTest()38 ~PkgAlgoUnitTest() override {} 39 TestCrcDigest() const40 int TestCrcDigest() const 41 { 42 std::unique_ptr<Crc32Algorithm> algo = std::make_unique<Crc32Algorithm>(); 43 EXPECT_NE(algo, nullptr); 44 int ret = algo->Init(); 45 EXPECT_EQ(0, ret); 46 uint8_t buff[BUFFER_LEN] = {1}; 47 PkgBuffer crcBuffer(buff, sizeof(buff)); 48 ret = algo->Update(crcBuffer, sizeof(buff)); 49 EXPECT_EQ(0, ret); 50 uint32_t crc = 0; 51 PkgBuffer crcResult(reinterpret_cast<uint8_t *>(&crc), sizeof(crc)); 52 ret = algo->Final(crcResult); 53 EXPECT_EQ(0, ret); 54 55 uint32_t crc2 = 0; 56 crcResult = {reinterpret_cast<uint8_t *>(&crc2), sizeof(crc)}; 57 ret = algo->Calculate(crcResult, crcBuffer, sizeof(buff)); 58 EXPECT_EQ(0, ret); 59 EXPECT_EQ(crc, crc2); 60 return ret; 61 } 62 TestHash256Digest() const63 int TestHash256Digest() const 64 { 65 std::unique_ptr<Sha256Algorithm> algo = std::make_unique<Sha256Algorithm>(); 66 EXPECT_NE(algo, nullptr); 67 int ret = algo->Init(); 68 EXPECT_EQ(0, ret); 69 uint8_t buff[BUFFER_LEN] = {1}; 70 PkgBuffer buffer(buff, sizeof(buff)); 71 ret = algo->Update(buffer, sizeof(buff)); 72 EXPECT_EQ(0, ret); 73 size_t bufferSize = 32; 74 PkgBuffer dig(bufferSize); 75 ret = algo->Final(dig); 76 EXPECT_EQ(0, ret); 77 ret = algo->Calculate(dig, buffer, sizeof(buff)); 78 EXPECT_EQ(0, ret); 79 return ret; 80 } 81 TestHash384Digest() const82 int TestHash384Digest() const 83 { 84 std::unique_ptr<Sha384Algorithm> algo = std::make_unique<Sha384Algorithm>(); 85 EXPECT_NE(algo, nullptr); 86 int ret = algo->Init(); 87 EXPECT_EQ(0, ret); 88 uint8_t buff[BUFFER_LEN] = {1}; 89 PkgBuffer buffer384(buff, sizeof(buff)); 90 ret = algo->Update(buffer384, sizeof(buff)); 91 EXPECT_EQ(0, ret); 92 size_t bufferSize = 64; 93 PkgBuffer dig(bufferSize); 94 ret = algo->Final(dig); 95 EXPECT_EQ(0, ret); 96 ret = algo->Calculate(dig, buffer384, sizeof(buff)); 97 EXPECT_EQ(0, ret); 98 return ret; 99 } 100 TestInvalidParam() const101 int TestInvalidParam() const 102 { 103 constexpr int8_t invalidType = 100; 104 constexpr size_t digestLen = 32; 105 constexpr int16_t magicNumber = 256; 106 int ret = DigestAlgorithm::GetDigestLen(invalidType); 107 EXPECT_EQ(0, ret); 108 ret = DigestAlgorithm::GetSignatureLen(invalidType); 109 EXPECT_EQ(magicNumber, ret); 110 111 DigestAlgorithm::DigestAlgorithmPtr algorithm = PkgAlgorithmFactory::GetDigestAlgorithm(invalidType); 112 EXPECT_NE(nullptr, algorithm); 113 algorithm->Init(); 114 uint8_t dig2[digestLen]; 115 PkgBuffer buffer(dig2, sizeof(dig2)); 116 algorithm->Update(buffer, sizeof(dig2)); 117 algorithm->Final(buffer); 118 119 SignAlgorithm::SignAlgorithmPtr sign = PkgAlgorithmFactory::GetSignAlgorithm(TEST_PATH_FROM, invalidType, 0); 120 EXPECT_EQ(nullptr, sign); 121 122 PkgAlgorithm::PkgAlgorithmPtr algo = PkgAlgorithmFactory::GetAlgorithm(nullptr); 123 EXPECT_EQ(nullptr, algo); 124 FileInfo config; 125 config.packMethod = invalidType; 126 algo = PkgAlgorithmFactory::GetAlgorithm(nullptr); 127 EXPECT_EQ(nullptr, algo); 128 EXPECT_EQ(nullptr, sign); 129 130 return 0; 131 } 132 133 private: 134 std::string testPackageName = "test_ecc_package.zip"; 135 std::vector<std::string> testFileNames_ = { 136 "loadScript.us", 137 "registerCmd.us", 138 "test_function.us", 139 "test_if.us", 140 "test_logic.us", 141 "test_math.us", 142 "test_native.us", 143 "testscript.us", 144 "Verse-script.us", 145 "libcrypto.a" 146 }; 147 }; 148 149 HWTEST_F(PkgAlgoUnitTest, TestHash256Digest, TestSize.Level1) 150 { 151 PkgAlgoUnitTest test; 152 EXPECT_EQ(0, test.TestCrcDigest()); 153 EXPECT_EQ(0, test.TestHash256Digest()); 154 EXPECT_EQ(0, test.TestHash384Digest()); 155 } 156 157 HWTEST_F(PkgAlgoUnitTest, TestInvalid, TestSize.Level1) 158 { 159 PkgAlgoUnitTest test; 160 EXPECT_EQ(0, test.TestInvalidParam()); 161 } 162 163 HWTEST_F(PkgAlgoUnitTest, TestPkgAlgoDeflate, TestSize.Level1) 164 { 165 ZipFileInfo info {}; 166 PkgAlgoDeflate a1(info); 167 Lz4FileInfo config {}; 168 PkgAlgorithmLz4 a2(config); 169 PkgAlgorithmBlockLz4 a3(config); 170 VerifyAlgorithm a4("aa", 0); 171 SignAlgorithmRsa a5("bb", 0); 172 SignAlgorithmEcc a6("cc", 0); 173 // just for executing these destructor 174 PkgAlgoDeflate *a7 = new PkgAlgoDeflate(info); 175 delete a7; 176 PkgAlgorithmLz4 *a8 = new PkgAlgorithmLz4(config); 177 delete a8; 178 PkgAlgorithmBlockLz4 *a9 = new PkgAlgorithmBlockLz4(config); 179 delete a9; 180 VerifyAlgorithm *a10 = new VerifyAlgorithm("aa", 0); 181 delete a10; 182 SignAlgorithmRsa *a11 = new SignAlgorithmRsa("bb", 0); 183 delete a11; 184 SignAlgorithmEcc *a12 = new SignAlgorithmEcc("cc", 0); 185 std::vector<uint8_t> b1; 186 std::vector<uint8_t> b2; 187 int32_t ret = a12->VerifyDigest(b1, b2); 188 delete a12; 189 EXPECT_EQ(ret, PKG_INVALID_SIGNATURE); 190 } 191 } 192