From cc5d040cd2756911a6c201800811a8d48871919c Mon Sep 17 00:00:00 2001 From: zhjs Date: Wed, 30 Jul 2025 17:34:35 +0800 Subject: [PATCH 1/2] ScreenLock Event listener Signed-off-by: zhjs --- interfaces/inner_api/iam_common_defines.h | 2 + services/core/BUILD.gn | 1 + services/core/inc/fingerprint_event_manager.h | 46 ++++++++++ .../core/src/fingerprint_event_manager.cpp | 87 +++++++++++++++++++ .../core/src/screenlock_status_listener.cpp | 5 ++ test/fuzztest/common_fuzzer/BUILD.gn | 1 + .../services/servicecore_fuzzer/BUILD.gn | 1 + .../inc/fingerprint_event_manager_fuzzer.h | 28 ++++++ .../src/fingerprint_event_manager_fuzzer.cpp | 56 ++++++++++++ test/unittest/services/BUILD.gn | 2 + .../inc/fingerprint_event_manager_test.h | 37 ++++++++ .../src/fingerprint_event_manager_test.cpp | 56 ++++++++++++ 12 files changed, 322 insertions(+) create mode 100644 services/core/inc/fingerprint_event_manager.h create mode 100644 services/core/src/fingerprint_event_manager.cpp create mode 100644 test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h create mode 100644 test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp create mode 100644 test/unittest/services/inc/fingerprint_event_manager_test.h create mode 100644 test/unittest/services/src/fingerprint_event_manager_test.cpp diff --git a/interfaces/inner_api/iam_common_defines.h b/interfaces/inner_api/iam_common_defines.h index e5b401649..22754c4a9 100644 --- a/interfaces/inner_api/iam_common_defines.h +++ b/interfaces/inner_api/iam_common_defines.h @@ -163,6 +163,8 @@ enum PropertyMode : uint32_t { PROPERTY_MODE_NOTIFY_COLLECTOR_READY = 8, /** The property mode is risk event. */ PROPERTY_MODE_RISK_EVENT = 9, + /** The property mode is fingerprint event. */ + PROPERTY_MODE_FINGERPRINT_EVENT = 10, }; /** diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 60888b650..325a940c4 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -82,6 +82,7 @@ ohos_source_set("userauth_service_core") { "src/user_idm_database_impl.cpp", "src/user_info_impl.cpp", "src/widget_schedule_node_impl.cpp", + "src/fingerprint_event_manager.cpp", ] deps = [ diff --git a/services/core/inc/fingerprint_event_manager.h b/services/core/inc/fingerprint_event_manager.h new file mode 100644 index 000000000..729ef4c2a --- /dev/null +++ b/services/core/inc/fingerprint_event_manager.h @@ -0,0 +1,46 @@ +/* +* 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 FINGERPRINT_EVENT_MANAGER_H +#define FINGERPRINT_EVENT_MANAGER_H + +#include + +#include "attributes.h" +#include "iam_common_defines.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +enum ServiceEventType : uint32_t { + UNKNOWN = 0, + SCREENLOCK = 1, +}; +class FingerprintEventManager { +public: + static FingerprintEventManager &GetInstance(); + void SendServiceEvent(bool status, ServiceEventType event); +#ifndef IAM_TEST_ENABLE +private: +#endif + FingerprintEventManager() = default; + ~FingerprintEventManager() = default; + void SetFingerprintEventPorperty(bool status, ServiceEventType event); + ResultCode SetAttributes(Attributes &Attributes, bool status, ServiceEventType event); +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // FINGERPRINT_EVENT_MANAGER_H \ No newline at end of file diff --git a/services/core/src/fingerprint_event_manager.cpp b/services/core/src/fingerprint_event_manager.cpp new file mode 100644 index 000000000..1f396003c --- /dev/null +++ b/services/core/src/fingerprint_event_manager.cpp @@ -0,0 +1,87 @@ +/* +* 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 "fingerprint_event_manager.h" + +#include "system_ability_definition.h" +#include "system_ability.h" + +#include "context_appstate_observer.h" +#include "ipc_common.h" +#include "system_ability_listener.h" +#include "resource_node_pool.h" +#include "user_idm_database.h" + +#define LOG_TAG "USER_AUTH_SA" +namespace OHOS { +namespace UserIam { +namespace UserAuth { +FingerprintEventManager &FingerprintEventManager::GetInstance() +{ + static FingerprintEventManager instance; + return instance; +} + +void FingerprintEventManager::SetFingerprintEventManager(bool status, ServiceEventType event) +{ + IAM_LOGD("start"); + Attributes Attributes; + int32_t getAttrRet = SetAttributes(Attributes, status, event); + IF_FALSE_LOGE_AND_RETURN(getAttrRet == SUCCESS); + + std::vector> authTypeNodes; + ResourceNodePool::Instance().GetResourceNodeByTypeAndRole(AuthType::FINGERPRINT, ALL_IN_ONE,authTypeNodes); + IF_FALSE_LOGE_AND_RETURN(authTypeNodes.size() != 0); + for (auto &authTypeNode : authTypeNodes) { + auto resourceNode = authTypeNode.look(); + if (resourceNode != nullptr) { + int32_t result = resourceNode->Setproperty(attributes); + IAM_LOGI("set fingerprint property finish, ret:%{public}d", result); + } else { + IAM_LOGE("resourceNode already expired"); + } + } +} + +ResultCode FingerprintEventManager::SetAttributes(Attributes &attributes, bool status, ServiceEventType event) +{ + IAM_LOGI("start"); + bool setModeRet = attributes.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, + PropertyMode::PROPERTY_MODE_FINGERPRINT_EVENT); + IF_FALSE_LOGE_AND_RETURN_VAL(setModeRet, ResultCode::GENERAL_ERROR); + + std::vector extraInfo; + extraInfo.push_back(event); + extraInfo.push_back(status); + + bool setExtraInfoRet = attributes.SetUint8ArrayValue(Attributes::ATTR_ALGORITHM_INFO,extraInfo); + IF_FALSE_LOGE_AND_RETURN_VAL(setExtraInfoRet, ResultCode::GENERAL_ERROR); + + return SUCCESS; +} + +void FingerprintEventManager::SendServiceEvent(bool status, ServiceEventType event) +{ + IAM_LOGI("start"); + auto handler = ThreadHandler::GetSingleThreadInstance(); + IF_FALSE_LOGE_AND_RETURN(handler != nullptr); + handler->PostTask([status,event]() { + FingerprintEventManager::GetInstance().SetFingerprintEventPorperty(status,event); + }); +} + +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/screenlock_status_listener.cpp b/services/core/src/screenlock_status_listener.cpp index 5b75a929c..6db7793ad 100644 --- a/services/core/src/screenlock_status_listener.cpp +++ b/services/core/src/screenlock_status_listener.cpp @@ -24,6 +24,7 @@ #include "credential_info_interface.h" #include "risk_event_manager.h" #include "user_idm_database.h" +#include "fingerprint_event_manager.h" #define LOG_TAG "USER_AUTH_SA" @@ -61,6 +62,7 @@ void ScreenlockStatusListenerManager::RegisterScreenLockedCallback() } EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED); EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); auto subscriber = Common::MakeShared(subscribeInfo); @@ -110,6 +112,9 @@ void ScreenlockStatusListener::OnReceiveEvent(const OHOS::EventFwk::CommonEventD } if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { RiskEventManager::GetInstance().OnScreenLock(); + FingerprintEventManager::GetInstance().SendServiceEvent(true, SCREENLOCK); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { + FingerprintEventManager::GetInstance().SendServiceEvent(false, SCREENLOCK); } } } // namespace UserAuth diff --git a/test/fuzztest/common_fuzzer/BUILD.gn b/test/fuzztest/common_fuzzer/BUILD.gn index aef2668f6..643c81fd7 100644 --- a/test/fuzztest/common_fuzzer/BUILD.gn +++ b/test/fuzztest/common_fuzzer/BUILD.gn @@ -136,6 +136,7 @@ ohos_source_set("userauth_service_core_fuzzer") { "../../../services/core/src/resource_node_pool_impl.cpp", "../../../services/core/src/resource_node_utils.cpp", "../../../services/core/src/risk_event_manager.cpp", + "../../../services/core/src/fingerprint_event_manager.cpp", "../../../services/core/src/schedule_node_builder.cpp", "../../../services/core/src/schedule_node_helper.cpp", "../../../services/core/src/schedule_node_impl.cpp", diff --git a/test/fuzztest/services/servicecore_fuzzer/BUILD.gn b/test/fuzztest/services/servicecore_fuzzer/BUILD.gn index 073378936..b47db7e1c 100644 --- a/test/fuzztest/services/servicecore_fuzzer/BUILD.gn +++ b/test/fuzztest/services/servicecore_fuzzer/BUILD.gn @@ -47,6 +47,7 @@ ohos_fuzztest("ServiceCoreFuzzTest") { "src/risk_event_manager_fuzzer.cpp", "src/schedule_node_fuzzer.cpp", "src/service_core_fuzzer.cpp", + "src/fingerprint_event_manager.cpp", ] deps = [ diff --git a/test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h b/test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h new file mode 100644 index 000000000..7f8221826 --- /dev/null +++ b/test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h @@ -0,0 +1,28 @@ +/* + * 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 FINGERPRINT_EVENT_MANAGER_FUZZ_TEST_H +#define FINGERPRINT_EVENT_MANAGER_FUZZ_TEST_H + +#include "parcel.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +void FingerprintEventManagerFuzzTest(Parcel &parcel); +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // FINGERPRINT_EVENT_MANAGER_FUZZ_TEST_H \ No newline at end of file diff --git a/test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp b/test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp new file mode 100644 index 000000000..cfd4f21d8 --- /dev/null +++ b/test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp @@ -0,0 +1,56 @@ +/* + * 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 "fingerprint_event_manager_fuzzer.h" + +#include "iam_fuzz_test.h" +#include "iam_logger.h" +#include "iam_ptr.h" +#include "fingerprint_event_manager.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +namespace { +void FuzzFingerPrintEventManagerMain(Parcel &parcel) +{ + IAM_LOGI("begin"); + bool status = parcel.ReadBool(); + ServiceEventType event = SCREENLOCK; + Attributes attributes; + FingerPrintEventManager::GetInstance().SendServiceEvent(status, event); + FingerPrintEventManager::GetInstance().SetFingerprintEventProperty(status, event); + FingerPrintEventManager::GetInstance().SetAttributes(attributes, status, event); + IAM_LOGI("end"); +} + +using FingerprintEventManagerFuzzFunc = decltype(FuzzFingerPrintEventManagerMain); +FingerprintEventManagerFuzzFunc *g_FingerprintEventManagerFuzzFunc[] = { + FuzzFingerPrintEventManagerMain, +}; +} + +viod FingerprintEventManagerFuzzTest(Parcel &parcel) +{ + uint32_t index = parcel.ReadUint32() % (sizeof(g_FingerprintEventManagerFuzzFunc) / sizeof(FingerprintEventManagerFuzzFunc *)); + auto fuzzFunc = g_FingerprintEventManagerFuzzFunc[index]; + fuzzFunc(parcel); + return; +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/services/BUILD.gn b/test/unittest/services/BUILD.gn index 3aa2a2b37..fa2cde940 100644 --- a/test/unittest/services/BUILD.gn +++ b/test/unittest/services/BUILD.gn @@ -97,6 +97,7 @@ ohos_unittest("iam_services_test") { "../../../services/core/src/resource_node_impl.cpp", "../../../services/core/src/resource_node_pool_impl.cpp", "../../../services/core/src/resource_node_utils.cpp", + "../../../services/core/src/fingerprint_event_manager.cpp", "../../../services/core/src/risk_event_manager.cpp", "../../../services/core/src/schedule_node_builder.cpp", "../../../services/core/src/schedule_node_helper.cpp", @@ -159,6 +160,7 @@ ohos_unittest("iam_services_test") { "src/remote_auth_invoker_context_test.cpp", "src/resource_node_pool_test.cpp", "src/resource_node_test.cpp", + "src/fingerprint_event_manager_test.cpp", "src/resource_node_utils_test.cpp", "src/risk_event_manager_test.cpp", "src/schedule_node_helper_test.cpp", diff --git a/test/unittest/services/inc/fingerprint_event_manager_test.h b/test/unittest/services/inc/fingerprint_event_manager_test.h new file mode 100644 index 000000000..1ead49d4a --- /dev/null +++ b/test/unittest/services/inc/fingerprint_event_manager_test.h @@ -0,0 +1,37 @@ +/* + * 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 FINGERPRINT_EVENT_MANAGER_TEST_H +#define FINGERPRINT_EVENT_MANAGER_TEST_H + +#include +#include + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class FingerprintEventManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // Fingerprint_EVENT_MANAGER_TEST_H \ No newline at end of file diff --git a/test/unittest/services/src/fingerprint_event_manager_test.cpp b/test/unittest/services/src/fingerprint_event_manager_test.cpp new file mode 100644 index 000000000..5890912ec --- /dev/null +++ b/test/unittest/services/src/fingerprint_event_manager_test.cpp @@ -0,0 +1,56 @@ +/* + * 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 "fingerprint_event_manager_test.h" + +#include "fingerprint_event_manager.h" + +#include "iam_logger.h" +#include "securec.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +using namespace testing; +using namespace testing::ext; + +viod FingerprintEventManagerTest::SetUpTestCase() +{ +} + +viod FingerprintEventManagerTest::TearDownTestCase() +{ +} + +viod FingerprintEventManagerTest::SetUp() +{ +} + +viod FingerprintEventManagerTest::TearDown() +{ +} + +HWTEST_F(FingerprintEventManagerTest, SendServiceEventTest, TestSize.Leve10) +{ + bool status = true; + ServiceEventType event = SCREENLOCK; + EXPECT_NO_THROW(FingerprintEventManagerTest::GetInstance().SendServiceEvent(status, event)); +} + +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file -- Gitee From 4e5796c898ea70336afaa416b278d796f6c9733f Mon Sep 17 00:00:00 2001 From: zhjs Date: Wed, 30 Jul 2025 17:34:35 +0800 Subject: [PATCH 2/2] ScreenLock Event listener Signed-off-by: zhjs --- interfaces/inner_api/iam_common_defines.h | 2 + services/core/BUILD.gn | 1 + services/core/inc/fingerprint_event_manager.h | 46 ++++++++++ .../core/src/fingerprint_event_manager.cpp | 87 +++++++++++++++++++ .../core/src/screenlock_status_listener.cpp | 5 ++ test/fuzztest/common_fuzzer/BUILD.gn | 1 + .../services/servicecore_fuzzer/BUILD.gn | 1 + .../inc/fingerprint_event_manager_fuzzer.h | 28 ++++++ .../src/fingerprint_event_manager_fuzzer.cpp | 56 ++++++++++++ test/unittest/services/BUILD.gn | 2 + .../inc/fingerprint_event_manager_test.h | 37 ++++++++ .../src/fingerprint_event_manager_test.cpp | 56 ++++++++++++ 12 files changed, 322 insertions(+) create mode 100644 services/core/inc/fingerprint_event_manager.h create mode 100644 services/core/src/fingerprint_event_manager.cpp create mode 100644 test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h create mode 100644 test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp create mode 100644 test/unittest/services/inc/fingerprint_event_manager_test.h create mode 100644 test/unittest/services/src/fingerprint_event_manager_test.cpp diff --git a/interfaces/inner_api/iam_common_defines.h b/interfaces/inner_api/iam_common_defines.h index e5b401649..22754c4a9 100644 --- a/interfaces/inner_api/iam_common_defines.h +++ b/interfaces/inner_api/iam_common_defines.h @@ -163,6 +163,8 @@ enum PropertyMode : uint32_t { PROPERTY_MODE_NOTIFY_COLLECTOR_READY = 8, /** The property mode is risk event. */ PROPERTY_MODE_RISK_EVENT = 9, + /** The property mode is fingerprint event. */ + PROPERTY_MODE_FINGERPRINT_EVENT = 10, }; /** diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 60888b650..325a940c4 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -82,6 +82,7 @@ ohos_source_set("userauth_service_core") { "src/user_idm_database_impl.cpp", "src/user_info_impl.cpp", "src/widget_schedule_node_impl.cpp", + "src/fingerprint_event_manager.cpp", ] deps = [ diff --git a/services/core/inc/fingerprint_event_manager.h b/services/core/inc/fingerprint_event_manager.h new file mode 100644 index 000000000..007f7f167 --- /dev/null +++ b/services/core/inc/fingerprint_event_manager.h @@ -0,0 +1,46 @@ +/* +* 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 FINGERPRINT_EVENT_MANAGER_H +#define FINGERPRINT_EVENT_MANAGER_H + +#include + +#include "attributes.h" +#include "iam_common_defines.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +enum ServiceEventType : uint32_t { + UNKNOWN = 0, + SCREENLOCK = 1, +}; +class FingerprintEventManager { +public: + static FingerprintEventManager &GetInstance(); + void SendServiceEvent(bool status, ServiceEventType event); +#ifndef IAM_TEST_ENABLE +private: +#endif + FingerprintEventManager() = default; + ~FingerprintEventManager() = default; + void SetFingerprintEventProperty(bool status, ServiceEventType event); + ResultCode SetAttributes(Attributes &attributes, bool status, ServiceEventType event); +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // FINGERPRINT_EVENT_MANAGER_H \ No newline at end of file diff --git a/services/core/src/fingerprint_event_manager.cpp b/services/core/src/fingerprint_event_manager.cpp new file mode 100644 index 000000000..b2cf0e2f2 --- /dev/null +++ b/services/core/src/fingerprint_event_manager.cpp @@ -0,0 +1,87 @@ +/* +* 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 "fingerprint_event_manager.h" + +#include "system_ability_definition.h" +#include "system_ability.h" + +#include "context_appstate_observer.h" +#include "ipc_common.h" +#include "system_ability_listener.h" +#include "resource_node_pool.h" +#include "user_idm_database.h" + +#define LOG_TAG "USER_AUTH_SA" +namespace OHOS { +namespace UserIam { +namespace UserAuth { +FingerprintEventManager &FingerprintEventManager::GetInstance() +{ + static FingerprintEventManager instance; + return instance; +} + +void FingerprintEventManager::SetFingerprintEventProperty(bool status, ServiceEventType event) +{ + IAM_LOGI("start"); + Attributes attributes; + int32_t getAttrRet = SetAttributes(attributes, status, event); + IF_FALSE_LOGE_AND_RETURN(getAttrRet == SUCCESS); + + std::vector> authTypeNodes; + ResourceNodePool::Instance().GetResourceNodeByTypeAndRole(AuthType::FINGERPRINT, ALL_IN_ONE, authTypeNodes); + IF_FALSE_LOGE_AND_RETURN(authTypeNodes.size() != 0); + for (auto &authTypeNode : authTypeNodes) { + auto resourceNode = authTypeNode.lock(); + if (resourceNode != nullptr) { + int32_t result = resourceNode->SetProperty(attributes); + IAM_LOGI("set fingerprint property finish, ret:%{public}d", result); + } else { + IAM_LOGE("resourceNode already expired"); + } + } +} + +ResultCode FingerprintEventManager::SetAttributes(Attributes &attributes, bool status, ServiceEventType event) +{ + IAM_LOGI("start"); + bool setModeRet = attributes.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, + PropertyMode::PROPERTY_MODE_FINGERPRINT_EVENT); + IF_FALSE_LOGE_AND_RETURN_VAL(setModeRet, ResultCode::GENERAL_ERROR); + + std::vector extraInfo; + extraInfo.push_back(event); + extraInfo.push_back(status); + + bool setExtraInfoRet = attributes.SetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo); + IF_FALSE_LOGE_AND_RETURN_VAL(setExtraInfoRet, ResultCode::GENERAL_ERROR); + + return SUCCESS; +} + +void FingerprintEventManager::SendServiceEvent(bool status, ServiceEventType event) +{ + IAM_LOGI("start"); + auto handler = ThreadHandler::GetSingleThreadInstance(); + IF_FALSE_LOGE_AND_RETURN(handler != nullptr); + handler->PostTask([status,event]() { + FingerprintEventManager::GetInstance().SetFingerprintEventProperty(status,event); + }); +} + +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/screenlock_status_listener.cpp b/services/core/src/screenlock_status_listener.cpp index 5b75a929c..6db7793ad 100644 --- a/services/core/src/screenlock_status_listener.cpp +++ b/services/core/src/screenlock_status_listener.cpp @@ -24,6 +24,7 @@ #include "credential_info_interface.h" #include "risk_event_manager.h" #include "user_idm_database.h" +#include "fingerprint_event_manager.h" #define LOG_TAG "USER_AUTH_SA" @@ -61,6 +62,7 @@ void ScreenlockStatusListenerManager::RegisterScreenLockedCallback() } EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED); EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); auto subscriber = Common::MakeShared(subscribeInfo); @@ -110,6 +112,9 @@ void ScreenlockStatusListener::OnReceiveEvent(const OHOS::EventFwk::CommonEventD } if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { RiskEventManager::GetInstance().OnScreenLock(); + FingerprintEventManager::GetInstance().SendServiceEvent(true, SCREENLOCK); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { + FingerprintEventManager::GetInstance().SendServiceEvent(false, SCREENLOCK); } } } // namespace UserAuth diff --git a/test/fuzztest/common_fuzzer/BUILD.gn b/test/fuzztest/common_fuzzer/BUILD.gn index aef2668f6..643c81fd7 100644 --- a/test/fuzztest/common_fuzzer/BUILD.gn +++ b/test/fuzztest/common_fuzzer/BUILD.gn @@ -136,6 +136,7 @@ ohos_source_set("userauth_service_core_fuzzer") { "../../../services/core/src/resource_node_pool_impl.cpp", "../../../services/core/src/resource_node_utils.cpp", "../../../services/core/src/risk_event_manager.cpp", + "../../../services/core/src/fingerprint_event_manager.cpp", "../../../services/core/src/schedule_node_builder.cpp", "../../../services/core/src/schedule_node_helper.cpp", "../../../services/core/src/schedule_node_impl.cpp", diff --git a/test/fuzztest/services/servicecore_fuzzer/BUILD.gn b/test/fuzztest/services/servicecore_fuzzer/BUILD.gn index 073378936..781ce77fb 100644 --- a/test/fuzztest/services/servicecore_fuzzer/BUILD.gn +++ b/test/fuzztest/services/servicecore_fuzzer/BUILD.gn @@ -47,6 +47,7 @@ ohos_fuzztest("ServiceCoreFuzzTest") { "src/risk_event_manager_fuzzer.cpp", "src/schedule_node_fuzzer.cpp", "src/service_core_fuzzer.cpp", + "src/fingerprint_event_manager_fuzzer.cpp", ] deps = [ diff --git a/test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h b/test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h new file mode 100644 index 000000000..29729a2f3 --- /dev/null +++ b/test/fuzztest/services/servicecore_fuzzer/inc/fingerprint_event_manager_fuzzer.h @@ -0,0 +1,28 @@ +/* + * 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 FINGERPRINT_EVENT_MANAGER_FUZZ_TEST_H +#define FINGERPRINT_EVENT_MANAGER_FUZZ_TEST_H + +#include "parcel.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +void FingerprintEventManagerFuzzTest(Parcel &parcel); +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // FINGERPRINT_EVENT_MANAGER_FUZZ_TEST_H \ No newline at end of file diff --git a/test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp b/test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp new file mode 100644 index 000000000..2c7933b59 --- /dev/null +++ b/test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp @@ -0,0 +1,56 @@ +/* + * 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 "fingerprint_event_manager_fuzzer.h" + +#include "iam_fuzz_test.h" +#include "iam_logger.h" +#include "iam_ptr.h" +#include "fingerprint_event_manager.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +namespace { +void FuzzFingerprintEventManagerMain(Parcel &parcel) +{ + IAM_LOGI("begin"); + bool status = parcel.ReadBool(); + ServiceEventType event = SCREENLOCK; + Attributes attributes; + FingerprintEventManager::GetInstance().SendServiceEvent(status, event); + FingerprintEventManager::GetInstance().SetFingerprintEventProperty(status,event); + FingerprintEventManager::GetInstance().SetAttributes(attributes, status, event); + IAM_LOGI("end"); +} + +using FingerprintEventManagerFuzzFunc = decltype(FuzzFingerprintEventManagerMain); +FingerprintEventManagerFuzzFunc *g_FingerprintEventManagerFuzzFuncs[] = { + FuzzFingerprintEventManagerMain, +}; +} // namespace + +void FingerprintEventManagerFuzzTest(Parcel &parcel) +{ + uint32_t index = parcel.ReadUint32() % (sizeof(g_FingerprintEventManagerFuzzFuncs) / sizeof(FingerprintEventManagerFuzzFunc *)); + auto fuzzFunc = g_FingerprintEventManagerFuzzFuncs[index]; + fuzzFunc(parcel); + return; +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/services/BUILD.gn b/test/unittest/services/BUILD.gn index 3aa2a2b37..fa2cde940 100644 --- a/test/unittest/services/BUILD.gn +++ b/test/unittest/services/BUILD.gn @@ -97,6 +97,7 @@ ohos_unittest("iam_services_test") { "../../../services/core/src/resource_node_impl.cpp", "../../../services/core/src/resource_node_pool_impl.cpp", "../../../services/core/src/resource_node_utils.cpp", + "../../../services/core/src/fingerprint_event_manager.cpp", "../../../services/core/src/risk_event_manager.cpp", "../../../services/core/src/schedule_node_builder.cpp", "../../../services/core/src/schedule_node_helper.cpp", @@ -159,6 +160,7 @@ ohos_unittest("iam_services_test") { "src/remote_auth_invoker_context_test.cpp", "src/resource_node_pool_test.cpp", "src/resource_node_test.cpp", + "src/fingerprint_event_manager_test.cpp", "src/resource_node_utils_test.cpp", "src/risk_event_manager_test.cpp", "src/schedule_node_helper_test.cpp", diff --git a/test/unittest/services/inc/fingerprint_event_manager_test.h b/test/unittest/services/inc/fingerprint_event_manager_test.h new file mode 100644 index 000000000..1ead49d4a --- /dev/null +++ b/test/unittest/services/inc/fingerprint_event_manager_test.h @@ -0,0 +1,37 @@ +/* + * 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 FINGERPRINT_EVENT_MANAGER_TEST_H +#define FINGERPRINT_EVENT_MANAGER_TEST_H + +#include +#include + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class FingerprintEventManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // Fingerprint_EVENT_MANAGER_TEST_H \ No newline at end of file diff --git a/test/unittest/services/src/fingerprint_event_manager_test.cpp b/test/unittest/services/src/fingerprint_event_manager_test.cpp new file mode 100644 index 000000000..36c1013ac --- /dev/null +++ b/test/unittest/services/src/fingerprint_event_manager_test.cpp @@ -0,0 +1,56 @@ +/* + * 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 "fingerprint_event_manager_test.h" + +#include "fingerprint_event_manager.h" + +#include "iam_logger.h" +#include "securec.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +using namespace testing; +using namespace testing::ext; + +void FingerprintEventManagerTest::SetUpTestCase() +{ +} + +void FingerprintEventManagerTest::TearDownTestCase() +{ +} + +void FingerprintEventManagerTest::SetUp() +{ +} + +void FingerprintEventManagerTest::TearDown() +{ +} + +HWTEST_F(FingerprintEventManagerTest, SendServiceEventTest, TestSize.Level0) +{ + bool status = true; + ServiceEventType event = SCREENLOCK; + EXPECT_NO_THROW(FingerprintEventManager::GetInstance().SendServiceEvent(status, event)); +} + +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file -- Gitee