diff --git a/frameworks/native/client/src/user_idm_callback_service.cpp b/frameworks/native/client/src/user_idm_callback_service.cpp index 2a64412c6e90e3f005d1e2dda78ace0499bedfdf..d78c68f65cb7a7c88bf88625fe5582c2885c43ca 100644 --- a/frameworks/native/client/src/user_idm_callback_service.cpp +++ b/frameworks/native/client/src/user_idm_callback_service.cpp @@ -114,6 +114,8 @@ int32_t IdmGetCredInfoCallbackService::OnCredentialInfos(int32_t resultCode, credentialInfo.pinType = static_cast(iter.pinType); credentialInfo.credentialId = iter.credentialId; credentialInfo.templateId = iter.templateId; + credentialInfo.isAbandoned = iter.isAbandoned; + credentialInfo.validityPeriod = iter.validityPeriod; credInfoList.push_back(credentialInfo); } getCredInfoCallback_->OnCredentialInfo(resultCode, credInfoList); diff --git a/frameworks/native/executors/BUILD.gn b/frameworks/native/executors/BUILD.gn index 727096cb3ffc7403658c3028370dc866fe71d23a..b841754968ad84d25281037e5a2136c4544d8d19 100644 --- a/frameworks/native/executors/BUILD.gn +++ b/frameworks/native/executors/BUILD.gn @@ -32,6 +32,7 @@ ohos_shared_library("userauth_executors") { } branch_protector_ret = "pac_ret" sources = [ + "src/async_command/abandon_command.cpp", "src/async_command/async_command_base.cpp", "src/async_command/auth_command.cpp", "src/async_command/collect_command.cpp", diff --git a/frameworks/native/executors/include/async_command/abandon_command.h b/frameworks/native/executors/include/async_command/abandon_command.h new file mode 100644 index 0000000000000000000000000000000000000000..ec5a4e3feb5bfdfce8b70b77d87ce1dba3b2e969 --- /dev/null +++ b/frameworks/native/executors/include/async_command/abandon_command.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ABANDON_COMMAND_H +#define ABANDON_COMMAND_H + +#include "async_command_base.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class AbandonCommand : public AsyncCommandBase { +public: + AbandonCommand(std::weak_ptr executor, uint64_t scheduleId, const Attributes &commandAttrs, + std::shared_ptr executorMessenger); + ~AbandonCommand() override = default; + +protected: + ResultCode SendRequest() override; + void OnResultInner(ResultCode result, const std::vector &extraInfo) override; + +private: + std::shared_ptr attributes_ {nullptr}; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS + +#endif // ABANDON_COMMAND_H \ No newline at end of file diff --git a/frameworks/native/executors/include/framework/framework_executor_callback.h b/frameworks/native/executors/include/framework/framework_executor_callback.h index 33052d24a20c7504da2100b1d1e62d9e38e8803f..bfb8f97512a4e22dd76ddc8d3d7bd4ee44da467a 100644 --- a/frameworks/native/executors/include/framework/framework_executor_callback.h +++ b/frameworks/native/executors/include/framework/framework_executor_callback.h @@ -64,6 +64,7 @@ private: ResultCode ProcessNotifyExecutorReady(const Attributes &properties); ResultCode ProcessCustomCommand(const Attributes &properties); ResultCode ProcessGetPropertyCommand(std::shared_ptr conditions, std::shared_ptr values); + ResultCode ProcessAbandonCommand(uint64_t scheduleId, const Attributes &properties); ResultCode FillPropertyToAttribute(const std::vector &keyList, const Property property, std::shared_ptr values); const char *GetDescription(); diff --git a/frameworks/native/executors/src/async_command/abandon_command.cpp b/frameworks/native/executors/src/async_command/abandon_command.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f92e5cd687f99207a27f29758d058ecbf4943d61 --- /dev/null +++ b/frameworks/native/executors/src/async_command/abandon_command.cpp @@ -0,0 +1,89 @@ +/* + * 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 "abandon_command.h" + +#include "iam_executor_framework_types.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_mem.h" +#include "iam_para2str.h" +#include "iam_ptr.h" +#include "iam_defines.h" +#include "hisysevent_adapter.h" + +#define LOG_TAG "USER_AUTH_EXECUTOR" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +AbandonCommand::AbandonCommand(std::weak_ptr executor, uint64_t scheduleId, + const Attributes &attributes, std::shared_ptr executorMessenger) + : AsyncCommandBase("Abandon", scheduleId, executor, executorMessenger), + attributes_(Common::MakeShared(attributes.Serialize())) +{ +} + +ResultCode AbandonCommand::SendRequest() +{ + IAM_LOGI("%{public}s send request start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR); + + auto hdi = GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + + uint32_t tokenId = 0; + bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId); + IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet == true, ResultCode::GENERAL_ERROR); + + std::vector extraInfo; + bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo); + IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet == true, ResultCode::GENERAL_ERROR); + + int32_t userId; + bool getUserId = attributes_->GetInt32Value(Attributes::ATTR_USER_ID, userId); + IF_FALSE_LOGE_AND_RETURN_VAL(getUserId == true, ResultCode::GENERAL_ERROR); + + std::vector templateIds; + bool getTempalteIds = attributes_->GetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIds); + IF_FALSE_LOGE_AND_RETURN_VAL(getTempalteIds == true, ResultCode::GENERAL_ERROR); + + ResultCode ret = hdi->Abandon(scheduleId_, (DeleteParam) { tokenId, userId, templateIds[0], extraInfo}, + shared_from_this()); + IAM_LOGI("%{public}s abandon result %{public}d", GetDescription(), ret); + return ret; +} + +void AbandonCommand::OnResultInner(ResultCode result, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on result start", GetDescription()); + std::vector nonConstExtraInfo(extraInfo.begin(), extraInfo.end()); + auto authAttributes = Common::MakeShared(); + IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr); + bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result); + IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true); + bool setAuthResultRet = + authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo); + IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true); + int32_t ret = MessengerFinish(scheduleId_, result, authAttributes); + if (ret != USERAUTH_SUCCESS) { + IAM_LOGE("%{public}s call finish fail", GetDescription()); + return; + } + IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result); +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS diff --git a/frameworks/native/executors/src/framework/framework_executor_callback.cpp b/frameworks/native/executors/src/framework/framework_executor_callback.cpp index 43db9e7fa23870a6002692e875125e3762e66dd7..837d8464bcbb27a2baba83b9251b76a0e2408579 100644 --- a/frameworks/native/executors/src/framework/framework_executor_callback.cpp +++ b/frameworks/native/executors/src/framework/framework_executor_callback.cpp @@ -18,6 +18,7 @@ #include #include +#include "abandon_command.h" #include "auth_command.h" #include "collect_command.h" #include "custom_command.h" @@ -74,6 +75,9 @@ ResultCode FrameworkExecutorCallback::OnBeginExecuteInner(uint64_t scheduleId, s case IDENTIFY: ret = ProcessIdentifyCommand(scheduleId, commandAttrs); break; + case ABANDON: + ret = ProcessAbandonCommand(scheduleId, commandAttrs); + break; default: IAM_LOGE("command id %{public}u is not supported", commandId); } @@ -354,6 +358,14 @@ ResultCode FrameworkExecutorCallback::ProcessGetPropertyCommand(std::shared_ptr< return ResultCode::SUCCESS; } +ResultCode FrameworkExecutorCallback::ProcessAbandonCommand(uint64_t scheduleId, const Attributes &properties) +{ + std::lock_guard lock(mutex_); + auto command = Common::MakeShared(executor_, scheduleId, properties, executorMessenger_); + IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR); + return command->StartProcess(); +} + ResultCode FrameworkExecutorCallback::FillPropertyToAttribute(const std::vector &keyList, const Property property, std::shared_ptr values) { diff --git a/frameworks/native/executors/src/iauth_executor_hdi.cpp b/frameworks/native/executors/src/iauth_executor_hdi.cpp index 7f703407a5022dd2e89a0a319b253150bd224760..56b601d781c71db35670590808adcfc95caf9767 100644 --- a/frameworks/native/executors/src/iauth_executor_hdi.cpp +++ b/frameworks/native/executors/src/iauth_executor_hdi.cpp @@ -76,6 +76,13 @@ ResultCode IAuthExecutorHdi::SetCachedTemplates(const std::vector &tem return GENERAL_ERROR; } +ResultCode IAuthExecutorHdi::Abandon(uint64_t scheduleId, const DeleteParam ¶m, + const std::shared_ptr &callbackObj) +{ + IAM_LOGE("method not implemented"); + return GENERAL_ERROR; +} + ResultCode IAuthExecutorHdi::NotifyCollectorReady(uint64_t scheduleId) { IAM_LOGE("method not implemented"); diff --git a/frameworks/native/ipc/idl/UserAuthTypes.idl b/frameworks/native/ipc/idl/UserAuthTypes.idl index 50273566de61a2c01945b0d5fd404d1915ed3195..be3edc4ccca1efe3d7b8dd8f2715e45f108c299e 100644 --- a/frameworks/native/ipc/idl/UserAuthTypes.idl +++ b/frameworks/native/ipc/idl/UserAuthTypes.idl @@ -173,6 +173,10 @@ struct IpcCredentialInfo { unsigned long credentialId; /* User templateId is generated by executor to represent user characteristics. */ unsigned long templateId; + /* Abandoned flag of pin credential. */ + boolean isAbandoned; + /* Valid period of credential remain. */ + long validityPeriod; }; /** diff --git a/interfaces/inner_api/iam_common_defines.h b/interfaces/inner_api/iam_common_defines.h index 963459e07446ed3ba7c19df1cdbcaf1b968b3cb5..2985756f23114dadf7bb69aa16e31f4bf6968eae 100644 --- a/interfaces/inner_api/iam_common_defines.h +++ b/interfaces/inner_api/iam_common_defines.h @@ -36,7 +36,7 @@ constexpr int32_t USER_AUTH_TIP_SINGLE_AUTH_RESULT = 9999; constexpr int INVALID_SOCKET_ID = -1; constexpr int INVALID_USER_ID = -1; constexpr int32_t MAIN_USER_ID = 100; -constexpr int32_t MAX_USER = 32; +constexpr int32_t MAX_USER = 5; /** Max value of verifyAuthToken allowable duration. */ constexpr uint32_t MAX_TOKEN_ALLOWABLE_DURATION = 24 * 60 * 60 * 1000; @@ -137,6 +137,8 @@ enum ScheduleMode : int32_t { AUTH = 1, /** The schedule mode is identification. */ IDENTIFY = 2, + /** The schedule mode is abandon. */ + ABANDON = 4, }; /** diff --git a/interfaces/inner_api/iam_executor/iam_executor_framework_types.h b/interfaces/inner_api/iam_executor/iam_executor_framework_types.h index 960891ec79f0e7df83df7eecc0c5f9a3fc69a466..6573b3f673ef797d688418c8abecd79b63a1817f 100644 --- a/interfaces/inner_api/iam_executor/iam_executor_framework_types.h +++ b/interfaces/inner_api/iam_executor/iam_executor_framework_types.h @@ -124,6 +124,19 @@ struct Property { int32_t nextFailLockoutDuration; }; +/** + * @brief Defines Delete parameter. + */ +struct DeleteParam { + /** Token id. */ + uint32_t tokenId; + /** User id. */ + int32_t userId; + /** Template id. */ + uint64_t templateId; + /** Extra info. */ + std::vector extraInfo; +}; } // namespace UserAuth } // namespace UserIam } // namespace OHOS diff --git a/interfaces/inner_api/iam_executor/iam_executor_iauth_executor_hdi.h b/interfaces/inner_api/iam_executor/iam_executor_iauth_executor_hdi.h index 34a7e483ffb140432a27c36f01179663e5e12541..08f3fed157eace40dfacf2a93e93e6168013d811 100644 --- a/interfaces/inner_api/iam_executor/iam_executor_iauth_executor_hdi.h +++ b/interfaces/inner_api/iam_executor/iam_executor_iauth_executor_hdi.h @@ -173,6 +173,17 @@ public: * @return Return the result success or error code{@link ResultCode}. */ virtual ResultCode NotifyCollectorReady(uint64_t scheduleId); + + /** + * @brief Begin abandon. + * + * @param scheduleId Current abandon schedule ID. + * @param param Abandon param. + * @param callbackObj Callback of abandon result. + * @return Return the result success or error code{@link ResultCode}. + */ + virtual ResultCode Abandon(uint64_t scheduleId, const DeleteParam ¶m, + const std::shared_ptr &callbackObj); }; } // namespace UserAuth } // namespace UserIam diff --git a/interfaces/inner_api/user_idm_client_defines.h b/interfaces/inner_api/user_idm_client_defines.h index d55f21a1962c90f77d630ea0d7be8b5ced6608a6..6593bd0aa0443cca3e87cc4fc342db18cf0b6fd1 100644 --- a/interfaces/inner_api/user_idm_client_defines.h +++ b/interfaces/inner_api/user_idm_client_defines.h @@ -44,6 +44,10 @@ struct CredentialInfo { uint64_t credentialId {0}; /* User templateId is generated by executor to represent user characteristics. */ uint64_t templateId {0}; + /* Abandoned flag of pin credential. */ + bool isAbandoned; + /* Valid period of credential remain. */ + int64_t validityPeriod {0}; }; /** diff --git a/services/base/BUILD.gn b/services/base/BUILD.gn index c8bb87834e937a368be8a8cfc86fef56e655832c..4f767465cfe043fcf0bcad6a7c19f67036ba2ffd 100644 --- a/services/base/BUILD.gn +++ b/services/base/BUILD.gn @@ -46,7 +46,7 @@ ohos_source_set("userauth_service_base") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hilog:libhilog", "init:libbegetutil", diff --git a/services/base/inc/user_auth_hdi.h b/services/base/inc/user_auth_hdi.h index 31c1c3696bda2f5454e6561ecaddfd4232579ee4..a963c91b0b05823e7c7465c368de39c8b33b1603 100644 --- a/services/base/inc/user_auth_hdi.h +++ b/services/base/inc/user_auth_hdi.h @@ -17,10 +17,10 @@ #define USER_AUTH_HDI #include "accesstoken_kit.h" -#include "v3_0/iuser_auth_interface.h" -#include "v3_0/message_callback_stub.h" -#include "v3_0/user_auth_types.h" -#include "v3_0/user_auth_interface_service.h" +#include "v4_0/iuser_auth_interface.h" +#include "v4_0/message_callback_stub.h" +#include "v4_0/user_auth_types.h" +#include "v4_0/user_auth_interface_service.h" namespace OHOS { namespace UserIam { @@ -45,34 +45,36 @@ static inline HdiCallerType ConvertATokenTypeToCallerType(int32_t in) return it->second; } -using IUserAuthInterface = OHOS::HDI::UserAuth::V3_0::IUserAuthInterface; -using HdiAuthType = OHOS::HDI::UserAuth::V3_0::AuthType; -using HdiExecutorRole = OHOS::HDI::UserAuth::V3_0::ExecutorRole; -using HdiExecutorSecureLevel = OHOS::HDI::UserAuth::V3_0::ExecutorSecureLevel; -using HdiPinSubType = OHOS::HDI::UserAuth::V3_0::PinSubType; -using HdiScheduleMode = OHOS::HDI::UserAuth::V3_0::ScheduleMode; -using HdiExecutorRegisterInfo = OHOS::HDI::UserAuth::V3_0::ExecutorRegisterInfo; -using HdiExecutorInfo = OHOS::HDI::UserAuth::V3_0::ExecutorInfo; -using HdiScheduleInfo = OHOS::HDI::UserAuth::V3_0::ScheduleInfo; -using HdiAuthParam = OHOS::HDI::UserAuth::V3_0::AuthParam; -using HdiExecutorSendMsg = OHOS::HDI::UserAuth::V3_0::ExecutorSendMsg; -using HdiAuthResultInfo = OHOS::HDI::UserAuth::V3_0::AuthResultInfo; -using HdiIdentifyResultInfo = OHOS::HDI::UserAuth::V3_0::IdentifyResultInfo; -using HdiEnrollParam = OHOS::HDI::UserAuth::V3_0::EnrollParam; -using HdiUserType = OHOS::HDI::UserAuth::V3_0::UserType; -using HdiCredentialInfo = OHOS::HDI::UserAuth::V3_0::CredentialInfo; -using HdiEnrolledInfo = OHOS::HDI::UserAuth::V3_0::EnrolledInfo; -using HdiEnrollResultInfo = OHOS::HDI::UserAuth::V3_0::EnrollResultInfo; -using HdiEnrolledState = OHOS::HDI::UserAuth::V3_0::EnrolledState; -using HdiReuseUnlockInfo = OHOS::HDI::UserAuth::V3_0::ReuseUnlockInfo; -using HdiReuseUnlockParam = OHOS::HDI::UserAuth::V3_0::ReuseUnlockParam; -using HdiIMessageCallback = OHOS::HDI::UserAuth::V3_0::IMessageCallback; -using UserInfo = OHOS::HDI::UserAuth::V3_0::UserInfo; -using ExtUserInfo = OHOS::HDI::UserAuth::V3_0::ExtUserInfo; -using HdiGlobalConfigType = OHOS::HDI::UserAuth::V3_0::GlobalConfigType; -using HdiGlobalConfigValue = OHOS::HDI::UserAuth::V3_0::GlobalConfigValue; -using HdiGlobalConfigParam = OHOS::HDI::UserAuth::V3_0::GlobalConfigParam; -using HdiUserAuthTokenPlain = OHOS::HDI::UserAuth::V3_0::UserAuthTokenPlain; +using IUserAuthInterface = OHOS::HDI::UserAuth::V4_0::IUserAuthInterface; +using HdiAuthType = OHOS::HDI::UserAuth::V4_0::AuthType; +using HdiExecutorRole = OHOS::HDI::UserAuth::V4_0::ExecutorRole; +using HdiExecutorSecureLevel = OHOS::HDI::UserAuth::V4_0::ExecutorSecureLevel; +using HdiPinSubType = OHOS::HDI::UserAuth::V4_0::PinSubType; +using HdiScheduleMode = OHOS::HDI::UserAuth::V4_0::ScheduleMode; +using HdiExecutorRegisterInfo = OHOS::HDI::UserAuth::V4_0::ExecutorRegisterInfo; +using HdiExecutorInfo = OHOS::HDI::UserAuth::V4_0::ExecutorInfo; +using HdiScheduleInfo = OHOS::HDI::UserAuth::V4_0::ScheduleInfo; +using HdiAuthParam = OHOS::HDI::UserAuth::V4_0::AuthParam; +using HdiExecutorSendMsg = OHOS::HDI::UserAuth::V4_0::ExecutorSendMsg; +using HdiAuthResultInfo = OHOS::HDI::UserAuth::V4_0::AuthResultInfo; +using HdiIdentifyResultInfo = OHOS::HDI::UserAuth::V4_0::IdentifyResultInfo; +using HdiEnrollParam = OHOS::HDI::UserAuth::V4_0::EnrollParam; +using HdiUserType = OHOS::HDI::UserAuth::V4_0::UserType; +using HdiCredentialInfo = OHOS::HDI::UserAuth::V4_0::CredentialInfo; +using HdiEnrolledInfo = OHOS::HDI::UserAuth::V4_0::EnrolledInfo; +using HdiEnrollResultInfo = OHOS::HDI::UserAuth::V4_0::EnrollResultInfo; +using HdiEnrolledState = OHOS::HDI::UserAuth::V4_0::EnrolledState; +using HdiReuseUnlockInfo = OHOS::HDI::UserAuth::V4_0::ReuseUnlockInfo; +using HdiReuseUnlockParam = OHOS::HDI::UserAuth::V4_0::ReuseUnlockParam; +using HdiIMessageCallback = OHOS::HDI::UserAuth::V4_0::IMessageCallback; +using UserInfo = OHOS::HDI::UserAuth::V4_0::UserInfo; +using ExtUserInfo = OHOS::HDI::UserAuth::V4_0::ExtUserInfo; +using HdiGlobalConfigType = OHOS::HDI::UserAuth::V4_0::GlobalConfigType; +using HdiGlobalConfigValue = OHOS::HDI::UserAuth::V4_0::GlobalConfigValue; +using HdiGlobalConfigParam = OHOS::HDI::UserAuth::V4_0::GlobalConfigParam; +using HdiUserAuthTokenPlain = OHOS::HDI::UserAuth::V4_0::UserAuthTokenPlain; +using HdiCredentialOperateType = OHOS::HDI::UserAuth::V4_0::CredentialOperateType; +using HdiCredentialOperateResult = OHOS::HDI::UserAuth::V4_0::CredentialOperateResult; } // namespace UserAuth } // namespace UserIam } // namespace OHOS diff --git a/services/context/BUILD.gn b/services/context/BUILD.gn index 45953159b3e0a0b626c7fb34d8fb7e2e292ff0c9..c8afb16d2edd3d99170c6745b1a24d689081b5ce 100644 --- a/services/context/BUILD.gn +++ b/services/context/BUILD.gn @@ -44,6 +44,7 @@ ohos_source_set("userauth_service_context") { "src/context_factory.cpp", "src/context_helper.cpp", "src/context_pool_impl.cpp", + "src/delete_context.cpp", "src/enroll_context.cpp", "src/identify_context.cpp", "src/keyguard_status_listener.cpp", @@ -80,7 +81,7 @@ ohos_source_set("userauth_service_context") { "ability_runtime:extension_manager", "access_token:libaccesstoken_sdk", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hilog:libhilog", "hitrace:hitrace_meter", diff --git a/services/context/inc/context.h b/services/context/inc/context.h index f93d14b91094280eee338b380417ff264f978b73..9376029b2cd22a4dba5a38cc3ef032b07196108b 100644 --- a/services/context/inc/context.h +++ b/services/context/inc/context.h @@ -44,6 +44,7 @@ enum ContextType { REMOTE_AUTH_CONTEXT, REMOTE_AUTH_INVOKER_CONTEXT, SCHEDULE_HOLDER_CONTEXT, + CONTEXT_DELETE, }; class Context { diff --git a/services/context/inc/context_factory.h b/services/context/inc/context_factory.h index 575a47178140aa8d537416c78c85fbad8fb9d1a1..068510ffcc199593c0c64cc0a944e3c2333e2303 100644 --- a/services/context/inc/context_factory.h +++ b/services/context/inc/context_factory.h @@ -23,6 +23,7 @@ #include "singleton.h" #include "authentication_impl.h" +#include "delete_impl.h" #include "enrollment_impl.h" #include "context.h" #include "context_callback.h" @@ -73,6 +74,8 @@ public: static std::shared_ptr CreateWidgetContext(const AuthWidgetContextPara ¶, std::shared_ptr callback); static std::shared_ptr CreateScheduleHolderContext(std::shared_ptr scheduleNode); + static std::shared_ptr CreateDeleteContext(const Deletion::DeleteParam ¶, + const std::shared_ptr &callback); }; } // namespace UserAuth } // namespace UserIam diff --git a/services/context/inc/delete_context.h b/services/context/inc/delete_context.h new file mode 100644 index 0000000000000000000000000000000000000000..f00f7f9ce0dcc53380cedcb41905540c8860f816 --- /dev/null +++ b/services/context/inc/delete_context.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IAM_DELETE_CONTEXT_H +#define IAM_DELETE_CONTEXT_H + +#include +#include + +#include "base_context.h" +#include "deletion.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class DeleteContext : public BaseContext { +public: + DeleteContext(uint64_t contextId, std::shared_ptr deletion, std::shared_ptr callback); + ~DeleteContext() override = default; + ContextType GetContextType() const override; + uint32_t GetTokenId() const override; + int32_t GetUserId() const override; + +protected: + bool OnStart() override; + void OnResult(int32_t resultCode, const std::shared_ptr &scheduleResultAttr) override; + bool OnStop() override; + +private: + bool UpdateScheduleResult(const std::shared_ptr &scheduleResultAttr); + void InvokeResultCallback(int32_t resultCode) const; + std::shared_ptr deletion_ = nullptr; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // IAM_DELETE_CONTEXT_H \ No newline at end of file diff --git a/services/context/src/context_factory.cpp b/services/context/src/context_factory.cpp index 7ae3823d5811cf6f1cc791777f774e94ee7ff3cc..bec7fe230b2ce67e3b355b0595b0154843c325e8 100644 --- a/services/context/src/context_factory.cpp +++ b/services/context/src/context_factory.cpp @@ -16,6 +16,7 @@ #include "context_callback_impl.h" #include "context_pool.h" +#include "delete_context.h" #include "enroll_context.h" #include "iam_check.h" #include "iam_logger.h" @@ -120,6 +121,17 @@ std::shared_ptr ContextFactory::CreateScheduleHolderContext(std::shared uint64_t newContextId = ContextPool::GetNewContextId(); return Common::MakeShared(newContextId, scheduleNode); } + +std::shared_ptr ContextFactory::CreateDeleteContext(const Deletion::DeleteParam ¶, + const std::shared_ptr &callback) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr); + uint64_t newContextId = ContextPool::GetNewContextId(); + auto deleteImpl = Common::MakeShared(para); + IF_FALSE_LOGE_AND_RETURN_VAL(deleteImpl != nullptr, nullptr); + deleteImpl->SetAccessTokenId(para.tokenId); + return Common::MakeShared(newContextId, deleteImpl, callback); +} } // namespace UserAuth } // namespace UserIam } // namespace OHOS diff --git a/services/context/src/delete_context.cpp b/services/context/src/delete_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6e883d7b5db31b505950d4f610ffb3af8694a30 --- /dev/null +++ b/services/context/src/delete_context.cpp @@ -0,0 +1,147 @@ +/* + * 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 "delete_context.h" + +#include "hisysevent_adapter.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_para2str.h" +#include "resource_node_utils.h" +#include "schedule_node.h" +#include "schedule_node_callback.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +DeleteContext::DeleteContext(uint64_t contextId, std::shared_ptr deletion, + std::shared_ptr callback) + : BaseContext("Delete", contextId, callback, false), deletion_(deletion) +{ +} + +ContextType DeleteContext::GetContextType() const +{ + return CONTEXT_DELETE; +} + +uint32_t DeleteContext::GetTokenId() const +{ + IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, 0); + return deletion_->GetAccessTokenId(); +} + +int32_t DeleteContext::GetUserId() const +{ + IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, INVALID_USER_ID); + return deletion_->GetUserId(); +} + +bool DeleteContext::OnStart() +{ + IAM_LOGI("%{public}s start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, false); + bool isCredentilaDelete = false; + bool startRet = deletion_->Start(scheduleList_, shared_from_this(), isCredentilaDelete); + if (!startRet) { + IAM_LOGE("%{public}s abandon start fail", GetDescription()); + SetLatestError(deletion_->GetLatestError()); + return startRet; + } + + if (isCredentilaDelete) { + InvokeResultCallback(ResultCode::SUCCESS); + } else { + IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false); + IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false); + bool startScheduleRet = scheduleList_[0]->StartSchedule(); + IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false); + IAM_LOGI("%{public}s Schedule:%{public}s Type:%{public}d success", GetDescription(), + GET_MASKED_STRING(scheduleList_[0]->GetScheduleId()).c_str(), scheduleList_[0]->GetAuthType()); + } + return true; +} + +void DeleteContext::OnResult(int32_t resultCode, const std::shared_ptr &scheduleResultAttr) +{ + IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode); + bool updateRet = UpdateScheduleResult(scheduleResultAttr); + if (!updateRet) { + IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription()); + if (resultCode == SUCCESS) { + resultCode = GetLatestError(); + } + } + InvokeResultCallback(resultCode); + IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode); +} + +bool DeleteContext::OnStop() +{ + IAM_LOGI("%{public}s start", GetDescription()); + if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) { + scheduleList_[0]->StopSchedule(); + } + + IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, false); + bool cancelRet = deletion_->Cancel(); + if (!cancelRet) { + IAM_LOGE("%{public}s abandon stop fail", GetDescription()); + SetLatestError(deletion_->GetLatestError()); + return cancelRet; + } + return true; +} + +bool DeleteContext::UpdateScheduleResult(const std::shared_ptr &scheduleResultAttr) +{ + IAM_LOGI("%{public}s start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, false); + IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false); + std::vector scheduleResult; + bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult); + IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false); + std::shared_ptr infoToDel; + bool updateRet = deletion_->Update(scheduleResult, infoToDel); + if (!updateRet) { + IAM_LOGE("%{public}s abandon update fail", GetDescription()); + SetLatestError(deletion_->GetLatestError()); + return updateRet; + } + if (infoToDel == nullptr) { + IAM_LOGI("no credential to delete"); + } else { + std::vector> credInfos = {infoToDel}; + int32_t ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(credInfos, "DeleteForUpdate"); + if (ret != SUCCESS) { + IAM_LOGE("failed to notify executor delete template, error code : %{public}d", ret); + } + } + + return true; +} + +void DeleteContext::InvokeResultCallback(int32_t resultCode) const +{ + IAM_LOGI("%{public}s start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr); + Attributes finalResult; + callback_->OnResult(resultCode, finalResult); + IAM_LOGI("%{public}s invoke result callback success", GetDescription()); +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 185cb305c3370bc76669d60f838fce1f8c8fd951..20d094fded3eca0537cd3266e099634745e525dd 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -58,6 +58,7 @@ ohos_source_set("userauth_service_core") { sources = [ "src/authentication_impl.cpp", "src/credential_info_impl.cpp", + "src/delete_impl.cpp", "src/driver_state_manager.cpp", "src/enrolled_info_impl.cpp", "src/enrollment_impl.cpp", @@ -95,7 +96,7 @@ ohos_source_set("userauth_service_core") { "access_token:libtokenid_sdk", "c_utils:utils", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hdf_core:libhdi", "hilog:libhilog", diff --git a/services/core/inc/credential_info_interface.h b/services/core/inc/credential_info_interface.h index 222aec092837e605a6660e9062b9a4baa3794f10..299ba6a963f9f32045eef77fd90e13dba9b4d559 100644 --- a/services/core/inc/credential_info_interface.h +++ b/services/core/inc/credential_info_interface.h @@ -34,6 +34,8 @@ public: virtual uint32_t GetExecutorSensorHint() const = 0; virtual uint32_t GetExecutorMatcher() const = 0; virtual PinSubType GetAuthSubType() const = 0; + virtual bool GetAbandonFlag() const; + virtual int64_t GetValidPeriod() const; }; } // namespace UserAuth } // namespace UserIam diff --git a/services/core/inc/deletion.h b/services/core/inc/deletion.h new file mode 100644 index 0000000000000000000000000000000000000000..384613e950dbb9b16632b82f992a044d1ea9eba0 --- /dev/null +++ b/services/core/inc/deletion.h @@ -0,0 +1,58 @@ +/* + * 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 IAM_DELETION_H +#define IAM_DELETION_H + +#include +#include +#include + +#include "credential_info_interface.h" +#include "update_pin_param_interface.h" +#include "schedule_node.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class Deletion { +public: + struct DeleteParam { + int32_t userId {0}; + uint64_t credentialId {0}; + uint32_t tokenId {0}; + std::vector token; + }; + + virtual ~Deletion() = default; + + virtual bool Start(std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) = 0; + virtual bool Update(const std::vector &scheduleResult, + std::shared_ptr &info) = 0; + virtual bool Cancel() = 0; + + virtual void SetAccessTokenId(uint32_t tokenId) = 0; + virtual uint32_t GetAccessTokenId() const = 0; + virtual int32_t GetLatestError() const = 0; + virtual int32_t GetUserId() const = 0; + +protected: + virtual void SetLatestError(int32_t error) = 0; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // IAM_DELETION_H \ No newline at end of file diff --git a/services/core/inc/user_idm_database.h b/services/core/inc/user_idm_database.h index f0b0db5c8c47f48f0ef35f83f807391bbfe456a8..5f417a1b701818d3e2d0e887421b96f58a651053 100644 --- a/services/core/inc/user_idm_database.h +++ b/services/core/inc/user_idm_database.h @@ -42,6 +42,8 @@ public: virtual int32_t GetAllExtUserInfo(std::vector> &userInfos) = 0; virtual int32_t GetCredentialInfoById(uint64_t credentialId, std::shared_ptr &credInfo) = 0; + virtual int32_t ClearUnavailableCredential(int32_t userId, + std::vector> &credInfos) = 0; }; } // namespace UserAuth } // namespace UserIam diff --git a/services/core/src/credential_info_impl.cpp b/services/core/src/credential_info_impl.cpp index 4a2a57d213039dfd5a1031939df9679c59bdb142..c6ab32ac075e913ec339b35ca4171eb267d89a4f 100644 --- a/services/core/src/credential_info_impl.cpp +++ b/services/core/src/credential_info_impl.cpp @@ -61,6 +61,16 @@ PinSubType CredentialInfoImpl::GetAuthSubType() const { return static_cast(info_.authSubType); } + +bool CredentialInfoImpl::GetAbandonFlag() const +{ + return info_.isAbandoned; +} + +int64_t CredentialInfoImpl::GetValidPeriod() const +{ + return info_.validityPeriod; +} } // namespace UserAuth } // namespace UserIam } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/credential_info_impl.h b/services/core/src/credential_info_impl.h index 99477f12c6c8e3a5439f63e872cd39852d8d2212..277f2b9de67613f69541c3f15a1c460958405dfc 100644 --- a/services/core/src/credential_info_impl.h +++ b/services/core/src/credential_info_impl.h @@ -36,6 +36,8 @@ public: uint32_t GetExecutorSensorHint() const override; uint32_t GetExecutorMatcher() const override; PinSubType GetAuthSubType() const override; + bool GetAbandonFlag() const override; + int64_t GetValidPeriod() const override; private: int32_t userId_; diff --git a/services/core/src/delete_impl.cpp b/services/core/src/delete_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65175b7028b0e367478ac02112e79202992b8700 --- /dev/null +++ b/services/core/src/delete_impl.cpp @@ -0,0 +1,181 @@ +/* + * 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 "delete_impl.h" + +#include "credential_info_impl.h" +#include "hdi_wrapper.h" +#include "iam_hitrace_helper.h" +#include "iam_logger.h" +#include "iam_ptr.h" +#include "ipc_common.h" +#include "load_mode_handler.h" +#include "publish_event_adapter.h" +#include "resource_node_utils.h" +#include "schedule_node_helper.h" +#include "update_pin_param_impl.h" +#include "user_idm_database.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +DeleteImpl::DeleteImpl(DeleteParam abandonPara) : abandonPara_(abandonPara) +{ +} + +DeleteImpl::~DeleteImpl() +{ + Cancel(); +} + +void DeleteImpl::SetLatestError(int32_t error) +{ + if (error != ResultCode::SUCCESS) { + latestError_ = error; + } +} + +int32_t DeleteImpl::GetLatestError() const +{ + return latestError_; +} + +void DeleteImpl::SetAccessTokenId(uint32_t tokenId) +{ + tokenId_ = tokenId; +} + +uint32_t DeleteImpl::GetAccessTokenId() const +{ + return tokenId_; +} + +int32_t DeleteImpl::GetUserId() const +{ + return abandonPara_.userId; +} + +bool DeleteImpl::Start(std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) +{ + IAM_LOGE("UserId:%{public}d", abandonPara_.userId); + auto hdi = HdiWrapper::GetHdiInstance(); + if (!hdi) { + IAM_LOGE("bad hdi"); + return false; + } + + HdiCredentialOperateResult hdiResult = {}; + IamHitraceHelper traceHelper("hdi DeleteCredential"); + int32_t ret = hdi->DeleteCredential(abandonPara_.userId, abandonPara_.credentialId, abandonPara_.token, + hdiResult); + if (ret != HDF_SUCCESS) { + IAM_LOGE("failed to delete credential, error code : %{public}d", ret); + return false; + } + + if (hdiResult.operateType == HdiCredentialOperateType::CREDENTIAL_DELETE) { + return DeleteCredential(abandonPara_.userId, hdiResult.credentialInfo, isCredentialDelete); + } else if (hdiResult.operateType == HdiCredentialOperateType::CREDENTIAL_ABANDON) { + return StartSchedule(abandonPara_.userId, hdiResult.scheduleInfo, scheduleList, callback); + } + + return false; +} + +bool DeleteImpl::Update(const std::vector &scheduleResult, std::shared_ptr &info) +{ + auto hdi = HdiWrapper::GetHdiInstance(); + if (!hdi) { + IAM_LOGE("bad hdi"); + return false; + } + + std::vector credentialInfos; + auto result = hdi->UpdateAbandonResult(abandonPara_.userId, scheduleResult, credentialInfos); + if (result != HDF_SUCCESS) { + IAM_LOGE("hdi UpdateAbandonResult failed, err is %{public}d, userId is %{public}d", result, + abandonPara_.userId); + SetLatestError(result); + return false; + } + + if (!credentialInfos.empty()) { + info = Common::MakeShared(abandonPara_.userId, credentialInfos[0]); + if (info == nullptr) { + IAM_LOGE("bad alloc"); + return false; + } + } + + return true; +} + +bool DeleteImpl::Cancel() +{ + if (!running_) { + return false; + } + running_ = false; + return true; +} + +bool DeleteImpl::StartSchedule(int32_t userId, HdiScheduleInfo &info, + std::vector> &scheduleList, std::shared_ptr callback) +{ + IAM_LOGI("start"); + std::vector infos = {}; + infos.emplace_back(info); + + ScheduleNodeHelper::NodeOptionalPara para; + para.tokenId = tokenId_; + para.userId = userId; + + if (!ScheduleNodeHelper::BuildFromHdi(infos, callback, scheduleList, para)) { + IAM_LOGE("BuildFromHdi failed"); + return false; + } + if (scheduleList.size() == 0 || scheduleList[0] == nullptr) { + IAM_LOGE("Bad Parameter!"); + return false; + } + + scheduleId_ = scheduleList[0]->GetScheduleId(); + running_ = true; + return true; +} + +bool DeleteImpl::DeleteCredential(int32_t userId, HdiCredentialInfo &credentialInfo, bool &isCredentialDelete) +{ + IAM_LOGI("start"); + auto info = Common::MakeShared(userId, credentialInfo); + if (info == nullptr) { + IAM_LOGE("bad alloc"); + return false; + } + + std::vector> list = {info}; + int32_t ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(list, "DeleteTemplate"); + if (ret != SUCCESS) { + IAM_LOGE("NotifyExecutorToDeleteTemplates fail, ret:%{public}d", ret); + return false; + } + isCredentialDelete = true; + return true; +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/delete_impl.h b/services/core/src/delete_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..8a04c458043acb8f9e78122f48a058f869fe2e34 --- /dev/null +++ b/services/core/src/delete_impl.h @@ -0,0 +1,62 @@ +/* + * 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 IAM_DELETE_IMPL_H +#define IAM_DELETE_IMPL_H + +#include +#include + +#include "deletion.h" +#include "schedule_node.h" +#include "user_auth_hdi.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class DeleteImpl final : public Deletion, public NoCopyable { +public: + explicit DeleteImpl(DeleteParam abandonPara); + ~DeleteImpl() override; + + bool Start(std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) override; + bool Update(const std::vector &scheduleResult, std::shared_ptr &info) override; + bool Cancel() override; + + void SetAccessTokenId(uint32_t tokenId) override; + uint32_t GetAccessTokenId() const override; + int32_t GetLatestError() const override; + int32_t GetUserId() const override; + +protected: + void SetLatestError(int32_t error) override; + +private: + bool StartSchedule(int32_t userId, HdiScheduleInfo &info, + std::vector> &scheduleList, std::shared_ptr callback); + bool DeleteCredential(int32_t userId, HdiCredentialInfo &credentialInfo, bool &isCredentialDelete); + + DeleteParam abandonPara_; + + uint32_t tokenId_ {0}; + uint64_t scheduleId_ {0}; + bool running_ {false}; + int32_t latestError_ = ResultCode::GENERAL_ERROR; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // IAM_DELETE_IMPL_H \ No newline at end of file diff --git a/services/core/src/user_idm_database_impl.cpp b/services/core/src/user_idm_database_impl.cpp index 43da208a6a3c7f4c3a7b48b0ddc6ffd5b79b7da1..1bbf5a1b62604b4250d9a77ead6e22b8350e299d 100644 --- a/services/core/src/user_idm_database_impl.cpp +++ b/services/core/src/user_idm_database_impl.cpp @@ -108,20 +108,23 @@ int32_t UserIdmDatabaseImpl::DeleteCredentialInfo(int32_t userId, uint64_t crede return INVALID_HDI_INTERFACE; } - HdiCredentialInfo hdiInfo = {}; + HdiCredentialOperateResult hdiResult = {}; IamHitraceHelper traceHelper("hdi DeleteCredential"); - int32_t ret = hdi->DeleteCredential(userId, credentialId, authToken, hdiInfo); + int32_t ret = hdi->DeleteCredential(userId, credentialId, authToken, hdiResult); if (ret != HDF_SUCCESS) { IAM_LOGE("failed to delete credential, error code : %{public}d", ret); return ret; } - auto info = Common::MakeShared(userId, hdiInfo); - if (info == nullptr) { - IAM_LOGE("bad alloc"); - return GENERAL_ERROR; + if (hdiResult.operateType == HdiCredentialOperateType::CREDENTIAL_DELETE) { + auto info = Common::MakeShared(userId, hdiResult.credentialInfo); + if (info == nullptr) { + IAM_LOGE("bad alloc"); + return GENERAL_ERROR; + } + credInfo = info; } - credInfo = info; + return SUCCESS; } @@ -235,6 +238,36 @@ int32_t UserIdmDatabaseImpl::GetCredentialInfoById(uint64_t credentialId, return SUCCESS; } +int32_t UserIdmDatabaseImpl::ClearUnavailableCredential(int32_t userId, + std::vector> &credInfos) +{ + auto hdi = HdiWrapper::GetHdiInstance(); + if (hdi == nullptr) { + IAM_LOGE("bad hdi"); + return GENERAL_ERROR; + } + + std::vector hdiInfos; + std::vector userIds; + userIds.push_back(userId); + int32_t ret = hdi->ClearUnavailableCredential(userIds, hdiInfos); + if (ret != HDF_SUCCESS) { + IAM_LOGE("ClearUnavailableCredential failed, error code : %{public}d", ret); + return ret; + } + + for (const auto &hdiInfo : hdiInfos) { + auto infoRet = Common::MakeShared(userId, hdiInfo); + if (infoRet == nullptr) { + IAM_LOGE("bad alloc"); + return GENERAL_ERROR; + } + credInfos.emplace_back(infoRet); + } + + return SUCCESS; +} + UserIdmDatabase &UserIdmDatabase::Instance() { return UserIdmDatabaseImpl::GetInstance(); diff --git a/services/core/src/user_idm_database_impl.h b/services/core/src/user_idm_database_impl.h index ea49b01877fc68e86967002948855ac7db06ded1..473a77b43a55f63d6cdd6392ce4cbe70f4390e79 100644 --- a/services/core/src/user_idm_database_impl.h +++ b/services/core/src/user_idm_database_impl.h @@ -40,6 +40,8 @@ public: std::vector> &credInfos) override; int32_t GetAllExtUserInfo(std::vector> &userInfos) override; int32_t GetCredentialInfoById(uint64_t credentialId, std::shared_ptr &credInfo) override; + int32_t ClearUnavailableCredential(int32_t userId, + std::vector> &credInfos) override; }; } // namespace UserAuth } // namespace UserIam diff --git a/services/ipc/BUILD.gn b/services/ipc/BUILD.gn index 848207599fddc29e40d24cff32c7c62a90eb2e0c..cbf359f65461e9865f8b77fdf8eb63d46f60217f 100644 --- a/services/ipc/BUILD.gn +++ b/services/ipc/BUILD.gn @@ -63,7 +63,7 @@ ohos_source_set("userauth_services_ipc") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "dsoftbus:softbus_client", "hdf_core:libhdf_utils", "hdf_core:libhdi", diff --git a/services/ipc/inc/user_idm_service.h b/services/ipc/inc/user_idm_service.h index 6b3dc96f76561ae47972ae574a0d803baed67c20..3d8391c63b4a3008838ae6bb000a5c199cd07a03 100644 --- a/services/ipc/inc/user_idm_service.h +++ b/services/ipc/inc/user_idm_service.h @@ -79,6 +79,9 @@ private: const std::shared_ptr &contextCallback); int32_t StartEnroll(Enrollment::EnrollmentPara ¶, const std::shared_ptr &contextCallback, Attributes &extraInfo, bool needSubscribeAppState); + int32_t StartDelete(Deletion::DeleteParam ¶, const std::shared_ptr &contextCallback, + Attributes &extraInfo); + void ClearUnavailableCredential(int32_t userId); void PublishCommonEvent(int32_t userId, uint64_t credentialId, AuthType authType); std::mutex mutex_; }; diff --git a/services/ipc/src/user_idm_service.cpp b/services/ipc/src/user_idm_service.cpp index 87fdae505336345d5f616ebccd96c60d29f0a996..1bfbfa086c6c81e6aa13a7809a96c5351d7befe7 100644 --- a/services/ipc/src/user_idm_service.cpp +++ b/services/ipc/src/user_idm_service.cpp @@ -142,6 +142,8 @@ int32_t UserIdmService::GetCredentialInfoInner(int32_t userId, AuthType authType info.templateId = credInfo->GetTemplateId(); info.authType = credInfo->GetAuthType(); info.pinType = credInfo->GetAuthSubType(); + info.isAbandoned = credInfo->GetAbandonFlag(); + info.validityPeriod = credInfo->GetValidPeriod(); credInfoList.push_back(info); } return SUCCESS; @@ -164,12 +166,19 @@ int32_t UserIdmService::GetCredentialInfo(int32_t userId, int32_t authType, credInfoList.clear(); } + bool hasAbandonedCredential = false; for (auto &iter : credInfoList) { + if (iter.isAbandoned && iter.validityPeriod == 0) { + hasAbandonedCredential = true; + continue; + } IpcCredentialInfo ipcCredInfo; ipcCredInfo.authType = static_cast(iter.authType); ipcCredInfo.pinType = static_cast(iter.pinType.value_or(PIN_SIX)); ipcCredInfo.credentialId = iter.credentialId; ipcCredInfo.templateId = iter.templateId; + ipcCredInfo.isAbandoned = iter.isAbandoned; + ipcCredInfo.validityPeriod = iter.validityPeriod; ipcCredInfoList.push_back(ipcCredInfo); } @@ -177,6 +186,10 @@ int32_t UserIdmService::GetCredentialInfo(int32_t userId, int32_t authType, if (retCode != SUCCESS) { IAM_LOGE("OnCredentialInfos fail, ret: %{public}d", retCode); } + + if (hasAbandonedCredential) { + ClearUnavailableCredential(userId); + } return ret; } @@ -494,6 +507,27 @@ int32_t UserIdmService::DelUser(int32_t userId, const std::vector &auth return SUCCESS; } +int32_t UserIdmService::StartDelete(Deletion::DeleteParam ¶, + const std::shared_ptr &contextCallback, Attributes &extraInfo) +{ + auto context = ContextFactory::CreateDeleteContext(para, contextCallback); + if (context == nullptr || !ContextPool::Instance().Insert(context)) { + IAM_LOGE("failed to insert context"); + contextCallback->OnResult(GENERAL_ERROR, extraInfo); + return GENERAL_ERROR; + } + contextCallback->SetTraceRequestContextId(context->GetContextId()); + auto cleaner = ContextHelper::Cleaner(context); + contextCallback->SetCleaner(cleaner); + + if (!context->Start()) { + IAM_LOGE("failed to start delete"); + contextCallback->OnResult(context->GetLatestError(), extraInfo); + return GENERAL_ERROR; + } + return SUCCESS; +} + int32_t UserIdmService::DelCredential(int32_t userId, uint64_t credentialId, const std::vector &authToken, const sptr &idmCallback) { @@ -522,31 +556,14 @@ int32_t UserIdmService::DelCredential(int32_t userId, uint64_t credentialId, std::lock_guard lock(mutex_); CancelCurrentEnrollIfExist(); - - std::shared_ptr oldInfo; - auto ret = UserIdmDatabase::Instance().DeleteCredentialInfo(userId, credentialId, authToken, oldInfo); - if (ret != SUCCESS) { - IAM_LOGE("failed to delete CredentialInfo"); - contextCallback->OnResult(ret, extraInfo); - return ret; - } - if (oldInfo != nullptr) { - contextCallback->SetTraceAuthType(oldInfo->GetAuthType()); - } - - IAM_LOGI("delete credentialInfo success"); - std::vector> list = {oldInfo}; - ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(list, "DeleteTemplate"); - if (ret != SUCCESS) { - IAM_LOGE("failed to delete executor info, error code : %{public}d", ret); - } - - contextCallback->OnResult(ret, extraInfo); - if (oldInfo != nullptr) { - PublishCommonEvent(userId, credentialId, oldInfo->GetAuthType()); - } - - return SUCCESS; + std::shared_ptr credInfo; + Deletion::DeleteParam deleteParam = { + .userId = userId, + .credentialId = credentialId, + .tokenId = IpcCommon::GetAccessTokenId(*this), + .token = authToken, + }; + return StartDelete(deleteParam, contextCallback, extraInfo); } int UserIdmService::Dump(int fd, const std::vector &args) @@ -731,15 +748,25 @@ int32_t UserIdmService::GetCredentialInfoSync(int32_t userId, int32_t authType, credentialInfoList.clear(); } + bool hasAbandonedCredential = false; for (auto &iter : credentialInfoList) { + if (iter.isAbandoned && iter.validityPeriod == 0) { + hasAbandonedCredential = true; + continue; + } IpcCredentialInfo ipcCredInfo; ipcCredInfo.authType = static_cast(iter.authType); ipcCredInfo.pinType = static_cast(iter.pinType.value_or(PIN_SIX)); ipcCredInfo.credentialId = iter.credentialId; ipcCredInfo.templateId = iter.templateId; + ipcCredInfo.isAbandoned = iter.isAbandoned; + ipcCredInfo.validityPeriod = iter.validityPeriod; ipcCredentialInfoList.push_back(ipcCredInfo); } + if (hasAbandonedCredential) { + ClearUnavailableCredential(userId); + } IAM_LOGI("GetCredentialInfoSync success, credential num:%{public}zu", ipcCredentialInfoList.size()); return ret; } @@ -799,6 +826,31 @@ int32_t UserIdmService::UnRegistCredChangeEventListener(const sptr> credInfos; + int32_t ret = UserIdmDatabase::Instance().ClearUnavailableCredential(userId, credInfos); + if (ret != SUCCESS) { + IAM_LOGE("clear expired credential fail, ret:%{public}d, userId:%{public}d", ret, userId); + return; + } + + if (credInfos.empty()) { + IAM_LOGI("no abandoned credential"); + return; + } + + ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(credInfos, "DeleteTemplate"); + if (ret != SUCCESS) { + IAM_LOGE("failed to delete executor info, error code : %{public}d", ret); + } + + return; +} + int32_t UserIdmService::CallbackEnter([[maybe_unused]] uint32_t code) { IAM_LOGI("start, code:%{public}u", code); diff --git a/services/load_mode/BUILD.gn b/services/load_mode/BUILD.gn index 5177271c5859798f9c17f95b48cabf5859877129..4ad32491d71e4eb15763657c735eac2621b7255b 100644 --- a/services/load_mode/BUILD.gn +++ b/services/load_mode/BUILD.gn @@ -62,7 +62,7 @@ ohos_source_set("userauth_service_load_mode") { "access_token:libtokenid_sdk", "c_utils:utils", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdi", "hilog:libhilog", "hitrace:hitrace_meter", diff --git a/test/fuzztest/common_fuzzer/BUILD.gn b/test/fuzztest/common_fuzzer/BUILD.gn index bfa45e776e1c7055950f86c5a7e39b4bf8986f78..466faf88a83d7dc36e8e1c43290d8601f3b43fb1 100644 --- a/test/fuzztest/common_fuzzer/BUILD.gn +++ b/test/fuzztest/common_fuzzer/BUILD.gn @@ -89,7 +89,7 @@ ohos_source_set("userauth_service_base_fuzzer") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hicollie:libhicollie", "hilog:libhilog", @@ -122,6 +122,7 @@ ohos_source_set("userauth_service_core_fuzzer") { sources = [ "../../../services/core/src/authentication_impl.cpp", "../../../services/core/src/credential_info_impl.cpp", + "../../../services/core/src/delete_impl.cpp", "../../../services/core/src/driver_state_manager.cpp", "../../../services/core/src/enrolled_info_impl.cpp", "../../../services/core/src/enrollment_impl.cpp", @@ -164,7 +165,7 @@ ohos_source_set("userauth_service_core_fuzzer") { "access_token:libtokenid_sdk", "c_utils:utils", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hdf_core:libhdi", "hicollie:libhicollie", @@ -214,6 +215,7 @@ ohos_source_set("userauth_service_context_fuzzer") { "../../../services/context/src/context_factory.cpp", "../../../services/context/src/context_helper.cpp", "../../../services/context/src/context_pool_impl.cpp", + "../../../services/context/src/delete_context.cpp", "../../../services/context/src/enroll_context.cpp", "../../../services/context/src/identify_context.cpp", "../../../services/context/src/keyguard_status_listener.cpp", @@ -252,7 +254,7 @@ ohos_source_set("userauth_service_context_fuzzer") { "access_token:libaccesstoken_sdk", "c_utils:utils", "common_event_service:cesfwk_innerkits", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hicollie:libhicollie", "hilog:libhilog", @@ -365,7 +367,7 @@ ohos_source_set("userauth_services_ipc_fuzzer") { "access_token:libaccesstoken_sdk", "c_utils:utils", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "dsoftbus:softbus_client", "hdf_core:libhdf_utils", "hdf_core:libhdi", diff --git a/test/fuzztest/dummy/mock_iuser_auth_interface.h b/test/fuzztest/dummy/mock_iuser_auth_interface.h index 511829def46dcd803fabe23a83cf10f7511aaa00..fd3ce937fa9b0160d213b3f6fed3c5e4828aaa9d 100644 --- a/test/fuzztest/dummy/mock_iuser_auth_interface.h +++ b/test/fuzztest/dummy/mock_iuser_auth_interface.h @@ -67,7 +67,7 @@ public: } int32_t DeleteCredential(int32_t userId, uint64_t credentialId, const std::vector &authToken, - HdiCredentialInfo &info) + HdiCredentialOperateResult &operateResult) { return 0; } @@ -211,6 +211,18 @@ public: { return 0; } + + int32_t UpdateAbandonResult(int32_t userId, const std::vector &scheduleResult, + std::vector &infos) + { + return 0; + } + + int32_t ClearUnavailableCredential(const std::vector &userIds, + std::vector &infos) + { + return 0; + } }; class MockIUserAuthInterface::Holder : public Singleton { diff --git a/test/fuzztest/executors/userauthdrivermanager_fuzzer/BUILD.gn b/test/fuzztest/executors/userauthdrivermanager_fuzzer/BUILD.gn index e17074f642917c172239708c929d48aab6e3a280..532a7cea6d57acb92436aacc10f7d7162259fb3f 100644 --- a/test/fuzztest/executors/userauthdrivermanager_fuzzer/BUILD.gn +++ b/test/fuzztest/executors/userauthdrivermanager_fuzzer/BUILD.gn @@ -36,6 +36,7 @@ ohos_fuzztest("UserAuthDriverManagerFuzzTest") { ] sources = [ + "../../../../frameworks/native/executors/src/async_command/abandon_command.cpp", "../../../../frameworks/native/executors/src/async_command/async_command_base.cpp", "../../../../frameworks/native/executors/src/async_command/auth_command.cpp", "../../../../frameworks/native/executors/src/async_command/collect_command.cpp", diff --git a/test/fuzztest/executors/userauthexecutor_fuzzer/BUILD.gn b/test/fuzztest/executors/userauthexecutor_fuzzer/BUILD.gn index a53ce1f50b2ade7fa9a9dfe4e2f1507dce235ea3..57f0c9e27e36bcb4ae79ec85fb4d9305951cd721 100644 --- a/test/fuzztest/executors/userauthexecutor_fuzzer/BUILD.gn +++ b/test/fuzztest/executors/userauthexecutor_fuzzer/BUILD.gn @@ -37,6 +37,7 @@ ohos_fuzztest("UserAuthExecutorFuzzTest") { ] sources = [ + "../../../../frameworks/native/executors/src/async_command/abandon_command.cpp", "../../../../frameworks/native/executors/src/async_command/async_command_base.cpp", "../../../../frameworks/native/executors/src/async_command/auth_command.cpp", "../../../../frameworks/native/executors/src/async_command/collect_command.cpp", diff --git a/test/fuzztest/executors/userauthexecutor_fuzzer/user_auth_executor_fuzzer.cpp b/test/fuzztest/executors/userauthexecutor_fuzzer/user_auth_executor_fuzzer.cpp index 6c377c339605595f1f1e982ceb487242d00f0361..939c6a4a0cde6dd24a6230252c861214aad10169 100644 --- a/test/fuzztest/executors/userauthexecutor_fuzzer/user_auth_executor_fuzzer.cpp +++ b/test/fuzztest/executors/userauthexecutor_fuzzer/user_auth_executor_fuzzer.cpp @@ -166,6 +166,16 @@ public: return static_cast(fuzzParcel_->ReadInt32()); } + ResultCode Abandon(uint64_t scheduleId, const DeleteParam ¶m, + const std::shared_ptr &callbackObj) override + { + std::lock_guard lock(mutex_); + if (fuzzParcel_ == nullptr) { + return ResultCode::GENERAL_ERROR; + } + return static_cast(fuzzParcel_->ReadInt32()); + } + void SetParcel(const std::shared_ptr &parcel) { std::lock_guard lock(mutex_); diff --git a/test/fuzztest/services/coauthservice_fuzzer/BUILD.gn b/test/fuzztest/services/coauthservice_fuzzer/BUILD.gn index 24f639313d5160d5cf6fd0b2091c7e03f8359a8c..761d0d0a58f102772044b068aa48a326813b99c3 100644 --- a/test/fuzztest/services/coauthservice_fuzzer/BUILD.gn +++ b/test/fuzztest/services/coauthservice_fuzzer/BUILD.gn @@ -54,7 +54,7 @@ ohos_fuzztest("CoAuthServiceFuzzTest") { external_deps = [ "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "eventhandler:libeventhandler", "hdf_core:libhdi", "hilog:libhilog", diff --git a/test/fuzztest/services/context/contextcallbackimpl_fuzzer/BUILD.gn b/test/fuzztest/services/context/contextcallbackimpl_fuzzer/BUILD.gn index 31714c459cd2d02b1c2ef3334c7d2ac7ef9b0a82..03b19f50ce73c8f973dd691d0a82d90a11d1141a 100644 --- a/test/fuzztest/services/context/contextcallbackimpl_fuzzer/BUILD.gn +++ b/test/fuzztest/services/context/contextcallbackimpl_fuzzer/BUILD.gn @@ -61,7 +61,7 @@ ohos_fuzztest("ContextCallbackImplFuzzTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "eventhandler:libeventhandler", "hdf_core:libhdi", "hilog:libhilog", diff --git a/test/fuzztest/services/context/contextpoolimpl_fuzzer/BUILD.gn b/test/fuzztest/services/context/contextpoolimpl_fuzzer/BUILD.gn index 456ebf1642e1bbaf4cf1d8cb09b64b0a7fa65f27..551a964f6f0f9d1def415eedc9206f7f64914d05 100644 --- a/test/fuzztest/services/context/contextpoolimpl_fuzzer/BUILD.gn +++ b/test/fuzztest/services/context/contextpoolimpl_fuzzer/BUILD.gn @@ -68,7 +68,7 @@ ohos_fuzztest("ContextPoolImplFuzzTest") { "ability_runtime:extension_manager", "access_token:libaccesstoken_sdk", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "eventhandler:libeventhandler", "hdf_core:libhdf_utils", "hdf_core:libhdi", diff --git a/test/fuzztest/services/context/remoteauthcontext_fuzzer/BUILD.gn b/test/fuzztest/services/context/remoteauthcontext_fuzzer/BUILD.gn index 87790ac374288e86da72cd1963b4840c7737d7fa..1322b0bc4cd1af4138b7f7a52634159092d6cda5 100644 --- a/test/fuzztest/services/context/remoteauthcontext_fuzzer/BUILD.gn +++ b/test/fuzztest/services/context/remoteauthcontext_fuzzer/BUILD.gn @@ -60,7 +60,7 @@ ohos_fuzztest("RemoteAuthContextFuzzTest") { external_deps = [ "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "eventhandler:libeventhandler", "hdf_core:libhdf_utils", "hdf_core:libhdi", diff --git a/test/fuzztest/services/core/remoteexecutorstub_fuzzer/BUILD.gn b/test/fuzztest/services/core/remoteexecutorstub_fuzzer/BUILD.gn index 335cb97aaa1cb26ff31ad7d8da603735894b09c8..f8ad20072049933c5577a165a4b7e92c5f6ee5ca 100644 --- a/test/fuzztest/services/core/remoteexecutorstub_fuzzer/BUILD.gn +++ b/test/fuzztest/services/core/remoteexecutorstub_fuzzer/BUILD.gn @@ -57,7 +57,7 @@ ohos_fuzztest("RemoteExecutorStubFuzzTest") { "access_token:libaccesstoken_sdk", "c_utils:utils", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hdf_core:libhdi", "hilog:libhilog", diff --git a/test/fuzztest/services/softbus_fuzzer/BUILD.gn b/test/fuzztest/services/softbus_fuzzer/BUILD.gn index fa346b2d33dc7afdb7d62840c1d061c4e435dc74..ce5039ee64d8325526dba6c532504a5044794506 100644 --- a/test/fuzztest/services/softbus_fuzzer/BUILD.gn +++ b/test/fuzztest/services/softbus_fuzzer/BUILD.gn @@ -53,7 +53,7 @@ ohos_fuzztest("SoftBusFuzzTest") { "c_utils:utils", "c_utils:utils", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "dsoftbus:softbus_client", "eventhandler:libeventhandler", "hdf_core:libhdi", diff --git a/test/fuzztest/services/templatecachemanager_fuzzer/BUILD.gn b/test/fuzztest/services/templatecachemanager_fuzzer/BUILD.gn index a27c34f1319947e1efcb477489e3a8ef1ff0c284..5b4ef83e683d548cf762f41386e99652b0074e8e 100644 --- a/test/fuzztest/services/templatecachemanager_fuzzer/BUILD.gn +++ b/test/fuzztest/services/templatecachemanager_fuzzer/BUILD.gn @@ -54,7 +54,7 @@ ohos_fuzztest("TemplateCacheManagerFuzzTest") { "ability_runtime:app_manager", "ability_runtime:extension_manager", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "eventhandler:libeventhandler", "hdf_core:libhdi", "hilog:libhilog", diff --git a/test/fuzztest/services/userauthservice_fuzzer/BUILD.gn b/test/fuzztest/services/userauthservice_fuzzer/BUILD.gn index 753f2180ecdc6277f6db4e29b2528437a55d0262..785a8c91b455d2e9bfb3e5e059bf27ce70083cda 100644 --- a/test/fuzztest/services/userauthservice_fuzzer/BUILD.gn +++ b/test/fuzztest/services/userauthservice_fuzzer/BUILD.gn @@ -55,7 +55,7 @@ ohos_fuzztest("UserAuthServiceFuzzTest") { "ability_runtime:extension_manager", "access_token:libaccesstoken_sdk", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "dsoftbus:softbus_client", "eventhandler:libeventhandler", "hdf_core:libhdf_utils", diff --git a/test/fuzztest/services/useridmservice_fuzzer/BUILD.gn b/test/fuzztest/services/useridmservice_fuzzer/BUILD.gn index 23b6a63e8582ea4aaa091f7b492e79a8e19c1847..674b61bd67bad1b0195328eddf991eb2cfefb2ea 100644 --- a/test/fuzztest/services/useridmservice_fuzzer/BUILD.gn +++ b/test/fuzztest/services/useridmservice_fuzzer/BUILD.gn @@ -59,7 +59,7 @@ ohos_fuzztest("UserIdmServiceFuzzTest") { "ability_runtime:extension_manager", "access_token:libaccesstoken_sdk", "c_utils:utils", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "hdf_core:libhdf_utils", "hdf_core:libhdi", "hilog:libhilog", diff --git a/test/fuzztest/services/useridmservice_fuzzer/user_idm_service_fuzzer.cpp b/test/fuzztest/services/useridmservice_fuzzer/user_idm_service_fuzzer.cpp index 2bf6406e9fd44d11078d05cc80aee3fdeafec218..d5e7a72c878947ccd22ec3a2f10ab5d1c6b0979a 100644 --- a/test/fuzztest/services/useridmservice_fuzzer/user_idm_service_fuzzer.cpp +++ b/test/fuzztest/services/useridmservice_fuzzer/user_idm_service_fuzzer.cpp @@ -349,6 +349,14 @@ void FuzzGetCredentialInfoSync(Parcel &parcel) IAM_LOGI("end"); } +void FuzzClearUnavailableCredential(Parcel &parcel) +{ + IAM_LOGI("begin"); + int32_t userId = parcel.ReadInt32(); + g_UserIdmService.ClearUnavailableCredential(userId); + IAM_LOGI("end"); +} + using FuzzFunc = decltype(FuzzOpenSession); FuzzFunc *g_fuzzFuncs[] = { FuzzOpenSession, @@ -369,6 +377,7 @@ FuzzFunc *g_fuzzFuncs[] = { FuzzStartEnroll, FuzzRegistCredChangeEventListener, FuzzGetCredentialInfoSync, + FuzzClearUnavailableCredential, }; void UserIdmFuzzTest(const uint8_t *data, size_t size) diff --git a/test/unittest/executors/BUILD.gn b/test/unittest/executors/BUILD.gn index 22fc35bddfa89a35b7a341c66076ba69271c3f77..a19dc3bb31bf61c495ea8c82d3c2b111e1e99efa 100644 --- a/test/unittest/executors/BUILD.gn +++ b/test/unittest/executors/BUILD.gn @@ -40,6 +40,7 @@ ohos_unittest("iam_executors_test") { ] sources = [ + "../../../frameworks/native/executors/src/async_command/abandon_command.cpp", "../../../frameworks/native/executors/src/async_command/async_command_base.cpp", "../../../frameworks/native/executors/src/async_command/auth_command.cpp", "../../../frameworks/native/executors/src/async_command/collect_command.cpp", @@ -55,6 +56,7 @@ ohos_unittest("iam_executors_test") { "../../../frameworks/native/executors/src/idriver_manager.cpp", "../../../frameworks/native/executors/src/listener/auth_executor_mgr_status_listener.cpp", "../../../frameworks/native/executors/src/listener/driver_manager_status_listener.cpp", + "abandon_command_unit_test.cpp", "auth_command_unit_test.cpp", "driver_manager_unit_test.cpp", "enroll_command_unit_test.cpp", diff --git a/test/unittest/executors/abandon_command_unit_test.cpp b/test/unittest/executors/abandon_command_unit_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59c5f452830a7929991b9735cb94d00d03daf22f --- /dev/null +++ b/test/unittest/executors/abandon_command_unit_test.cpp @@ -0,0 +1,267 @@ +/* + * 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 "gtest/gtest.h" + +#include "abandon_command.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_mem.h" +#include "iam_ptr.h" +#include "async_command_base.h" + +#include "executor.h" +#include "mock_iexecutor_messenger.h" + +#define LOG_TAG "USER_AUTH_EXECUTOR" + +using namespace std; +using namespace testing; +using namespace testing::ext; +using namespace OHOS::UserIam; +using namespace OHOS::UserIam::Common; +using namespace OHOS::UserIam::UserAuth; + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class AbandonCommandUnitTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AbandonCommandUnitTest::SetUpTestCase() +{ +} + +void AbandonCommandUnitTest::TearDownTestCase() +{ +} + +void AbandonCommandUnitTest::SetUp() +{ +} + +void AbandonCommandUnitTest::TearDown() +{ +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_OnResultTest_001, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const ResultCode testResultCode = static_cast(456); + static const std::vector testExtraInfo = {7, 8, 9}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, Finish(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) { + EXPECT_EQ(scheduleId, testScheduleId); + EXPECT_EQ(resultCode, testResultCode); + uint32_t attrResultCode; + EXPECT_EQ(finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode), true); + EXPECT_EQ(attrResultCode, static_cast(testResultCode)); + std::vector extraInfo; + EXPECT_EQ(finalResult.GetUint8ArrayValue(Attributes::ATTR_RESULT, extraInfo), true); + EXPECT_EQ(extraInfo, testExtraInfo); + return USERAUTH_SUCCESS; + }); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnResult(testResultCode, testExtraInfo); +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_OnResultTest_002, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const ResultCode testResultCode = static_cast(456); + static const std::vector testExtraInfo = {7, 8, 9}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, Finish(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) { + // return error + return USERAUTH_ERROR; + }); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnResult(testResultCode, testExtraInfo); +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_OnResultTest_003, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const ResultCode testResultCode = static_cast(456); + static const std::vector testExtraInfo = {}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, Finish(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) { + EXPECT_EQ(scheduleId, testScheduleId); + EXPECT_EQ(resultCode, testResultCode); + uint32_t attrResultCode; + EXPECT_EQ(finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode), true); + EXPECT_EQ(attrResultCode, static_cast(testResultCode)); + std::vector extraInfo; + EXPECT_EQ(finalResult.GetUint8ArrayValue(Attributes::ATTR_RESULT, extraInfo), true); + EXPECT_EQ(extraInfo, testExtraInfo); + return USERAUTH_SUCCESS; + }); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnResult(testResultCode); +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_OnResultTest_004, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const ResultCode testResultCode = static_cast(456); + static const std::vector testExtraInfo = {}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, Finish(_, _, _)).Times(Exactly(1)); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnResult(testResultCode); + command->OnResult(testResultCode); + command->OnResult(testResultCode); +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_OnAcquireInfoTest_001, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const uint64_t testAcquire = 456; + static const std::vector testExtraInfo = {7, 8, 9}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, SendData(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](uint64_t scheduleId, ExecutorRole dstRole, const std::shared_ptr &msg) { + EXPECT_EQ(scheduleId, testScheduleId); + EXPECT_EQ(dstRole, SCHEDULER); + EXPECT_NE(msg, nullptr); + std::vector extraInfo; + return USERAUTH_SUCCESS; + }); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnAcquireInfo(testAcquire, testExtraInfo); +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_OnAcquireInfoTest_002, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const uint64_t testAcquire = 456; + static const std::vector testExtraInfo = {7, 8, 9}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, SendData(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](uint64_t scheduleId, ExecutorRole dstRole, + const std::shared_ptr &msg) { return USERAUTH_ERROR; }); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnAcquireInfo(testAcquire, testExtraInfo); +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_OnAcquireInfoTest_003, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const uint64_t testAcquire = 456; + static const std::vector testExtraInfo = {7, 8, 9}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, SendData(_, _, _)) + .Times(Exactly(3)) + .WillOnce([](uint64_t scheduleId, int32_t dstType, std::shared_ptr msg) { + return USERAUTH_SUCCESS; + }) + .WillOnce([](uint64_t scheduleId, int32_t dstType, std::shared_ptr msg) { + return USERAUTH_ERROR; + }) + .WillOnce([](uint64_t scheduleId, int32_t dstType, std::shared_ptr msg) { + return USERAUTH_SUCCESS; + }); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnAcquireInfo(testAcquire, testExtraInfo); + command->OnAcquireInfo(testAcquire, testExtraInfo); + command->OnAcquireInfo(testAcquire, testExtraInfo); +} + +HWTEST_F(AbandonCommandUnitTest, AbandonCommand_MixTest_003, TestSize.Level0) +{ + static const uint64_t testScheduleId = 123; + static const uint64_t testAcquire = 456; + static const ResultCode testResultCode = static_cast(456); + static const std::vector testExtraInfo = {7, 8, 9}; + + auto messenger = MakeShared(); + ASSERT_NE(messenger, nullptr); + EXPECT_CALL(*messenger, Finish(_, _, _)).Times(Exactly(1)); + EXPECT_CALL(*messenger, SendData(_, _, _)) + .Times(Exactly(3)) + .WillRepeatedly([](uint64_t scheduleId, int32_t dstType, + std::shared_ptr msg) { return USERAUTH_SUCCESS; }); + auto executor = Common::MakeShared(nullptr, nullptr, 3); + ASSERT_NE(executor, nullptr); + Attributes attr; + auto command = Common::MakeShared(executor, testScheduleId, attr, messenger); + ASSERT_NE(command, nullptr); + command->OnAcquireInfo(testAcquire, testExtraInfo); + command->OnAcquireInfo(testAcquire, testExtraInfo); + command->OnAcquireInfo(testAcquire, testExtraInfo); + command->OnResult(testResultCode); + command->OnAcquireInfo(testAcquire, testExtraInfo); + command->OnResult(testResultCode); + command->OnAcquireInfo(testAcquire, testExtraInfo); + command->OnAcquireInfo(testAcquire, testExtraInfo); +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS diff --git a/test/unittest/services/BUILD.gn b/test/unittest/services/BUILD.gn index 0381186c944b49f650e50094345a250e31952e02..e6d263023d42c22b351bd02ceb125150e7dc8fc4 100644 --- a/test/unittest/services/BUILD.gn +++ b/test/unittest/services/BUILD.gn @@ -64,6 +64,7 @@ ohos_unittest("iam_services_test") { "../../../services/context/src/context_factory.cpp", "../../../services/context/src/context_helper.cpp", "../../../services/context/src/context_pool_impl.cpp", + "../../../services/context/src/delete_context.cpp", "../../../services/context/src/enroll_context.cpp", "../../../services/context/src/identify_context.cpp", "../../../services/context/src/keyguard_status_listener.cpp", @@ -82,6 +83,7 @@ ohos_unittest("iam_services_test") { "../../../services/context/src/widget_json.cpp", "../../../services/core/src/authentication_impl.cpp", "../../../services/core/src/credential_info_impl.cpp", + "../../../services/core/src/delete_impl.cpp", "../../../services/core/src/driver_state_manager.cpp", "../../../services/core/src/enrolled_info_impl.cpp", "../../../services/core/src/enrollment_impl.cpp", @@ -135,6 +137,8 @@ ohos_unittest("iam_services_test") { "src/context_factory_test.cpp", "src/context_pool_test.cpp", "src/credential_info_test.cpp", + "src/delete_context_test.cpp", + "src/delete_impl_test.cpp", "src/driver_state_manager_test.cpp", "src/enroll_context_test.cpp", "src/enrolled_info_test.cpp", @@ -204,7 +208,7 @@ ohos_unittest("iam_services_test") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "device_manager:devicemanagersdk", - "drivers_interface_user_auth:libuser_auth_proxy_3.0", + "drivers_interface_user_auth:libuser_auth_proxy_4.0", "dsoftbus:softbus_client", "googletest:gmock", "hdf_core:libhdf_utils", diff --git a/test/unittest/services/mocks/mock_credential_info.h b/test/unittest/services/mocks/mock_credential_info.h index 6020da30ebe0b9179ee73bff7ca5b3214fbfca74..262b54248d13eb2232011fd60c003858211331d8 100644 --- a/test/unittest/services/mocks/mock_credential_info.h +++ b/test/unittest/services/mocks/mock_credential_info.h @@ -33,6 +33,8 @@ public: MOCK_CONST_METHOD0(GetExecutorSensorHint, uint32_t()); MOCK_CONST_METHOD0(GetExecutorMatcher, uint32_t()); MOCK_CONST_METHOD0(GetAuthSubType, PinSubType()); + MOCK_CONST_METHOD0(GetAbandonFlag, bool()); + MOCK_CONST_METHOD0(GetValidPeriod, int64_t()); }; } // namespace UserAuth } // namespace UserIam diff --git a/test/unittest/services/mocks/mock_deletion.h b/test/unittest/services/mocks/mock_deletion.h new file mode 100644 index 0000000000000000000000000000000000000000..5ca027fa05f79d55c77fb9678549498f11afe90b --- /dev/null +++ b/test/unittest/services/mocks/mock_deletion.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022-2023 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 IAM_MOCK_ABANDON_H +#define IAM_MOCK_ABANDON_H + +#include + +#include "deletion.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class MockDeletion final : public Deletion { +public: + virtual ~MockDeletion() = default; + MOCK_METHOD3(Start, bool(std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete)); + MOCK_METHOD2(Update, bool(const std::vector &scheduleResult, + std::shared_ptr &info)); + MOCK_METHOD0(Cancel, bool()); + + MOCK_METHOD1(SetAccessTokenId, void(uint32_t tokenId)); + MOCK_CONST_METHOD0(GetAccessTokenId, uint32_t()); + MOCK_CONST_METHOD0(GetLatestError, int32_t()); + MOCK_CONST_METHOD0(GetUserId, int32_t()); + +protected: + MOCK_METHOD1(SetLatestError, void(int32_t error)); +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // IAM_MOCK_ABANDON_H \ No newline at end of file diff --git a/test/unittest/services/mocks/mock_iuser_auth_interface.h b/test/unittest/services/mocks/mock_iuser_auth_interface.h index 1ecdb3c238e6fd920504e049b1f969891fbb703b..ade62733d048632307b2602e29955795b1efc520 100644 --- a/test/unittest/services/mocks/mock_iuser_auth_interface.h +++ b/test/unittest/services/mocks/mock_iuser_auth_interface.h @@ -41,8 +41,8 @@ public: MOCK_METHOD3(UpdateEnrollmentResult, int32_t(int32_t userId, const std::vector &scheduleResult, HdiEnrollResultInfo &info)); MOCK_METHOD1(CancelEnrollment, int32_t(int32_t userId)); - MOCK_METHOD4(DeleteCredential, - int32_t(int32_t userId, uint64_t credentialId, const std::vector &authToken, HdiCredentialInfo &info)); + MOCK_METHOD4(DeleteCredential, int32_t(int32_t userId, uint64_t credentialId, + const std::vector &authToken, HdiCredentialOperateResult &operateResult)); MOCK_METHOD3(GetCredential, int32_t(int32_t userId, int32_t authType, std::vector &infos)); MOCK_METHOD4(GetUserInfo, int32_t(int32_t userId, uint64_t &secureUid, int32_t &pinSubType, std::vector &infos)); @@ -84,6 +84,10 @@ public: MOCK_METHOD4(VerifyAuthToken, int32_t(const std::vector& tokenIn, uint64_t allowableDuration, HdiUserAuthTokenPlain &tokenPlainOut, std::vector& rootSecret)); MOCK_METHOD2(GetCredentialById, int32_t(uint64_t credentialId, HdiCredentialInfo &info)); + MOCK_METHOD3(UpdateAbandonResult, int32_t(int32_t userId, const std::vector &scheduleResult, + std::vector &infos)); + MOCK_METHOD2(ClearUnavailableCredential, int32_t(const std::vector &userIds, + std::vector &infos)); }; class MockIUserAuthInterface::Holder : public Singleton { diff --git a/test/unittest/services/src/context_factory_test.cpp b/test/unittest/services/src/context_factory_test.cpp index a604381f0faf366ed413e9bf13a7322ce30a30df..551b2e149550201a9b43ebde97e4ddd77b057505 100644 --- a/test/unittest/services/src/context_factory_test.cpp +++ b/test/unittest/services/src/context_factory_test.cpp @@ -170,6 +170,34 @@ HWTEST_F(ContextFactoryTest, ContextFactoryCreateWidgetContext_002, TestSize.Lev ASSERT_EQ(context, nullptr); } +HWTEST_F(ContextFactoryTest, ContextFactoryCreateDeleteContext_001, TestSize.Level0) +{ + auto factory = ContextFactory::GetInstance(); + ASSERT_NE(factory, nullptr); + std::vector token; + sptr callback(new (std::nothrow) MockIdmCallback()); + ASSERT_NE(callback, nullptr); + auto contextCallback = ContextCallback::NewInstance(callback, TRACE_DELETE_CREDENTIAL); + Deletion::DeleteParam para = {}; + auto context = factory->CreateDeleteContext(para, contextCallback); + ASSERT_NE(context, nullptr); + EXPECT_NE(context->GetContextId(), 0U); + ASSERT_EQ(context->GetContextType(), CONTEXT_DELETE); +} + +HWTEST_F(ContextFactoryTest, ContextFactoryCreateDeleteContext_002, TestSize.Level0) +{ + auto factory = ContextFactory::GetInstance(); + ASSERT_NE(factory, nullptr); + std::vector token; + // Error: callback is null + sptr callback(nullptr); + auto contextCallback = ContextCallback::NewInstance(callback, TRACE_DELETE_CREDENTIAL); + Deletion::DeleteParam para = {}; + auto context = factory->CreateDeleteContext(para, contextCallback); + ASSERT_EQ(context, nullptr); +} + } // namespace UserAuth } // namespace UserIam } // namespace OHOS diff --git a/test/unittest/services/src/delete_context_test.cpp b/test/unittest/services/src/delete_context_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4caff60b98a0347c7dae38c9731378d392da66d4 --- /dev/null +++ b/test/unittest/services/src/delete_context_test.cpp @@ -0,0 +1,539 @@ +/* + * 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 "delete_context.h" + +#include "mock_context.h" +#include "mock_credential_info.h" +#include "mock_deletion.h" +#include "mock_schedule_node.h" +#include "mock_update_pin_param_info.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class DeleteContextTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; + +void DeleteContextTest::SetUpTestCase() +{ +} + +void DeleteContextTest::TearDownTestCase() +{ +} + +void DeleteContextTest::SetUp() +{ +} + +void DeleteContextTest::TearDown() +{ +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_NullHdi, TestSize.Level0) +{ + const uint64_t testContestId = 2; + const int32_t testResultCode = 7; + const auto finalResult = Common::MakeShared(); + ASSERT_NE(finalResult, nullptr); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(Exactly(1)); + std::shared_ptr deletion = nullptr; + + auto oriContext = Common::MakeShared(testContestId, deletion, contextCallback); + ASSERT_NE(oriContext, nullptr); + std::shared_ptr context = oriContext; + std::shared_ptr nodeCallback = oriContext; + + ASSERT_EQ(context->Start(), false); + ASSERT_EQ(context->Stop(), false); + nodeCallback->OnScheduleStoped(testResultCode, finalResult); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_NullCallback, TestSize.Level0) +{ + const uint64_t testContestId = 2; + const ExecutorRole testRole = static_cast(3); + const int32_t testModuleType = 4; + const std::vector testAcquireMsg = {4, 5, 6}; + const int32_t testResultCode = 7; + const auto finalResult = Common::MakeShared(); + ASSERT_NE(finalResult, nullptr); + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + std::shared_ptr contextCallback = nullptr; + + auto oriContext = Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(oriContext, nullptr); + std::shared_ptr nodeCallback = oriContext; + + nodeCallback->OnScheduleStarted(); + nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg); + nodeCallback->OnScheduleStoped(testResultCode, finalResult); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_BasicInfo, TestSize.Level0) +{ + const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + + auto oriContext = Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(oriContext, nullptr); + std::shared_ptr context = oriContext; + + ASSERT_EQ(context->GetContextId(), testContestId); + ASSERT_EQ(context->GetContextType(), CONTEXT_DELETE); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Start_001, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, GetLatestError()).Times(1); + EXPECT_CALL(*mockDeletion, Start(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) { + isCredentialDelete = false; + return false; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Start(), false); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Start_002, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Start(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) { + isCredentialDelete = false; + return true; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Start(), false); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Start_003, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Start(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) { + scheduleList.push_back(Common::MakeShared()); + scheduleList.push_back(Common::MakeShared()); + return true; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Start(), false); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Start_004, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Start(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) { + auto scheduleNode = Common::MakeShared(); + EXPECT_NE(scheduleNode, nullptr); + + EXPECT_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(false)); + scheduleList.push_back(scheduleNode); + return true; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Start(), false); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Start_005, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + static const uint64_t testScheduleId = 3; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Start(_, _, _)) + .Times(Exactly(1)) + .WillOnce([](std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) { + EXPECT_EQ(scheduleList.size(), 0U); + auto scheduleNode = Common::MakeShared(); + EXPECT_NE(scheduleNode, nullptr); + EXPECT_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(true)); + EXPECT_CALL(*scheduleNode, GetScheduleId()).WillRepeatedly(Return(testScheduleId)); + scheduleList.push_back(scheduleNode); + return true; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Start(), true); + ASSERT_EQ(context->Start(), false); + auto node = context->GetScheduleNode(testScheduleId); + ASSERT_NE(node, nullptr); + node = context->GetScheduleNode(testScheduleId + 1); + ASSERT_EQ(node, nullptr); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Stop_001, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, GetLatestError()).Times(1); + EXPECT_CALL(*mockDeletion, Cancel()).Times(Exactly(1)).WillOnce([]() { + return false; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Stop(), false); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Stop_002, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Cancel()).Times(Exactly(1)).WillOnce([]() { return true; }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Stop(), true); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Stop_003, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Start(_, _, _)).Times(1); + ON_CALL(*mockDeletion, Start) + .WillByDefault( + [](std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) { + scheduleList.push_back(nullptr); + return true; + } + ); + EXPECT_CALL(*mockDeletion, Cancel()).Times(Exactly(1)).WillOnce([]() { + return true; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Start(), false); + ASSERT_EQ(context->Stop(), true); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_Stop_004, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Start(_, _, _)).Times(1); + ON_CALL(*mockDeletion, Start) + .WillByDefault( + [](std::vector> &scheduleList, + std::shared_ptr callback, bool &isCredentialDelete) { + auto scheduleNode = Common::MakeShared(); + EXPECT_NE(scheduleNode, nullptr); + EXPECT_CALL(*scheduleNode, StartSchedule()).Times(1); + EXPECT_CALL(*scheduleNode, StopSchedule()).Times(1); + scheduleList.push_back(scheduleNode); + return true; + } + ); + EXPECT_CALL(*mockDeletion, GetLatestError()).Times(1); + EXPECT_CALL(*mockDeletion, Cancel()).Times(Exactly(1)).WillOnce([]() { + return false; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + std::shared_ptr context = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(context, nullptr); + ASSERT_EQ(context->Start(), false); + ASSERT_EQ(context->Stop(), false); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleStarted, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + nodeCallback->OnScheduleStarted(); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleProcessed, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + const ExecutorRole testRole = static_cast(3); + const int32_t testModuleType = 4; + const std::vector testAcquireMsg = {4, 5, 6}; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + auto contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnAcquireInfo(_, _, _)) + .WillOnce( + [](ExecutorRole src, int32_t moduleType, const std::vector &acquireMsg) { + EXPECT_EQ(moduleType, 4); + } + ); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleStoped_001, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + static const int32_t testResultCode = 7; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnResult(_, _)) + .Times(Exactly(1)) + .WillOnce([](int32_t resultCode, const Attributes &finalResult) { + EXPECT_EQ(resultCode, testResultCode); + }); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + // Error: result is null when testResultCode is not success + std::shared_ptr result = nullptr; + nodeCallback->OnScheduleStoped(testResultCode, result); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleStoped_002, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + static const int32_t testResultCode = ResultCode::SUCCESS; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnResult(_, _)) + .Times(Exactly(1)) + .WillOnce([](int32_t resultCode, const Attributes &finalResult) { + EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR); + }); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + // Error: result is null when testResultCode is success + std::shared_ptr result = nullptr; + nodeCallback->OnScheduleStoped(testResultCode, result); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleStoped_003, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + static const int32_t testResultCode = ResultCode::SUCCESS; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnResult(_, _)) + .Times(Exactly(1)) + .WillOnce([](int32_t resultCode, const Attributes &finalResult) { + EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR); + }); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + std::shared_ptr result = Common::MakeShared(); + ASSERT_NE(result, nullptr); + nodeCallback->OnScheduleStoped(testResultCode, result); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleStoped_004, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + static const int32_t testResultCode = ResultCode::SUCCESS; + static const std::vector testScheduleResult = {3, 4, 5, 6}; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, GetLatestError()).Times(1); + EXPECT_CALL(*mockDeletion, Update(_, _)) + .Times(Exactly(1)) + .WillOnce([](const std::vector &scheduleResult, std::shared_ptr &info) { + EXPECT_EQ(scheduleResult, testScheduleResult); + return false; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnResult(_, _)) + .Times(Exactly(1)) + .WillOnce([](int32_t resultCode, const Attributes &finalResult) { + EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR); + }); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + std::shared_ptr result = Common::MakeShared(); + ASSERT_NE(result, nullptr); + bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult); + ASSERT_EQ(ret1, true); + + bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult); + ASSERT_EQ(ret2, true); + nodeCallback->OnScheduleStoped(testResultCode, result); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleStoped_005, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + static const std::vector testScheduleResult = {3, 4, 5, 6}; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Update(_, _)) + .Times(Exactly(1)) + .WillOnce([](const std::vector &scheduleResult, std::shared_ptr &info) { + EXPECT_EQ(scheduleResult, testScheduleResult); + return true; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnResult(_, _)) + .Times(Exactly(1)) + .WillOnce([](int32_t resultCode, const Attributes &finalResult) { + EXPECT_EQ(resultCode, ResultCode::SUCCESS); + }); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + std::shared_ptr result = Common::MakeShared(); + ASSERT_NE(result, nullptr); + bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult); + ASSERT_EQ(ret1, true); + + bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult); + ASSERT_EQ(ret2, true); + nodeCallback->OnScheduleStoped(ResultCode::SUCCESS, result); +} + +HWTEST_F(DeleteContextTest, DeleteContextTest_OnScheduleStoped_006, TestSize.Level0) +{ + static const uint64_t testContestId = 2; + static const std::vector testScheduleResult = {3, 4, 5, 6}; + + std::shared_ptr mockDeletion = Common::MakeShared(); + ASSERT_NE(mockDeletion, nullptr); + EXPECT_CALL(*mockDeletion, Update(_, _)) + .Times(Exactly(1)) + .WillOnce([](const std::vector &scheduleResult, std::shared_ptr &info) { + EXPECT_EQ(scheduleResult, testScheduleResult); + return true; + }); + std::shared_ptr contextCallback = Common::MakeShared(); + ASSERT_NE(contextCallback, nullptr); + EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(1); + + std::shared_ptr nodeCallback = + Common::MakeShared(testContestId, mockDeletion, contextCallback); + ASSERT_NE(nodeCallback, nullptr); + std::shared_ptr result = Common::MakeShared(); + ASSERT_NE(result, nullptr); + bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult); + ASSERT_EQ(ret1, true); + + bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult); + ASSERT_EQ(ret2, true); + nodeCallback->OnScheduleStoped(ResultCode::SUCCESS, result); +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS diff --git a/test/unittest/services/src/delete_impl_test.cpp b/test/unittest/services/src/delete_impl_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b470a01accfeeb6f5fb884863667826bb7959a3c --- /dev/null +++ b/test/unittest/services/src/delete_impl_test.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2022 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 "iam_ptr.h" + +#include "credential_info_impl.h" +#include "delete_impl.h" +#include "resource_node_pool.h" +#include "mock_iuser_auth_interface.h" +#include "mock_resource_node.h" +#include "mock_schedule_node_callback.h" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +using namespace testing; +using namespace testing::ext; +class DeleteImplTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; + +void DeleteImplTest::SetUpTestCase() +{ +} + +void DeleteImplTest::TearDownTestCase() +{ +} + +void DeleteImplTest::SetUp() +{ + MockIUserAuthInterface::Holder::GetInstance().Reset(); +} + +void DeleteImplTest::TearDown() +{ + MockIUserAuthInterface::Holder::GetInstance().Reset(); +} + +HWTEST_F(DeleteImplTest, AbandonHdiError, TestSize.Level0) +{ + std::vector testAuthToken = {1, 2, 3, 4}; + DeleteImpl::DeleteParam para = {}; + para.userId = 1; + para.credentialId = 1; + para.tokenId = 1; + para.token = testAuthToken; + auto mock = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_CALL(*mock, DeleteCredential(_, _, _, _)).WillRepeatedly(Return(1)); + + auto abandon = std::make_shared(para); + std::vector> scheduleList; + bool isCredentialDelete = false; + EXPECT_FALSE(abandon->Start(scheduleList, nullptr, isCredentialDelete)); +} + +HWTEST_F(DeleteImplTest, AbandonHdiEmpty, TestSize.Level0) +{ + std::vector testAuthToken = {1, 2, 3, 4}; + DeleteImpl::DeleteParam para = {}; + para.userId = 1; + para.credentialId = 1; + para.tokenId = 1; + para.token = testAuthToken; + + auto mock = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_CALL(*mock, DeleteCredential(_, _, _, _)).WillRepeatedly(Return(0)); + + auto abandon = std::make_shared(para); + std::vector> scheduleList; + bool isCredentialDelete = false; + EXPECT_FALSE(abandon->Start(scheduleList, nullptr, isCredentialDelete)); +} + +HWTEST_F(DeleteImplTest, AbandonUpdateHdiError, TestSize.Level0) +{ + std::vector testAuthToken = {1, 2, 3, 4}; + DeleteImpl::DeleteParam para = {}; + para.userId = 1; + para.credentialId = 1; + para.tokenId = 1; + para.token = testAuthToken; + + auto mock = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_CALL(*mock, UpdateAbandonResult(para.userId, _, _)).WillRepeatedly(Return(1)); + + auto abandon = std::make_shared(para); + std::vector scheduleResult = {1, 2, 3}; + std::shared_ptr info = nullptr; + EXPECT_FALSE(abandon->Update(scheduleResult, info)); +} + +HWTEST_F(DeleteImplTest, AbandonUpdateHdiSuccessful_001, TestSize.Level0) +{ + std::vector testAuthToken = {1, 2, 3, 4}; + DeleteImpl::DeleteParam para = {}; + para.userId = 1; + para.credentialId = 1; + para.tokenId = 1; + para.token = testAuthToken; + std::vector scheduleResult = {1, 2, 3}; + auto mock = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_CALL(*mock, UpdateAbandonResult(para.userId, _, _)) + .WillRepeatedly( + [](int32_t userId, const std::vector& scheduleResult, + std::vector& infos) { + HdiCredentialInfo info = { + .credentialId = 1, + .executorIndex = 2, + .templateId = 3, + .authType = static_cast(0), + .executorMatcher = 5, + .executorSensorHint = 6, + }; + infos.push_back(info); + return HDF_SUCCESS; + } + ); + + auto abandon = std::make_shared(para); + HdiCredentialInfo oldInfo = {}; + std::shared_ptr info = std::make_shared(para.userId, oldInfo); + EXPECT_TRUE(abandon->Update(scheduleResult, info)); + + EXPECT_EQ(info->GetCredentialId(), 1U); + EXPECT_EQ(info->GetAuthType(), static_cast(0)); + EXPECT_EQ(info->GetExecutorIndex(), 2U); + EXPECT_EQ(info->GetUserId(), 1); + EXPECT_EQ(info->GetTemplateId(), 3U); + EXPECT_EQ(info->GetExecutorMatcher(), 5U); + EXPECT_EQ(info->GetExecutorSensorHint(), 6U); +} + +HWTEST_F(DeleteImplTest, AbandonUpdateHdiSuccessful_002, TestSize.Level0) +{ + std::vector testAuthToken = {1, 2, 3, 4}; + DeleteImpl::DeleteParam para = {}; + para.userId = 1; + para.credentialId = 1; + para.tokenId = 1; + para.token = testAuthToken; + auto mock = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_CALL(*mock, UpdateAbandonResult(_, _, _)).WillRepeatedly(Return(0)); + auto abandon = std::make_shared(para); + + std::vector scheduleResult = {1, 2, 3}; + std::shared_ptr info = nullptr; + EXPECT_TRUE(abandon->Update(scheduleResult, info)); +} + +HWTEST_F(DeleteImplTest, DeleteImplTestStart_001, TestSize.Level0) +{ + std::vector testAuthToken = {1, 2, 3, 4}; + DeleteImpl::DeleteParam para = {}; + para.userId = 1; + para.credentialId = 1; + para.tokenId = 1; + para.token = testAuthToken; + constexpr uint64_t executorIndex = 60; + + auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_NE(mockHdi, nullptr); + EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _)) + .WillRepeatedly( + [](int32_t userId, uint64_t credentialId, const std::vector &authToken, + HdiCredentialOperateResult &operateResult) { + operateResult.operateType = HdiCredentialOperateType::CREDENTIAL_ABANDON; + operateResult.scheduleInfo.authType = HdiAuthType::FACE; + operateResult.scheduleInfo.executorMatcher = 10; + operateResult.scheduleInfo.executorIndexes.push_back(60); + std::vector executorMessages; + executorMessages.resize(1); + operateResult.scheduleInfo.executorMessages.push_back(executorMessages); + operateResult.scheduleInfo.scheduleId = 20; + operateResult.scheduleInfo.scheduleMode = HdiScheduleMode::ABANDON; + operateResult.scheduleInfo.templateIds.push_back(30); + return HDF_SUCCESS; + } + ); + auto resourceNode = MockResourceNode::CreateWithExecuteIndex(executorIndex, FACE, ALL_IN_ONE); + EXPECT_NE(resourceNode, nullptr); + EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode)); + auto abandon = std::make_shared(para); + EXPECT_NE(abandon, nullptr); + std::vector> scheduleList; + auto callback = Common::MakeShared(); + EXPECT_NE(callback, nullptr); + bool isCredentialDelete = false; + EXPECT_TRUE(abandon->Start(scheduleList, callback, isCredentialDelete)); + EXPECT_TRUE(abandon->Cancel()); + EXPECT_TRUE(ResourceNodePool::Instance().Delete(executorIndex)); +} + +HWTEST_F(DeleteImplTest, DeleteImplTestStart_002, TestSize.Level0) +{ + std::vector testAuthToken = {1, 2, 3, 4}; + DeleteImpl::DeleteParam para = {}; + para.userId = 1; + para.credentialId = 1; + para.tokenId = 1; + para.token = testAuthToken; + auto mock = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_CALL(*mock, DeleteCredential(_, _, _, _)).WillRepeatedly(Return(1)); + auto abandon = std::make_shared(para); + std::vector> scheduleList; + auto callback = Common::MakeShared(); + bool isCredentialDelete = false; + EXPECT_FALSE(abandon->Start(scheduleList, callback, isCredentialDelete)); +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/services/src/user_idm_database_test.cpp b/test/unittest/services/src/user_idm_database_test.cpp index 979dcb2e54c44647d85417843d57ae121d7b2f51..9196c376f66aea66979bdbb5f1eb4e6a94655258 100644 --- a/test/unittest/services/src/user_idm_database_test.cpp +++ b/test/unittest/services/src/user_idm_database_test.cpp @@ -225,13 +225,15 @@ HWTEST_F(UserIdmDatabaseTest, DeleteCredentialInfo002, TestSize.Level0) EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _)).Times(1); ON_CALL(*mockHdi, DeleteCredential) .WillByDefault( - [](int32_t userId, uint64_t credentialId, const std::vector &authToken, HdiCredentialInfo &info) { - info.authType = static_cast(1); - info.credentialId = 10; - info.executorIndex = 20; - info.executorMatcher = 30; - info.executorSensorHint = 40; - info.templateId = 50; + [](int32_t userId, uint64_t credentialId, const std::vector &authToken, + HdiCredentialOperateResult &operateResult) { + operateResult.operateType = HdiCredentialOperateType::CREDENTIAL_DELETE; + operateResult.credentialInfo.authType = static_cast(1); + operateResult.credentialInfo.credentialId = 10; + operateResult.credentialInfo.executorIndex = 20; + operateResult.credentialInfo.executorMatcher = 30; + operateResult.credentialInfo.executorSensorHint = 40; + operateResult.credentialInfo.templateId = 50; return HDF_SUCCESS; } ); diff --git a/test/unittest/services/src/user_idm_service_test.cpp b/test/unittest/services/src/user_idm_service_test.cpp index f453bae51af30e51fb61657a54ff1caed2f836e2..dce1351379ebd687d82a26427ae8823070273238 100644 --- a/test/unittest/services/src/user_idm_service_test.cpp +++ b/test/unittest/services/src/user_idm_service_test.cpp @@ -780,7 +780,7 @@ HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential001, TestSize.Level0) ) .WillOnce( [](int32_t result, const std::vector &extraInfo) { - EXPECT_EQ(result, HDF_FAILURE); + EXPECT_EQ(result, GENERAL_ERROR); return SUCCESS; } ); @@ -793,7 +793,7 @@ HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential001, TestSize.Level0) CHECK_PERMISSION_FAILED); IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION); EXPECT_EQ(service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback), - HDF_FAILURE); + GENERAL_ERROR); IpcCommon::DeleteAllPermission(); } @@ -828,13 +828,15 @@ HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential003, TestSize.Level0) EXPECT_NE(mockHdi, nullptr); EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _)) .WillOnce( - [](int32_t userId, uint64_t credentialId, const std::vector &authToken, HdiCredentialInfo &info) { - info.authType = static_cast(1); - info.credentialId = 10; - info.executorIndex = 20; - info.executorMatcher = 30; - info.executorSensorHint = 40; - info.templateId = 50; + [](int32_t userId, uint64_t credentialId, const std::vector &authToken, + HdiCredentialOperateResult &operateResult) { + operateResult.operateType = HdiCredentialOperateType::CREDENTIAL_DELETE; + operateResult.credentialInfo.authType = static_cast(1); + operateResult.credentialInfo.credentialId = 10; + operateResult.credentialInfo.executorIndex = 20; + operateResult.credentialInfo.executorMatcher = 30; + operateResult.credentialInfo.executorSensorHint = 40; + operateResult.credentialInfo.templateId = 50; return HDF_SUCCESS; } ); @@ -1102,6 +1104,61 @@ HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfoSync003, TestSize.Le EXPECT_EQ(ret, SUCCESS); IpcCommon::DeleteAllPermission(); } + +HWTEST_F(UserIdmServiceTest, UserIdmServiceClearUnavailableCredential001, TestSize.Level0) +{ + UserIdmService service(123123, true); + int32_t testUserId = 1; + + auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_NE(mockHdi, nullptr); + EXPECT_CALL(*mockHdi, ClearUnavailableCredential(_, _)).WillOnce(Return(HDF_FAILURE)); + service.ClearUnavailableCredential(testUserId); +} + +HWTEST_F(UserIdmServiceTest, UserIdmServiceClearUnavailableCredential002, TestSize.Level0) +{ + UserIdmService service(123123, true); + int32_t testUserId = 1; + + auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_NE(mockHdi, nullptr); + EXPECT_CALL(*mockHdi, ClearUnavailableCredential(_, _)).Times(1); + ON_CALL(*mockHdi, ClearUnavailableCredential) + .WillByDefault( + [](const std::vector& userIds, std::vector& infos) { + return HDF_SUCCESS; + } + ); + service.ClearUnavailableCredential(testUserId); +} + +HWTEST_F(UserIdmServiceTest, UserIdmServiceClearUnavailableCredential003, TestSize.Level0) +{ + UserIdmService service(123123, true); + int32_t testUserId = 1; + std::vector credentialInfoList; + + auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_NE(mockHdi, nullptr); + EXPECT_CALL(*mockHdi, ClearUnavailableCredential(_, _)).Times(1); + ON_CALL(*mockHdi, ClearUnavailableCredential) + .WillByDefault( + [](const std::vector& userIds, std::vector& infos) { + HdiCredentialInfo tempInfo = { + .credentialId = 1, + .executorIndex = 2, + .templateId = 3, + .authType = static_cast(1), + .executorMatcher = 2, + .executorSensorHint = 3, + }; + infos.push_back(tempInfo); + return HDF_SUCCESS; + } + ); + service.ClearUnavailableCredential(testUserId); +} } // namespace UserAuth } // namespace UserIam } // namespace OHOS \ No newline at end of file