diff --git a/services/abilitymgr/include/interceptor/ability_interceptor_interface.h b/services/abilitymgr/include/interceptor/ability_interceptor_interface.h index 8fbaabed3c356c259978d3d284ac205ec1dc0ac5..bd47fd0c2d0b57cfbd82d1421fa0577485f0fc1f 100644 --- a/services/abilitymgr/include/interceptor/ability_interceptor_interface.h +++ b/services/abilitymgr/include/interceptor/ability_interceptor_interface.h @@ -37,7 +37,7 @@ struct AbilityInterceptorParam { int32_t requestCode; int32_t userId; bool isWithUI = false; - const sptr &callerToken; + sptr callerToken = nullptr; std::shared_ptr abilityInfo; // target abilityInfo get in afterCheckExecuter_ bool isStartAsCaller = false; int32_t appIndex = 0; diff --git a/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp b/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp index 9c2f79288a79bd2d145dada4366590dc76148f32..4838f00eacde9e901e3ab49e1f38f9ccee4620fe 100644 --- a/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp +++ b/services/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp @@ -143,6 +143,7 @@ ErrCode EcologicalRuleInterceptor::QueryAtomicServiceStartupRule(Want &want, spt int32_t appIndex = 0; StartAbilityUtils::startAbilityInfo = StartAbilityInfo::CreateStartAbilityInfo(want, userId, appIndex, nullptr); + CHECK_POINTER_AND_RETURN_LOG(StartAbilityUtils::startAbilityInfo, INNER_ERR, "null startAbilityInfo"); CHECK_RET_RETURN_RET(StartAbilityUtils::startAbilityInfo->status, "Get targetApplicationInfo failed"); ErmsCallerInfo callerInfo; diff --git a/test/unittest/ecological_rule_interceptor_test/BUILD.gn b/test/unittest/ecological_rule_interceptor_test/BUILD.gn index ddb74f38d78401b6a69c3369b00e2a6b6faa7988..85e1abcf7e6128ca53d8db71edd59cf4f70a0058 100644 --- a/test/unittest/ecological_rule_interceptor_test/BUILD.gn +++ b/test/unittest/ecological_rule_interceptor_test/BUILD.gn @@ -20,25 +20,21 @@ ohos_unittest("ecological_rule_interceptor_test") { module_out_path = module_output_path cflags_cc = [] include_dirs = [ - "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper", - "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/system_ability_mock", - "${ability_runtime_innerkits_path}/app_manager/include/appmgr", - "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", - "${ability_runtime_services_path}/abilitymgr/include/utils", - "${ability_runtime_services_path}/abilitymgr/include", - "${ability_runtime_services_path}/abilitymgr/include/interceptor", + "./mock", + "${ability_runtime_services_path}/abilitymgr/include/ecological_rule", ] sources = [ - # add mock file "${ability_runtime_services_path}/abilitymgr/src/ecological_rule/ability_ecological_rule_mgr_service_param.cpp", - "${ability_runtime_services_path}/abilitymgr/src/interceptor/ability_interceptor_executer.cpp", - "${ability_runtime_services_path}/abilitymgr/src/utils/start_ability_utils.cpp", + "${ability_runtime_services_path}/abilitymgr/src/interceptor/ecological_rule_interceptor.cpp", + "./mock/ecological_rule/ability_ecological_rule_mgr_service.cpp", + "./mock/ability_record.cpp", + "./mock/bundle_mgr_helper.cpp", + "./mock/start_ability_utils.cpp", "ecological_rule_interceptor_test.cpp", ] configs = [ - "${ability_runtime_services_path}/abilitymgr:abilityms_config", "${ability_runtime_test_path}/mock/services_abilitymgr_test:aafwk_mock_config", ] cflags = [ @@ -52,7 +48,6 @@ ohos_unittest("ecological_rule_interceptor_test") { "${ability_runtime_innerkits_path}/ability_manager:ability_manager", "${ability_runtime_innerkits_path}/app_manager:app_manager", "${ability_runtime_native_path}/ability/native:abilitykit_native", - "${ability_runtime_native_path}/appkit:appkit_manager_helper", "${ability_runtime_path}/utils/server/startup:startup_util", "${ability_runtime_services_path}/abilitymgr:abilityms", "${ability_runtime_services_path}/common:app_util", @@ -67,6 +62,7 @@ ohos_unittest("ecological_rule_interceptor_test") { "ability_base:want", "ability_base:zuri", "ability_runtime:ability_deps_wrapper", + "bundle_framework:appexecfwk_base", "c_utils:utils", "common_event_service:cesfwk_innerkits", "dsoftbus:softbus_client", diff --git a/test/unittest/ecological_rule_interceptor_test/ecological_rule_interceptor_test.cpp b/test/unittest/ecological_rule_interceptor_test/ecological_rule_interceptor_test.cpp index 171862d7cfc769b1da16deb9fe210b6a9bb6a355..53e60514d3cce4fc9a6f74ed4fd209157fca76da 100644 --- a/test/unittest/ecological_rule_interceptor_test/ecological_rule_interceptor_test.cpp +++ b/test/unittest/ecological_rule_interceptor_test/ecological_rule_interceptor_test.cpp @@ -14,7 +14,11 @@ */ #include +#include "ability_record.h" +#include "ability_util.h" +#include "bundle_mgr_helper.h" #include "ecological_rule/ability_ecological_rule_mgr_service_param.h" +#include "ecological_rule/ability_ecological_rule_mgr_service.h" #include "interceptor/ecological_rule_interceptor.h" #include "start_ability_utils.h" #include "parameters.h" @@ -25,8 +29,14 @@ using ErmsCallerInfo = OHOS::EcologicalRuleMgrService::AbilityCallerInfo; namespace OHOS { namespace AAFwk { +namespace { constexpr const char* ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE = "persist.sys.abilityms.support.ecologicalrulemgrservice"; +constexpr int32_t ERMS_ISALLOW_RESULTCODE = 10; +constexpr int32_t ERMS_ISALLOW_EMBED_RESULTCODE = 1; +constexpr const char* BUNDLE_NAME_SCENEBOARD = "com.ohos.sceneboard"; +} + class EcologicalRuleInterceptorTest : public testing::Test { public: static void SetUpTestCase(); @@ -113,9 +123,9 @@ HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_001, TestS want.SetElement(element); want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, bundleName); sptr callerToken = nullptr; - int userId = 100; AtomicServiceStartupRule rule; sptr replaceWant = nullptr; + int userId = 100; ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); EXPECT_EQ(result, ERR_INVALID_CALLER); } @@ -136,14 +146,241 @@ HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_002, TestS want.SetElement(element); want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); sptr callerToken = nullptr; - int userId = 100; AtomicServiceStartupRule rule; sptr replaceWant = nullptr; OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "false"); + int userId = 100; ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); EXPECT_EQ(result, ERR_CAPABILITY_NOT_SUPPORT); } +/** + * @tc.name: QueryAtomicServiceStartupRule_003 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_003, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = true; + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, BMS_NOT_CONNECTED); +} + +/** + * @tc.name: QueryAtomicServiceStartupRule_004 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_004, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = -1; + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, -1); +} + +/** + * @tc.name: QueryAtomicServiceStartupRule_005 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_005, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + StartAbilityInfo::createStartAbilityInfo = nullptr; + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, INNER_ERR); +} + +/** + * @tc.name: QueryAtomicServiceStartupRule_006 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_006, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = -1; + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, -1); +} + +/** + * @tc.name: QueryAtomicServiceStartupRule_007 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_007, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = 0; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = -1; + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, -1); +} + +/** + * @tc.name: QueryAtomicServiceStartupRule_008 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_008, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = 0; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = ERMS_ISALLOW_RESULTCODE; + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: QueryAtomicServiceStartupRule_009 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_009, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = 0; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = -1; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.replaceWant = sptr::MakeSptr(); + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, ERR_ECOLOGICAL_CONTROL_STATUS); +} + +/** + * @tc.name: QueryAtomicServiceStartupRule_010 + * @tc.desc: QueryAtomicServiceStartupRule + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_010, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + sptr callerToken = nullptr; + AtomicServiceStartupRule rule; + sptr replaceWant = nullptr; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = 0; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = -1; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.replaceWant = nullptr; + int userId = 100; + ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant); + EXPECT_EQ(result, ERR_OK); +} + /** * @tc.name: GetEcologicalTargetInfo_001 * @tc.desc: Tests GetEcologicalTargetInfo with non-null StartAbilityInfo matching the want @@ -274,10 +511,11 @@ HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_001, TestSize.Le want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); ErmsCallerInfo callerInfo; sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = true; + StartAbilityUtils::callerAbiltyInfo.type = AppExecFwk::AbilityType::PAGE; int userId = 100; interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); - EXPECT_EQ(callerInfo.callerAppProvisionType, ""); - EXPECT_EQ(callerInfo.callerAppType, 0); + EXPECT_EQ(callerInfo.callerAppType, static_cast(AppExecFwk::AbilityType::PAGE)); } /** @@ -305,135 +543,393 @@ HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_002, TestSize.Le } /** - * @tc.name: InitErmsCallerInfo_001 - * @tc.desc: Tests InitErmsCallerInfo with non-null abilityInfo when want element bundleName is empty + * @tc.name: GetEcologicalCallerInfo_003 + * @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case * @tc.type: FUNC * @tc.require: No */ -HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_001, TestSize.Level1) +HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_003, TestSize.Level1) { std::shared_ptr interceptor = std::make_shared(); Want want; - ElementName element("", "", "MainAbility"); - want.SetElement(element); - std::string callerBundleName = "com.example.caller"; + std::string callerBundleName = "com.ohos.sceneboard"; want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); - want.SetParam("send_to_erms_embedded", 1); - std::shared_ptr abilityInfo = std::make_shared(); - abilityInfo->bundleName = "com.example.target"; ErmsCallerInfo callerInfo; + callerInfo.callerAppType = ErmsCallerInfo::TYPE_HARMONY_APP; + callerInfo.packageName = ""; sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = false; + BundleMgrHelper::isBundleManagerHelperNull = true; int userId = 100; - interceptor->InitErmsCallerInfo(want, abilityInfo, callerInfo, userId, callerToken); - EXPECT_EQ(callerInfo.packageName, callerBundleName); - EXPECT_EQ(callerInfo.uid, 1000); - EXPECT_EQ(callerInfo.pid, 2000); - EXPECT_EQ(callerInfo.embedded, 1); - EXPECT_EQ(callerInfo.userId, userId); - EXPECT_EQ(callerInfo.targetBundleName, "com.example.target"); + interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerAppType, ErmsCallerInfo::TYPE_HARMONY_APP); + EXPECT_EQ(callerInfo.packageName, ""); } /** - * @tc.name: InitErmsCallerInfo_002 - * @tc.desc: Tests InitErmsCallerInfo with non-empty want element bundleName + * @tc.name: GetEcologicalCallerInfo_004 + * @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case * @tc.type: FUNC * @tc.require: No */ -HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_002, TestSize.Level1) +HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_004, TestSize.Level1) { std::shared_ptr interceptor = std::make_shared(); Want want; - std::string bundleName = "com.example.target"; - ElementName element("", bundleName, "MainAbility"); - want.SetElement(element); - std::string callerBundleName = "com.example.caller"; + std::string callerBundleName = "com.ohos.sceneboard"; want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); - want.SetParam("send_to_erms_embedded", 1); - std::shared_ptr abilityInfo = std::make_shared(); - abilityInfo->bundleName = "com.example.different"; ErmsCallerInfo callerInfo; + callerInfo.callerAppType = ErmsCallerInfo::TYPE_HARMONY_APP; + callerInfo.packageName = ""; sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = false; + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetNameForUid = -1; int userId = 100; - interceptor->InitErmsCallerInfo(want, abilityInfo, callerInfo, userId, callerToken); - EXPECT_EQ(callerInfo.packageName, callerBundleName); - EXPECT_EQ(callerInfo.uid, 1000); - EXPECT_EQ(callerInfo.pid, 2000); - EXPECT_EQ(callerInfo.embedded, 1); - EXPECT_EQ(callerInfo.userId, userId); - EXPECT_EQ(callerInfo.targetBundleName, ""); + interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerAppType, ErmsCallerInfo::TYPE_HARMONY_APP); + EXPECT_EQ(callerInfo.packageName, ""); } /** - * @tc.name: InitErmsCallerInfo_003 - * @tc.desc: Tests InitErmsCallerInfo with null abilityInfo + * @tc.name: GetEcologicalCallerInfo_005 + * @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case * @tc.type: FUNC * @tc.require: No */ -HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_003, TestSize.Level1) +HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_005, TestSize.Level1) { std::shared_ptr interceptor = std::make_shared(); Want want; - ElementName element("", "", "MainAbility"); - want.SetElement(element); - std::string callerBundleName = "com.example.caller"; + std::string callerBundleName = "com.ohos.sceneboard"; want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); - want.SetParam("send_to_erms_embedded", 1); ErmsCallerInfo callerInfo; + callerInfo.callerAppType = ErmsCallerInfo::TYPE_HARMONY_APP; + callerInfo.packageName = ""; sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = false; + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetNameForUid = ERR_OK; + BundleMgrHelper::retGetApplicationInfo = -1; int userId = 100; - interceptor->InitErmsCallerInfo(want, nullptr, callerInfo, userId, callerToken); - EXPECT_EQ(callerInfo.packageName, callerBundleName); - EXPECT_EQ(callerInfo.uid, 1000); - EXPECT_EQ(callerInfo.pid, 2000); - EXPECT_EQ(callerInfo.embedded, 1); - EXPECT_EQ(callerInfo.userId, userId); - EXPECT_EQ(callerInfo.targetBundleName, ""); + interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerAppType, ErmsCallerInfo::TYPE_HARMONY_APP); + EXPECT_EQ(callerInfo.packageName, ""); } + /** - * @tc.name: DoProcess_001 - * @tc.desc: Tests the first branch in DoProcess where skipErms is true + * @tc.name: GetEcologicalCallerInfo_006 + * @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case * @tc.type: FUNC * @tc.require: No */ -HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_001, TestSize.Level1) +HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_006, TestSize.Level1) { std::shared_ptr interceptor = std::make_shared(); Want want; - int requestCode = 0; + std::string callerBundleName = "com.ohos.sceneboard"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + ErmsCallerInfo callerInfo; + callerInfo.callerAppType = ErmsCallerInfo::TYPE_HARMONY_APP; + callerInfo.packageName = ""; + sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = false; + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetNameForUid = ERR_OK; + BundleMgrHelper::retGetApplicationInfo = false; int userId = 100; - StartAbilityUtils::skipErms = true; - auto shouldBlockFunc = []() { return false; }; - AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr, - shouldBlockFunc); - ErrCode result = interceptor->DoProcess(param); - EXPECT_EQ(result, ERR_OK); - EXPECT_EQ(StartAbilityUtils::skipErms, false); + interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerAppType, ErmsCallerInfo::TYPE_HARMONY_APP); + EXPECT_EQ(callerInfo.packageName, ""); } /** - * @tc.name: DoProcess_002 - * @tc.desc: Tests the branch where caller bundle name equals target bundle name + * @tc.name: GetEcologicalCallerInfo_007 + * @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case * @tc.type: FUNC * @tc.require: No */ -HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_002, TestSize.Level1) +HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_007, TestSize.Level1) { std::shared_ptr interceptor = std::make_shared(); Want want; - std::string bundleName = "com.example.test"; - ElementName element("", bundleName, "MainAbility"); - want.SetElement(element); - want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, bundleName); - int requestCode = 0; - int userId = 100; - auto shouldBlockFunc = []() { return false; }; - AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr, + std::string callerBundleName = "com.ohos.sceneboard"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + ErmsCallerInfo callerInfo; + sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = false; + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetNameForUid = ERR_OK; + BundleMgrHelper::retGetApplicationInfo = true; + BundleMgrHelper::applicationInfo.bundleType = AppExecFwk::BundleType::ATOMIC_SERVICE; + int userId = 100; + interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerAppType, ErmsCallerInfo::TYPE_ATOM_SERVICE); +} + +/** + * @tc.name: GetEcologicalCallerInfo_008 + * @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_008, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string callerBundleName = "com.ohos.sceneboard"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + ErmsCallerInfo callerInfo; + callerInfo.packageName == ""; + sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = false; + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetNameForUid = ERR_OK; + BundleMgrHelper::retGetApplicationInfo = true; + BundleMgrHelper::applicationInfo.bundleType = AppExecFwk::BundleType::APP; + BundleMgrHelper::applicationInfo.name = BUNDLE_NAME_SCENEBOARD; + int userId = 100; + interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerAppType, ErmsCallerInfo::TYPE_HARMONY_APP); + EXPECT_EQ(callerInfo.packageName, BUNDLE_NAME_SCENEBOARD); +} + +/** + * @tc.name: GetEcologicalCallerInfo_009 + * @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_009, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string callerBundleName = "com.ohos.sceneboard"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + ErmsCallerInfo callerInfo; + sptr callerToken = nullptr; + StartAbilityUtils::retGetCallerAbilityInfo = false; + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetNameForUid = ERR_OK; + BundleMgrHelper::retGetApplicationInfo = true; + BundleMgrHelper::applicationInfo.bundleType = AppExecFwk::BundleType::APP_SERVICE_FWK; + int userId = 100; + interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerAppType, ErmsCallerInfo::TYPE_APP_SERVICE); +} + +/** + * @tc.name: InitErmsCallerInfo_001 + * @tc.desc: Tests InitErmsCallerInfo with non-null abilityInfo when want element bundleName is empty + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_001, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + ElementName element("", "", "MainAbility"); + want.SetElement(element); + std::string callerBundleName = "com.example.caller"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + want.SetParam("send_to_erms_embedded", 1); + std::shared_ptr abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.example.target"; + ErmsCallerInfo callerInfo; + sptr callerToken = nullptr; + int userId = 100; + interceptor->InitErmsCallerInfo(want, abilityInfo, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.packageName, callerBundleName); + EXPECT_EQ(callerInfo.uid, 1000); + EXPECT_EQ(callerInfo.pid, 2000); + EXPECT_EQ(callerInfo.embedded, 1); + EXPECT_EQ(callerInfo.userId, userId); + EXPECT_EQ(callerInfo.targetBundleName, "com.example.target"); +} + +/** + * @tc.name: InitErmsCallerInfo_002 + * @tc.desc: Tests InitErmsCallerInfo with non-empty want element bundleName + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_002, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string bundleName = "com.example.target"; + ElementName element("", bundleName, "MainAbility"); + want.SetElement(element); + std::string callerBundleName = "com.example.caller"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + want.SetParam("send_to_erms_embedded", 1); + std::shared_ptr abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.example.different"; + ErmsCallerInfo callerInfo; + sptr callerToken = nullptr; + int userId = 100; + interceptor->InitErmsCallerInfo(want, abilityInfo, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.packageName, callerBundleName); + EXPECT_EQ(callerInfo.uid, 1000); + EXPECT_EQ(callerInfo.pid, 2000); + EXPECT_EQ(callerInfo.embedded, 1); + EXPECT_EQ(callerInfo.userId, userId); + EXPECT_EQ(callerInfo.targetBundleName, ""); +} + +/** + * @tc.name: InitErmsCallerInfo_003 + * @tc.desc: Tests InitErmsCallerInfo with null abilityInfo + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_003, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + ElementName element("", "", "MainAbility"); + want.SetElement(element); + std::string callerBundleName = "com.example.caller"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + want.SetParam("send_to_erms_embedded", 1); + ErmsCallerInfo callerInfo; + sptr callerToken = nullptr; + int userId = 100; + interceptor->InitErmsCallerInfo(want, nullptr, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.packageName, callerBundleName); + EXPECT_EQ(callerInfo.uid, 1000); + EXPECT_EQ(callerInfo.pid, 2000); + EXPECT_EQ(callerInfo.embedded, 1); + EXPECT_EQ(callerInfo.userId, userId); + EXPECT_EQ(callerInfo.targetBundleName, ""); +} + +/** + * @tc.name: InitErmsCallerInfo_004 + * @tc.desc: Tests InitErmsCallerInfo with null abilityInfo + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_004, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + ElementName element("", "", "MainAbility"); + want.SetElement(element); + std::string callerBundleName = "com.example.caller"; + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000); + want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000); + want.SetParam("send_to_erms_embedded", 1); + ErmsCallerInfo callerInfo; + sptr callerToken = sptr::MakeSptr(); + Token::abilityRecord = std::make_shared(); + AbilityRecord::abilityInfo.isStageBasedModel = false; + int userId = 100; + interceptor->InitErmsCallerInfo(want, nullptr, callerInfo, userId, callerToken); + EXPECT_EQ(callerInfo.callerModelType, ErmsCallerInfo::MODEL_FA); +} + +/** + * @tc.name: InitErmsCallerInfo_005 + * @tc.desc: Tests InitErmsCallerInfo with null abilityInfo + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_005, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + ElementName element("", "", "MainAbility"); + want.SetElement(element); + ErmsCallerInfo callerInfo; + sptr callerToken = sptr::MakeSptr(); + Token::abilityRecord = nullptr; + int userId = 100; + interceptor->InitErmsCallerInfo(want, nullptr, callerInfo, userId, callerToken); + EXPECT_NE(callerInfo.callerModelType, ErmsCallerInfo::MODEL_FA); +} + +/** + * @tc.name: InitErmsCallerInfo_006 + * @tc.desc: Tests InitErmsCallerInfo with null abilityInfo + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_006, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + ElementName element("", "", "MainAbility"); + want.SetElement(element); + ErmsCallerInfo callerInfo; + sptr callerToken = sptr::MakeSptr(); + Token::abilityRecord = std::make_shared(); + AbilityRecord::abilityInfo.isStageBasedModel = true; + int userId = 100; + interceptor->InitErmsCallerInfo(want, nullptr, callerInfo, userId, callerToken); + EXPECT_NE(callerInfo.callerModelType, ErmsCallerInfo::MODEL_FA); +} + +/** + * @tc.name: DoProcess_001 + * @tc.desc: Tests the first branch in DoProcess where skipErms is true + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_001, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + int requestCode = 0; + StartAbilityUtils::skipErms = true; + int userId = 100; + auto shouldBlockFunc = []() { return false; }; + AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr, + shouldBlockFunc); + ErrCode result = interceptor->DoProcess(param); + EXPECT_EQ(result, ERR_OK); + EXPECT_EQ(StartAbilityUtils::skipErms, false); +} + +/** + * @tc.name: DoProcess_002 + * @tc.desc: Tests the branch where caller bundle name equals target bundle name + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_002, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string bundleName = "com.example.test"; + ElementName element("", bundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, bundleName); + int requestCode = 0; + int userId = 100; + auto shouldBlockFunc = []() { return false; }; + AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr, shouldBlockFunc); ErrCode result = interceptor->DoProcess(param); EXPECT_EQ(result, ERR_OK); @@ -500,11 +996,348 @@ HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_005, TestSize.Level1) want.SetElement(element); want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "false"); - int userId = 100; StartAbilityUtils::skipErms = true; + int userId = 100; bool result = interceptor->DoProcess(want, userId); EXPECT_TRUE(result); StartAbilityUtils::skipErms = false; } + +/** + * @tc.name: DoProcess_006 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_006, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + int requestCode = 0; + StartAbilityUtils::skipErms = false; + int userId = 100; + auto shouldBlockFunc = []() { return false; }; + AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr, + shouldBlockFunc); + param.isStartAsCaller = true; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = -1; + ErrCode result = interceptor->DoProcess(param); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: DoProcess_007 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_007, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + int requestCode = 0; + StartAbilityUtils::skipErms = false; + int userId = 100; + auto shouldBlockFunc = []() { return false; }; + AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr, + shouldBlockFunc); + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = ERMS_ISALLOW_RESULTCODE; + ErrCode result = interceptor->DoProcess(param); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: DoProcess_008 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_008, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + int requestCode = 0; + StartAbilityUtils::skipErms = false; + int userId = 100; + auto shouldBlockFunc = []() { return false; }; + AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr, + shouldBlockFunc); + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = -1; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "false"); + ErrCode result = interceptor->DoProcess(param); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: DoProcess_009 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_009, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + int requestCode = 0; + StartAbilityUtils::skipErms = false; + int userId = 100; + auto shouldBlockFunc = []() { return false; }; + AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, true, nullptr, + shouldBlockFunc); + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = -1; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.replaceWant = + sptr::MakeSptr(); + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.replaceWant->SetElementName( + "bundleName", "abilityName"); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + ErrCode result = interceptor->DoProcess(param); +#ifdef SUPPORT_GRAPHICS + EXPECT_EQ(want.GetElement().GetBundleName(), "bundleName"); + EXPECT_EQ(want.GetElement().GetAbilityName(), "abilityName"); + EXPECT_EQ(want.GetBoolParam("queryWantFromErms", false), true); +#endif + EXPECT_EQ(result, ERR_ECOLOGICAL_CONTROL_STATUS); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "false"); +} + +/** + * @tc.name: DoProcess_010 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_010, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + int requestCode = 0; + StartAbilityUtils::skipErms = false; + int userId = 100; + auto shouldBlockFunc = []() { return false; }; + AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, true, nullptr, + shouldBlockFunc); + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = -1; + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "false"); + ErrCode result = interceptor->DoProcess(param); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: DoProcess_012 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_012, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = true; + int userId = 100; + bool result = interceptor->DoProcess(want, userId); + EXPECT_FALSE(result); +} + +/** + * @tc.name: DoProcess_013 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_013, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + BundleMgrHelper::retGetLaunchWantForBundle = -1; + int userId = 100; + bool result = interceptor->DoProcess(want, userId); + EXPECT_FALSE(result); +} + +/** + * @tc.name: DoProcess_014 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_014, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + std::string bundleName = "bundleName"; + std::string abilityName = "abilityName"; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + BundleMgrHelper::launchWant.SetElementName(bundleName, abilityName); + StartAbilityInfo::createStartAbilityInfo = nullptr; + int userId = 100; + bool result = interceptor->DoProcess(want, userId); + EXPECT_FALSE(result); +} + +/** + * @tc.name: DoProcess_015 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_015, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + std::string bundleName = "bundleName"; + std::string abilityName = "abilityName"; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + BundleMgrHelper::launchWant.SetElementName(bundleName, abilityName); + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = -1; + int userId = 100; + bool result = interceptor->DoProcess(want, userId); + EXPECT_FALSE(result); +} + +/** + * @tc.name: DoProcess_016 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_016, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + std::string bundleName = "bundleName"; + std::string abilityName = "abilityName"; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + BundleMgrHelper::launchWant.SetElementName(bundleName, abilityName); + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = 0; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = -1; + int userId = 100; + bool result = interceptor->DoProcess(want, userId); + EXPECT_TRUE(result); +} + +/** + * @tc.name: DoProcess_017 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_017, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + std::string bundleName = "bundleName"; + std::string abilityName = "abilityName"; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + BundleMgrHelper::launchWant.SetElementName(bundleName, abilityName); + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = 0; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = 0; + int userId = 100; + bool result = interceptor->DoProcess(want, userId); + EXPECT_FALSE(result); +} + +/** + * @tc.name: DoProcess_018 + * @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names + * @tc.type: FUNC + * @tc.require: No + */ +HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_018, TestSize.Level1) +{ + std::shared_ptr interceptor = std::make_shared(); + Want want; + std::string targetBundleName = "com.example.target"; + std::string callerBundleName = "com.example.caller"; + ElementName element("", targetBundleName, "MainAbility"); + want.SetElement(element); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName); + OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "true"); + BundleMgrHelper::isBundleManagerHelperNull = false; + std::string bundleName = "bundleName"; + std::string abilityName = "abilityName"; + BundleMgrHelper::retGetLaunchWantForBundle = 0; + BundleMgrHelper::launchWant.SetElementName(bundleName, abilityName); + StartAbilityInfo::createStartAbilityInfo = std::make_shared(); + StartAbilityInfo::createStartAbilityInfo->status = 0; + AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; + AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule.resultCode = ERMS_ISALLOW_RESULTCODE; + int userId = 100; + bool result = interceptor->DoProcess(want, userId); + EXPECT_TRUE(result); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ecological_rule_interceptor_test/mock/ability_record.cpp b/test/unittest/ecological_rule_interceptor_test/mock/ability_record.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c0551fffad7127a299725023840cee1785942f3 --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/ability_record.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ability_record.h" + +namespace OHOS { +namespace AAFwk { +std::shared_ptr Token::abilityRecord = nullptr; +AppExecFwk::AbilityInfo AbilityRecord::abilityInfo; + +Token::Token() +{} + +Token::~Token() +{} + +std::shared_ptr Token::GetAbilityRecordByToken(sptr token) +{ + return abilityRecord; +} + +AbilityRecord::AbilityRecord() +{} + +AbilityRecord::~AbilityRecord() +{} + +const AppExecFwk::AbilityInfo &AbilityRecord::GetAbilityInfo() const +{ + return abilityInfo; +} +} // namespace AAFwk +} // namespace OHOS diff --git a/test/unittest/ecological_rule_interceptor_test/mock/ability_record.h b/test/unittest/ecological_rule_interceptor_test/mock/ability_record.h new file mode 100644 index 0000000000000000000000000000000000000000..d29e9aaa5bc16183b5b9cf258114bff741a8dd2f --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/ability_record.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_ABILITY_RECORD_H +#define OHOS_ABILITY_RUNTIME_ABILITY_RECORD_H + +#include + +#include "ability_info.h" +#include "ability_token_stub.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace AAFwk { +class AbilityRecord; + +/** + * @class Token + * Token is identification of ability and used to interact with kit and wms. + */ +class Token : public AbilityTokenStub { +public: + Token(); + virtual ~Token(); + + static std::shared_ptr GetAbilityRecordByToken(sptr token); + +public: + static std::shared_ptr abilityRecord; +}; + + +/** + * @class AbilityRecord + * AbilityRecord records ability info and states and used to schedule ability life. + */ +class AbilityRecord : public std::enable_shared_from_this { +public: + AbilityRecord(); + + ~AbilityRecord(); + + /** + * get ability's info. + * + * @return ability info. + */ + const AppExecFwk::AbilityInfo &GetAbilityInfo() const; + +public: + static AppExecFwk::AbilityInfo abilityInfo; +}; +} // namespace AAFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_ABILITY_RECORD_H diff --git a/test/unittest/ecological_rule_interceptor_test/mock/ability_util.h b/test/unittest/ecological_rule_interceptor_test/mock/ability_util.h new file mode 100644 index 0000000000000000000000000000000000000000..fec7dd4cd67723ae079462ec4e92db6098a691ff --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/ability_util.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H +#define OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H + +#include "bundle_mgr_helper.h" +#include "hilog_tag_wrapper.h" +#include "in_process_call_wrapper.h" + +namespace OHOS { +namespace AAFwk { +namespace AbilityUtil { +#define CHECK_POINTER_CONTINUE(object) \ + if (!object) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "nullptr"); \ + continue; \ + } + +#define CHECK_POINTER_IS_NULLPTR(object) \ + if (object == nullptr) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "nullptr"); \ + return; \ + } + +#define CHECK_POINTER(object) \ + if (!object) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "nullptr"); \ + return; \ + } + +#define CHECK_POINTER_LOG(object, log) \ + if (!object) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s:", log); \ + return; \ + } + +#define CHECK_POINTER_TAG_LOG(object, tag, log) \ + if (!object) { \ + TAG_LOGE(tag, "%{public}s:", log); \ + return; \ + } + +#define CHECK_POINTER_AND_RETURN(object, value) \ + if (!object) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "nullptr"); \ + return value; \ + } + +#define CHECK_POINTER_AND_RETURN_LOG(object, value, log) \ + if (!object) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s:", log); \ + return value; \ + } + +#define CHECK_POINTER_RETURN_BOOL(object) \ + if (!object) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "nullptr"); \ + return false; \ + } + +#define CHECK_RET_RETURN_RET(object, log) \ + if (object != ERR_OK) { \ + TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s, ret : %{public}d", log, object); \ + return object; \ + } + +#define CHECK_TRUE_RETURN_RET(object, value, log) \ + if (object) { \ + TAG_LOGW(AAFwkTag::ABILITYMGR, "%{public}s", log); \ + return value; \ + } + +[[maybe_unused]] static std::shared_ptr GetBundleManagerHelper() +{ + return AppExecFwk::BundleMgrHelper::GetInstance(); +} +} // namespace AbilityUtil +} // namespace AAFwk +} // namespace OHOS + +#endif // OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H diff --git a/test/unittest/ecological_rule_interceptor_test/mock/bundle_mgr_helper.cpp b/test/unittest/ecological_rule_interceptor_test/mock/bundle_mgr_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5d1d1686155da94f1c4c4ba55c299640c1e782a --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/bundle_mgr_helper.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bundle_mgr_helper.h" + +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { +int32_t BundleMgrHelper::retGetLaunchWantForBundle = 0; +Want BundleMgrHelper::launchWant; +int32_t BundleMgrHelper::retGetNameForUid = 0; +std::string BundleMgrHelper::nameForUid; +bool BundleMgrHelper::retGetApplicationInfo = true; +ApplicationInfo BundleMgrHelper::applicationInfo; +bool BundleMgrHelper::isBundleManagerHelperNull = false; + +BundleMgrHelper::BundleMgrHelper() +{} + +BundleMgrHelper::~BundleMgrHelper() +{} + +std::shared_ptr BundleMgrHelper::GetInstance() +{ + if (isBundleManagerHelperNull) { + return nullptr; + } + static std::shared_ptr instance = std::make_shared(); + return instance; +} + +int32_t BundleMgrHelper::GetLaunchWantForBundle(const std::string &bundleName, Want &want, int32_t userId) +{ + want = launchWant; + return retGetLaunchWantForBundle; +} + +int32_t BundleMgrHelper::GetNameForUid(const int32_t uid, std::string &name) +{ + name = nameForUid; + return retGetNameForUid; +} + +bool BundleMgrHelper::GetApplicationInfo( + const std::string &appName, const ApplicationFlag flag, const int32_t userId, ApplicationInfo &appInfo) +{ + appInfo = applicationInfo; + return retGetApplicationInfo; +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/ecological_rule_interceptor_test/mock/bundle_mgr_helper.h b/test/unittest/ecological_rule_interceptor_test/mock/bundle_mgr_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..abf8e2c97cf5302d8ad7f844dd6ed94647d50636 --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/bundle_mgr_helper.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_BUNDLE_MGR_HELPER_H +#define OHOS_ABILITY_RUNTIME_BUNDLE_MGR_HELPER_H + +#include "bundle_mgr_interface.h" + +namespace OHOS { +namespace AppExecFwk { +using Want = OHOS::AAFwk::Want; + +class BundleMgrHelper { +public: + ~BundleMgrHelper(); + + static std::shared_ptr GetInstance(); + + int32_t GetLaunchWantForBundle(const std::string &bundleName, Want &want, int32_t userId); + + int32_t GetNameForUid(int32_t uid, std::string &name); + bool GetApplicationInfo( + const std::string &appName, ApplicationFlag flag, int32_t userId, ApplicationInfo &appInfo); + + int32_t GetCloneAppIndexes(const std::string &bundleName, std::vector &appIndexes, int32_t userId) + { + return 0; + } + + int32_t QueryCloneAbilityInfo(const ElementName &element, int32_t flags, int32_t appCloneIndex, + AbilityInfo &abilityInfo, int32_t userId) + { + return 0; + } + + int32_t GetPluginAbilityInfo(const std::string &hostBundleName, const std::string &pluginBundleName, + const std::string &pluginModuleName, const std::string &pluginAbilityName, int32_t userId, + AbilityInfo &pluginAbilityInfo) + { + return 0; + } + + bool QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo) + { + return true; + } + + int32_t GetSandboxAbilityInfo(const Want &want, int32_t appIndex, int32_t flags, int32_t userId, + AbilityInfo &abilityInfo) + { + return 0; + } + + bool QueryExtensionAbilityInfos(const Want &want, const int32_t &flag, int32_t &userId, + std::vector &extensionInfos) + { + return true; + } + + int32_t GetSandboxExtAbilityInfos(const Want &want, int32_t appIndex, int32_t flags, int32_t userId, + std::vector &extensionInfos) + { + return 0; + } + + int32_t QueryCloneExtensionAbilityInfoWithAppIndex(const ElementName &element, int32_t flags, int32_t appCloneIndex, + ExtensionAbilityInfo &extensionInfo, int32_t userId) + { + return 0; + } + +public: + static int32_t retGetLaunchWantForBundle; + static Want launchWant; + static int32_t retGetNameForUid; + static std::string nameForUid; + static bool retGetApplicationInfo; + static ApplicationInfo applicationInfo; + static bool isBundleManagerHelperNull; + +private: + BundleMgrHelper(); +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_BUNDLE_MGR_HELPER_H \ No newline at end of file diff --git a/test/unittest/ecological_rule_interceptor_test/mock/ecological_rule/ability_ecological_rule_mgr_service.cpp b/test/unittest/ecological_rule_interceptor_test/mock/ecological_rule/ability_ecological_rule_mgr_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65aa7409165e65dd6fa6ff8a4d34b0a94c065391 --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/ecological_rule/ability_ecological_rule_mgr_service.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ecological_rule/ability_ecological_rule_mgr_service.h" + +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace EcologicalRuleMgrService { +int32_t AbilityEcologicalRuleMgrServiceClient::retQueryStartExperience = 0; +AbilityExperienceRule AbilityEcologicalRuleMgrServiceClient::queryStartExperienceRule; + +AbilityEcologicalRuleMgrServiceClient::~AbilityEcologicalRuleMgrServiceClient() +{} + +sptr AbilityEcologicalRuleMgrServiceClient::GetInstance() +{ + static sptr instance_ = new AbilityEcologicalRuleMgrServiceClient; + return instance_; +} + +int32_t AbilityEcologicalRuleMgrServiceClient::QueryStartExperience(const OHOS::AAFwk::Want &want, + const AbilityCallerInfo &callerInfo, AbilityExperienceRule &rule) +{ + rule = queryStartExperienceRule; + return retQueryStartExperience; +} +} // namespace EcologicalRuleMgrService +} // namespace OHOS diff --git a/test/unittest/ecological_rule_interceptor_test/mock/ecological_rule/ability_ecological_rule_mgr_service.h b/test/unittest/ecological_rule_interceptor_test/mock/ecological_rule/ability_ecological_rule_mgr_service.h new file mode 100644 index 0000000000000000000000000000000000000000..a40685e0b2b91e4dfc7234bdbc908bd113b9fef4 --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/ecological_rule/ability_ecological_rule_mgr_service.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SERVICES_INCLUDE_ECOLOGICAL_RULE_MANAGER_SERVICE_PROXY_H +#define SERVICES_INCLUDE_ECOLOGICAL_RULE_MANAGER_SERVICE_PROXY_H + +#include "ability_ecological_rule_mgr_service_interface.h" + +namespace OHOS { +namespace EcologicalRuleMgrService { + +using namespace std; +using Want = OHOS::AAFwk::Want; + +class AbilityEcologicalRuleMgrServiceClient : public RefBase { +public: + DISALLOW_COPY_AND_MOVE(AbilityEcologicalRuleMgrServiceClient); + static sptr GetInstance(); + int32_t QueryStartExperience(const Want &want, const AbilityCallerInfo &callerInfo, AbilityExperienceRule &rule); + +public: + static int32_t retQueryStartExperience; + static AbilityExperienceRule queryStartExperienceRule; + +private: + AbilityEcologicalRuleMgrServiceClient() {}; + ~AbilityEcologicalRuleMgrServiceClient(); +}; +} // namespace EcologicalRuleMgrService +} // namespace OHOS + +#endif // SERVICES_INCLUDE_ECOLOGICAL_RULE_MANAGER_SERVICE_PROXY_H diff --git a/test/unittest/ecological_rule_interceptor_test/mock/start_ability_utils.cpp b/test/unittest/ecological_rule_interceptor_test/mock/start_ability_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae13377cf803b9e5987d12fae16f2d270988b605 --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/start_ability_utils.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "start_ability_utils.h" + +namespace OHOS { +namespace AAFwk { +namespace { +constexpr int32_t ERMS_ISALLOW_RESULTCODE = 10; +} +std::shared_ptr StartAbilityUtils::startAbilityInfo; +std::shared_ptr StartAbilityUtils::callerAbilityInfo; +bool StartAbilityUtils::skipCrowTest = false; +bool StartAbilityUtils::skipStartOther = false; +bool StartAbilityUtils::skipErms = false; +int32_t StartAbilityUtils::ermsResultCode = ERMS_ISALLOW_RESULTCODE; +bool StartAbilityUtils::isWantWithAppCloneIndex = false; +bool StartAbilityUtils::ermsSupportBackToCallerFlag = false; +bool StartAbilityUtils::retGetCallerAbilityInfo; +AppExecFwk::AbilityInfo StartAbilityUtils::callerAbiltyInfo; + +std::shared_ptr StartAbilityInfo::createStartAbilityInfo = nullptr; + +std::shared_ptr StartAbilityInfo::CreateStartAbilityInfo(const Want &want, int32_t userId, + int32_t appIndex, sptr callerToken) +{ + return createStartAbilityInfo; +} + +bool StartAbilityUtils::GetCallerAbilityInfo(const sptr &callerToken, + AppExecFwk::AbilityInfo &abilityInfo) +{ + abilityInfo = callerAbiltyInfo; + return retGetCallerAbilityInfo; +} +} +} \ No newline at end of file diff --git a/test/unittest/ecological_rule_interceptor_test/mock/start_ability_utils.h b/test/unittest/ecological_rule_interceptor_test/mock/start_ability_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..ba1a8daeb37b5929ba47e9f44f582461c3b017b3 --- /dev/null +++ b/test/unittest/ecological_rule_interceptor_test/mock/start_ability_utils.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_START_ABILITY_UTILS_H +#define OHOS_ABILITY_RUNTIME_START_ABILITY_UTILS_H + +#include +#include + +#include "ability_info.h" +#include "extension_ability_info.h" +#include "want.h" + +namespace OHOS { +namespace AAFwk { + +struct StartAbilityInfo { + static std::shared_ptr CreateStartAbilityInfo(const Want &want, int32_t userId, + int32_t appIndex, sptr callerToken); + + static std::shared_ptr createStartAbilityInfo; + int32_t status = ERR_OK; + AppExecFwk::AbilityInfo abilityInfo; + AppExecFwk::ExtensionProcessMode extensionProcessMode = AppExecFwk::ExtensionProcessMode::UNDEFINED; + std::string customProcess; +}; + +struct StartAbilityUtils { + static bool GetCallerAbilityInfo(const sptr &callerToken, + AppExecFwk::AbilityInfo &abilityInfo); + + static std::shared_ptr startAbilityInfo; + static std::shared_ptr callerAbilityInfo; + static bool skipCrowTest; + static bool skipStartOther; + static bool skipErms; + static int32_t ermsResultCode; + static bool isWantWithAppCloneIndex; + static bool ermsSupportBackToCallerFlag; + static bool retGetCallerAbilityInfo; + static AppExecFwk::AbilityInfo callerAbiltyInfo; +}; +} +} +#endif // OHOS_ABILITY_RUNTIME_START_ABILITY_UTILS_H \ No newline at end of file