From 862246aada3dbc28aafb22a3828bb5a13f8487ac Mon Sep 17 00:00:00 2001 From: litao Date: Mon, 6 Jan 2025 14:48:46 +0800 Subject: [PATCH] add GetLocalAbilityManagerProxy Signed-off-by: litao --- .../source/system_ability_manager_proxy.cpp | 31 +++++++++++- .../include/if_local_ability_manager.h | 2 + .../include/local_ability_manager_proxy.h | 1 + .../include/safwk_ipc_interface_code.h | 1 + .../include/samgr_ipc_interface_code.h | 1 + .../src/local_ability_manager_proxy.cpp | 42 +++++++++++++++++ .../native/include/system_ability_manager.h | 7 ++- .../include/system_ability_manager_stub.h | 6 +++ .../source/system_ability_manager_stub.cpp | 33 ++++++++++++- .../src/local_ability_manager_proxy_test.cpp | 38 +++++++++++++++ .../src/system_ability_mgr_proxy_test.cpp | 26 ++++++++++ .../src/system_ability_mgr_stub_test.cpp | 47 +++++++++++++++++++ .../unittest/src/system_ability_mgr_test.cpp | 43 +++++++++++++++++ 13 files changed, 275 insertions(+), 3 deletions(-) diff --git a/frameworks/native/source/system_ability_manager_proxy.cpp b/frameworks/native/source/system_ability_manager_proxy.cpp index 98e550f9..db7598f8 100644 --- a/frameworks/native/source/system_ability_manager_proxy.cpp +++ b/frameworks/native/source/system_ability_manager_proxy.cpp @@ -1561,6 +1561,35 @@ int32_t SystemAbilityManagerProxy::GetCommonEventExtraDataIdlist(int32_t saId, s sptr SystemAbilityManagerProxy::GetLocalAbilityManagerProxy(int32_t systemAbilityId) { - return nullptr; + if (!CheckInputSysAbilityId(systemAbilityId)) { + HILOGW("GetLocalAbilityManagerProxy SA invalid:%{public}d!", systemAbilityId); + return nullptr; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) { + HILOGE("GetLocalAbilityManagerProxy write token failed!"); + return nullptr; + } + if (!data.WriteInt32(systemAbilityId)) { + HILOGE("GetLocalAbilityManagerProxy write said failed!"); + return nullptr; + } + + auto remote = Remote(); + if (remote == nullptr) { + HILOGI("GetLocalAbilityManagerProxy remote is nullptr"); + return nullptr; + } + + MessageParcel reply; + MessageOption option; + int32_t err = remote->SendRequest( + static_cast(SamgrInterfaceCode::GET_LOCAL_ABILITY_MANAGER_PROXY_TRANSCATION), data, reply, option); + if (err != ERR_NONE) { + HILOGE("GetLocalAbilityManagerProxy SendRequest error: %{public}d!", err); + return nullptr; + } + return reply.ReadRemoteObject(); } } // namespace OHOS diff --git a/interfaces/innerkits/samgr_proxy/include/if_local_ability_manager.h b/interfaces/innerkits/samgr_proxy/include/if_local_ability_manager.h index 2f9dccfa..c20bcd36 100755 --- a/interfaces/innerkits/samgr_proxy/include/if_local_ability_manager.h +++ b/interfaces/innerkits/samgr_proxy/include/if_local_ability_manager.h @@ -69,6 +69,8 @@ public: virtual bool FfrtDumperProc(std::string& result) = 0; virtual int32_t SystemAbilityExtProc(const std::string& extension, int32_t said, SystemAbilityExtensionPara* callback, bool isAsync = false) = 0; + virtual int32_t ServiceControlCmd(int32_t fd, int32_t systemAbilityId, + const std::vector& args) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ILocalAbilityManager"); protected: static inline const std::u16string LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN = u"ohos.localabilitymanager.accessToken"; diff --git a/interfaces/innerkits/samgr_proxy/include/local_ability_manager_proxy.h b/interfaces/innerkits/samgr_proxy/include/local_ability_manager_proxy.h index 7fb3a630..7613b559 100755 --- a/interfaces/innerkits/samgr_proxy/include/local_ability_manager_proxy.h +++ b/interfaces/innerkits/samgr_proxy/include/local_ability_manager_proxy.h @@ -41,6 +41,7 @@ public: bool FfrtDumperProc(std::string& ffrtDumperInfo); int32_t SystemAbilityExtProc(const std::string& extension, int32_t said, SystemAbilityExtensionPara* callback, bool isAsync = false); + int32_t ServiceControlCmd(int32_t fd, int32_t systemAbilityId, const std::vector& args); private: static inline BrokerDelegator delegator_; bool PrepareData(MessageParcel& data, int32_t said, const std::string& extension); diff --git a/interfaces/innerkits/samgr_proxy/include/safwk_ipc_interface_code.h b/interfaces/innerkits/samgr_proxy/include/safwk_ipc_interface_code.h index 708df087..c04bd340 100755 --- a/interfaces/innerkits/samgr_proxy/include/safwk_ipc_interface_code.h +++ b/interfaces/innerkits/samgr_proxy/include/safwk_ipc_interface_code.h @@ -27,6 +27,7 @@ enum SafwkInterfaceCode : uint32_t { IPC_STAT_CMD_TRANSACTION = 6, FFRT_DUMPER_TRANSACTION = 7, SYSTEM_ABILITY_EXT_TRANSACTION = 8, + SERVICE_CONTROL_CMD_TRANSACTION = 9, }; } #endif // !defined(SAFWK_IPC_INTERFACE_CODE_H) \ No newline at end of file diff --git a/interfaces/innerkits/samgr_proxy/include/samgr_ipc_interface_code.h b/interfaces/innerkits/samgr_proxy/include/samgr_ipc_interface_code.h index 5ca883b6..a37feca4 100644 --- a/interfaces/innerkits/samgr_proxy/include/samgr_ipc_interface_code.h +++ b/interfaces/innerkits/samgr_proxy/include/samgr_ipc_interface_code.h @@ -50,6 +50,7 @@ enum class SamgrInterfaceCode : uint32_t { GET_EXTERNSION_SA_LIST_TRANSCATION = 34, GET_SA_EXTENSION_INFO_TRANSCATION = 35, GET_COMMON_EVENT_EXTRA_ID_LIST_TRANSCATION = 36, + GET_LOCAL_ABILITY_MANAGER_PROXY_TRANSCATION = 37, }; } // namespace OHOS #endif // !defined(INTERFACES_INNERKITS_SAMGR_INCLUDE_SAMGR_INTERFACE_CODE_H) \ No newline at end of file diff --git a/services/lsamgr/src/local_ability_manager_proxy.cpp b/services/lsamgr/src/local_ability_manager_proxy.cpp index 8ae8dee6..e578a322 100644 --- a/services/lsamgr/src/local_ability_manager_proxy.cpp +++ b/services/lsamgr/src/local_ability_manager_proxy.cpp @@ -401,4 +401,46 @@ bool LocalAbilityManagerProxy::PrepareData(MessageParcel& data, int32_t said, co } return true; } + +int32_t LocalAbilityManagerProxy::ServiceControlCmd(int32_t fd, int32_t systemAbilityId, + const std::vector& args) +{ + if (systemAbilityId <= 0) { + HILOG_WARN(LOG_CORE, "ServiceControlCmd systemAbilityId invalid."); + return INVALID_DATA; + } + + sptr iro = Remote(); + if (iro == nullptr) { + HILOG_ERROR(LOG_CORE, "ServiceControlCmd remote return null"); + return OBJECT_NULL; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) { + HILOG_WARN(LOG_CORE, "ServiceControlCmd interface token check failed"); + return INVALID_DATA; + } + if (!data.WriteInt32(systemAbilityId)) { + HILOG_WARN(LOG_CORE, "ServiceControlCmd write systemAbilityId failed"); + return INVALID_DATA; + } + if (!data.WriteFileDescriptor(fd)) { + HILOG_WARN(LOG_CORE, "ServiceControlCmd write fd failed"); + return INVALID_DATA; + } + if (!data.WriteString16Vector(args)) { + HILOG_WARN(LOG_CORE, "ServiceControlCmd write args failed"); + return INVALID_DATA; + } + + MessageParcel reply; + MessageOption option; + int32_t status = iro->SendRequest( + static_cast(SafwkInterfaceCode::SERVICE_CONTROL_CMD_TRANSACTION), data, reply, option); + if (status != NO_ERROR) { + HILOG_ERROR(LOG_CORE, "ServiceControlCmd SendRequest failed, return value : %{public}d", status); + } + return status; +} } diff --git a/services/samgr/native/include/system_ability_manager.h b/services/samgr/native/include/system_ability_manager.h index 0a3cd1bc..715d708d 100644 --- a/services/samgr/native/include/system_ability_manager.h +++ b/services/samgr/native/include/system_ability_manager.h @@ -231,7 +231,12 @@ public: sptr GetSystemProcess(const std::u16string& procName); sptr GetLocalAbilityManagerProxy(int32_t systemAbilityId) override { - return nullptr; + CommonSaProfile saProfile; + if (!GetSaProfile(systemAbilityId, saProfile)) { + HILOGD("SA:%{public}d no profile!", systemAbilityId); + return nullptr; + } + return GetSystemProcess(saProfile.process); } bool IsModuleUpdate(int32_t systemAbilityId) { diff --git a/services/samgr/native/include/system_ability_manager_stub.h b/services/samgr/native/include/system_ability_manager_stub.h index 0d16d3b1..837284a5 100644 --- a/services/samgr/native/include/system_ability_manager_stub.h +++ b/services/samgr/native/include/system_ability_manager_stub.h @@ -179,6 +179,11 @@ private: { return stub->GetCommonEventExtraDataIdlistInner(data, reply); } + static int32_t LocalGetLocalAbilityManagerProxy(SystemAbilityManagerStub* stub, + MessageParcel& data, MessageParcel& reply) + { + return stub->GetLocalAbilityManagerProxyInner(data, reply); + } int32_t ListSystemAbilityInner(MessageParcel& data, MessageParcel& reply); int32_t SubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply); int32_t UnSubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply); @@ -209,6 +214,7 @@ private: int32_t GetExtensionRunningSaListInner(MessageParcel& data, MessageParcel& reply); int32_t GetRunningSaExtensionInfoListInner(MessageParcel& data, MessageParcel& reply); int32_t GetCommonEventExtraDataIdlistInner(MessageParcel& data, MessageParcel& reply); + int32_t GetLocalAbilityManagerProxyInner(MessageParcel& data, MessageParcel& reply); static int32_t GetHapIdMultiuser(int32_t uid); void SetAbilityFuncMap(); void SetProcessFuncMap(); diff --git a/services/samgr/native/source/system_ability_manager_stub.cpp b/services/samgr/native/source/system_ability_manager_stub.cpp index 0a4dd921..b5aadc86 100644 --- a/services/samgr/native/source/system_ability_manager_stub.cpp +++ b/services/samgr/native/source/system_ability_manager_stub.cpp @@ -105,7 +105,8 @@ namespace { using namespace OHOS::Security; namespace OHOS { namespace { -constexpr const char *EXT_TRANSACTION_PERMISSION = "ohos.permission.ACCESS_EXT_SYSTEM_ABILITY"; +const std::string EXT_TRANSACTION_PERMISSION = "ohos.permission.ACCESS_EXT_SYSTEM_ABILITY"; +const std::string PERMISSION_SVC = "ohos.permission.CONTROL_SVC_CMD"; } void SystemAbilityManagerStub::SetAbilityFuncMap() @@ -178,6 +179,8 @@ SystemAbilityManagerStub::SystemAbilityManagerStub() SystemAbilityManagerStub::LocalGetRunningSaExtensionInfoList; memberFuncMap_[static_cast(SamgrInterfaceCode::GET_COMMON_EVENT_EXTRA_ID_LIST_TRANSCATION)] = SystemAbilityManagerStub::LocalGetCommonEventExtraDataIdlist; + memberFuncMap_[static_cast(SamgrInterfaceCode::GET_LOCAL_ABILITY_MANAGER_PROXY_TRANSCATION)] = + SystemAbilityManagerStub::LocalGetLocalAbilityManagerProxy; } int32_t SystemAbilityManagerStub::OnRemoteRequest(uint32_t code, @@ -1241,4 +1244,32 @@ int32_t SystemAbilityManagerStub::GetCommonEventExtraDataIdlistInner(MessageParc } return ERR_NONE; } + +int32_t SystemAbilityManagerStub::GetLocalAbilityManagerProxyInner(MessageParcel& data, MessageParcel& reply) +{ + if (!CheckPermission(PERMISSION_SVC)) { + HILOGE("GetLocalSystemAbilityProxyInner permission denied! CallSid:%{public}s", + OHOS::IPCSkeleton::GetCallingSid().c_str()); + return ERR_PERMISSION_DENIED; + } + + int32_t systemAbilityId = -1; + bool ret = data.ReadInt32(systemAbilityId); + if (!ret || !CheckInputSysAbilityId(systemAbilityId)) { + HILOGE("GetLocalSystemAbilityProxyInner get SAId failed."); + return ERR_NULL_OBJECT; + } + + sptr remoteObject = GetLocalAbilityManagerProxy(systemAbilityId); + if (remoteObject == nullptr) { + HILOGE("GetLocalSystemAbilityProxyInner SA:%{public}d GetLocalSystemAbilityProxy failed.", systemAbilityId); + return ERR_NULL_OBJECT; + } + ret = reply.WriteRemoteObject(remoteObject); + if (!ret) { + HILOGE("GetLocalSystemAbilityProxyInner SA:%{public}d write reply failed.", systemAbilityId); + return ERR_FLATTEN_OBJECT; + } + return ERR_NONE; +} } // namespace OHOS diff --git a/services/samgr/native/test/unittest/src/local_ability_manager_proxy_test.cpp b/services/samgr/native/test/unittest/src/local_ability_manager_proxy_test.cpp index c191b23c..c208d8ce 100644 --- a/services/samgr/native/test/unittest/src/local_ability_manager_proxy_test.cpp +++ b/services/samgr/native/test/unittest/src/local_ability_manager_proxy_test.cpp @@ -424,4 +424,42 @@ HWTEST_F(LocalAbilityManagerProxyTest, SystemAbilityExtProc004, TestSize.Level3) int32_t result = localAbility->SystemAbilityExtProc("OnBackup", TEST_SAID_INVAILD, &callback, true); EXPECT_EQ(result, INVALID_DATA); } + +/** + * @tc.name: ServiceControlCmd001 + * @tc.desc: test ServiceControlCmd001 + * @tc.type: FUNC + * @tc.require: I9IE98 + */ +HWTEST_F(LocalAbilityManagerProxyTest, ServiceControlCmd001, TestSize.Level3) +{ + sptr testAbility(new MockIroSendrequesteStub()); + sptr localAbility(new LocalAbilityManagerProxy(testAbility)); + + int32_t fd = 1; + int32_t systemAbilityId = -1; + std::vector args (1, Str8ToStr16(std::string("help"))); + + int32_t result = localAbility->ServiceControlCmd(fd, systemAbilityId, args); + EXPECT_EQ(result, INVALID_DATA); +} + +/** + * @tc.name: ServiceControlCmd002 + * @tc.desc: test ServiceControlCmd002 + * @tc.type: FUNC + * @tc.require: I9IE98 + */ +HWTEST_F(LocalAbilityManagerProxyTest, ServiceControlCmd002, TestSize.Level3) +{ + sptr testAbility(new MockIroSendrequesteStub()); + sptr localAbility(new LocalAbilityManagerProxy(testAbility)); + + int32_t fd = 1; + int32_t systemAbilityId = 1; + std::vector args (1, Str8ToStr16(std::string("help"))); + + int32_t result = localAbility->ServiceControlCmd(fd, systemAbilityId, args); + EXPECT_EQ(result, NO_ERROR); +} } \ No newline at end of file diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_proxy_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_proxy_test.cpp index 46b7e501..89c2da27 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_proxy_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_proxy_test.cpp @@ -1259,4 +1259,30 @@ HWTEST_F(SystemAbilityMgrProxyTest, OnLoadSystemAbilityFail005, TestSize.Level3) callback->OnLoadSystemAbilityFail(-1); EXPECT_EQ(nullptr, callback->loadproxy_); } + +/** + * @tc.name: GetLocalAbilityManagerProxy001 + * @tc.desc: test GetLocalAbilityManagerProxy + * @tc.type: FUNC + */ +HWTEST_F(SystemAbilityMgrProxyTest, GetLocalAbilityManagerProxy001, TestSize.Level3) +{ + sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + + auto ret = samgrProxy->GetLocalAbilityManagerProxy(TEST_ID_NORANGE_SAID); + EXPECT_TRUE(ret == nullptr); +} + +/** + * @tc.name: GetLocalAbilityManagerProxy002 + * @tc.desc: test GetLocalAbilityManagerProxy + * @tc.type: FUNC + */ +HWTEST_F(SystemAbilityMgrProxyTest, GetLocalAbilityManagerProxy002, TestSize.Level3) +{ + sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + + auto ret = samgrProxy->GetLocalAbilityManagerProxy(TEST_SAID_INVALID); + EXPECT_TRUE(ret == nullptr); +} } \ No newline at end of file diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_stub_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_stub_test.cpp index c1b00a6e..4ffbfae4 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_stub_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_stub_test.cpp @@ -808,4 +808,51 @@ HWTEST_F(SystemAbilityMgrStubTest, SendStrategyInner001, TestSize.Level3) int32_t result = saMgr->SendStrategyInner(data, reply); EXPECT_EQ(result, ERR_FLATTEN_OBJECT); } + +/** + * @tc.name: GetLocalAbilityManagerProxyInner001 + * @tc.desc: test GetLocalAbilityManagerProxyInner + * @tc.type: FUNC + * @tc.require: I6XB42 + */ +HWTEST_F(SystemAbilityMgrStubTest, GetLocalAbilityManagerProxyInner001, TestSize.Level3) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + MessageParcel data; + MessageParcel reply; + int32_t ret = saMgr->GetLocalAbilityManagerProxyInner(data, reply); + EXPECT_EQ(ret, ERR_NULL_OBJECT); +} + +/** + * @tc.name: GetLocalAbilityManagerProxyInner002 + * @tc.desc: test GetLocalAbilityManagerProxyInner + * @tc.type: FUNC + * @tc.require: I6XB42 + */ +HWTEST_F(SystemAbilityMgrStubTest, GetLocalAbilityManagerProxyInner002, TestSize.Level3) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + MessageParcel data; + MessageParcel reply; + EXPECT_TRUE(data.WriteInt32(INVALID_SAID)); + int32_t ret = saMgr->GetLocalAbilityManagerProxyInner(data, reply); + EXPECT_EQ(ret, ERR_NULL_OBJECT); +} + +/** + * @tc.name: GetLocalAbilityManagerProxyInner003 + * @tc.desc: test GetLocalAbilityManagerProxyInner + * @tc.type: FUNC + * @tc.require: I6XB42 + */ +HWTEST_F(SystemAbilityMgrStubTest, GetLocalAbilityManagerProxyInner003, TestSize.Level3) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + MessageParcel data; + MessageParcel reply; + EXPECT_TRUE(data.WriteInt32(SAID)); + int32_t ret = saMgr->GetLocalAbilityManagerProxyInner(data, reply); + EXPECT_EQ(ret, ERR_NULL_OBJECT); +} } \ No newline at end of file diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp index 69743254..f8fa4909 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp @@ -1623,4 +1623,47 @@ HWTEST_F(SystemAbilityMgrTest, OnRemoteDied002, TestSize.Level3) saMgr->remoteCallbackDeath_->OnRemoteDied(nullptr); DTEST_LOG<<"OnRemoteDied002 END"; } + +/** + * @tc.name: Test GetLocalAbilityManagerProxy001 + * @tc.desc: GetLocalAbilityManagerProxy001 + * @tc.type: FUNC + * @tc.require: I7VQQG + */ +HWTEST_F(SystemAbilityMgrTest, GetLocalAbilityManagerProxy001, TestSize.Level3) +{ + DTEST_LOG << "GetLocalAbilityManagerProxy001 BEGIN" << std::endl; + + sptr saMgr = new SystemAbilityManager; + saMgr->saProfileMap_.clear(); + auto ret = saMgr->GetLocalAbilityManagerProxy(SAID); + EXPECT_TRUE(ret == nullptr); + DTEST_LOG << "GetLocalAbilityManagerProxy001 END" << std::endl; +} + +/** + * @tc.name: Test GetLocalAbilityManagerProxy002 + * @tc.desc: GetLocalAbilityManagerProxy001 + * @tc.type: FUNC + * @tc.require: I7VQQG + */ +HWTEST_F(SystemAbilityMgrTest, GetLocalAbilityManagerProxy002, TestSize.Level3) +{ + DTEST_LOG << "GetLocalAbilityManagerProxy002 BEGIN" << std::endl; + + sptr saMgr = new SystemAbilityManager; + CommonSaProfile saProfile; + saProfile.process = u"test"; + saProfile.saId = SAID; + saMgr->saProfileMap_[SAID] = saProfile; + + sptr testAbility = new TestTransactionService(); + EXPECT_FALSE(testAbility == nullptr); + saMgr->systemProcessMap_[u"test"] = testAbility; + + auto ret = saMgr->GetLocalAbilityManagerProxy(SAID); + EXPECT_FALSE(ret == nullptr); + + DTEST_LOG << "GetLocalAbilityManagerProxy002 END" << std::endl; +} } // namespace OHOS \ No newline at end of file -- Gitee