diff --git a/services/abilitymgr/BUILD.gn b/services/abilitymgr/BUILD.gn index 822dce7b58e8c0985e8b2b01b96b60b23604a3a6..51fbe487c25ca163063727899b105a087fad6bcb 100644 --- a/services/abilitymgr/BUILD.gn +++ b/services/abilitymgr/BUILD.gn @@ -202,6 +202,7 @@ ohos_shared_library("abilityms") { "window_manager:scene_session", "window_manager:session_manager_lite", "window_manager:sms", + "udmf:udmf_client", ] public_external_deps = [ "background_task_mgr:bgtaskmgr_innerkits" ] diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index ea0b7039abb851c2a1628a15a052149b478cd13f..6e23995589ce98eb03762bffb07f30a2e08cb7a1 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -180,7 +180,8 @@ abilityms_files = [ "src/utils/window_options_utils.cpp", "src/utils/timeout_state_utils.cpp", "src/kiosk_status.cpp", - "src/kiosk_manager.cpp" + "src/kiosk_manager.cpp", + "src/utils/udmf_utils.cpp", ] if (ability_runtime_graphics) { diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index a7d0e3862091bbdebd3a6d1f3963ec9977eaad42..3cf4e8a2d303be0ea732ba4073d54c50c0b30b31 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2814,6 +2814,9 @@ private: int32_t OpenLinkInner(const Want &want, sptr callerToken, int32_t userId, int requestCode, bool removeInsightIntentFlag); int32_t KillProcessWithReasonInner(int32_t pid, const ExitReason &reason, bool isKillPrecedeStart); + + int32_t ProcessUdmfKey( + const Want &want, uint32_t targetTokenId, AppExecFwk::ExtensionAbilityType extensionType); #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE std::shared_ptr bgtaskObserver_; #endif diff --git a/services/abilitymgr/include/utils/udmf_utils.h b/services/abilitymgr/include/utils/udmf_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..a2546eec75b2a2d1d15d095853bba77e0e1cabb9 --- /dev/null +++ b/services/abilitymgr/include/utils/udmf_utils.h @@ -0,0 +1,33 @@ +/* +* 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_UDMF_UTILS_H +#define OHOS_ABILITY_RUNTIME_UDMF_UTILS_H + +#include +#include + +namespace OHOS { +namespace AbilityRuntime { + +class UdmfUtils final { +public: + static int32_t ProcessUdmfKey(const std::string &key, uint32_t targetTokenId); +private: + static int32_t AddPrivilege(const std::string &key, uint32_t tokenId, const std::string &readPermission); +}; +} // AbilityRuntime +} // OHOS +#endif // OHOS_ABILITY_RUNTIME_UDMF_UTILS_H \ No newline at end of file diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 8053d83f55dd1bac9d9f71fa7b0abfa7948f9ec1..b4df3bec8f2d4979d43b339379b1e6f39490464c 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -113,6 +113,7 @@ #include "utils/update_caller_info_util.h" #include "utils/want_utils.h" #include "utils/window_options_utils.h" +#include "utils/udmf_utils.h" #include "view_data.h" #ifdef SUPPORT_GRAPHICS #include "ability_first_frame_state_observer_manager.h" @@ -245,6 +246,7 @@ constexpr int32_t PIPE_MSG_READ_BUFFER = 1024; constexpr const char* APPSPAWN_STARTED = "startup.service.ctl.appspawn.pid"; constexpr const char* APP_LINKING_ONLY = "appLinkingOnly"; constexpr const char* SCREENCONFIG_SCREENMODE = "ohos.verticalpanel.screenconfig.screenmode"; +constexpr const char* UD_KEY = "ability.want.params.udKey"; void SendAbilityEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo) { @@ -4042,6 +4044,12 @@ int AbilityManagerService::StartUIExtensionAbility(const sptr &exte TAG_LOGD(AAFwkTag::UI_EXT, "userId is : %{public}d, singleton is : %{public}d", validUserId, static_cast(abilityInfo.applicationInfo.singleton)); + result = ProcessUdmfKey(extensionSessionInfo->want, abilityInfo.applicationInfo.accessTokenId, extensionType); + if (result != ERR_OK) { + TAG_LOGE(AAFwkTag::UI_EXT, "ProcessUdmfKey error"); + return result; + } + result = CheckOptExtensionAbility(extensionSessionInfo->want, abilityRequest, validUserId, extensionType); if (result != ERR_OK) { TAG_LOGE(AAFwkTag::UI_EXT, "checkOptExtensionAbility error"); @@ -15113,5 +15121,15 @@ int32_t AbilityManagerService::PreloadApplication(const std::string &bundleName, { return PreloadManagerService::GetInstance().PreloadApplication(bundleName, userId, appIndex); } + +int32_t AbilityManagerService::ProcessUdmfKey( + const Want &want, uint32_t targetTokenId, AppExecFwk::ExtensionAbilityType extensionType) +{ + std::string key = want.GetStringParam(UD_KEY); + if (UIExtensionUtils::IsProcessUdkeyExtension(extensionType) && !key.empty()){ + return AbilityRuntime::UdmfUtils::ProcessUdmfKey(key, targetTokenId); + } + return ERR_OK; +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/utils/udmf_utils.cpp b/services/abilitymgr/src/utils/udmf_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7aba7412b95e559146b307a4c031467ba699b8e0 --- /dev/null +++ b/services/abilitymgr/src/utils/udmf_utils.cpp @@ -0,0 +1,50 @@ +/* +* 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 "udmf_utils.h" + +#include "ability_manager_errors.h" +#include "hilog_tag_wrapper.h" +#include "hitrace_meter.h" +#include "in_process_call_wrapper.h" +#include "udmf_client.h" + +namespace OHOS { +namespace AbilityRuntime { + +int32_t UdmfUtils::AddPrivilege(const std::string &key, uint32_t tokenId, const std::string &readPermission) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + TAG_LOGI(AAFwkTag::ABILITYMGR, "AddPrivilege for tokenId:%{public}u", tokenId); + UDMF::QueryOption query = { .key = key }; + UDMF::Privilege privilege = { .tokenId = tokenId, .readPermission = readPermission }; + auto ret = IN_PROCESS_CALL(UDMF::UdmfClient::GetInstance().AddPrivilege(query, privilege)); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "AddPrivilege failed:%{public}d", ret); + } + return ret; +} + +int32_t UdmfUtils::ProcessUdmfKey(const std::string &key, uint32_t targetTokenId) +{ + auto ret = AddPrivilege(key, targetTokenId, ""); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "ProcessUdmfKey failed:%{public}d", ret); + return ERR_UPMS_ADD_PRIVILEGED_FAILED; + } + return ERR_OK; +} +} // AbilityRuntime +} // OHOS \ No newline at end of file diff --git a/services/common/include/ui_extension_utils.h b/services/common/include/ui_extension_utils.h index e4079c43dde440e2d9d013241269895cebfb0a70..cbcd9038ee6d1cdb68c396aeb3cd76bf74aca5cc 100755 --- a/services/common/include/ui_extension_utils.h +++ b/services/common/include/ui_extension_utils.h @@ -175,6 +175,14 @@ inline bool IsWindowExtension(const AppExecFwk::ExtensionAbilityType type) { return type == AppExecFwk::ExtensionAbilityType::WINDOW; } + +inline bool IsProcessUdkeyExtension(const AppExecFwk::ExtensionAbilityType type) +{ + const std::unordered_set processUdkeyExtensionSet = { + AppExecFwk::ExtensionAbilityType::SYSPICKER_FILEPICKER + }; + return processUdkeyExtensionSet.find(type) != processUdkeyExtensionSet.end(); +} } // namespace UIExtensionUtils } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 43c44fea8a3b3bcffb232794f7f221623ba64dca..bfa3e9573ca58ff75805de79e7cb7db397340af9 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -478,6 +478,7 @@ group("unittest") { "ui_extension_record_test:unittest", "ui_extension_utils_test:unittest", "update_caller_info_util_test:unittest", + "udmf_utils_test:unittest", "uri_utils_second_test", "uri_utils_test:unittest", "user_controller_test:unittest", diff --git a/test/unittest/ability_manager_service_fourteenth_test/BUILD.gn b/test/unittest/ability_manager_service_fourteenth_test/BUILD.gn index ac4125480f28626740863ded1ede5c4376c50070..f12b2c99ce4f98ac06e5b97837c002fb9e844668 100644 --- a/test/unittest/ability_manager_service_fourteenth_test/BUILD.gn +++ b/test/unittest/ability_manager_service_fourteenth_test/BUILD.gn @@ -233,6 +233,7 @@ ohos_unittest("ability_manager_service_fourteenth_test") { "${ability_runtime_services_path}/abilitymgr/src/kiosk_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/interceptor/kiosk_interceptor.cpp", "${ability_runtime_services_path}/abilitymgr/src/preload_manager_service.cpp", + "../ability_manager_service_mock_test/mock/src/mock_udmf_utils.cpp", ] deps = [ diff --git a/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp b/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp index 03b8f4e3c8ad9be47257f58c50cb01420e57a049..ad9532d665b23c89210c675a9e174f80f788b12e 100644 --- a/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp +++ b/test/unittest/ability_manager_service_fourteenth_test/ability_manager_service_fourteenth_test.cpp @@ -1397,5 +1397,79 @@ HWTEST_F(AbilityManagerServiceFourteenthTest, NotifyStartupExceptionBySCB_002, T EXPECT_EQ(result, ERR_OK); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest NotifyStartupExceptionBySCB_002 end"); } + +/* + * Feature: AbilityManagerService + * Function: ProcessUdmfKey + * SubFunction: NA + * FunctionPoints: AbilityManagerService ProcessUdmfKey + */ +HWTEST_F(AbilityManagerServiceFourteenthTest, ProcessUdmfKey_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_001 start"); + Want want; + want.SetParam("ability.want.params.udkey", std::string("udkey")); + uint32_t targetTokenId = 1000001; + AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::SYSPICKER_FILEPICKER; + auto abilityMs = std::make_shared(); + int32_t result = abilityMs->ProcessUdmfKey(want, targetTokenId, extensionType); + EXPECT_EQ(result, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_001 end"); +} + +/* + * Feature: AbilityManagerService + * Function: ProcessUdmfKey + * SubFunction: NA + * FunctionPoints: AbilityManagerService ProcessUdmfKey + */ +HWTEST_F(AbilityManagerServiceFourteenthTest, ProcessUdmfKey_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_002 start"); + Want want; + want.SetParam("ability.want.params.udkey", std::string("udkey")); + uint32_t targetTokenId = 1000001; + AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::EMBEDDED_UI; + auto abilityMs = std::make_shared(); + int32_t result = abilityMs->ProcessUdmfKey(want, targetTokenId, extensionType); + EXPECT_EQ(result, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_002 end"); +} + +/* + * Feature: AbilityManagerService + * Function: ProcessUdmfKey + * SubFunction: NA + * FunctionPoints: AbilityManagerService ProcessUdmfKey + */ +HWTEST_F(AbilityManagerServiceFourteenthTest, ProcessUdmfKey_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_003 start"); + Want want; + uint32_t targetTokenId = 1000001; + AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::SYSPICKER_FILEPICKER; + auto abilityMs = std::make_shared(); + int32_t result = abilityMs->ProcessUdmfKey(want, targetTokenId, extensionType); + EXPECT_EQ(result, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_003 end"); +} + +/* + * Feature: AbilityManagerService + * Function: ProcessUdmfKey + * SubFunction: NA + * FunctionPoints: AbilityManagerService ProcessUdmfKey + */ +HWTEST_F(AbilityManagerServiceFourteenthTest, ProcessUdmfKey_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_004 start"); + Want want; + uint32_t targetTokenId = 1000001; + AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::EMBEDDED_UI; + auto abilityMs = std::make_shared(); + int32_t result = abilityMs->ProcessUdmfKey(want, targetTokenId, extensionType); + EXPECT_EQ(result, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourteenthTest ProcessUdmfKey_004 end"); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ability_manager_service_mock_test/BUILD.gn b/test/unittest/ability_manager_service_mock_test/BUILD.gn index 2d4c140cd04e8b022d9dad07d4167e753373354a..75b07c15102608cdc9fa983c98474fc791fdff12 100644 --- a/test/unittest/ability_manager_service_mock_test/BUILD.gn +++ b/test/unittest/ability_manager_service_mock_test/BUILD.gn @@ -64,6 +64,7 @@ ohos_unittest("ability_manager_service_mock_test") { "ability_manager_service_mock_test.cpp", "mock/src/insight_intent_execute_manager_mock.cpp", "mock/src/mock_bundle_mgr_helper.cpp", + "mock/src/mock_udmf_utils.cpp", ] configs = [ "${ability_runtime_services_path}/abilitymgr:abilityms_config" ] diff --git a/test/unittest/ability_manager_service_mock_test/mock/include/udmf_utils.h b/test/unittest/ability_manager_service_mock_test/mock/include/udmf_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..baf606e98e9a877027655767b0878a8c63a48de4 --- /dev/null +++ b/test/unittest/ability_manager_service_mock_test/mock/include/udmf_utils.h @@ -0,0 +1,33 @@ +/* +* 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_UDMF_UTILS_MOCK_H +#define OHOS_ABILITY_RUNTIME_UDMF_UTILS_MOCK_H + +#include +#include + +namespace OHOS { +namespace AbilityRuntime { + +class UdmfUtils final { +public: + static int32_t ProcessUdmfKey(const std::string &key, uint32_t targetTokenId); +private: + static int32_t AddPrivilege(const std::string &key, uint32_t tokenId, const std::string &readPermission); +}; +} // AAFwk +} // OHOS +#endif // OHOS_ABILITY_RUNTIME_UDMF_UTILS_MOCK_H \ No newline at end of file diff --git a/test/unittest/ability_manager_service_mock_test/mock/src/mock_udmf_utils.cpp b/test/unittest/ability_manager_service_mock_test/mock/src/mock_udmf_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06232e2d7d51d9ddf9b375c95b5a773bbc6e9bcc --- /dev/null +++ b/test/unittest/ability_manager_service_mock_test/mock/src/mock_udmf_utils.cpp @@ -0,0 +1,33 @@ +/* +* 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 "udmf_utils.h" + +#include "ability_manager_errors.h" + +namespace OHOS { +namespace AbilityRuntime { + +int32_t UdmfUtils::AddPrivilege(const std::string &key, uint32_t tokenId, const std::string &readPermission) +{ + return ERR_OK; +} + +int32_t UdmfUtils::ProcessUdmfKey(const std::string &key, uint32_t targetTokenId) +{ + return ERR_OK; +} +} // OHOS +} // AAFwk \ No newline at end of file diff --git a/test/unittest/ability_manager_service_second_test/BUILD.gn b/test/unittest/ability_manager_service_second_test/BUILD.gn index 9fc3819734720ea97e123e52f29705ca822f3e10..bfbd767d918fc074900c59d438e99b08c88b690b 100644 --- a/test/unittest/ability_manager_service_second_test/BUILD.gn +++ b/test/unittest/ability_manager_service_second_test/BUILD.gn @@ -234,6 +234,7 @@ ohos_unittest("ability_manager_service_second_test") { "../ability_manager_service_thirteenth_test/mock/src/mock_scene_board_judgement.cpp", "../ability_manager_service_thirteenth_test/mock/src/mock_sub_managers_helper.cpp", "../ability_manager_service_thirteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp", + "../ability_manager_service_mock_test/mock/src/mock_udmf_utils.cpp", ] deps = [ diff --git a/test/unittest/ability_manager_service_third_test/abilitymgr.gni b/test/unittest/ability_manager_service_third_test/abilitymgr.gni index 5136f2a5e2289abdcaf040b93e768dfc880f9fdd..f8f156ed39877a47bee5aa565427f0557e65d1c5 100644 --- a/test/unittest/ability_manager_service_third_test/abilitymgr.gni +++ b/test/unittest/ability_manager_service_third_test/abilitymgr.gni @@ -170,7 +170,8 @@ abilityms_files = [ "${ability_runtime_services_path}/abilitymgr/src/utils/window_options_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/utils/timeout_state_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/kiosk_status.cpp", - "${ability_runtime_services_path}/abilitymgr/src/kiosk_manager.cpp" + "${ability_runtime_services_path}/abilitymgr/src/kiosk_manager.cpp", + "../ability_manager_service_mock_test/mock/src/mock_udmf_utils.cpp", ] if (ability_runtime_graphics) { diff --git a/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn b/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn index e5c8b96f331e7b1da08554a81de83b9465cc1541..b9601ffea2a72348bc38d38b98cfeb222ad407f7 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn +++ b/test/unittest/ability_manager_service_thirteenth_test/BUILD.gn @@ -234,6 +234,7 @@ ohos_unittest("ability_manager_service_thirteenth_test") { "${ability_runtime_services_path}/abilitymgr/src/kiosk_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/interceptor/kiosk_interceptor.cpp", "${ability_runtime_services_path}/abilitymgr/src/preload_manager_service.cpp", + "../ability_manager_service_mock_test/mock/src/mock_udmf_utils.cpp", ] deps = [ diff --git a/test/unittest/udmf_utils_test/BUILD.gn b/test/unittest/udmf_utils_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..21525d837efb3abdfdd4aa738cc290155e24cfc2 --- /dev/null +++ b/test/unittest/udmf_utils_test/BUILD.gn @@ -0,0 +1,53 @@ +# 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. + +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/ability_runtime/abilitymgr" + +ohos_unittest("udmf_utils_test") { + module_out_path = module_output_path + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "../../../test/cfi_blocklist.txt" + } + branch_protector_ret = "pac_ret" + include_dirs = [ + "mock/include", + ] + + sources = [ + "${ability_runtime_services_path}/abilitymgr/src/utils/udmf_utils.cpp", + "mock/src/mock_udmf_client.cpp", + "udmf_utils_test.cpp", + ] + + deps = [] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_manager", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + ] +} + +group("unittest") { + testonly = true + deps = [ ":udmf_utils_test" ] +} diff --git a/test/unittest/udmf_utils_test/mock/include/udmf_client.h b/test/unittest/udmf_utils_test/mock/include/udmf_client.h new file mode 100644 index 0000000000000000000000000000000000000000..954bba05d1aa31b192a84c36032103a1b0dc7cf3 --- /dev/null +++ b/test/unittest/udmf_utils_test/mock/include/udmf_client.h @@ -0,0 +1,108 @@ +/* +* 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 UDMF_CLIENT_H +#define UDMF_CLIENT_H + +#include +#include +#include + +namespace OHOS { +namespace UDMF { +namespace { +constexpr const char* EMPTY_ENTRY_URI = "null"; +} + +struct QueryOption { + std::string key; +}; + +struct Object { + Object() {} + + Object(const std::string &uri) + { + value_ = uri; + } + + bool GetValue(const std::string &key, std::string &value) + { + value = value_; + return true; + } + std::string value_; +}; + +using ValueType = std::variant>; + +struct UnifiedRecord { + UnifiedRecord(const std::string &uri) + { + if (uri != EMPTY_ENTRY_URI) { + entry = std::make_shared(uri); + return; + } + entry = nullptr; + } + + ValueType GetEntry(const std::string &utdId) + { + return entry; + } + + std::shared_ptr entry = nullptr; +}; + +struct UnifiedData { + UnifiedData(std::vector> records) + { + unifiedRecords_ = records; + } + + std::vector> GetRecords() + { + return unifiedRecords_; + } + std::vector> unifiedRecords_; +}; + +struct Privilege { + uint32_t tokenId = 0; + std::string readPermission = ""; +}; + +class UdmfClient { + public: + static UdmfClient &GetInstance(); + + ~UdmfClient() {}; + + int32_t GetBatchData(const QueryOption &query, std::vector &unifiedDataset); + int32_t AddPrivilege(const QueryOption &query, const Privilege &privilege); + std::string GetBundleNameByUdKey(const std::string &key); + static void Init(); + static std::vector unifiedData_; + static int32_t getBatchDataRet_; + static int32_t addPrivilegeRet_; + static int32_t privilegeTokenId_; + static std::string keyAuthority; + + private: + UdmfClient() {}; +}; +} // OHOS +} // UDMF +#endif // UDMF_CLIENT_H \ No newline at end of file diff --git a/test/unittest/udmf_utils_test/mock/src/mock_udmf_client.cpp b/test/unittest/udmf_utils_test/mock/src/mock_udmf_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd67b89298860ee0b7c8dde556987a720a8125c6 --- /dev/null +++ b/test/unittest/udmf_utils_test/mock/src/mock_udmf_client.cpp @@ -0,0 +1,59 @@ +/* +* 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 "udmf_client.h" + +namespace OHOS { +namespace UDMF { + +UdmfClient &UdmfClient::GetInstance() +{ + static UdmfClient utils; + return utils; +} + +void UdmfClient::Init() +{ + getBatchDataRet_ = 0; + addPrivilegeRet_ = 0; + UdmfClient::unifiedData_.clear(); +} + +int32_t UdmfClient::GetBatchData(const QueryOption &query, std::vector &unifiedDataset) +{ + unifiedDataset = unifiedData_; + return getBatchDataRet_; +} + +int32_t UdmfClient::AddPrivilege(const QueryOption &query, const Privilege &privilege) +{ + if (privilege.tokenId == privilegeTokenId_) { + return 0; + } + return addPrivilegeRet_; +} + +std::string UdmfClient::GetBundleNameByUdKey(const std::string &key) +{ + return keyAuthority; +} + +int32_t UdmfClient::getBatchDataRet_ = 0; +int32_t UdmfClient::addPrivilegeRet_ = 0; +int32_t UdmfClient::privilegeTokenId_ = 10000; +std::vector UdmfClient::unifiedData_ = {}; +std::string UdmfClient::keyAuthority = ""; +} // UDMF +} // OHOS \ No newline at end of file diff --git a/test/unittest/udmf_utils_test/udmf_utils_test.cpp b/test/unittest/udmf_utils_test/udmf_utils_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb29259ab34f505e3334d4e32af2310ad53a8948 --- /dev/null +++ b/test/unittest/udmf_utils_test/udmf_utils_test.cpp @@ -0,0 +1,104 @@ +/* + * 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 +#define private public +#include "udmf_utils.h" +#undef private +#include "hilog_tag_wrapper.h" +#include "udmf_client.h" +#include "ability_manager_errors.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AbilityRuntime { +class UdmfUtilsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void UdmfUtilsTest::SetUpTestCase() {} + +void UdmfUtilsTest::TearDownTestCase() {} + +void UdmfUtilsTest::SetUp() +{ + UDMF::UdmfClient::Init(); +} + +void UdmfUtilsTest::TearDown() {} + +/* + * Feature: UdmfUtilsTest + * Function: AddPrivilege_001 + * SubFunction: NA + */ +HWTEST_F(UdmfUtilsTest, AddPrivilege_001, TestSize.Level1) +{ + std::string key = "udmfKey"; + uint32_t targetTokenId = 100001; + std::string readPermission = ""; + auto ret = UdmfUtils::AddPrivilege(key, targetTokenId, readPermission); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * Feature: UdmfUtilsTest + * Function: AddPrivilege_002 + * SubFunction: NA + */ +HWTEST_F(UdmfUtilsTest, AddPrivilege_002, TestSize.Level1) +{ + std::string key = "udmfKey"; + uint32_t targetTokenId = 100001; + std::string readPermission = ""; + UDMF::UdmfClient::addPrivilegeRet_ = INNER_ERR; + auto ret = UdmfUtils::AddPrivilege(key, targetTokenId, readPermission); + EXPECT_EQ(ret, INNER_ERR); +} + +/* + * Feature: UdmfUtilsTest + * Function: ProcessUdmfKey_001 + * SubFunction: NA + */ +HWTEST_F(UdmfUtilsTest, ProcessUdmfKey_001, TestSize.Level1) +{ + std::string key = "udmfKey"; + uint32_t targetTokenId = 100001; + auto ret = UdmfUtils::ProcessUdmfKey(key, targetTokenId); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * Feature: UdmfUtilsTest + * Function: ProcessUdmfKey_002 + * SubFunction: NA + */ +HWTEST_F(UdmfUtilsTest, ProcessUdmfKey_002, TestSize.Level1) +{ + std::string key = "udmfKey"; + uint32_t targetTokenId = 100001; + UDMF::UdmfClient::addPrivilegeRet_ = INNER_ERR; + auto ret = UdmfUtils::ProcessUdmfKey(key, targetTokenId); + EXPECT_EQ(ret, ERR_UPMS_ADD_PRIVILEGED_FAILED); +} +} // namespace AAFwk +} // namespace OHOS diff --git a/test/unittest/ui_extension_utils_test/ui_extension_utils_test.cpp b/test/unittest/ui_extension_utils_test/ui_extension_utils_test.cpp index 92b30b34e21a766d3b9400233bf96ecb6ffbcd70..7721eb56595d71cd236c240f2d1fe78ce5c8b6a9 100755 --- a/test/unittest/ui_extension_utils_test/ui_extension_utils_test.cpp +++ b/test/unittest/ui_extension_utils_test/ui_extension_utils_test.cpp @@ -103,5 +103,21 @@ HWTEST_F(UIExtensionUtilsTest, IsSystemUIExtension_0100, TestSize.Level0) bool result = UIExtensionUtils::IsUIExtension(extensionAbilityType); EXPECT_TRUE(result); } + +/** + * @tc.name: IsProcessUdkeyExtension_0100 + * @tc.desc: IsProcessUdkeyExtension Test + * @tc.type: FUNC + */ +HWTEST_F(UIExtensionUtilsTest, IsProcessUdkeyExtension_0100, TestSize.Level0) +{ + AppExecFwk::ExtensionAbilityType extensionAbilityType = AppExecFwk::ExtensionAbilityType::SYSDIALOG_COMMON; + bool result = UIExtensionUtils::IsProcessUdkeyExtension(extensionAbilityType); + EXPECT_FALSE(result); + + extensionAbilityType = AppExecFwk::ExtensionAbilityType::SYSPICKER_FILEPICKER; + result = UIExtensionUtils::IsProcessUdkeyExtension(extensionAbilityType); + EXPECT_TRUE(result); +} } // namespace AAFwk } // namespace OHOS