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 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 return 0; 82 } 83 TestPackagePack(int type=PKG_DIGEST_TYPE_SHA256)84 int TestPackagePack(int type = PKG_DIGEST_TYPE_SHA256) 85 { 86 int32_t ret; 87 uint32_t updateFileVersion = 1000; 88 UpgradePkgInfoExt pkgInfo; 89 pkgInfo.softwareVersion = strdup("100.100.100.100"); 90 pkgInfo.date = strdup("2021-02-02"); 91 pkgInfo.time = strdup("21:23:49"); 92 pkgInfo.productUpdateId = strdup("555.555.100.555"); 93 pkgInfo.entryCount = testFileNames_.size(); 94 pkgInfo.digestMethod = type; 95 pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 96 pkgInfo.pkgType = PKG_PACK_TYPE_UPGRADE; 97 pkgInfo.updateFileVersion = updateFileVersion; 98 std::string filePath; 99 uint32_t componentIdBase = 100; 100 uint8_t componentFlags = 22; 101 ComponentInfoExt comp[testFileNames_.size()]; 102 for (size_t i = 0; i < testFileNames_.size(); i++) { 103 comp[i].componentAddr = strdup(testFileNames_[i].c_str()); 104 filePath = TEST_PATH_FROM; 105 filePath += testFileNames_[i].c_str(); 106 comp[i].filePath = strdup(filePath.c_str()); 107 comp[i].version = strdup("55555555"); 108 ret = BuildFileDigest(*comp[i].digest, sizeof(comp[i].digest), filePath); 109 EXPECT_EQ(ret, PKG_SUCCESS); 110 comp[i].size = GetFileSize(filePath); 111 comp[i].originalSize = comp[i].size; 112 comp[i].id = i + componentIdBase; 113 comp[i].resType = 1; 114 comp[i].type = 1; 115 comp[i].flags = componentFlags; 116 filePath.clear(); 117 } 118 std::string packagePath = TEST_PATH_TO; 119 packagePath += testPackageName; 120 ret = CreatePackage(&pkgInfo, comp, packagePath.c_str(), 121 GetTestPrivateKeyName(pkgInfo.digestMethod).c_str()); 122 EXPECT_EQ(ret, PKG_SUCCESS); 123 for (size_t i = 0; i < testFileNames_.size(); i++) { 124 free(comp[i].componentAddr); 125 free(comp[i].filePath); 126 free(comp[i].version); 127 } 128 free(pkgInfo.softwareVersion); 129 free(pkgInfo.date); 130 free(pkgInfo.time); 131 free(pkgInfo.productUpdateId); 132 return ret; 133 } 134 TestPackageUnpack(int type)135 int TestPackageUnpack(int type) 136 { 137 pkgManager_ = static_cast<PkgManagerImpl*>(PkgManager::GetPackageInstance()); 138 EXPECT_NE(pkgManager_, nullptr); 139 std::vector<std::string> components; 140 // 使用上面打包的包进行解析 141 int32_t ret = pkgManager_->LoadPackage( 142 "/data/updater/package/test_package.zip", GetTestCertName(type), components); 143 EXPECT_EQ(ret, PKG_SUCCESS); 144 145 for (size_t i = 0; i < components.size(); i++) { 146 PKG_LOGI("comp [%zu] file name: %s \r\n", i, (TEST_PATH_TO + components[i]).c_str()); 147 ExtractFile(pkgManager_, components, i); 148 } 149 return PKG_SUCCESS; 150 } 151 TestZipPkgCompress(int digestMethod)152 int TestZipPkgCompress(int digestMethod) 153 { 154 return CreateZipPackage(testFileNames_, TEST_PATH_TO + testZipPackageName, TEST_PATH_FROM, digestMethod); 155 } 156 TestZipPkgDecompress(int digestMethod)157 int TestZipPkgDecompress(int digestMethod) 158 { 159 pkgManager_ = static_cast<PkgManagerImpl*>(PkgManager::GetPackageInstance()); 160 EXPECT_NE(pkgManager_, nullptr); 161 std::vector<std::string> components; 162 int32_t ret = pkgManager_->LoadPackage(TEST_PATH_TO + testZipPackageName, 163 GetTestCertName(digestMethod), components); 164 EXPECT_EQ(ret, PKG_SUCCESS); 165 166 for (size_t i = 0; i < components.size(); i++) { 167 PKG_LOGI("file name: %s \r\n", (TEST_PATH_TO + components[i]).c_str()); 168 ExtractFile(pkgManager_, components, i); 169 } 170 return ret; 171 } 172 TestGZipPkgCompress()173 int TestGZipPkgCompress() 174 { 175 int ret = TestPackagePack(); 176 EXPECT_EQ(ret, PKG_SUCCESS); 177 pkgManager_ = static_cast<PkgManagerImpl*>(PkgManager::GetPackageInstance()); 178 EXPECT_NE(pkgManager_, nullptr); 179 std::vector<std::pair<std::string, ZipFileInfo>> files; 180 ZipFileInfo file; 181 file.fileInfo.identity = testPackageName; 182 file.fileInfo.packMethod = PKG_COMPRESS_METHOD_GZIP; 183 file.fileInfo.digestMethod = PKG_DIGEST_TYPE_CRC; 184 std::string fileName = TEST_PATH_TO + testPackageName; 185 files.push_back(std::pair<std::string, ZipFileInfo>(fileName, file)); 186 187 PkgInfo pkgInfo; 188 pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 189 pkgInfo.digestMethod = PKG_DIGEST_TYPE_SHA256; 190 pkgInfo.pkgType = PKG_PACK_TYPE_GZIP; 191 return pkgManager_->CreatePackage(TEST_PATH_TO + testGZipPackageName, 192 GetTestPrivateKeyName(pkgInfo.digestMethod), &pkgInfo, files); 193 } 194 TestVerifyUpgradePackage()195 int TestVerifyUpgradePackage() 196 { 197 constexpr size_t digestSize = 32; 198 std::vector<uint8_t> digest(digestSize); 199 std::string path = "/data/updater/package/test_package.zip"; 200 BuildFileDigest(*digest.data(), digest.capacity(), path.c_str()); 201 int ret = VerifyPackage(path.c_str(), GetTestCertName(0).c_str(), "", digest.data(), digest.capacity()); 202 EXPECT_EQ(0, ret); 203 ret = VerifyPackage(nullptr, nullptr, nullptr, nullptr, digest.capacity()); 204 EXPECT_EQ(PKG_INVALID_PARAM, ret); 205 return 0; 206 } 207 TestVerifyPackageWithCallback()208 int TestVerifyPackageWithCallback() 209 { 210 std::string path = "/data/updater/package/test_package.zip"; 211 int ret = VerifyPackageWithCallback(path.c_str(), GetTestCertName(0).c_str(), 212 [](int32_t result, uint32_t percent) { PKG_LOGI("current progress: %u\n", percent); }); 213 EXPECT_EQ(0, ret); 214 215 std::string keyPath = ""; 216 ret = VerifyPackageWithCallback(path.c_str(), keyPath.c_str(), 217 [](int32_t result, uint32_t percent) { PKG_LOGI("current progress: %u\n", percent); }); 218 EXPECT_EQ(PKG_INVALID_PARAM, ret); 219 220 std::function<void(int32_t result, uint32_t percent)> cb = nullptr; 221 ret = VerifyPackageWithCallback(path.c_str(), GetTestCertName(0).c_str(), cb); 222 EXPECT_EQ(PKG_INVALID_PARAM, ret); 223 224 path = ""; 225 ret = VerifyPackageWithCallback(path.c_str(), GetTestCertName(0).c_str(), 226 [](int32_t result, uint32_t percent) { PKG_LOGI("current progress: %u\n", percent); }); 227 EXPECT_EQ(PKG_INVALID_PARAM, ret); 228 return 0; 229 } 230 TestLz4PkgCompress()231 int TestLz4PkgCompress() 232 { 233 int ret = TestPackagePack(); 234 EXPECT_EQ(ret, PKG_SUCCESS); 235 pkgManager_ = static_cast<PkgManagerImpl*>(PkgManager::GetPackageInstance()); 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