From fced1270dda18f8e200070f0d40cfc9e04e0872e Mon Sep 17 00:00:00 2001 From: Tianshi Liu Date: Tue, 24 May 2022 14:19:44 +0800 Subject: [PATCH] fix coding issue; get template onRegisterFinish Signed-off-by: Tianshi Liu Change-Id: I9672b23d4765122983190b1e5887d7cd7c537dc6 --- common/BUILD.gn | 19 ++++++ .../async_command/async_command_base.h | 6 +- .../include/async_command/auth_command.h | 2 +- .../include/async_command/custom_command.h | 2 +- .../include/async_command/enroll_command.h | 2 +- .../include/async_command/identify_command.h | 2 +- common/executors/include/driver.h | 2 +- common/executors/include/executor.h | 5 +- .../framework/framework_executor_callback.h | 28 ++++----- .../include/system_ability_status_listener.h | 6 +- .../src/async_command/async_command_base.cpp | 4 +- common/executors/src/executor.cpp | 26 +++------ .../framework/framework_executor_callback.cpp | 3 + common/test/iam_fuzz_test.cpp | 58 +++++++++++++++++++ common/test/iam_fuzz_test.h | 32 ++++++++++ common/utils/iam_para2str.h | 3 +- 16 files changed, 151 insertions(+), 49 deletions(-) create mode 100644 common/test/iam_fuzz_test.cpp create mode 100644 common/test/iam_fuzz_test.h diff --git a/common/BUILD.gn b/common/BUILD.gn index d031427..367b367 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -64,3 +64,22 @@ ohos_source_set("iam_utils") { part_name = "user_auth" } + +config("iam_test_config") { + include_dirs = [ "test" ] +} + +ohos_source_set("iam_test") { + testonly = true + include_dirs = [ "//base/user_iam/user_auth/common/logs" ] + + external_deps = [ + "hilog_native:libhilog", + "utils_base:utils", + ] + + public_configs = [ ":iam_test_config" ] + + sources = [ "test/iam_fuzz_test.cpp" ] + part_name = "user_auth" +} diff --git a/common/executors/include/async_command/async_command_base.h b/common/executors/include/async_command/async_command_base.h index 2cd9dea..678f30c 100644 --- a/common/executors/include/async_command/async_command_base.h +++ b/common/executors/include/async_command/async_command_base.h @@ -32,13 +32,13 @@ class AsyncCommandBase : public std::enable_shared_from_this, public NoCopyable { public: AsyncCommandBase(std::string type, uint64_t scheduleId, std::shared_ptr executor); - virtual ~AsyncCommandBase() = default; + ~AsyncCommandBase() override = default; void OnHdiDisconnect() override; ResultCode StartProcess() override; void OnResult(ResultCode result) override; void OnResult(ResultCode result, const std::vector &extraInfo) override; - virtual void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override = 0; + void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override = 0; protected: static uint32_t GenerateCommandId(); @@ -51,7 +51,7 @@ protected: private: void EndProcess(); - std::string str_; + std::string description_; }; } // namespace UserAuth } // namespace UserIAM diff --git a/common/executors/include/async_command/auth_command.h b/common/executors/include/async_command/auth_command.h index 00af533..a670212 100644 --- a/common/executors/include/async_command/auth_command.h +++ b/common/executors/include/async_command/auth_command.h @@ -25,7 +25,7 @@ class AuthCommand : public AsyncCommandBase { public: AuthCommand(std::shared_ptr executor, uint64_t scheduleId, std::shared_ptr commandAttrs); - virtual ~AuthCommand() = default; + ~AuthCommand() override = default; void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; diff --git a/common/executors/include/async_command/custom_command.h b/common/executors/include/async_command/custom_command.h index d2172f4..35d1c11 100644 --- a/common/executors/include/async_command/custom_command.h +++ b/common/executors/include/async_command/custom_command.h @@ -26,7 +26,7 @@ namespace UserAuth { class CustomCommand : public AsyncCommandBase { public: CustomCommand(std::shared_ptr executor, std::shared_ptr attributes); - virtual ~CustomCommand() = default; + ~CustomCommand() override = default; void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; diff --git a/common/executors/include/async_command/enroll_command.h b/common/executors/include/async_command/enroll_command.h index 2990e41..31f736b 100644 --- a/common/executors/include/async_command/enroll_command.h +++ b/common/executors/include/async_command/enroll_command.h @@ -25,7 +25,7 @@ class EnrollCommand : public AsyncCommandBase { public: EnrollCommand(std::shared_ptr executor, uint64_t scheduleId, std::shared_ptr commandAttrs); - virtual ~EnrollCommand() = default; + ~EnrollCommand() override = default; void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; diff --git a/common/executors/include/async_command/identify_command.h b/common/executors/include/async_command/identify_command.h index e737548..bc7e821 100644 --- a/common/executors/include/async_command/identify_command.h +++ b/common/executors/include/async_command/identify_command.h @@ -25,7 +25,7 @@ class IdentifyCommand : public AsyncCommandBase { public: IdentifyCommand(std::shared_ptr executor, uint64_t scheduleId, std::shared_ptr commandAttrs); - virtual ~IdentifyCommand() = default; + ~IdentifyCommand() override = default; void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; diff --git a/common/executors/include/driver.h b/common/executors/include/driver.h index 83ba9ef..fbdf93f 100644 --- a/common/executors/include/driver.h +++ b/common/executors/include/driver.h @@ -30,7 +30,7 @@ namespace UserAuth { class Driver : public NoCopyable { public: Driver(const std::string &serviceName, HdiConfig hdiConfig); - virtual ~Driver() = default; + ~Driver() override = default; void OnHdiConnect(); void OnHdiDisconnect(); diff --git a/common/executors/include/executor.h b/common/executors/include/executor.h index 83be2be..b123935 100644 --- a/common/executors/include/executor.h +++ b/common/executors/include/executor.h @@ -34,7 +34,7 @@ namespace UserAuth { class Executor : public std::enable_shared_from_this, public NoCopyable { public: Executor(std::shared_ptr executorHdi, uint16_t hdiId); - virtual ~Executor() = default; + ~Executor() override = default; void OnHdiConnect(); void OnHdiDisconnect(); @@ -49,12 +49,11 @@ public: private: void RegisterExecutorCallback(ExecutorInfo &executorInfo); void RespondCallbackOnDisconnect(); - void SetStr(const int32_t executorId); sptr executorMessenger_; std::recursive_mutex mutex_; std::set> command2Respond_; std::shared_ptr executorHdi_; - std::string str_; + std::string description_; uint16_t hdiId_; }; } // namespace UserAuth diff --git a/common/executors/include/framework/framework_executor_callback.h b/common/executors/include/framework/framework_executor_callback.h index e0f15ef..e14ab1e 100644 --- a/common/executors/include/framework/framework_executor_callback.h +++ b/common/executors/include/framework/framework_executor_callback.h @@ -33,32 +33,34 @@ enum ScheduleMode { SCHEDULE_MODE_ENROLL = 0, SCHEDULE_MODE_AUTH = 1, }; -using pAuthAttributes = std::shared_ptr; + class FrameworkExecutorCallback : public AuthResPool::ExecutorCallback, public NoCopyable { public: explicit FrameworkExecutorCallback(std::shared_ptr executor); - virtual ~FrameworkExecutorCallback() = default; + ~FrameworkExecutorCallback() override = default; - int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, pAuthAttributes commandAttrs) override; - int32_t OnEndExecute(uint64_t scheduleId, pAuthAttributes consumerAttr) override; - int32_t OnSetProperty(pAuthAttributes properties) override; + int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs) override; + int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr) override; + int32_t OnSetProperty(std::shared_ptr properties) override; void OnMessengerReady(const sptr &messenger, std::vector &publicKey, std::vector &templateIds) override; int32_t OnGetProperty(std::shared_ptr conditions, std::shared_ptr values) override; private: - ResultCode OnBeginExecuteInner(uint64_t scheduleId, std::vector &publicKey, pAuthAttributes commandAttrs); - ResultCode OnEndExecuteInner(uint64_t scheduleId, pAuthAttributes consumerAttr); - ResultCode OnSetPropertyInner(pAuthAttributes properties); + ResultCode OnBeginExecuteInner(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs); + ResultCode OnEndExecuteInner(uint64_t scheduleId, std::shared_ptr consumerAttr); + ResultCode OnSetPropertyInner(std::shared_ptr properties); ResultCode OnGetPropertyInner( std::shared_ptr conditions, std::shared_ptr values); - ResultCode ProcessEnrollCommand(uint64_t scheduleId, pAuthAttributes properties); - ResultCode ProcessAuthCommand(uint64_t scheduleId, pAuthAttributes properties); - ResultCode ProcessIdentifyCommand(uint64_t scheduleId, pAuthAttributes properties); + ResultCode ProcessEnrollCommand(uint64_t scheduleId, std::shared_ptr properties); + ResultCode ProcessAuthCommand(uint64_t scheduleId, std::shared_ptr properties); + ResultCode ProcessIdentifyCommand(uint64_t scheduleId, std::shared_ptr properties); ResultCode ProcessCancelCommand(uint64_t scheduleId); - ResultCode ProcessDeleteTemplateCommand(pAuthAttributes properties); - ResultCode ProcessCustomCommand(pAuthAttributes properties); + ResultCode ProcessDeleteTemplateCommand(std::shared_ptr properties); + ResultCode ProcessCustomCommand(std::shared_ptr properties); ResultCode ProcessGetTemplateCommand( std::shared_ptr conditions, std::shared_ptr values); diff --git a/common/executors/include/system_ability_status_listener.h b/common/executors/include/system_ability_status_listener.h index 8ff9db5..19fa9ee 100644 --- a/common/executors/include/system_ability_status_listener.h +++ b/common/executors/include/system_ability_status_listener.h @@ -32,12 +32,12 @@ class SystemAbilityStatusListener : public OHOS::SystemAbilityStatusChangeStub, public: static sptr GetInstance(); - virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; - virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; private: SystemAbilityStatusListener() = default; - virtual ~SystemAbilityStatusListener() = default; + ~SystemAbilityStatusListener() override = default; static sptr instance_; static std::mutex mutex_; diff --git a/common/executors/src/async_command/async_command_base.cpp b/common/executors/src/async_command/async_command_base.cpp index 6811402..1cb4840 100644 --- a/common/executors/src/async_command/async_command_base.cpp +++ b/common/executors/src/async_command/async_command_base.cpp @@ -36,7 +36,7 @@ AsyncCommandBase::AsyncCommandBase(std::string type, uint64_t scheduleId, std::s { std::ostringstream ss; ss << "Command(type:" << type << ", id:" << commandId_ << ", scheduleId:" << GET_MASKED_STRING(scheduleId_) << ")"; - str_ = ss.str(); + description_ = ss.str(); } void AsyncCommandBase::OnHdiDisconnect() @@ -82,7 +82,7 @@ void AsyncCommandBase::EndProcess() const char *AsyncCommandBase::GetDescription() { - return str_.c_str(); + return description_.c_str(); } uint32_t AsyncCommandBase::GenerateCommandId() diff --git a/common/executors/src/executor.cpp b/common/executors/src/executor.cpp index 1a5a656..58fcd39 100644 --- a/common/executors/src/executor.cpp +++ b/common/executors/src/executor.cpp @@ -31,13 +31,17 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -const int32_t INVALID_EXECUTOR_ID = -1; - Executor::Executor(std::shared_ptr executorHdi, uint16_t hdiId) : executorHdi_(executorHdi), hdiId_(hdiId) { - SetStr(INVALID_EXECUTOR_ID); + auto hdi = executorHdi_; + IF_FALSE_LOGE_AND_RETURN(hdi != nullptr); + ExecutorInfo executorInfo = {}; + IF_FALSE_LOGE_AND_RETURN(hdi->GetExecutorInfo(executorInfo) == ResultCode::SUCCESS); + std::ostringstream ss; + ss << "Executor(hdiId:" << hdiId_ << ", executorId:" << executorInfo.executorId << ")"; + description_ = ss.str(); } void Executor::OnHdiConnect() @@ -67,7 +71,6 @@ void Executor::OnFrameworkReady() IAM_LOGE("Get executor info failed"); return; } - SetStr(executorInfo.executorId); RegisterExecutorCallback(executorInfo); } @@ -135,20 +138,7 @@ std::shared_ptr Executor::GetExecutorHdi() const char *Executor::GetDescription() { - std::lock_guard lock(mutex_); - return str_.c_str(); -} - -void Executor::SetStr(const int32_t executorId) -{ - std::lock_guard lock(mutex_); - std::ostringstream ss; - if (executorId == INVALID_EXECUTOR_ID) { - ss << "Executor(hdiId:" << hdiId_ << ", executorId: not set)"; - } else { - ss << "Executor(hdiId:" << hdiId_ << ", executorId:" << executorId << ")"; - } - str_ = ss.str(); + return description_.c_str(); } } // namespace UserAuth } // namespace UserIAM diff --git a/common/executors/src/framework/framework_executor_callback.cpp b/common/executors/src/framework/framework_executor_callback.cpp index 1812183..46489f6 100644 --- a/common/executors/src/framework/framework_executor_callback.cpp +++ b/common/executors/src/framework/framework_executor_callback.cpp @@ -14,6 +14,7 @@ */ #include "framework_executor_callback.h" + #include "auth_command.h" #include "custom_command.h" #include "enroll_command.h" @@ -111,6 +112,7 @@ int32_t FrameworkExecutorCallback::OnSetProperty(std::shared_ptr properties) { IF_FALSE_LOGE_AND_RETURN_VAL(properties != nullptr, ResultCode::GENERAL_ERROR); @@ -133,6 +135,7 @@ int32_t FrameworkExecutorCallback::OnGetProperty( { return OnGetPropertyInner(conditions, values); } + ResultCode FrameworkExecutorCallback::OnGetPropertyInner( std::shared_ptr conditions, std::shared_ptr values) { diff --git a/common/test/iam_fuzz_test.cpp b/common/test/iam_fuzz_test.cpp new file mode 100644 index 0000000..90c91bf --- /dev/null +++ b/common/test/iam_fuzz_test.cpp @@ -0,0 +1,58 @@ +/* + * 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_fuzz_test.h" + +#include "iam_logger.h" +#include "securec.h" + +#define LOG_LABEL OHOS::UserIAM::Common::LABEL_IAM_COMMON + +namespace OHOS { +namespace UserIAM { +namespace Common { +namespace { +constexpr int32_t MAX_DATA_LEN = 200; +} + +void FillFuzzUint8Vector(Parcel &parcel, std::vector &data) +{ + uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN; + auto buffer = parcel.ReadBuffer(len); + if (buffer == nullptr) { + IAM_LOGE("ReadBuffer len %{public}u fail", len); + return; + } + data.resize(len); + memcpy_s(static_cast(&data[0]), len, buffer, len); + IAM_LOGI("fill buffer len %{public}u ok", len); +} + +void FillFuzzUint64Vector(Parcel &parcel, std::vector &data) +{ + uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN; + uint32_t memLen = len * sizeof(uint64_t); + auto buffer = parcel.ReadBuffer(memLen); + if (buffer == nullptr) { + IAM_LOGE("ReadBuffer len %{public}u fail", memLen); + return; + } + data.resize(len); + memcpy_s(static_cast(&data[0]), memLen, buffer, memLen); + IAM_LOGI("fill buffer len %{public}u ok", len); +} +} // namespace Common +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/common/test/iam_fuzz_test.h b/common/test/iam_fuzz_test.h new file mode 100644 index 0000000..a4b87d8 --- /dev/null +++ b/common/test/iam_fuzz_test.h @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#ifndef IAM_FUZZ_TEST_H +#define IAM_FUZZ_TEST_H + +#include + +#include "parcel.h" + +namespace OHOS { +namespace UserIAM { +namespace Common { +void FillFuzzUint8Vector(Parcel &parcel, std::vector &data); +void FillFuzzUint64Vector(Parcel &parcel, std::vector &data); +} // namespace Common +} // namespace UserIAM +} // namespace OHOS + +#endif // IAM_FUZZ_TEST_H \ No newline at end of file diff --git a/common/utils/iam_para2str.h b/common/utils/iam_para2str.h index 222de95..be994a1 100644 --- a/common/utils/iam_para2str.h +++ b/common/utils/iam_para2str.h @@ -35,8 +35,7 @@ static inline std::string GetMaskedString(uint16_t val) return ss.str(); } -#define GET_MASKED_STRING(val) \ - OHOS::UserIAM::Common::GetMaskedString(static_cast(val)) +#define GET_MASKED_STRING(val) OHOS::UserIAM::Common::GetMaskedString(static_cast(val)) static inline std::string GetPointerNullStateString(void *p) {