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 <gtest/gtest.h> 19 #include <iostream> 20 #include <sys/mman.h> 21 #include <sys/stat.h> 22 #include <unistd.h> 23 #include "log.h" 24 #include "pkg_algorithm.h" 25 #include "pkg_manager.h" 26 #include "pkg_manager_impl.h" 27 #include "pkg_test.h" 28 #include "pkg_utils.h" 29 30 using namespace std; 31 using namespace Hpackage; 32 using namespace Updater; 33 using namespace testing::ext; 34 35 namespace UpdaterUt { 36 class PackageUnitTest : public PkgTest { 37 public: PackageUnitTest()38 PackageUnitTest() {} ~PackageUnitTest()39 ~PackageUnitTest() override {} 40 public: TestInvalidCreatePackage()41 int TestInvalidCreatePackage() 42 { 43 std::vector<ComponentInfoExt> compInfo; 44 uint8_t pkgType = PkgPackType::PKG_PACK_TYPE_UPGRADE; 45 int ret = CreatePackage(nullptr, compInfo, nullptr, GetTestPrivateKeyName(0).c_str()); 46 EXPECT_EQ(ret, PKG_INVALID_PARAM); 47 48 UpgradePkgInfoExt pkgInfoExt {}; 49 pkgInfoExt.pkgType = pkgType; 50 ret = CreatePackage(&pkgInfoExt, compInfo, nullptr, GetTestPrivateKeyName(0).c_str()); 51 EXPECT_EQ(ret, PKG_INVALID_PARAM); 52 53 constexpr uint32_t digestLen = 32; 54 ret = VerifyPackage(nullptr, GetTestCertName(0).c_str(), nullptr, nullptr, digestLen); 55 EXPECT_EQ(ret, PKG_INVALID_PARAM); 56 57 std::string packagePath = TEST_PATH_TO + testPackageName; 58 pkgInfoExt.pkgType = pkgType; 59 ret = CreatePackage(&pkgInfoExt, compInfo, packagePath.c_str(), GetTestPrivateKeyName(0).c_str()); 60 EXPECT_EQ(ret, PKG_INVALID_PARAM); 61 62 pkgType = PkgPackType::PKG_PACK_TYPE_ZIP; 63 pkgInfoExt.pkgType = pkgType; 64 ret = CreatePackage(&pkgInfoExt, compInfo, packagePath.c_str(), GetTestPrivateKeyName(0).c_str()); 65 EXPECT_EQ(ret, PKG_INVALID_PARAM); 66 67 pkgType = PkgPackType::PKG_PACK_TYPE_LZ4; 68 pkgInfoExt.pkgType = pkgType; 69 ret = CreatePackage(&pkgInfoExt, compInfo, packagePath.c_str(), GetTestPrivateKeyName(0).c_str()); 70 EXPECT_EQ(ret, PKG_INVALID_PARAM); 71 72 pkgType = PkgPackType::PKG_PACK_TYPE_GZIP; 73 pkgInfoExt.pkgType = pkgType; 74 ret = CreatePackage(&pkgInfoExt, compInfo, packagePath.c_str(), GetTestPrivateKeyName(0).c_str()); 75 EXPECT_EQ(ret, PKG_INVALID_PARAM); 76 77 pkgType = PkgPackType::PKG_PACK_TYPE_NONE; 78 pkgInfoExt.pkgType = pkgType; 79 ret = CreatePackage(&pkgInfoExt, compInfo, packagePath.c_str(), GetTestPrivateKeyName(0).c_str()); 80 EXPECT_EQ(ret, PKG_INVALID_PARAM); 81 82 pkgInfoExt.entryCount = 4097; // 4097: max entry count 83 ret = CreatePackage(&pkgInfoExt, compInfo, packagePath.c_str(), GetTestPrivateKeyName(0).c_str()); 84 EXPECT_EQ(ret, PKG_INVALID_PARAM); 85 return 0; 86 } 87 TestPackagePack(int type=PKG_DIGEST_TYPE_SHA256)88 int TestPackagePack(int type = PKG_DIGEST_TYPE_SHA256) 89 { 90 int32_t ret; 91 uint32_t updateFileVersion = 1000; 92 UpgradePkgInfoExt pkgInfo; 93 pkgInfo.softwareVersion = strdup("100.100.100.100"); 94 pkgInfo.date = strdup("2021-02-02"); 95 pkgInfo.time = strdup("21:23:49"); 96 pkgInfo.productUpdateId = strdup("555.555.100.555"); 97 pkgInfo.entryCount = testFileNames_.size(); 98 pkgInfo.digestMethod = type; 99 pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 100 pkgInfo.pkgType = PKG_PACK_TYPE_UPGRADE; 101 pkgInfo.updateFileVersion = updateFileVersion; 102 std::string filePath; 103 uint32_t componentIdBase = 100; 104 uint8_t componentFlags = 22; 105 std::vector<ComponentInfoExt> comp(testFileNames_.size()); 106 for (size_t i = 0; i < testFileNames_.size(); i++) { 107 comp[i].componentAddr = strdup(testFileNames_[i].c_str()); 108 filePath = TEST_PATH_FROM; 109 filePath += testFileNames_[i].c_str(); 110 comp[i].filePath = strdup(filePath.c_str()); 111 comp[i].version = strdup("55555555"); 112 ret = BuildFileDigest(*comp[i].digest, sizeof(comp[i].digest), filePath); 113 EXPECT_EQ(ret, PKG_SUCCESS); 114 comp[i].size = GetFileSize(filePath); 115 comp[i].originalSize = comp[i].size; 116 comp[i].id = i + componentIdBase; 117 comp[i].resType = 1; 118 comp[i].type = 1; 119 comp[i].flags = componentFlags; 120 filePath.clear(); 121 } 122 std::string packagePath = TEST_PATH_TO; 123 packagePath += testPackageName; 124 ret = CreatePackage(&pkgInfo, comp, packagePath.c_str(), 125 GetTestPrivateKeyName(pkgInfo.digestMethod).c_str()); 126 EXPECT_EQ(ret, PKG_SUCCESS); 127 for (size_t i = 0; i < testFileNames_.size(); i++) { 128 free(comp[i].componentAddr); 129 free(comp[i].filePath); 130 free(comp[i].version); 131 } 132 free(pkgInfo.softwareVersion); 133 free(pkgInfo.date); 134 free(pkgInfo.time); 135 free(pkgInfo.productUpdateId); 136 return ret; 137 } 138 TestPackageUnpack(int type)139 int TestPackageUnpack(int type) 140 { 141 EXPECT_NE(pkgManager_, nullptr); 142 std::vector<std::string> components; 143 // 使用上面打包的包进行解析 144 int32_t ret = pkgManager_->LoadPackage( 145 testPackagePath + "test_package.zip", GetTestCertName(type), components); 146 EXPECT_EQ(ret, PKG_SUCCESS); 147 148 for (size_t i = 0; i < components.size(); i++) { 149 PKG_LOGI("comp [%zu] file name: %s \r\n", i, (TEST_PATH_TO + components[i]).c_str()); 150 ExtractFile(pkgManager_, components, i); 151 } 152 return PKG_SUCCESS; 153 } 154 TestZipPkgCompress(int digestMethod)155 int TestZipPkgCompress(int digestMethod) 156 { 157 return CreateZipPackage(testFileNames_, TEST_PATH_TO + testZipPackageName, TEST_PATH_FROM, digestMethod); 158 } 159 TestZipPkgDecompress(int digestMethod)160 int TestZipPkgDecompress(int digestMethod) 161 { 162 EXPECT_NE(pkgManager_, nullptr); 163 std::vector<std::string> components; 164 int32_t ret = pkgManager_->LoadPackage(TEST_PATH_TO + testZipPackageName, 165 GetTestCertName(digestMethod), components); 166 EXPECT_EQ(ret, PKG_SUCCESS); 167 168 for (size_t i = 0; i < components.size(); i++) { 169 PKG_LOGI("file name: %s \r\n", (TEST_PATH_TO + components[i]).c_str()); 170 ExtractFile(pkgManager_, components, i); 171 } 172 return ret; 173 } 174 TestGZipPkgCompress()175 int TestGZipPkgCompress() 176 { 177 int ret = TestPackagePack(); 178 EXPECT_EQ(ret, PKG_SUCCESS); 179 EXPECT_NE(pkgManager_, nullptr); 180 std::vector<std::pair<std::string, ZipFileInfo>> files; 181 ZipFileInfo file; 182 file.fileInfo.identity = testPackageName; 183 file.fileInfo.packMethod = PKG_COMPRESS_METHOD_GZIP; 184 file.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC; 185 std::string fileName = TEST_PATH_TO + testPackageName; 186 files.push_back(std::pair<std::string, ZipFileInfo>(fileName, file)); 187 188 PkgInfo pkgInfo; 189 pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 190 pkgInfo.digestMethod = PKG_DIGEST_TYPE_SHA256; 191 pkgInfo.pkgType = PKG_PACK_TYPE_GZIP; 192 return pkgManager_->CreatePackage(TEST_PATH_TO + testGZipPackageName, 193 GetTestPrivateKeyName(pkgInfo.digestMethod), &pkgInfo, files); 194 } 195 TestVerifyUpgradePackage()196 int TestVerifyUpgradePackage() 197 { 198 constexpr size_t digestSize = 32; 199 std::vector<uint8_t> digest(digestSize); 200 std::string path = testPackagePath + "test_package.zip"; 201 BuildFileDigest(*digest.data(), digest.capacity(), path.c_str()); 202 int ret = VerifyPackage(path.c_str(), GetTestCertName(0).c_str(), "", digest.data(), digest.capacity()); 203 EXPECT_EQ(0, ret); 204 ret = VerifyPackage(nullptr, nullptr, nullptr, nullptr, digest.capacity()); 205 EXPECT_EQ(PKG_INVALID_PARAM, ret); 206 return 0; 207 } 208 TestVerifyPackageWithCallback()209 int TestVerifyPackageWithCallback() 210 { 211 std::string path = testPackagePath + "test_package.zip"; 212 int ret = VerifyPackageWithCallback(path.c_str(), GetTestCertName(0).c_str(), 213 [](int32_t result, uint32_t percent) { PKG_LOGI("current progress: %u\n", percent); }); 214 EXPECT_EQ(0, ret); 215 216 std::string keyPath = ""; 217 ret = VerifyPackageWithCallback(path.c_str(), keyPath.c_str(), 218 [](int32_t result, uint32_t percent) { PKG_LOGI("current progress: %u\n", percent); }); 219 EXPECT_EQ(PKG_INVALID_PARAM, ret); 220 221 std::function<void(int32_t result, uint32_t percent)> cb = nullptr; 222 ret = VerifyPackageWithCallback(path.c_str(), GetTestCertName(0).c_str(), cb); 223 EXPECT_EQ(PKG_INVALID_PARAM, ret); 224 225 path = ""; 226 ret = VerifyPackageWithCallback(path.c_str(), GetTestCertName(0).c_str(), 227 [](int32_t result, uint32_t percent) { PKG_LOGI("current progress: %u\n", percent); }); 228 EXPECT_EQ(PKG_INVALID_PARAM, ret); 229 return 0; 230 } 231 TestLz4PkgCompress()232 int TestLz4PkgCompress() 233 { 234 int ret = TestPackagePack(); 235 EXPECT_EQ(ret, PKG_SUCCESS); 236 EXPECT_NE(pkgManager_, nullptr); 237 std::vector<std::pair<std::string, Lz4FileInfo>> files; 238 Lz4FileInfo file; 239 int8_t compressionLevel = 14; 240 file.fileInfo.identity = testPackageName; 241 file.fileInfo.packMethod = PKG_COMPRESS_METHOD_LZ4; 242 file.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC; 243 file.compressionLevel = compressionLevel; 244 file.blockSizeID = 0; 245 file.contentChecksumFlag = 0; 246 file.blockIndependence = 0; 247 std::string fileName = TEST_PATH_TO + testPackageName; 248 files.push_back(std::pair<std::string, Lz4FileInfo>(fileName, file)); 249 250 PkgInfo pkgInfo; 251 pkgInfo.pkgType = PKG_PACK_TYPE_LZ4; 252 pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 253 pkgInfo.digestMethod = PKG_DIGEST_TYPE_SHA256; 254 return pkgManager_->CreatePackage(TEST_PATH_TO + testLz4PackageName, 255 GetTestPrivateKeyName(pkgInfo.digestMethod), &pkgInfo, files); 256 } 257 }; 258 259 HWTEST_F(PackageUnitTest, TestLz4Package, TestSize.Level1) 260 { 261 PackageUnitTest test; 262 EXPECT_EQ(0, test.TestLz4PkgCompress()); 263 } 264 265 HWTEST_F(PackageUnitTest, TestInvalidCreatePackage, TestSize.Level1) 266 { 267 PackageUnitTest test; 268 EXPECT_EQ(0, test.TestInvalidCreatePackage()); 269 } 270 271 HWTEST_F(PackageUnitTest, TestVerifyUpgradePackage, TestSize.Level1) 272 { 273 PackageUnitTest test; 274 EXPECT_EQ(0, test.TestVerifyUpgradePackage()); 275 } 276 277 HWTEST_F(PackageUnitTest, TestVerifyPackageWithCallback, TestSize.Level1) 278 { 279 PackageUnitTest test; 280 EXPECT_EQ(0, test.TestVerifyPackageWithCallback()); 281 } 282 283 HWTEST_F(PackageUnitTest, TestPackage, TestSize.Level1) 284 { 285 PackageUnitTest test; 286 EXPECT_EQ(0, test.TestPackagePack(PKG_DIGEST_TYPE_SHA256)); 287 EXPECT_EQ(0, test.TestPackageUnpack(PKG_DIGEST_TYPE_SHA256)); 288 } 289 290 HWTEST_F(PackageUnitTest, TestZipPackage, TestSize.Level1) 291 { 292 PackageUnitTest test; 293 EXPECT_EQ(0, test.TestZipPkgCompress(PKG_DIGEST_TYPE_SHA256)); 294 EXPECT_EQ(0, test.TestZipPkgDecompress(PKG_DIGEST_TYPE_SHA256)); 295 } 296 297 HWTEST_F(PackageUnitTest, TestGZipPkg, TestSize.Level1) 298 { 299 PackageUnitTest test; 300 EXPECT_EQ(0, test.TestGZipPkgCompress()); 301 } 302 } 303