diff --git a/interfaces/inner_api/iam_common_defines.h b/interfaces/inner_api/iam_common_defines.h index e5b40164969292654ed8587726d461945bdfb68b..22754c4a940d7c5cf6eb0b5f59c2c3d6a9fe7a1b 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 60888b650d68d65323e7e232444c8b8a9f736a6f..325a940c431a2a020c5ea8e7d1332a1962eaa0ec 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 0000000000000000000000000000000000000000..9babc27bcdfe24aa4402c2e3e69a12573c2d80a5 --- /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 0000000000000000000000000000000000000000..0f9f3dc023eea4e6fe6b9c3e79f6c414d29e705c --- /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 5b75a929c5523249b399584be113a8fb11196c3a..6db7793ad0306e950f67af334d186664ca9c2f40 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 aef2668f60460f6379a9b43aaa95886fb46bb916..643c81fd7e81959cd3de4179224e5c217e036649 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 07337893645a745a6934b9ac2a934ab0fb74c5b1..781ce77fb47f8488ce166441d6f29b197b276e8d 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 0000000000000000000000000000000000000000..29729a2f3c154d4f994083a02d49677fb268ccb3 --- /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 0000000000000000000000000000000000000000..85ab377b2eeabced62c92d82937afac69ba22265 --- /dev/null +++ b/test/fuzztest/services/servicecore_fuzzer/src/fingerprint_event_manager_fuzzer.cpp @@ -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. + */ + +#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 3aa2a2b375b3a5949fbf52266b0224ea66646c06..89a1d2498ef16354d48b3c50e2d65f95d99345f8 100644 --- a/test/unittest/services/BUILD.gn +++ b/test/unittest/services/BUILD.gn @@ -98,6 +98,7 @@ ohos_unittest("iam_services_test") { "../../../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", @@ -161,6 +162,7 @@ ohos_unittest("iam_services_test") { "src/resource_node_test.cpp", "src/resource_node_utils_test.cpp", "src/risk_event_manager_test.cpp", + "src/fingerprint_event_manager_test.cpp", "src/schedule_node_helper_test.cpp", "src/schedule_node_test.cpp", "src/screenlock_status_listener_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 0000000000000000000000000000000000000000..1ead49d4a3f9e0be87750aff8a0e0271799f913d --- /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 0000000000000000000000000000000000000000..36c1013ac4e711cb5afafcdd622d590df46e669f --- /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