From 89766585e63a1faeb8f23e627a30c91e9db8f36a Mon Sep 17 00:00:00 2001 From: z30034863 Date: Fri, 18 Jul 2025 17:39:19 +0800 Subject: [PATCH] fix debug validation rules Signed-off-by: z30034863 --- adapter/ohos/HapVerify.java | 45 +++++++++++++++---- .../include/json/hap_verify_utils.h | 1 + .../frameworks/src/json/hap_verify_utils.cpp | 37 ++++++++++++--- .../hap_verify_info_test.cpp | 2 +- .../hap_verify_utils_test.cpp | 2 +- .../test/unittest/utils_Test/utils_test.cpp | 6 +-- 6 files changed, 75 insertions(+), 18 deletions(-) diff --git a/adapter/ohos/HapVerify.java b/adapter/ohos/HapVerify.java index e20bc602..090565ef 100644 --- a/adapter/ohos/HapVerify.java +++ b/adapter/ohos/HapVerify.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -284,6 +285,7 @@ class HapVerify { verifyInfo = optional.get(); } List hapList = new ArrayList<>(); + List hspList = new ArrayList<>(); int hspMinCompatibleVersionCodeMax = -1; int hspTargetApiVersionMax = -1; int hspMinApiVersionMax = -1; @@ -321,9 +323,10 @@ class HapVerify { LOG.warning("Module: (" + moduleName + ") and Module: (" + hapVerifyInfo.getModuleName() + ") has different values."); } - if (hapVerifyInfo.getFileType() == HAP_SUFFIX) { + if (Objects.equals(hapVerifyInfo.getFileType(), HAP_SUFFIX)) { hapList.add(hapVerifyInfo); - } else if (hapVerifyInfo.getFileType() == HSP_SUFFIX) { + } else if (Objects.equals(hapVerifyInfo.getFileType(), HSP_SUFFIX)) { + hspList.add(hapVerifyInfo); if (hapVerifyInfo.getVersion().minCompatibleVersionCode > hspMinCompatibleVersionCodeMax) { hspMinCompatibleVersionCodeMax = hapVerifyInfo.getVersion().minCompatibleVersionCode; } @@ -338,6 +341,9 @@ class HapVerify { if (!appFieldsIsValid(hapList, hspMinCompatibleVersionCodeMax, hspTargetApiVersionMax, hspMinApiVersionMax)) { return false; } + if (!moduleDebugValidation(hapList, hspList)) { + return false; + } return true; } @@ -373,6 +379,35 @@ class HapVerify { return true; } + private static boolean moduleDebugValidation(List hapVerifyInfos, + List hspVerifyInfos) { + if (hapVerifyInfos.isEmpty()) { + LOG.warning("Hap verify infos is empty"); + return true; + } + HapVerifyInfo hap = hapVerifyInfos.get(0); + for (HapVerifyInfo hapInfo : hapVerifyInfos) { + if (hap.isDebug() != hapInfo.isDebug()) { + String errMsg = "The debug fields of multiple Hap are different."; + String solution = "Ensure that the values of 'debug' in the module.json file of each HAP module" + + " are the same."; + LOG.error(PackingToolErrMsg.CHECK_APP_FIELDS_INVALID.toString(errMsg, solution)); + return false; + } + } + if (hap.isDebug() || hspVerifyInfos.isEmpty()) { + LOG.warning("Hap debug is true or Hsp verify infos is empty"); + return true; + } + if (hspVerifyInfos.stream().anyMatch(HapVerifyInfo::isDebug)) { + String errMsg = "Detected HAP(s) with debug=false, but some HSP(s) are debug=true."; + String solution = "When the debug value of Hap is false,the debug value of Hsp should also be false."; + LOG.error(PackingToolErrMsg.CHECK_APP_FIELDS_INVALID.toString(errMsg, solution)); + return false; + } + return true; + } + private static boolean appFieldsIsSame(VerifyCollection verifyCollection, HapVerifyInfo hapVerifyInfo) { if (hapVerifyInfo.getBundleName().isEmpty() || !verifyCollection.bundleName.equals(hapVerifyInfo.getBundleName())) { @@ -417,12 +452,6 @@ class HapVerify { LOG.error(PackingToolErrMsg.APP_FIELDS_DIFFERENT_ERROR.toString(errMsg, solution)); return false; } - if (verifyCollection.debug != hapVerifyInfo.isDebug()) { - String errMsg = "The debug parameter values are different."; - String solution = "Check if the debug setting is the same in different modules."; - LOG.error(PackingToolErrMsg.APP_FIELDS_DIFFERENT_ERROR.toString(errMsg, solution)); - return false; - } if (isEntryOrFeature(verifyCollection.getModuleType()) && isEntryOrFeature(hapVerifyInfo.getModuleType())) { if (!verifyCollection.getMultiAppMode().equals(hapVerifyInfo.getMultiAppMode())) { String errMsg = "The multiAppMode parameter values are different."; diff --git a/packing_tool/frameworks/include/json/hap_verify_utils.h b/packing_tool/frameworks/include/json/hap_verify_utils.h index aecc7d13..be1794d7 100644 --- a/packing_tool/frameworks/include/json/hap_verify_utils.h +++ b/packing_tool/frameworks/include/json/hap_verify_utils.h @@ -136,6 +136,7 @@ private: static bool CheckContinueTypeIsValid(const std::list& hapVerifyInfos); static bool CheckContinueTypeIsValid(const HapVerifyInfo& hapVerifyInfo); static bool CheckContinueTypeIsValid(const HapVerifyInfo& hapVerifyInfo1, const HapVerifyInfo& hapVerifyInfo2); + static bool ModuleDebugValidation(const std::list hapList, const std::list hspList); static bool AppFieldsIsValid(const std::list& hapVerifyInfos, int32_t minCompatibleVersionCode, int32_t targetApiVersion, int32_t minApiVersion); static bool AppAssetAccessGroupsIsSame(const std::list& assetAccessGroups, diff --git a/packing_tool/frameworks/src/json/hap_verify_utils.cpp b/packing_tool/frameworks/src/json/hap_verify_utils.cpp index a0da824b..051079f3 100644 --- a/packing_tool/frameworks/src/json/hap_verify_utils.cpp +++ b/packing_tool/frameworks/src/json/hap_verify_utils.cpp @@ -129,6 +129,7 @@ bool HapVerifyUtils::CheckAppFieldsIsSame(const std::list& hapVer return false; } std::list hapList; + std::list hspList; int32_t hspMinCompatibleVersionCodeMax = -1; int32_t hspTargetApiVersionMax = -1; int32_t hspMinApiVersionMax = -1; @@ -174,6 +175,7 @@ bool HapVerifyUtils::CheckAppFieldsIsSame(const std::list& hapVer if (hapVerifyInfo.GetFileType() == HAP_SUFFIX) { hapList.emplace_back(hapVerifyInfo); } else if (hapVerifyInfo.GetFileType() == HSP_SUFFIX) { + hspList.emplace_back(hapVerifyInfo); if (hapVerifyInfo.GetVersion().minCompatibleVersionCode > hspMinCompatibleVersionCodeMax) { hspMinCompatibleVersionCodeMax = hapVerifyInfo.GetVersion().minCompatibleVersionCode; } @@ -188,6 +190,36 @@ bool HapVerifyUtils::CheckAppFieldsIsSame(const std::list& hapVer if (!AppFieldsIsValid(hapList, hspMinCompatibleVersionCodeMax, hspTargetApiVersionMax, hspMinApiVersionMax)) { return false; } + if (!ModuleDebugValidation(hapList, hspList)) { + return false; + } + return true; +} + +bool HapVerifyUtils::ModuleDebugValidation(const std::list hapList, + const std::list hspList) +{ + if (hapList.empty()) { + LOGW("hapList empty"); + return true; + } + HapVerifyInfo hap = *hapList.begin(); + for (const HapVerifyInfo& hapInfo : hapList) { + if (hap.IsDebug() != hapInfo.IsDebug()) { + LOGE("The debug fields of multiple Hap are different."); + return false; + } + } + if (hap.IsDebug() || hspList.empty()) { + LOGW("hap debug is true or hspList empty"); + return true; + } + for (const HapVerifyInfo& hapInfo : hspList) { + if (hapInfo.IsDebug()) { + LOGE("Detected HAP(s) with debug=false, but some HSP(s) are debug=true."); + return false; + } + } return true; } @@ -248,11 +280,6 @@ bool HapVerifyUtils::AppFieldsIsSame(const VerifyCollection& verifyCollection, c LOGE("targetPriority is different."); return false; } - if (verifyCollection.debug != hapVerifyInfo.IsDebug()) { - LOGE("debug is different."); - LOGE("Solutions: > Check if the debug type is the same in different modules."); - return false; - } if (IsEntryOrFeature(verifyCollection.moduleType) && IsEntryOrFeature(hapVerifyInfo.GetModuleType())) { if (verifyCollection.multiAppMode != (hapVerifyInfo.GetMultiAppMode())) { LOGE("multiAppMode is different."); diff --git a/packing_tool/frameworks/test/unittest/json/hap_verify_info_test/hap_verify_info_test.cpp b/packing_tool/frameworks/test/unittest/json/hap_verify_info_test/hap_verify_info_test.cpp index 88904432..1e886a55 100644 --- a/packing_tool/frameworks/test/unittest/json/hap_verify_info_test/hap_verify_info_test.cpp +++ b/packing_tool/frameworks/test/unittest/json/hap_verify_info_test/hap_verify_info_test.cpp @@ -618,6 +618,6 @@ HWTEST_F(HapVerifyInfoTest, ConvertToDependency_0200, Function | MediumTest | Le hapVerifyInfo.ConvertToDependency(); EXPECT_FALSE(hapVerifyInfo.dependencies_.empty()); EXPECT_TRUE(std::find(hapVerifyInfo.dependencies_.begin(), hapVerifyInfo.dependencies_.end(), - dependencyItem1.bundleName) != hapVerifyInfo.dependencies_.end()); + dependencyItem1.moduleName) != hapVerifyInfo.dependencies_.end()); } } \ No newline at end of file diff --git a/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/hap_verify_utils_test.cpp b/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/hap_verify_utils_test.cpp index 60d59cda..7de62972 100644 --- a/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/hap_verify_utils_test.cpp +++ b/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/hap_verify_utils_test.cpp @@ -463,7 +463,7 @@ HWTEST_F(HapVerifyUtilsTest, CheckAppFieldsIsSame_0900, Function | MediumTest | std::list hapVerifyInfos; OHOS::AppPackingTool::HapVerifyInfo hapVerifyInfo3; hapVerifyInfo3.SetBundleName("test"); - hapVerifyInfo3.SetBundleType("hap"); + hapVerifyInfo3.SetBundleType("hsp"); OHOS::AppPackingTool::Version version; version.versionCode = 1; version.versionName = "1.0.0"; diff --git a/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp b/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp index e0bad42d..a4e60f65 100644 --- a/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp +++ b/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp @@ -385,10 +385,10 @@ HWTEST_F(UtilsTest, GetListDistinctCount_1900, Function | MediumTest | Level1) */ HWTEST_F(UtilsTest, GetCeilFileSize_2000, Function | MediumTest | Level1) { - long fileSize = 12 * 1024 * 1014L; - int sizeLimit = 12; + long fileSize = 12 * 1024 * 1024L; + int sizeLimit = 12 * 1024; - EXPECT_TRUE(OHOS::AppPackingTool::Utils::GetCeilFileSize(fileSize, sizeLimit) < 12); + EXPECT_TRUE(OHOS::AppPackingTool::Utils::GetCeilFileSize(fileSize, sizeLimit) > 12 * 1024); } /* -- Gitee