From 8359478e9788ee737c330d01d384babba40b1138 Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 26 Dec 2022 15:44:36 +0800 Subject: [PATCH 01/59] Initial empty repository Match-id-2cb7441f228e04e08999dc54414f767810e9aaf7 --- README.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 5ce308b..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# security_component - -null \ No newline at end of file -- Gitee From 751255c4b12405c16e0e92fb7db1f0422fd89ebc Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 9 Feb 2023 18:50:21 +0800 Subject: [PATCH 02/59] Description:add security_component_service Match-id-db410f5b32498de4698793133b5ad07dd71f6b46 --- BUILD.gn | 36 ++ bundle.json | 64 +++ config/BUILD.gn | 23 + frameworks/BUILD.gn | 46 ++ frameworks/common/include/sec_comp_err.h | 40 ++ frameworks/common/include/sec_comp_log.h | 62 +++ frameworks/common/include/sec_comp_tool.h | 27 ++ frameworks/common/src/sec_comp_tool.cpp | 38 ++ .../include/sec_comp_click_event_parcel.h | 37 ++ .../src/location_button.cpp | 112 +++++ .../security_component/src/sec_comp_base.cpp | 123 ++++++ .../src/sec_comp_click_event_parcel.cpp | 101 +++++ .../security_component/src/sec_comp_rect.cpp | 45 ++ .../inner_api/security_component/BUILD.gn | 62 +++ .../include/i_sec_comp_service.h | 49 +++ .../include/location_button.h | 44 ++ .../security_component/include/paste_button.h | 35 ++ .../security_component/include/save_button.h | 36 ++ .../include/sec_comp_base.h | 41 ++ .../include/sec_comp_click_event.h | 37 ++ .../include/sec_comp_client.h | 65 +++ .../include/sec_comp_death_recipient.h | 32 ++ .../include/sec_comp_info.h | 29 ++ .../security_component/include/sec_comp_kit.h | 35 ++ .../include/sec_comp_load_callback.h | 33 ++ .../include/sec_comp_pattern.h | 92 ++++ .../include/sec_comp_proxy.h | 41 ++ .../include/sec_comp_rect.h | 50 +++ .../src/sec_comp_client.cpp | 220 ++++++++++ .../src/sec_comp_death_recipient.cpp | 27 ++ .../security_component/src/sec_comp_kit.cpp | 66 +++ .../src/sec_comp_load_callback.cpp | 61 +++ .../security_component/src/sec_comp_proxy.cpp | 204 +++++++++ .../security_component/test/BUILD.gn | 51 +++ .../unittest/src/location_button_test.cpp | 261 ++++++++++++ .../test/unittest/src/location_button_test.h | 38 ++ .../test/unittest/src/sec_comp_base_test.cpp | 215 ++++++++++ .../test/unittest/src/sec_comp_base_test.h | 37 ++ .../test/unittest/src/sec_comp_kit_test.cpp | 89 ++++ .../test/unittest/src/sec_comp_kit_test.h | 37 ++ ohos.build | 17 + security_component.gni | 14 + .../security_component_service/sa/BUILD.gn | 63 +++ .../sa/sa_main/app_state_observer.cpp | 118 ++++++ .../sa/sa_main/app_state_observer.h | 57 +++ .../sa/sa_main/sec_comp_entity.cpp | 81 ++++ .../sa/sa_main/sec_comp_entity.h | 75 ++++ .../sa/sa_main/sec_comp_info_helper.cpp | 124 ++++++ .../sa/sa_main/sec_comp_info_helper.h | 48 +++ .../sa/sa_main/sec_comp_manager.cpp | 264 ++++++++++++ .../sa/sa_main/sec_comp_manager.h | 81 ++++ .../sa/sa_main/sec_comp_service.cpp | 228 ++++++++++ .../sa/sa_main/sec_comp_service.h | 63 +++ .../sa/sa_main/sec_comp_stub.cpp | 186 ++++++++ .../sa/sa_main/sec_comp_stub.h | 45 ++ .../sa/sa_profile/3506.xml | 24 ++ .../sa/sa_profile/BUILD.gn | 19 + .../sa/security_component_service.cfg | 17 + .../sa/security_component_service.rc | 19 + .../sa/test/BUILD.gn | 66 +++ .../sa/test/mock/include/access_token.h | 48 +++ .../sa/test/mock/include/accesstoken_kit.h | 45 ++ .../sa/test/mock/include/app_mgr_interface.h | 38 ++ .../include/application_state_observer_stub.h | 73 ++++ .../mock/include/if_system_ability_manager.h | 44 ++ .../sa/test/mock/include/ipc_mock_skeleton.h | 38 ++ .../sa/test/mock/include/iservice_registry.h | 30 ++ .../sa/test/mock/include/mock_app_mgr_proxy.h | 37 ++ .../mock/include/mock_system_ability_proxy.h | 32 ++ .../sa/test/mock/include/system_ability.h | 47 ++ .../sa/test/mock/src/mock_app_mgr_proxy.cpp | 20 + .../sa/test/mock/src/mock_ipc_skeleton.cpp | 20 + .../test/mock/src/mock_iservice_registry.cpp | 24 ++ .../unittest/src/app_state_observer_test.cpp | 157 +++++++ .../unittest/src/app_state_observer_test.h | 41 ++ .../unittest/src/sec_comp_entity_test.cpp | 158 +++++++ .../test/unittest/src/sec_comp_entity_test.h | 42 ++ .../src/sec_comp_info_helper_test.cpp | 122 ++++++ .../unittest/src/sec_comp_info_helper_test.h | 37 ++ .../unittest/src/sec_comp_manager_test.cpp | 400 ++++++++++++++++++ .../test/unittest/src/sec_comp_manager_test.h | 39 ++ .../unittest/src/sec_comp_service_test.cpp | 319 ++++++++++++++ .../test/unittest/src/sec_comp_service_test.h | 42 ++ .../test/unittest/src/sec_comp_stub_test.cpp | 175 ++++++++ .../sa/test/unittest/src/sec_comp_stub_test.h | 69 +++ 85 files changed, 6447 insertions(+) create mode 100644 BUILD.gn create mode 100644 bundle.json create mode 100644 config/BUILD.gn create mode 100644 frameworks/BUILD.gn create mode 100644 frameworks/common/include/sec_comp_err.h create mode 100644 frameworks/common/include/sec_comp_log.h create mode 100644 frameworks/common/include/sec_comp_tool.h create mode 100644 frameworks/common/src/sec_comp_tool.cpp create mode 100644 frameworks/security_component/include/sec_comp_click_event_parcel.h create mode 100644 frameworks/security_component/src/location_button.cpp create mode 100644 frameworks/security_component/src/sec_comp_base.cpp create mode 100644 frameworks/security_component/src/sec_comp_click_event_parcel.cpp create mode 100644 frameworks/security_component/src/sec_comp_rect.cpp create mode 100644 interfaces/inner_api/security_component/BUILD.gn create mode 100644 interfaces/inner_api/security_component/include/i_sec_comp_service.h create mode 100644 interfaces/inner_api/security_component/include/location_button.h create mode 100644 interfaces/inner_api/security_component/include/paste_button.h create mode 100644 interfaces/inner_api/security_component/include/save_button.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_base.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_click_event.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_client.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_death_recipient.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_info.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_kit.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_load_callback.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_pattern.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_proxy.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_rect.h create mode 100644 interfaces/inner_api/security_component/src/sec_comp_client.cpp create mode 100644 interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp create mode 100644 interfaces/inner_api/security_component/src/sec_comp_kit.cpp create mode 100644 interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp create mode 100644 interfaces/inner_api/security_component/src/sec_comp_proxy.cpp create mode 100644 interfaces/inner_api/security_component/test/BUILD.gn create mode 100644 interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/location_button_test.h create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.h create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.h create mode 100644 ohos.build create mode 100644 security_component.gni create mode 100644 services/security_component_service/sa/BUILD.gn create mode 100644 services/security_component_service/sa/sa_main/app_state_observer.cpp create mode 100644 services/security_component_service/sa/sa_main/app_state_observer.h create mode 100644 services/security_component_service/sa/sa_main/sec_comp_entity.cpp create mode 100644 services/security_component_service/sa/sa_main/sec_comp_entity.h create mode 100644 services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp create mode 100644 services/security_component_service/sa/sa_main/sec_comp_info_helper.h create mode 100644 services/security_component_service/sa/sa_main/sec_comp_manager.cpp create mode 100644 services/security_component_service/sa/sa_main/sec_comp_manager.h create mode 100644 services/security_component_service/sa/sa_main/sec_comp_service.cpp create mode 100644 services/security_component_service/sa/sa_main/sec_comp_service.h create mode 100644 services/security_component_service/sa/sa_main/sec_comp_stub.cpp create mode 100644 services/security_component_service/sa/sa_main/sec_comp_stub.h create mode 100644 services/security_component_service/sa/sa_profile/3506.xml create mode 100644 services/security_component_service/sa/sa_profile/BUILD.gn create mode 100644 services/security_component_service/sa/security_component_service.cfg create mode 100644 services/security_component_service/sa/security_component_service.rc create mode 100644 services/security_component_service/sa/test/BUILD.gn create mode 100644 services/security_component_service/sa/test/mock/include/access_token.h create mode 100644 services/security_component_service/sa/test/mock/include/accesstoken_kit.h create mode 100644 services/security_component_service/sa/test/mock/include/app_mgr_interface.h create mode 100644 services/security_component_service/sa/test/mock/include/application_state_observer_stub.h create mode 100644 services/security_component_service/sa/test/mock/include/if_system_ability_manager.h create mode 100644 services/security_component_service/sa/test/mock/include/ipc_mock_skeleton.h create mode 100644 services/security_component_service/sa/test/mock/include/iservice_registry.h create mode 100644 services/security_component_service/sa/test/mock/include/mock_app_mgr_proxy.h create mode 100644 services/security_component_service/sa/test/mock/include/mock_system_ability_proxy.h create mode 100644 services/security_component_service/sa/test/mock/include/system_ability.h create mode 100644 services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp create mode 100644 services/security_component_service/sa/test/mock/src/mock_ipc_skeleton.cpp create mode 100644 services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/app_state_observer_test.h create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.h create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.h create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_service_test.h create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000..2dbefbe --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,36 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/ohos.gni") + +group("security_component_build_module") { + if (is_standard_system) { + deps = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + "${sec_comp_root_dir}/services/security_component_service/sa:security_component_service", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_profile:security_component_sa_profile_standard", + ] + } +} + +group("security_component_build_module_test") { + testonly = true + deps = [] + if (is_standard_system) { + deps += [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/test:unittest", + "${sec_comp_root_dir}/services/security_component_service/sa/test:unittest", + ] + } +} diff --git a/bundle.json b/bundle.json new file mode 100644 index 0000000..355285c --- /dev/null +++ b/bundle.json @@ -0,0 +1,64 @@ +{ + "name": "@openharmony/security_component", + "description": "security_component", + "version": "3.1.0", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "base/security/security_component" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "security_component", + "subsystem": "security", + "syscap": [], + "hisysevent_config": [ + ], + "features": [ "security_component_feature_coverage = false" ], + "adapted_system_type": [ + "standard" + ], + "rom": "2048KB", + "ram": "5102KB", + "deps": { + "components": [ + "aafwk_standard", + "ability_base", + "ability_runtime", + "appexecfwk_standard", + "hiviewdfx_hilog_native", + "hisysevent_native", + "ipc_core", + "safwk", + "samgr", + "c_utils", + "samgr_proxy", + "samgr", + "system_ability_fwk", + "utils_base" + ], + "third_party": [ + "cJSON", + "sqlite", + "mbedtls" + ] + }, + "build": { + "sub_component": [ + "//base/security/security_component:security_component_build_module" + ], + "inner_kits": [ + { + "name": "//base/security/security_component/interfaces/inner_api/security_component:libsecurity_component_sdk", + "header": { + "header_files": [ + "sec_comp_kit.h" + ], + "header_base": "//base/security/security_component/interfaces/inner_api/security_component/include" + } + } + ] + } + } +} diff --git a/config/BUILD.gn b/config/BUILD.gn new file mode 100644 index 0000000..9cf7554 --- /dev/null +++ b/config/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 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. + +declare_args() { + security_component_feature_coverage = false +} + +config("coverage_flags") { + if (security_component_feature_coverage) { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + } +} diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn new file mode 100644 index 0000000..08be8de --- /dev/null +++ b/frameworks/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/ohos.gni") + +ohos_shared_library("libsecurity_component_framework") { + subsystem_name = "security" + part_name = "security_component" + output_name = "libsecurity_component_framework" + + include_dirs = [ + "include", + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + "//third_party/json/include", + ] + + sources = [ + "common/src/sec_comp_tool.cpp", + "security_component/src/location_button.cpp", + "security_component/src/sec_comp_base.cpp", + "security_component/src/sec_comp_click_event_parcel.cpp", + "security_component/src/sec_comp_rect.cpp", + ] + + configs = [ "//base/security/security_component/config:coverage_flags" ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] +} diff --git a/frameworks/common/include/sec_comp_err.h b/frameworks/common/include/sec_comp_err.h new file mode 100644 index 0000000..e16edc5 --- /dev/null +++ b/frameworks/common/include/sec_comp_err.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 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 FRAMEWORKS_COMMON_SECURITY_COMPONENT_ERR_H +#define FRAMEWORKS_COMMON_SECURITY_COMPONENT_ERR_H + +#include +namespace OHOS { +namespace Security { +namespace SecurityComponent { +enum SCErrCode : int32_t { + SC_OK = 0, + + SC_SERVICE_ERROR_VALUE_INVALID = -50, + SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL = -51, + SC_SERVICE_ERROR_MEMORY_OPERATE_FAIL = -52, + SC_SERVICE_ERROR_IPC_REQUEST_FAIL = -54, + SC_SERVICE_ERROR_SERVICE_NOT_EXIST = -55, + SC_SERVICE_ERROR_COMPONENT_INFO_INVALID = -56, + SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP = -57, + SC_SERVICE_ERROR_COMPONENT_NOT_EXIST = -58, + SC_SERVICE_ERROR_PERMISSION_OPER_FAIL = -59, + SC_SERVICE_ERROR_CLICK_EVENT_INVALID = -60, + SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL = -61, +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // FRAMEWORKS_COMMON_SECURITY_COMPONENT_ERR_H diff --git a/frameworks/common/include/sec_comp_log.h b/frameworks/common/include/sec_comp_log.h new file mode 100644 index 0000000..71ab801 --- /dev/null +++ b/frameworks/common/include/sec_comp_log.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_LOG_H +#define SECURITY_COMPONENT_LOG_H + +#ifdef HILOG_ENABLE + +#include "hilog/log.h" + +#ifndef __cplusplus + +#undef LOG_DOMAIN +#define LOG_DOMAIN 0xD002F07 + +#define SC_LOG_DEBUG(fmt, ...) HILOG_DEBUG(LOG_CORE, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_INFO(fmt, ...) HILOG_INFO(LOG_CORE, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_WARN(fmt, ...) HILOG_WARN(LOG_CORE, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_ERROR(fmt, ...) HILOG_ERROR(LOG_CORE, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_FATAL(fmt, ...) HILOG_FATAL(LOG_CORE, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) + +#else + +static constexpr unsigned int SECURITY_DOMAIN_SECURITY_COMPONENT = 0xD002F07; + +#define SC_LOG_DEBUG(label, fmt, ...) \ + OHOS::HiviewDFX::HiLog::Debug(label, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_INFO(label, fmt, ...) OHOS::HiviewDFX::HiLog::Info(label, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_WARN(label, fmt, ...) OHOS::HiviewDFX::HiLog::Warn(label, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_ERROR(label, fmt, ...) \ + OHOS::HiviewDFX::HiLog::Error(label, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) +#define SC_LOG_FATAL(label, fmt, ...) \ + OHOS::HiviewDFX::HiLog::Fatal(label, "[%{public}s]:" fmt, __func__, ##__VA_ARGS__) + +#endif // __cplusplus + +#else + +#include + +#undef LOG_TAG + +#define SC_LOG_DEBUG(fmt, ...) printf("[%s] debug: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__) +#define SC_LOG_INFO(fmt, ...) printf("[%s] info: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__) +#define SC_LOG_WARN(fmt, ...) printf("[%s] warn: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__) +#define SC_LOG_ERROR(fmt, ...) printf("[%s] error: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__) +#define SC_LOG_FATAL(fmt, ...) printf("[%s] fatal: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__) + +#endif // HILOG_ENABLE + +#endif // SECURITY_COMPONENT_LOG_H diff --git a/frameworks/common/include/sec_comp_tool.h b/frameworks/common/include/sec_comp_tool.h new file mode 100644 index 0000000..1ee6108 --- /dev/null +++ b/frameworks/common/include/sec_comp_tool.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_TOOL_H +#define SECURITY_COMPONENT_TOOL_H + +#include + +namespace OHOS { +namespace Security { +namespace SecurityComponent { + uint64_t GetCurrentTime(); +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_TOOL_H diff --git a/frameworks/common/src/sec_comp_tool.cpp b/frameworks/common/src/sec_comp_tool.cpp new file mode 100644 index 0000000..24a273b --- /dev/null +++ b/frameworks/common/src/sec_comp_tool.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 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. + */ + +#include +#include + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr uint32_t MS_PER_SECOND = 1000; +static constexpr uint32_t NS_PER_MILLISECOND = 1000000; +} + +uint64_t GetCurrentTime() +{ + struct timespec current = {0}; + int ret = clock_gettime(CLOCK_REALTIME, ¤t); + if (ret != 0) { + return 0; + } + return static_cast(current.tv_sec) * MS_PER_SECOND + current.tv_nsec / NS_PER_MILLISECOND; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/frameworks/security_component/include/sec_comp_click_event_parcel.h b/frameworks/security_component/include/sec_comp_click_event_parcel.h new file mode 100644 index 0000000..3e1d1a0 --- /dev/null +++ b/frameworks/security_component/include/sec_comp_click_event_parcel.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_TOUCH_INFO_PARCEL_H +#define SECURITY_COMPONENT_TOUCH_INFO_PARCEL_H +#include "parcel.h" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +struct SecCompClickEventParcel final : public Parcelable { + SecCompClickEventParcel() = default; + + ~SecCompClickEventParcel() override = default; + + bool Marshalling(Parcel& out) const override; + + static SecCompClickEventParcel* Unmarshalling(Parcel& in); + + SecCompClickEvent touchInfoParams_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_TOUCH_INFO_PARCEL_H diff --git a/frameworks/security_component/src/location_button.cpp b/frameworks/security_component/src/location_button.cpp new file mode 100644 index 0000000..dca93c5 --- /dev/null +++ b/frameworks/security_component/src/location_button.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 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. + */ + +#include "location_button.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButton"}; +static const std::string JSON_FONT_FAMILY = "font"; +static const std::string JSON_LABEL_TYPE = "label"; +static const std::string JSON_ICON = "icon"; +static const std::string JSON_FONT_SIZE = "fontSize"; +static const std::string JSON_FONT_COLOR = "fontColor"; +static const std::string JSON_BG_COLOR = "bgColor"; +static const std::string JSON_BUTTON_TYPE = "buttonType"; +} + +void LocationButton::FromJson(const nlohmann::json& jsonSrc) +{ + SecCompBase::FromJson(jsonSrc); + if (jsonSrc.find(JSON_FONT_FAMILY) != jsonSrc.end() && jsonSrc.at(JSON_FONT_FAMILY).is_number()) { + int32_t font = jsonSrc.at(JSON_FONT_FAMILY).get(); + if (IsComponentFontFamilyValid(font)) { + font_ = static_cast(font); + } + } + + if (jsonSrc.find(JSON_LABEL_TYPE) != jsonSrc.end() && jsonSrc.at(JSON_LABEL_TYPE).is_number()) { + int32_t label = jsonSrc.at(JSON_LABEL_TYPE).get(); + if (IsComponentLabelValid(label)) { + label_ = static_cast(label); + } + } + + if (jsonSrc.find(JSON_ICON) != jsonSrc.end() && jsonSrc.at(JSON_ICON).is_number()) { + int32_t icon = jsonSrc.at(JSON_ICON).get(); + if (IsComponentIconValid(icon)) { + icon_ = static_cast(icon); + } + } + + if (jsonSrc.find(JSON_FONT_SIZE) != jsonSrc.end() && jsonSrc.at(JSON_FONT_SIZE).is_number()) { + fontSize_ = jsonSrc.at(JSON_FONT_SIZE).get(); + } + + if (jsonSrc.find(JSON_FONT_COLOR) != jsonSrc.end() && jsonSrc.at(JSON_FONT_COLOR).is_number()) { + fontColor_.value = jsonSrc.at(JSON_FONT_COLOR).get(); + } + + if (jsonSrc.find(JSON_BG_COLOR) != jsonSrc.end() && jsonSrc.at(JSON_BG_COLOR).is_number()) { + bgColor_.value = jsonSrc.at(JSON_BG_COLOR).get(); + } + + if (jsonSrc.find(JSON_BUTTON_TYPE) != jsonSrc.end() && jsonSrc.at(JSON_BUTTON_TYPE).is_number()) { + int32_t type = jsonSrc.at(JSON_BUTTON_TYPE).get(); + if (IsComponentButtonTypeValid(type)) { + buttonType_ = static_cast(type); + } + } +} + +void LocationButton::ToJson(nlohmann::json& jsonRes) const +{ + SecCompBase::ToJson(jsonRes); + jsonRes[JSON_FONT_FAMILY] = font_; + jsonRes[JSON_LABEL_TYPE] = label_; + jsonRes[JSON_ICON] = icon_; + jsonRes[JSON_FONT_SIZE] = fontSize_; + jsonRes[JSON_FONT_COLOR] = fontColor_.value; + jsonRes[JSON_BG_COLOR] = bgColor_.value; + jsonRes[JSON_BUTTON_TYPE] = buttonType_; +} + +bool LocationButton::IsValid(void) const +{ + bool locationValid = IsComponentFontFamilyValid(font_) && IsComponentLabelValid(label_) && + IsComponentIconValid(icon_) && IsComponentFontSizeValid(fontSize_) && + IsComponentButtonTypeValid(buttonType_); + + return locationValid && SecCompBase::IsValid(); +} + +bool LocationButton::operator==(const LocationButton& other) const +{ + if (font_ != other.font_ || label_ != other.label_ || icon_ != other.icon_ || + fontSize_ != other.fontSize_ || fontColor_.value != other.fontColor_.value || + bgColor_.value != other.bgColor_.value || buttonType_ != other.buttonType_) { + SC_LOG_INFO(LABEL, "LocationButton is not equal"); + return false; + } + + return SecCompBase::operator==(other); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp new file mode 100644 index 0000000..99ac1ae --- /dev/null +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_base.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompBase"}; +static const std::string JSON_RECT = "rect"; +static const std::string JSON_SC_TYPE = "type"; +static const std::string JSON_RECT_X = "x"; +static const std::string JSON_RECT_Y = "y"; +static const std::string JSON_RECT_WIDTH = "width"; +static const std::string JSON_RECT_HEIGHT = "height"; +} + +void from_json(const nlohmann::json& j, SecCompRect& p) +{ + SecCompRect res; + if (j.is_discarded()) { + SC_LOG_ERROR(LABEL, "component info invalid"); + return ; + } + + if (j.find(JSON_RECT_X) != j.end() && j.at(JSON_RECT_X).is_number_float()) { + res.x_ = j.at(JSON_RECT_X).get(); + } else { + SC_LOG_ERROR(LABEL, "x is invalid"); + return; + } + + if (j.find(JSON_RECT_Y) != j.end() && j.at(JSON_RECT_Y).is_number_float()) { + res.y_ = j.at(JSON_RECT_Y).get(); + } else { + SC_LOG_ERROR(LABEL, "y is invalid."); + return; + } + + if (j.find(JSON_RECT_WIDTH) != j.end() && j.at(JSON_RECT_WIDTH).is_number_float()) { + res.width_ = j.at(JSON_RECT_WIDTH).get(); + } else { + SC_LOG_ERROR(LABEL, "width is invalid."); + return; + } + + if (j.find(JSON_RECT_HEIGHT) != j.end() && j.at(JSON_RECT_HEIGHT).is_number_float()) { + res.height_ = j.at(JSON_RECT_HEIGHT).get(); + } else { + SC_LOG_ERROR(LABEL, "height is invalid."); + return; + } + + p = res; +} + +void SecCompBase::FromJson(const nlohmann::json& jsonSrc) +{ + if (jsonSrc.find(JSON_SC_TYPE) != jsonSrc.end() && jsonSrc.at(JSON_SC_TYPE).is_number()) { + int32_t type = jsonSrc.at(JSON_SC_TYPE).get(); + if (IsComponentTypeValid(type)) { + type_ = static_cast(type); + } else { + SC_LOG_ERROR(LABEL, "type is invalid."); + return; + } + } else { + SC_LOG_ERROR(LABEL, "get type fail."); + return; + } + + if (jsonSrc.find(JSON_RECT) != jsonSrc.end() && jsonSrc.at(JSON_RECT).is_object()) { + rect_ = jsonSrc.at(JSON_RECT).get(); + } else { + SC_LOG_ERROR(LABEL, "rect is invalid."); + return; + } +} + +void SecCompBase::ToJson(nlohmann::json& jsonRes) const +{ + jsonRes[JSON_SC_TYPE] = type_; + jsonRes[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, rect_.x_}, + {JSON_RECT_Y, rect_.y_}, + {JSON_RECT_WIDTH, rect_.width_}, + {JSON_RECT_HEIGHT, rect_.height_} + }; +} + +bool SecCompBase::IsValid(void) const +{ + return IsComponentTypeValid(type_) && rect_.IsValid(); +} + +bool SecCompBase::operator==(const SecCompBase& other) const +{ + bool result = (type_ == other.type_ && rect_.x_ == other.rect_.x_ && rect_.y_ == other.rect_.y_ && + rect_.width_ == other.rect_.width_ && rect_.height_ == other.rect_.height_); + if (!result) { + SC_LOG_INFO(LABEL, "SecCompBase is not equal"); + } + return result; +} +} // namespace base +} // namespace Security +} // namespace OHOS + diff --git a/frameworks/security_component/src/sec_comp_click_event_parcel.cpp b/frameworks/security_component/src/sec_comp_click_event_parcel.cpp new file mode 100644 index 0000000..8b7f290 --- /dev/null +++ b/frameworks/security_component/src/sec_comp_click_event_parcel.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_click_event_parcel.h" +#include "sec_comp_log.h" +#include "securec.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompClickEventParcel"}; +} + +bool SecCompClickEventParcel::Marshalling(Parcel& out) const +{ + if (!(out.WriteFloat(this->touchInfoParams_.touchX)) || !(out.WriteFloat(this->touchInfoParams_.touchY))) { + SC_LOG_ERROR(LABEL, "Write touch xy pointer fail"); + return false; + } + + if (!(out.WriteUint64(this->touchInfoParams_.timestamp))) { + SC_LOG_ERROR(LABEL, "Write touch timestamp fail"); + return false; + } + + if (!(out.WriteUint32(this->touchInfoParams_.extraInfo.dataSize))) { + SC_LOG_ERROR(LABEL, "Write extraInfo dataSize fail"); + return false; + } + + if (this->touchInfoParams_.extraInfo.dataSize != 0 && + !(out.WriteBuffer(this->touchInfoParams_.extraInfo.data, this->touchInfoParams_.extraInfo.dataSize))) { + SC_LOG_ERROR(LABEL, "Write touch extraInfo data fail"); + return false; + } + + return true; +} + +SecCompClickEventParcel* SecCompClickEventParcel::Unmarshalling(Parcel& in) +{ + SecCompClickEventParcel* touchInfoParcel = new (std::nothrow) SecCompClickEventParcel(); + if (touchInfoParcel == nullptr) { + SC_LOG_ERROR(LABEL, "Alloc policy parcel fail"); + return nullptr; + } + + SecCompClickEvent touchInfo; + if (!in.ReadFloat(touchInfo.touchX) || !in.ReadFloat(touchInfo.touchY)) { + SC_LOG_ERROR(LABEL, "Read touch xy porinter fail"); + delete touchInfoParcel; + return nullptr; + } + + if (!in.ReadUint64(touchInfo.timestamp)) { + SC_LOG_ERROR(LABEL, "Read timestamp fail"); + delete touchInfoParcel; + return nullptr; + } + + if (!in.ReadUint32(touchInfo.extraInfo.dataSize)) { + SC_LOG_ERROR(LABEL, "Read extraInfo data size fail"); + delete touchInfoParcel; + return nullptr; + } + + if (touchInfo.extraInfo.dataSize == 0) { + touchInfoParcel->touchInfoParams_ = touchInfo; + return touchInfoParcel; + } else if (touchInfo.extraInfo.dataSize > MAX_EXTRA_SIZE) { + SC_LOG_ERROR(LABEL, "Read extraInfo data size invalid"); + delete touchInfoParcel; + return nullptr; + } + + touchInfo.extraInfo.data = const_cast(in.ReadBuffer(touchInfo.extraInfo.dataSize)); + if (touchInfo.extraInfo.data == nullptr) { + SC_LOG_ERROR(LABEL, "Read extraInfo data failed"); + delete touchInfoParcel; + return nullptr; + } + + touchInfoParcel->touchInfoParams_ = touchInfo; + return touchInfoParcel; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/frameworks/security_component/src/sec_comp_rect.cpp b/frameworks/security_component/src/sec_comp_rect.cpp new file mode 100644 index 0000000..ccb9cad --- /dev/null +++ b/frameworks/security_component/src/sec_comp_rect.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_rect.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +bool SecCompRect::IsValid() const +{ + return GreatNotEqual(x_, 0.0) && GreatNotEqual(y_, 0.0) && + GreatNotEqual(width_, 0.0) && GreatNotEqual(height_, 0.0); +} + +bool SecCompRect::IsRectOverlaped(const SecCompRect& other) const +{ + double xLow = other.x_; + double xHigh = other.x_ + other.width_ ; + double yLow = other.y_ ; + double yHigh = other.y_ + other.height_; + + return (!(GreatOrEqual(xLow, (x_ + width_)) || GreatOrEqual(x_, xHigh))) && + (!(GreatOrEqual(y_, yHigh) || GreatOrEqual(yLow, (y_ + height_)))); +} + +bool SecCompRect::IsInRect(double x, double y) const +{ + return (GreatOrEqual(x, x_) && GreatOrEqual((x_ + width_), x) && + GreatOrEqual(y, y_) && GreatOrEqual((y_ + height_), y)); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn new file mode 100644 index 0000000..2178b5c --- /dev/null +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/ohos.gni") + +config("sec_comp_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${sec_comp_root_dir}/frameworks/common/include", + "//third_party/json/include", + ] +} + +ohos_shared_library("libsecurity_component_sdk") { + subsystem_name = "security" + part_name = "security_component" + output_name = "libsecurity_component_sdk" + + public_configs = [ ":sec_comp_config" ] + + include_dirs = [ + "include", + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] + + sources = [ + "src/sec_comp_client.cpp", + "src/sec_comp_death_recipient.cpp", + "src/sec_comp_kit.cpp", + "src/sec_comp_load_callback.cpp", + "src/sec_comp_proxy.cpp", + ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + ] + + configs = [ "//base/security/security_component/config:coverage_flags" ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] +} diff --git a/interfaces/inner_api/security_component/include/i_sec_comp_service.h b/interfaces/inner_api/security_component/include/i_sec_comp_service.h new file mode 100644 index 0000000..4ca8e97 --- /dev/null +++ b/interfaces/inner_api/security_component/include/i_sec_comp_service.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 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 I_SECURITY_COMPONENT_SERVICE_H +#define I_SECURITY_COMPONENT_SERVICE_H + +#include +#include "iremote_broker.h" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +constexpr int32_t SA_ID_SECURITY_COMPONENT_SERVICE = 3506; + +class ISecCompService : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.ISecCompService"); + + virtual int32_t RegisterSecurityComponent(SecCompType type, + const std::string& componentInfo, int32_t& scId) = 0; + virtual int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) = 0; + virtual int32_t UnregisterSecurityComponent(int32_t scId) = 0; + virtual int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, + const SecCompClickEvent& touchInfo) = 0; + + enum class InterfaceCode { + REGISTER_SECURITY_COMPONENT = 0xff01, + UPDATE_SECURITY_COMPONENT = 0xff02, + UNREGISTER_SECURITY_COMPONENT = 0xff03, + REPORT_SECURITY_COMPONENT_CLICK_EVENT = 0xff04, + GET_SECURITY_COMPONENT_ENHANCE_OBJECT = 0xff05, + }; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // I_SECURITY_COMPONENT_SERVICE_H diff --git a/interfaces/inner_api/security_component/include/location_button.h b/interfaces/inner_api/security_component/include/location_button.h new file mode 100644 index 0000000..00d155c --- /dev/null +++ b/interfaces/inner_api/security_component/include/location_button.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 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 I_SECURITY_COMPONENT_LOCATION_BUTTON_H +#define I_SECURITY_COMPONENT_LOCATION_BUTTON_H + +#include +#include "nlohmann/json.hpp" +#include "sec_comp_base.h" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class LocationButton : public SecCompBase { +public: + SecCompFontFamily font_ = UNKNOWN_FONT_FAMILY_TYPE; + SecCompLabel label_ = UNKNOWN_LABEL; + SecCompIcon icon_ = UNKNOWN_ICON; + uint32_t fontSize_ = 0; + SecCompColor fontColor_; + SecCompColor bgColor_; + SecCompButtonType buttonType_ = UNKNOWN_BUTTON_TYPE; + + virtual void FromJson(const nlohmann::json& jsonSrc) override; + virtual void ToJson(nlohmann::json& jsonRes) const override; + virtual bool IsValid(void) const override; + bool operator==(const LocationButton& other) const; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // I_SECURITY_COMPONENT_LOCATION_BUTTON_H diff --git a/interfaces/inner_api/security_component/include/paste_button.h b/interfaces/inner_api/security_component/include/paste_button.h new file mode 100644 index 0000000..91a4465 --- /dev/null +++ b/interfaces/inner_api/security_component/include/paste_button.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_PASTE_BUTTON_H +#define SECURITY_COMPONENT_PASTE_BUTTON_H + +#include +#include "nlohmann/json.hpp" +#include "sec_comp_base.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class PasteButton : public SecCompBase { +public: + bool operator==(const PasteButton& other) const + { + return SecCompBase::operator==(other); + }; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_LOCATION_BUTTON_H diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h new file mode 100644 index 0000000..53e5752 --- /dev/null +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_SAVE_BUTTON_H +#define SECURITY_COMPONENT_SAVE_BUTTON_H + +#include +#include "nlohmann/json.hpp" +#include "sec_comp_base.h" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SaveButton : public SecCompBase { +public: + bool operator==(const SaveButton& other) const + { + return SecCompBase::operator==(other); + }; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_SAVE_BUTTON_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h new file mode 100644 index 0000000..a29ce44 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_BASE_H +#define SECURITY_COMPONENT_BASE_H + +#include "nlohmann/json.hpp" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompBase { +public: + SecCompBase() = default; + virtual ~SecCompBase() = default; + + SecCompType type_ = UNKNOWN_SC_TYPE; + SecCompRect rect_; + + virtual void FromJson(const nlohmann::json& jsonSrc); + virtual void ToJson(nlohmann::json& jsonRes) const; + virtual bool IsValid(void) const; + bool operator==(const SecCompBase& other) const; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_BASE_H + diff --git a/interfaces/inner_api/security_component/include/sec_comp_click_event.h b/interfaces/inner_api/security_component/include/sec_comp_click_event.h new file mode 100644 index 0000000..b084cc8 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_click_event.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_CLICK_EVENT_H +#define SECURITY_COMPONENT_CLICK_EVENT_H +namespace OHOS { +namespace Security { +namespace SecurityComponent { +static constexpr uint32_t MAX_EXTRA_SIZE = 0x1000; + +struct ExtraInfo { + uint32_t dataSize; + uint8_t* data; +}; + +struct SecCompClickEvent { + float touchX; + float touchY; + uint64_t timestamp; + ExtraInfo extraInfo; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_CLICK_EVENT_H + diff --git a/interfaces/inner_api/security_component/include/sec_comp_client.h b/interfaces/inner_api/security_component/include/sec_comp_client.h new file mode 100644 index 0000000..d30f1ea --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_client.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_CLIENT_H +#define SECURITY_COMPONENT_CLIENT_H + +#include +#include +#include +#include "i_sec_comp_service.h" +#include "sec_comp_death_recipient.h" +#include "sec_comp_err.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompClient final { +public: + static SecCompClient& GetInstance(); + + int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId); + int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); + int32_t UnregisterSecurityComponent(int32_t scId); + int32_t ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo); + + void FinishStartSASuccess(const sptr& remoteObject); + void FinishStartSAFail(); + void OnRemoteDiedHandle(); + +private: + SecCompClient(); + virtual ~SecCompClient(); + DISALLOW_COPY_AND_MOVE(SecCompClient); + + bool StartLoadSecCompSa(); + void WaitForSecCompSa(); + void GetSecCompSa(); + void LoadSecCompSa(); + sptr GetProxy(); + void GetProxyFromRemoteObject(const sptr& remoteObject); + + std::mutex cvLock_; + bool readyFlag_ = false; + std::condition_variable secComCon_; + std::mutex proxyMutex_; + sptr proxy_ = nullptr; + sptr serviceDeathObserver_ = nullptr; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_CLIENT_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_death_recipient.h b/interfaces/inner_api/security_component/include/sec_comp_death_recipient.h new file mode 100644 index 0000000..4a18525 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_death_recipient.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_DEATH_RECIPIENT_H +#define SECURITY_COMPONENT_DEATH_RECIPIENT_H + +#include "iremote_object.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompDeathRecipient : public IRemoteObject::DeathRecipient { +public: + SecCompDeathRecipient() {} + virtual ~SecCompDeathRecipient() = default; + void OnRemoteDied(const wptr& object) override; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_DEATH_RECIPIENT_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_info.h b/interfaces/inner_api/security_component/include/sec_comp_info.h new file mode 100644 index 0000000..3f1540f --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_info.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_INFO_H +#define SECURITY_COMPONENT_INFO_H + +#include "sec_comp_rect.h" +#include "sec_comp_pattern.h" +#include "sec_comp_click_event.h" +namespace OHOS { +namespace Security { +namespace SecurityComponent { +static constexpr int32_t INVALID_SC_ID = -1; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_INFO_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_kit.h b/interfaces/inner_api/security_component/include/sec_comp_kit.h new file mode 100644 index 0000000..e281412 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_kit.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 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 INTERFACES_INNER_API_SECURITY_COMPONENT_KIT_H +#define INTERFACES_INNER_API_SECURITY_COMPONENT_KIT_H + +#include +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompKit { +public: + static int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId); + static int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); + static int32_t UnregisterSecurityComponent(int32_t scId); + static int32_t ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // INTERFACES_INNER_API_SECURITY_COMPONENT_KIT_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_load_callback.h b/interfaces/inner_api/security_component/include/sec_comp_load_callback.h new file mode 100644 index 0000000..5513896 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_load_callback.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_LOAD_CALLBACK_H +#define SECURITY_COMPONENT_LOAD_CALLBACK_H + +#include "system_ability_load_callback_stub.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompLoadCallback : public SystemAbilityLoadCallbackStub { +public: + explicit SecCompLoadCallback(); + + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject); + void OnLoadSystemAbilityFail(int32_t systemAbilityId); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_LOAD_CALLBACK_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_pattern.h b/interfaces/inner_api/security_component/include/sec_comp_pattern.h new file mode 100644 index 0000000..96b5675 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_pattern.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_PATTERN_H +#define SECURITY_COMPONENT_PATTERN_H + +static constexpr int32_t MIN_FONT_SIZE = 10; +enum SecCompType { + UNKNOWN_SC_TYPE = 0, + LOCATION_COMPONENT, + PASTE_COMPONENT, + SAVE_COMPONENT, + MAX_SC_TYPE +}; + +enum SecCompFontFamily { + UNKNOWN_FONT_FAMILY_TYPE = 0, + FONT_HO_SANS, // default font family + MAX_FONT_FAMILY_TYPE +}; + +enum SecCompLabel { + UNKNOWN_LABEL = 0, + DEFAULT_LABEL, + MAX_LABEL_TYPE +}; + +enum SecCompIcon { + UNKNOWN_ICON = 0, + DEFAULT_ICON, + MAX_ICON_TYPE +}; + +union SecCompColor { + struct { + uint8_t blue; + uint8_t green; + uint8_t red; + uint8_t alpha; + } argb; + uint32_t value; +}; + +enum SecCompButtonType { + UNKNOWN_BUTTON_TYPE = 0, + CAPSULE, + CIRCLE, + NORMAL, + MAX_BUTTON_TYPE +}; + +static inline bool IsComponentTypeValid(int32_t type) +{ + return type > UNKNOWN_SC_TYPE && type < MAX_SC_TYPE; +} + +static inline bool IsComponentFontFamilyValid(int32_t font) +{ + return font > UNKNOWN_FONT_FAMILY_TYPE && font < MAX_FONT_FAMILY_TYPE; +} + +static inline bool IsComponentLabelValid(int32_t label) +{ + return label > UNKNOWN_LABEL && label < MAX_LABEL_TYPE; +} + +static inline bool IsComponentIconValid(int32_t icon) +{ + return icon > UNKNOWN_ICON && icon < MAX_ICON_TYPE; +} + +static inline bool IsComponentButtonTypeValid(int32_t buttonType) +{ + return buttonType > UNKNOWN_BUTTON_TYPE && buttonType < MAX_BUTTON_TYPE; +} + +static inline bool IsComponentFontSizeValid(int32_t fontSize) +{ + return fontSize > MIN_FONT_SIZE; +} +#endif // SECURITY_COMPONENT_PATTERN_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_proxy.h b/interfaces/inner_api/security_component/include/sec_comp_proxy.h new file mode 100644 index 0000000..4b91ed7 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_proxy.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_PROXY_H +#define SECURITY_COMPONENT_PROXY_H + +#include +#include "i_sec_comp_service.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompProxy : public IRemoteProxy { +public: + explicit SecCompProxy(const sptr& impl); + ~SecCompProxy() override; + int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId) override; + int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) override; + int32_t UnregisterSecurityComponent(int32_t scId) override; + int32_t ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_PROXY_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_rect.h b/interfaces/inner_api/security_component/include/sec_comp_rect.h new file mode 100644 index 0000000..e865216 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_rect.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_RECT_H +#define SECURITY_COMPONENT_RECT_H +namespace OHOS { +namespace Security { +namespace SecurityComponent { +inline bool GreatOrEqual(double left, double right) +{ + constexpr double epsilon = -0.001f; + return (left - right) > epsilon; +} + +inline bool GreatNotEqual(double left, double right) +{ + constexpr double epsilon = 0.001f; + return (left - right) > epsilon; +} + +class SecCompRect { +public: + SecCompRect(double x, double y, double width, double height) : x_(x), y_(y), width_(width), height_(height) {}; + SecCompRect() = default; + ~SecCompRect() = default; + + bool IsValid() const; + bool IsRectOverlaped(const SecCompRect& other) const; + bool IsInRect(double x, double y) const; + + double x_ = 0.0; + double y_ = 0.0; + double width_ = 0.0; + double height_ = 0.0; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_INFO_H diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp new file mode 100644 index 0000000..5a805ae --- /dev/null +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -0,0 +1,220 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_client.h" +#include "iservice_registry.h" +#include "sec_comp_load_callback.h" +#include "sec_comp_log.h" +#include "sec_comp_proxy.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompClient"}; +} // namespace + +SecCompClient& SecCompClient::GetInstance() +{ + static SecCompClient instance; + return instance; +} + +SecCompClient::SecCompClient() +{} + +SecCompClient::~SecCompClient() +{ + if (proxy_ == nullptr) { + return; + } + auto remoteObj = proxy_->AsObject(); + if (remoteObj == nullptr) { + return; + } + if (serviceDeathObserver_ != nullptr) { + remoteObj->RemoveDeathRecipient(serviceDeathObserver_); + } +} + +int32_t SecCompClient::RegisterSecurityComponent(SecCompType type, + const std::string& componentInfo, int32_t& scId) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + SC_LOG_ERROR(LABEL, "Proxy is null"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + return proxy->RegisterSecurityComponent(type, componentInfo, scId); +} + +int32_t SecCompClient::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + SC_LOG_ERROR(LABEL, "Proxy is null"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + return proxy->UpdateSecurityComponent(scId, componentInfo); +} + +int32_t SecCompClient::UnregisterSecurityComponent(int32_t scId) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + SC_LOG_ERROR(LABEL, "Proxy is null"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + return proxy->UnregisterSecurityComponent(scId); +} + +int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + SC_LOG_ERROR(LABEL, "Proxy is null"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + return proxy->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo); +} + +bool SecCompClient::StartLoadSecCompSa() +{ + { + std::unique_lock lock(cvLock_); + readyFlag_ = false; + } + auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + SC_LOG_ERROR(LABEL, "GetSystemAbilityManager return null"); + return false; + } + + sptr ptrSecCompLoadCallback = + new (std::nothrow) SecCompLoadCallback(); + if (ptrSecCompLoadCallback == nullptr) { + SC_LOG_ERROR(LABEL, "New ptrSecCompLoadCallback fail."); + return false; + } + + int32_t result = sam->LoadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE, + ptrSecCompLoadCallback); + if (result != SC_OK) { + SC_LOG_ERROR(LABEL, "LoadSystemAbility %{public}d failed", SA_ID_SECURITY_COMPONENT_SERVICE); + return false; + } + SC_LOG_INFO(LABEL, "Notify samgr load sa %{public}d success", SA_ID_SECURITY_COMPONENT_SERVICE); + return true; +} + +void SecCompClient::WaitForSecCompSa() +{ + // wait_for release lock and block until time out(1s) or match the condition with notice + std::unique_lock lock(cvLock_); + auto waitStatus = secComCon_.wait_for( + lock, std::chrono::milliseconds(SA_ID_SECURITY_COMPONENT_SERVICE), [this]() { return readyFlag_; }); + if (!waitStatus) { + // time out or loadcallback fail + SC_LOG_ERROR(LABEL, "security component load sa timeout"); + return; + } +} + +void SecCompClient::FinishStartSASuccess(const sptr& remoteObject) +{ + SC_LOG_INFO(LABEL, "Get security component sa success."); + + GetProxyFromRemoteObject(remoteObject); + + // get lock which wait_for release and send a notice so that wait_for can out of block + std::unique_lock lock(cvLock_); + readyFlag_ = true; + secComCon_.notify_one(); +} + +void SecCompClient::FinishStartSAFail() +{ + SC_LOG_ERROR(LABEL, "get security component sa failed."); + + // get lock which wait_for release and send a notice + std::unique_lock lock(cvLock_); + readyFlag_ = true; + secComCon_.notify_one(); +} + +void SecCompClient::LoadSecCompSa() +{ + if (!StartLoadSecCompSa()) { + return; + } + WaitForSecCompSa(); +} + +void SecCompClient::OnRemoteDiedHandle() +{ + SC_LOG_ERROR(LABEL, "Remote service died"); + std::unique_lock lock(proxyMutex_); + proxy_ = nullptr; + serviceDeathObserver_ = nullptr; + { + std::unique_lock lock1(cvLock_); + readyFlag_ = false; + } +} + +void SecCompClient::GetProxyFromRemoteObject(const sptr& remoteObject) +{ + if (remoteObject == nullptr) { + return; + } + + sptr serviceDeathObserver = new (std::nothrow) SecCompDeathRecipient(); + if (serviceDeathObserver == nullptr) { + SC_LOG_ERROR(LABEL, "Alloc service death observer fail"); + return; + } + + if (!remoteObject->AddDeathRecipient(serviceDeathObserver)) { + SC_LOG_ERROR(LABEL, "Add service death observer fail"); + return; + } + + auto proxy = iface_cast(remoteObject); + if (proxy == nullptr) { + SC_LOG_ERROR(LABEL, "iface_cast get null"); + return; + } + proxy_ = proxy; + serviceDeathObserver_ = serviceDeathObserver; + SC_LOG_INFO(LABEL, "GetSystemAbility %{public}d success", SA_ID_SECURITY_COMPONENT_SERVICE); + return; +} + +sptr SecCompClient::GetProxy() +{ + std::unique_lock lock(proxyMutex_); + if (proxy_ != nullptr) { + return proxy_; + } + LoadSecCompSa(); + return proxy_; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp b/interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp new file mode 100644 index 0000000..d1c21ef --- /dev/null +++ b/interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_death_recipient.h" +#include "sec_comp_client.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +void SecCompDeathRecipient::OnRemoteDied(const wptr& object) +{ + SecCompClient::GetInstance().OnRemoteDiedHandle(); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp new file mode 100644 index 0000000..7728c15 --- /dev/null +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_kit.h" +#include "sec_comp_client.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompKit"}; +} // namespace + +int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, + const std::string& componentInfo, int32_t& scId) +{ + int32_t res = SecCompClient::GetInstance().RegisterSecurityComponent(type, componentInfo, scId); + if (res != SC_OK) { + SC_LOG_ERROR(LABEL, "register security component fail, error: %{public}d", res); + } + return res; +} + +int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) +{ + int32_t res = SecCompClient::GetInstance().UpdateSecurityComponent(scId, componentInfo); + if (res != SC_OK) { + SC_LOG_ERROR(LABEL, "update security component fail, error: %{public}d", res); + } + return res; +} + +int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) +{ + int32_t res = SecCompClient::GetInstance().UnregisterSecurityComponent(scId); + if (res != SC_OK) { + SC_LOG_ERROR(LABEL, "unregister security component fail, error: %{public}d", res); + } + return res; +} + +int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo) +{ + int32_t res = + SecCompClient::GetInstance().ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo); + if (res != SC_OK) { + SC_LOG_ERROR(LABEL, "report click event fail, error: %{public}d", res); + } + return res; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp b/interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp new file mode 100644 index 0000000..a302e92 --- /dev/null +++ b/interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_load_callback.h" +#include "i_sec_comp_service.h" +#include "sec_comp_client.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompClient"}; +} // namespace +SecCompLoadCallback::SecCompLoadCallback() {} + +void SecCompLoadCallback::OnLoadSystemAbilitySuccess( + int32_t systemAbilityId, const sptr& remoteObject) +{ + if (systemAbilityId != SA_ID_SECURITY_COMPONENT_SERVICE) { + SC_LOG_ERROR(LABEL, "start systemabilityId is not security_component!"); + return; + } + + if (remoteObject == nullptr) { + SC_LOG_ERROR(LABEL, "remoteObject is null."); + SecCompClient::GetInstance().FinishStartSAFail(); + return; + } + + SC_LOG_INFO(LABEL, "Start systemAbilityId: %{public}d success!", systemAbilityId); + + SecCompClient::GetInstance().FinishStartSASuccess(remoteObject); +} + +void SecCompLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + if (systemAbilityId != SA_ID_SECURITY_COMPONENT_SERVICE) { + SC_LOG_ERROR(LABEL, "start systemabilityId is not security_component!"); + return; + } + + SC_LOG_ERROR(LABEL, "Start systemAbilityId: %{public}d failed.", systemAbilityId); + + SecCompClient::GetInstance().FinishStartSAFail(); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp new file mode 100644 index 0000000..e7271b0 --- /dev/null +++ b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_proxy.h" +#include "parcel.h" +#include "sec_comp_click_event_parcel.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompProxy"}; +} + +SecCompProxy::SecCompProxy(const sptr& impl) : IRemoteProxy(impl) +{} + +SecCompProxy::~SecCompProxy() +{} + +int32_t SecCompProxy::RegisterSecurityComponent(SecCompType type, + const std::string& componentInfo, int32_t& scId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { + SC_LOG_ERROR(LABEL, "Write descriptor fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (!data.WriteUint32(type)) { + SC_LOG_ERROR(LABEL, "Write Uint32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + if (!data.WriteString(componentInfo)) { + SC_LOG_ERROR(LABEL, "Write string fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + SC_LOG_ERROR(LABEL, "Remote service is null"); + return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; + } + int32_t requestResult = remote->SendRequest( + static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); + return requestResult; + } + int32_t res; + if (!reply.ReadInt32(res)) { + SC_LOG_ERROR(LABEL, "Read result int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (!reply.ReadInt32(scId)) { + SC_LOG_ERROR(LABEL, "Read scId int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return res; +} + +int32_t SecCompProxy::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { + SC_LOG_ERROR(LABEL, "Write descriptor fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (!data.WriteInt32(scId)) { + SC_LOG_ERROR(LABEL, "Write Int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + if (!data.WriteString(componentInfo)) { + SC_LOG_ERROR(LABEL, "Write string fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + SC_LOG_ERROR(LABEL, "Remote service is null"); + return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; + } + int32_t requestResult = remote->SendRequest( + static_cast(ISecCompService::InterfaceCode::UPDATE_SECURITY_COMPONENT), data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); + return requestResult; + } + int32_t res; + if (!reply.ReadInt32(res)) { + SC_LOG_ERROR(LABEL, "Read result int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return res; +} + +int32_t SecCompProxy::UnregisterSecurityComponent(int32_t scId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { + SC_LOG_ERROR(LABEL, "Write descriptor fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (!data.WriteInt32(scId)) { + SC_LOG_ERROR(LABEL, "Write Int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + SC_LOG_ERROR(LABEL, "Remote service is null"); + return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; + } + int32_t requestResult = remote->SendRequest( + static_cast(ISecCompService::InterfaceCode::UNREGISTER_SECURITY_COMPONENT), data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); + return requestResult; + } + int32_t res; + if (!reply.ReadInt32(res)) { + SC_LOG_ERROR(LABEL, "Read int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return res; +} + +int32_t SecCompProxy::ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { + SC_LOG_ERROR(LABEL, "Write descriptor fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (!data.WriteInt32(scId)) { + SC_LOG_ERROR(LABEL, "Write Uint32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (!data.WriteString(componentInfo)) { + SC_LOG_ERROR(LABEL, "Write string fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + sptr parcel = new (std::nothrow) SecCompClickEventParcel(); + if (parcel == nullptr) { + SC_LOG_ERROR(LABEL, "New click event parcel failed"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + parcel->touchInfoParams_ = touchInfo; + if (!data.WriteParcelable(parcel)) { + SC_LOG_ERROR(LABEL, "Write touchInfo fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + SC_LOG_ERROR(LABEL, "Remote service is null"); + return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; + } + int32_t requestResult = remote->SendRequest( + static_cast(ISecCompService::InterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT), + data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); + return requestResult; + } + int32_t res; + if (!reply.ReadInt32(res)) { + SC_LOG_ERROR(LABEL, "Read int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return res; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn new file mode 100644 index 0000000..74747a5 --- /dev/null +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/test.gni") + +ohos_unittest("sec_comp_sdk_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] + + sources = [ + "unittest/src/sec_comp_base_test.cpp", + "unittest/src/sec_comp_kit_test.cpp", + "unittest/src/location_button_test.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + +group("unittest") { + testonly = true + deps = [ ":sec_comp_sdk_test" ] +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp new file mode 100644 index 0000000..69995b9 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -0,0 +1,261 @@ +/* + * Copyright (c) 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. + */ +#include "location_button_test.h" +#include +#include "sec_comp_log.h" +#include "sec_comp_err.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButtonTest"}; + +static const std::string JSON_FONT_FAMILY = "font"; +static const std::string JSON_LABEL_TYPE = "label"; +static const std::string JSON_ICON = "icon"; +static const std::string JSON_FONT_SIZE = "fontSize"; +static const std::string JSON_FONT_COLOR = "fontColor"; +static const std::string JSON_BG_COLOR = "bgColor"; +static const std::string JSON_BUTTON_TYPE = "buttonType"; +static const std::string JSON_RECT = "rect"; +static const std::string JSON_SC_TYPE = "type"; +static const std::string JSON_RECT_X = "x"; +static const std::string JSON_RECT_Y = "y"; +static const std::string JSON_RECT_WIDTH = "width"; +static const std::string JSON_RECT_HEIGHT = "height"; + +static constexpr uint32_t TEST_FONT_SIZE = 100; +static constexpr uint32_t TEST_INVALID_FONT_SIZE = 0; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr uint32_t TEST_COLOR = 0xffffffff; +static constexpr uint32_t TEST_DIFF_COLOR = 0; + +static void BuildLocationComponentInfo(LocationButton& button) +{ + button.fontSize_ = TEST_FONT_SIZE; + button.font_ = FONT_HO_SANS; + button.bgColor_.value = TEST_COLOR; + button.fontColor_.value = TEST_COLOR; + button.icon_ = DEFAULT_ICON; + button.label_ = DEFAULT_LABEL; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.type_ = LOCATION_COMPONENT; + button.buttonType_ = CAPSULE; +} +} + +void LocationButtonTest::SetUpTestCase() +{} + +void LocationButtonTest::TearDownTestCase() +{} + +void LocationButtonTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); +} + +void LocationButtonTest::TearDown() +{} + +/** + * @tc.name: FromJson001 + * @tc.desc: Test location button from json success + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) +{ + nlohmann::json jsonComponent; + + jsonComponent[JSON_FONT_FAMILY] = FONT_HO_SANS; + jsonComponent[JSON_LABEL_TYPE] = DEFAULT_LABEL; + jsonComponent[JSON_ICON] = DEFAULT_ICON; + jsonComponent[JSON_FONT_SIZE] = TEST_FONT_SIZE; + jsonComponent[JSON_FONT_COLOR] = TEST_COLOR; + jsonComponent[JSON_BG_COLOR] = TEST_COLOR; + jsonComponent[JSON_BUTTON_TYPE] = CAPSULE; + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + + LocationButton button; + button.FromJson(jsonComponent); + ASSERT_TRUE(button.IsValid()); +} + +/** + * @tc.name: FromJson002 + * @tc.desc: Test empty location button from json + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton button; + button.FromJson(jsonComponent); + ASSERT_FALSE(button.IsValid()); +} + +/** + * @tc.name: FromJson003 + * @tc.desc: Test location button from wrong type params json + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) +{ + nlohmann::json jsonComponent; + jsonComponent[JSON_FONT_FAMILY] = "wrongType"; + jsonComponent[JSON_LABEL_TYPE] = "wrongType"; + jsonComponent[JSON_ICON] = "wrongType"; + jsonComponent[JSON_FONT_SIZE] = "wrongType"; + jsonComponent[JSON_FONT_COLOR] = "wrongType"; + jsonComponent[JSON_BG_COLOR] = "wrongType"; + jsonComponent[JSON_BUTTON_TYPE] = "wrongType"; + jsonComponent[JSON_BUTTON_TYPE] = "wrongType"; + jsonComponent[JSON_SC_TYPE] = "wrongType"; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + + LocationButton button; + button.FromJson(jsonComponent); + ASSERT_FALSE(button.IsValid()); + ASSERT_EQ(button.rect_.x_, 0); + ASSERT_EQ(button.rect_.y_, 0); + ASSERT_EQ(button.rect_.width_, 0); + ASSERT_EQ(button.rect_.height_, 0); +} + +/** + * @tc.name: FromJson004 + * @tc.desc: Test location button from wrong value params json + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) +{ + nlohmann::json jsonComponent; + jsonComponent[JSON_FONT_FAMILY] = 0; + jsonComponent[JSON_LABEL_TYPE] = 0; + jsonComponent[JSON_ICON] = 0; + jsonComponent[JSON_FONT_SIZE] = 0; + jsonComponent[JSON_FONT_COLOR] = 0; + jsonComponent[JSON_BG_COLOR] = 0; + jsonComponent[JSON_BUTTON_TYPE] = 0; + jsonComponent[JSON_BUTTON_TYPE] = 0; + jsonComponent[JSON_SC_TYPE] = 0; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + + LocationButton button; + button.FromJson(jsonComponent); + ASSERT_FALSE(button.IsValid()); +} + +/** + * @tc.name: IsValid001 + * @tc.desc: Test check location button invalid + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(LocationButtonTest, IsValid001, TestSize.Level1) +{ + LocationButton button; + BuildLocationComponentInfo(button); + + button.font_ = UNKNOWN_FONT_FAMILY_TYPE; + ASSERT_FALSE(button.IsValid()); + button.font_ = FONT_HO_SANS; + + button.label_ = UNKNOWN_LABEL; + ASSERT_FALSE(button.IsValid()); + button.label_ = DEFAULT_LABEL; + + button.icon_ = UNKNOWN_ICON; + ASSERT_FALSE(button.IsValid()); + button.icon_ = DEFAULT_ICON; + + button.fontSize_ = TEST_INVALID_FONT_SIZE; + ASSERT_FALSE(button.IsValid()); + button.fontSize_ = TEST_FONT_SIZE; + + button.buttonType_ = UNKNOWN_BUTTON_TYPE; + ASSERT_FALSE(button.IsValid()); + button.buttonType_ = CAPSULE; + + button.type_ = UNKNOWN_SC_TYPE; + ASSERT_FALSE(button.IsValid()); +} + +/** + * @tc.name: CompareLocationButton001 + * @tc.desc: Test compare location button + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) +{ + LocationButton button1; + BuildLocationComponentInfo(button1); + + LocationButton button2; + BuildLocationComponentInfo(button2); + + button1.font_ = UNKNOWN_FONT_FAMILY_TYPE; + ASSERT_FALSE(button1 == button2); + button1.font_ = FONT_HO_SANS; + + button1.label_ = UNKNOWN_LABEL; + ASSERT_FALSE(button1 == button2); + button1.label_ = DEFAULT_LABEL; + + button1.icon_ = UNKNOWN_ICON; + ASSERT_FALSE(button1 == button2); + button1.icon_ = DEFAULT_ICON; + + button1.fontSize_ = TEST_INVALID_FONT_SIZE; + ASSERT_FALSE(button1 == button2); + button1.fontSize_ = TEST_FONT_SIZE; + + button1.fontColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(button1 == button2); + button1.fontColor_.value = TEST_COLOR; + + button1.bgColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(button1 == button2); + button1.bgColor_.value = TEST_COLOR; + + ASSERT_TRUE(button1 == button2); +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.h b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.h new file mode 100644 index 0000000..7324bd7 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 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 SEC_COMP_LOCATION_BUTTON_TEST_H +#define SEC_COMP_LOCATION_BUTTON_TEST_H + +#include +#include "location_button.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class LocationButtonTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_LOCATION_BUTTON_TEST_H diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp new file mode 100644 index 0000000..64dc1f1 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp @@ -0,0 +1,215 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_base_test.h" +#include +#include "sec_comp_log.h" +#include "sec_comp_err.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompBaseTest"}; + +static const std::string JSON_RECT = "rect"; +static const std::string JSON_SC_TYPE = "type"; +static const std::string JSON_RECT_X = "x"; +static const std::string JSON_RECT_Y = "y"; +static const std::string JSON_RECT_WIDTH = "width"; +static const std::string JSON_RECT_HEIGHT = "height"; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr double WRONG_COORDINATE = -100.0; +static constexpr double TEST_DIFF_COORDINATE = 0.0; +} + +void SecCompBaseTest::SetUpTestCase() +{} + +void SecCompBaseTest::TearDownTestCase() +{} + +void SecCompBaseTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); +} + +void SecCompBaseTest::TearDown() +{} + +/** + * @tc.name: FromJson001 + * @tc.desc: Test SecCompBase from json success + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompBaseTest, FromJson001, TestSize.Level1) +{ + nlohmann::json jsonComponent; + + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + + SecCompBase comp; + comp.FromJson(jsonComponent); + ASSERT_TRUE(comp.IsValid()); +} + +/** + * @tc.name: FromJson002 + * @tc.desc: Test empty SecCompBase from json + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompBaseTest, FromJson002, TestSize.Level1) +{ + nlohmann::json jsonComponent; + SecCompBase comp; + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); +} + +/** + * @tc.name: FromJson003 + * @tc.desc: Test location button from wrong type json + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompBaseTest, FromJson003, TestSize.Level1) +{ + nlohmann::json jsonComponent; + SecCompBase comp; + jsonComponent[JSON_SC_TYPE] = "wrongType"; + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); + + jsonComponent[JSON_SC_TYPE] = 0; + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); +} + +/** + * @tc.name: FromJson004 + * @tc.desc: Test location button from wrong rect json + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompBaseTest, FromJson004, TestSize.Level1) +{ + nlohmann::json jsonComponent; + SecCompBase comp; + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); + jsonComponent[JSON_RECT] = wrongJson; + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, "wrongType"}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, "wrongType"}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, "wrongType"}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, "wrongType"} + }; + comp.FromJson(jsonComponent); + ASSERT_FALSE(comp.IsValid()); +} + +/** + * @tc.name: IsValid001 + * @tc.desc: Test SecCompBase invalid + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompBaseTest, IsValid001, TestSize.Level1) +{ + SecCompBase comp; + comp.type_ = UNKNOWN_SC_TYPE; + ASSERT_FALSE(comp.IsValid()); + + comp.type_ = LOCATION_COMPONENT; + comp.rect_.x_ = WRONG_COORDINATE; + ASSERT_FALSE(comp.IsValid()); +} + +/** + * @tc.name: Compare001 + * @tc.desc: Test SecCompBase compare + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompBaseTest, Compare001, TestSize.Level1) +{ + SecCompBase comp1; + comp1.type_ = LOCATION_COMPONENT; + comp1.rect_.x_ = TEST_COORDINATE; + comp1.rect_.y_ = TEST_COORDINATE; + comp1.rect_.width_ = TEST_COORDINATE; + comp1.rect_.height_ = TEST_COORDINATE; + SecCompBase comp2 = comp1; + + comp1.type_ = SAVE_COMPONENT; + ASSERT_FALSE(comp1 == comp2); + comp1.type_ = LOCATION_COMPONENT; + + comp1.rect_.x_ = TEST_DIFF_COORDINATE; + ASSERT_FALSE(comp1 == comp2); + comp1.rect_.x_ = TEST_COORDINATE; + + comp1.rect_.y_ = TEST_DIFF_COORDINATE; + ASSERT_FALSE(comp1 == comp2); + comp1.rect_.y_ = TEST_COORDINATE; + + comp1.rect_.width_ = TEST_DIFF_COORDINATE; + ASSERT_FALSE(comp1 == comp2); + comp1.rect_.width_ = TEST_COORDINATE; + + comp1.rect_.height_ = TEST_DIFF_COORDINATE; + ASSERT_FALSE(comp1 == comp2); + comp1.rect_.height_ = TEST_COORDINATE; +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.h b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.h new file mode 100644 index 0000000..78a9c6a --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 SEC_COMP_BASE_TEST_H +#define SEC_COMP_BASE_TEST_H + +#include +#include "sec_comp_base.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompBaseTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_BASE_TEST_H diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp new file mode 100644 index 0000000..1821e8b --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_kit_test.h" +#include "location_button.h" +#include "sec_comp_err.h" +#include "sec_comp_info.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" + +using namespace testing::ext; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompKitTest"}; +static constexpr uint32_t TEST_FONT_SIZE = 100; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr uint32_t TEST_COLOR = 0xffffffff; +} // namespace + +void SecCompKitTest::SetUpTestCase() +{ + SC_LOG_INFO(LABEL, "SetUpTestCase."); +} + +void SecCompKitTest::TearDownTestCase() +{ + SC_LOG_INFO(LABEL, "TearDownTestCase."); +} + +void SecCompKitTest::SetUp() +{ + SC_LOG_INFO(LABEL, "SetUp ok."); +} + +void SecCompKitTest::TearDown() +{ + SC_LOG_INFO(LABEL, "TearDown."); +} + +/** + * @tc.name: ExceptCall001 + * @tc.desc: do kit except call. + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) +{ + LocationButton button; + button.fontSize_ = TEST_FONT_SIZE; + button.font_ = FONT_HO_SANS; + button.bgColor_.value = TEST_COLOR; + button.fontColor_.value = TEST_COLOR; + button.icon_ = DEFAULT_ICON; + button.label_ = DEFAULT_LABEL; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.type_ = LOCATION_COMPONENT; + button.buttonType_ = CAPSULE; + + nlohmann::json jsonRes; + button.ToJson(jsonRes); + int32_t scId = -1; + ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonRes.dump(), scId)); + ASSERT_EQ(-1, scId); + ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonRes.dump())); + + struct SecCompClickEvent touch = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = GetCurrentTime() + }; + EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonRes.dump(), touch)); + EXPECT_NE(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.h b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.h new file mode 100644 index 0000000..3bd1241 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_KIT_TEST +#define SECURITY_COMPONENT_KIT_TEST + +#include +#include "sec_comp_kit.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompKitTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_KIT_TEST \ No newline at end of file diff --git a/ohos.build b/ohos.build new file mode 100644 index 0000000..0429522 --- /dev/null +++ b/ohos.build @@ -0,0 +1,17 @@ +{ + "subsystem": "security", + "parts": { + "security_component": { + "variants": [ + "phone", + "wearable" + ], + "module_list": [ + "//base/security/security_component:security_component_build_module" + ], + "test_list": [ + "//base/security/security_component:security_component_build_module_test" + ] + } + } +} diff --git a/security_component.gni b/security_component.gni new file mode 100644 index 0000000..8e38353 --- /dev/null +++ b/security_component.gni @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +sec_comp_root_dir = "//base/security/security_component" diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn new file mode 100644 index 0000000..7453c49 --- /dev/null +++ b/services/security_component_service/sa/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/ohos.gni") + +ohos_prebuilt_etc("security_component_service.rc") { + source = "security_component_service.cfg" + relative_install_dir = "init" + subsystem_name = "security" + part_name = "security_component" +} + +ohos_shared_library("security_component_service") { + subsystem_name = "security" + part_name = "security_component" + + include_dirs = [ + "sa_main", + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] + + sources = [ + "sa_main/app_state_observer.cpp", + "sa_main/sec_comp_entity.cpp", + "sa_main/sec_comp_info_helper.cpp", + "sa_main/sec_comp_manager.cpp", + "sa_main/sec_comp_service.cpp", + "sa_main/sec_comp_stub.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + cflags = [ "-DHILOG_ENABLE" ] + + deps = [ + ":security_component_service.rc", + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + ] + + configs = [ "//base/security/security_component/config:coverage_flags" ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} diff --git a/services/security_component_service/sa/sa_main/app_state_observer.cpp b/services/security_component_service/sa/sa_main/app_state_observer.cpp new file mode 100644 index 0000000..000c0d7 --- /dev/null +++ b/services/security_component_service/sa/sa_main/app_state_observer.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 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. + */ + +#include "app_state_observer.h" +#include "sec_comp_log.h" +#include "sec_comp_manager.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "AppStateObserver"}; +} + +AppStateObserver::AppStateObserver() +{ +} + +AppStateObserver::~AppStateObserver() +{ +} + +bool AppStateObserver::IsProcessForeground(int32_t uid) +{ + Utils::UniqueReadGuard infoGuard(this->fgProcLock_); + for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { + if (uid == iter->uid) { + return true; + } + } + return false; +} + +void AppStateObserver::AddProcessToForegroundSet(int32_t uid, const SecCompProcessData& data) +{ + Utils::UniqueWriteGuard infoGuard(this->fgProcLock_); + for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { + if (uid == iter->uid) { + SC_LOG_INFO(LABEL, "uid %{public}d is already in foreground", uid); + return; + } + } + foregrandProcList_.emplace_back(data); +} + +void AppStateObserver::AddProcessToForegroundSet(const AppExecFwk::AppStateData& stateData) +{ + SecCompProcessData proc = { + .bundleName = stateData.bundleName, + .pid = stateData.pid, + .uid = stateData.uid + }; + AddProcessToForegroundSet(stateData.uid, proc); +} + +void AppStateObserver::AddProcessToForegroundSet(const AppExecFwk::ProcessData &processData) +{ + SecCompProcessData proc = { + .bundleName = processData.bundleName, + .pid = processData.pid, + .uid = processData.uid + }; + AddProcessToForegroundSet(processData.uid, proc); +} + +void AppStateObserver::RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData &processData) +{ + Utils::UniqueWriteGuard infoGuard(this->fgProcLock_); + for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { + if (processData.uid == iter->uid) { + foregrandProcList_.erase(iter); + return; + } + } +} + +void AppStateObserver::OnProcessStateChanged(const AppExecFwk::ProcessData &processData) +{ + if (processData.state == AppExecFwk::AppProcessState::APP_STATE_FOREGROUND) { + AddProcessToForegroundSet(processData); + } else if (processData.state == AppExecFwk::AppProcessState::APP_STATE_BACKGROUND) { + RemoveProcessFromForegroundSet(processData); + SecCompManager::GetInstance().NotifyProcessBackground(processData.uid); + } +} + +void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData& processData) +{ + SC_LOG_INFO(LABEL, "OnProcessDied die %{public}s uid %{public}d", + processData.bundleName.c_str(), processData.uid); + RemoveProcessFromForegroundSet(processData); + SecCompManager::GetInstance().NotifyProcessDied(processData.uid); +} + +void AppStateObserver::DumpProcess(std::string& dumpStr) +{ + Utils::UniqueWriteGuard infoGuard(this->fgProcLock_); + for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { + dumpStr.append("uid:" + std::to_string(iter->uid) + ", pid:" + std::to_string(iter->pid)); + dumpStr.append(", procName:" + iter->bundleName + "\n"); + } +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + diff --git a/services/security_component_service/sa/sa_main/app_state_observer.h b/services/security_component_service/sa/sa_main/app_state_observer.h new file mode 100644 index 0000000..2ab134d --- /dev/null +++ b/services/security_component_service/sa/sa_main/app_state_observer.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H +#define SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H + +#include +#include "app_mgr_interface.h" +#include "application_state_observer_stub.h" +#include "iremote_object.h" +#include "rwlock.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +struct SecCompProcessData { + std::string bundleName; + int32_t pid = -1; + int32_t uid = -1; +}; + +class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub { +public: + explicit AppStateObserver(); + virtual ~AppStateObserver(); + + void OnProcessStateChanged(const AppExecFwk::ProcessData &processData) override; + void OnProcessDied(const AppExecFwk::ProcessData& processData) override; + bool IsProcessForeground(int32_t uid); + void AddProcessToForegroundSet(int32_t uid, const SecCompProcessData& data); + void AddProcessToForegroundSet(const AppExecFwk::ProcessData &processData); + void AddProcessToForegroundSet(const AppExecFwk::AppStateData& stateData); + void DumpProcess(std::string& dumpStr); + +private: + void RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData &processData); + std::vector foregrandProcList_; + OHOS::Utils::RWLock fgProcLock_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + +#endif // SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H + diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp new file mode 100644 index 0000000..70c7294 --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_entity.h" +#include "sec_comp_err.h" +#include "sec_comp_info_helper.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntity"}; +static constexpr uint64_t MAX_TOUCH_INTERVAL = 1000L; // 1000ms +} + +int32_t SecCompEntity::RevokeTempPermission() +{ + if (!isGrant_) { + SC_LOG_ERROR(LABEL, "security component is not granted"); + return SC_OK; + } + isGrant_ = false; + return SecCompInfoHelper::RevokeTempPermission(tokenId_, componentInfo_); +} + +int32_t SecCompEntity::GrantTempPermission() +{ + if (isGrant_) { + SC_LOG_ERROR(LABEL, "security component is already granted"); + return SC_OK; + } + isGrant_ = true; + return SecCompInfoHelper::GrantTempPermission(tokenId_, componentInfo_); +} + +bool SecCompEntity::CompareComponentInfo(const SecCompBase* other) const +{ + return SecCompInfoHelper::CompareSecCompInfo(componentInfo_.get(), other); +} + +bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const +{ + uint64_t current = GetCurrentTime(); + if (touchInfo.timestamp < current - MAX_TOUCH_INTERVAL || touchInfo.timestamp > current) { + SC_LOG_ERROR(LABEL, "touch timestamp invalid"); + return false; + } + + if (!componentInfo_->rect_.IsInRect(touchInfo.touchX, touchInfo.touchY)) { + SC_LOG_ERROR(LABEL, "touch point is not in component rect"); + return false; + } + return true; +} + +bool SecCompEntity::IsRectOverlaped(const SecCompEntity& other) const +{ + std::shared_ptr otherInfo = other.GetComponentInfo(); + if (otherInfo == nullptr) { + SC_LOG_ERROR(LABEL, "component info is null"); + return true; + } + + return componentInfo_->rect_.IsRectOverlaped(otherInfo->rect_); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.h b/services/security_component_service/sa/sa_main/sec_comp_entity.h new file mode 100644 index 0000000..d1e6a25 --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_ENTITY_H +#define SECURITY_COMPONENT_ENTITY_H + +#include +#include "access_token.h" +#include "sec_comp_base.h" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompEntity { +public: + SecCompEntity(std::shared_ptr component, + AccessToken::AccessTokenID token, int32_t scId) : componentInfo_(component), tokenId_(token), scId_(scId) {}; + ~SecCompEntity() = default; + int32_t RevokeTempPermission(); + int32_t GrantTempPermission(); + int32_t GetScId() const + { + return scId_; + }; + + SecCompType GetType() const + { + if (componentInfo_ == nullptr) { + return UNKNOWN_SC_TYPE; + } + return componentInfo_->type_; + }; + + bool IsGrant() const + { + return isGrant_; + } + + std::shared_ptr GetComponentInfo() const + { + return componentInfo_; + }; + + void SetComponentInfo(const std::shared_ptr& com) + { + componentInfo_ = com; + }; + + bool CompareComponentInfo(const SecCompBase* other) const; + bool IsRectOverlaped(const SecCompEntity& other) const; + bool CheckTouchInfo(const SecCompClickEvent& touchInfo) const; + +private: + std::shared_ptr componentInfo_; + bool isGrant_; + AccessToken::AccessTokenID tokenId_; + int32_t scId_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_ENTITY_H diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp new file mode 100644 index 0000000..855a0d7 --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_info_helper.h" + +#include "location_button.h" +#include "paste_button.h" +#include "save_button.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelper"}; +} + +SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const nlohmann::json& jsonComponent) +{ + SecCompBase* comp = nullptr; + switch (type) { + case LOCATION_COMPONENT: + comp = ConstructComponent(jsonComponent); + break; + case PASTE_COMPONENT: + comp = ConstructComponent(jsonComponent); + break; + case SAVE_COMPONENT: + comp = ConstructComponent(jsonComponent); + break; + default: + SC_LOG_ERROR(LABEL, "Parse component type unknown"); + break; + } + if (comp != nullptr && !comp->IsValid()) { + delete comp; + return nullptr; + } + return comp; +} + +bool SecCompInfoHelper::CompareSecCompInfo(const SecCompBase* comA, const SecCompBase* comB) +{ + if (comA == nullptr || comB == nullptr) { + SC_LOG_ERROR(LABEL, "Security component info is null"); + return false; + } + + if (comA->type_ != comB->type_) { + SC_LOG_ERROR(LABEL, "Security component type is not equal"); + return false; + } + + bool res = false; + switch (comA->type_) { + case LOCATION_COMPONENT: + res = *reinterpret_cast(comA) == *reinterpret_cast(comB); + break; + case PASTE_COMPONENT: + res = *reinterpret_cast(comA) == *reinterpret_cast(comB); + break; + case SAVE_COMPONENT: + res = *reinterpret_cast(comA) == *reinterpret_cast(comB); + break; + default: + SC_LOG_ERROR(LABEL, "Compare component component type unknown"); + break; + } + return res; +} + +int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID tokenId, + const std::shared_ptr& componentInfo) +{ + if (componentInfo == nullptr) { + SC_LOG_ERROR(LABEL, "revoke component is null"); + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + SecCompType type = componentInfo->type_; + switch (type) { + case LOCATION_COMPONENT: + return AccessToken::AccessTokenKit::RevokePermission(tokenId, "ohos.permission.LOCATION", + AccessToken::PermissionFlag::PERMISSION_DEFAULT_FLAG); + default: + SC_LOG_ERROR(LABEL, "revoke component type unknown"); + break; + } + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; +} + +int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenId, + const std::shared_ptr& componentInfo) +{ + if (componentInfo == nullptr) { + SC_LOG_ERROR(LABEL, "revoke component is null"); + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + SecCompType type = componentInfo->type_; + switch (type) { + case LOCATION_COMPONENT: + return AccessToken::AccessTokenKit::GrantPermission(tokenId, "ohos.permission.LOCATION", + AccessToken::PermissionFlag::PERMISSION_DEFAULT_FLAG); + default: + SC_LOG_ERROR(LABEL, "Parse component type unknown"); + break; + } + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h new file mode 100644 index 0000000..05d8cae --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 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 I_SECURITY_COMPONENT_INFO_HELPER_H +#define I_SECURITY_COMPONENT_INFO_HELPER_H + +#include "accesstoken_kit.h" +#include "nlohmann/json.hpp" +#include "sec_comp_base.h" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +template +T* ConstructComponent(const nlohmann::json& jsonComponent) +{ + T *componentPtr = new (std::nothrow)T(); + if (componentPtr != nullptr) { + componentPtr->FromJson(jsonComponent); + } + return componentPtr; +} + +class SecCompInfoHelper { +public: + static SecCompBase* ParseComponent(SecCompType type, const nlohmann::json& jsonComponent); + static bool CompareSecCompInfo(const SecCompBase* comA, const SecCompBase* comB); + static int32_t RevokeTempPermission(AccessToken::AccessTokenID tokenId, + const std::shared_ptr& componentInfo); + static int32_t GrantTempPermission(AccessToken::AccessTokenID tokenId, + const std::shared_ptr& componentInfo); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // I_SECURITY_COMPONENT_INFO_HELPER_H diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp new file mode 100644 index 0000000..b8e963c --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -0,0 +1,264 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_manager.h" + +#include "sec_comp_err.h" +#include "sec_comp_info_helper.h" +#include "sec_comp_log.h" +#include "iservice_registry.h" +#include "i_sec_comp_service.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompManager"}; +static constexpr int32_t SC_ID_START = 1000; +static constexpr int32_t MAX_INT_NUM = 0x7fffffff; +} + +SecCompManager::SecCompManager() +{ + scIdStart_ = SC_ID_START; + SC_LOG_INFO(LABEL, "SecCompManager()"); +} + +SecCompManager& SecCompManager::GetInstance() +{ + static SecCompManager instance; + return instance; +} + +int32_t SecCompManager::CreateScId() +{ + std::lock_guard lock(scIdMtx_); + if (scIdStart_ == MAX_INT_NUM) { + scIdStart_ = SC_ID_START; + } else { + scIdStart_++; + } + return scIdStart_; +} + +int32_t SecCompManager::AddProcessComponent(std::vector& componentList, + const SecCompEntity& newEntity) +{ + for (auto it = componentList.begin(); it != componentList.end(); ++it) { + if (it->IsRectOverlaped(newEntity)) { + SC_LOG_ERROR(LABEL, "Components overlap"); + return SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP; + } + } + + componentList.emplace_back(newEntity); + return SC_OK; +} + +int32_t SecCompManager::AddSecurityComponentToList(int32_t uid, const SecCompEntity& newEntity) +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + auto iter = componentMap_.find(uid); + if (iter != componentMap_.end()) { + return AddProcessComponent(iter->second, newEntity); + } + + std::vector newComponentList; + AddProcessComponent(newComponentList, newEntity); + componentMap_[uid] = newComponentList; + return SC_OK; +} + +int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t uid, int32_t scId) +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + auto iter = componentMap_.find(uid); + if (iter == componentMap_.end()) { + SC_LOG_ERROR(LABEL, "Can not find registered process"); + return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; + } + std::vector& list = iter->second; + for (auto it = list.begin(); it != list.end(); ++it) { + if (it->GetScId() == scId) { + it->RevokeTempPermission(); + list.erase(it); + return SC_OK; + } + } + SC_LOG_ERROR(LABEL, "Can not find component"); + return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; +} + +SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t uid, int32_t scId) +{ + auto iter = componentMap_.find(uid); + if (iter == componentMap_.end()) { + return nullptr; + } + std::vector& list = iter->second; + for (auto it = list.begin(); it != list.end(); ++it) { + if (it->GetScId() == scId) { + return std::addressof(*it); + } + } + return nullptr; +} + +void SecCompManager::NotifyProcessBackground(int32_t uid) +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + auto iter = componentMap_.find(uid); + if (iter == componentMap_.end()) { + return; + } + + SC_LOG_INFO(LABEL, "uid %{public}d to background", uid); + std::vector& list = iter->second; + for (auto it = list.begin(); it != list.end(); ++it) { + it->RevokeTempPermission(); + } + list.clear(); +} + +void SecCompManager::NotifyProcessDied(int32_t uid) +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + auto iter = componentMap_.find(uid); + if (iter == componentMap_.end()) { + return; + } + SC_LOG_INFO(LABEL, "uid %{public}d died", uid); + std::vector& list = iter->second; + for (auto it = list.begin(); it != list.end(); ++it) { + it->RevokeTempPermission(); + } + list.clear(); + componentMap_.erase(uid); + ExitSaAfterAllProcessDie(); +} + +void SecCompManager::ExitSaAfterAllProcessDie() +{ + if (componentMap_.empty()) { + SC_LOG_INFO(LABEL, "All processes using security component died, start sa exit"); + auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + SC_LOG_ERROR(LABEL, "Failed to get SystemAbilityManager."); + return; + } + int32_t ret = systemAbilityMgr->UnloadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE); + if (ret != SC_OK) { + SC_LOG_ERROR(LABEL, "Failed to UnloadSystemAbility service! errcode=%{public}d", ret); + return; + } + SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); + } +} + +int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, + const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) +{ + SecCompBase* componentPtr = SecCompInfoHelper::ParseComponent(type, jsonComponent); + std::shared_ptr component(componentPtr); + if (component == nullptr) { + SC_LOG_ERROR(LABEL, "Parse component info invalid"); + return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + } + + int32_t registerId = CreateScId(); + SecCompEntity entity(component, caller.tokenId, registerId); + int32_t ret = AddSecurityComponentToList(caller.uid, entity); + if (ret == SC_OK) { + scId = registerId; + } else { + SC_LOG_ERROR(LABEL, "Register security component failed"); + scId = INVALID_SC_ID; + } + return ret; +} + +int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, + const SecCompCallerInfo& caller) +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + SecCompEntity* sc = GetSecurityComponentFromList(caller.uid, scId); + if (sc == nullptr) { + SC_LOG_ERROR(LABEL, "Can not find target component"); + return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; + } + SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); + std::shared_ptr reportComponentInfo(report); + if (reportComponentInfo == nullptr) { + SC_LOG_ERROR(LABEL, "Update component info invalid"); + return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + } + sc->SetComponentInfo(reportComponentInfo); + return SC_OK; +} + +int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller) +{ + if (scId < 0) { + SC_LOG_ERROR(LABEL, "ScId is invalid"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + return DeleteSecurityComponentFromList(caller.uid, scId); +} + +int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, + const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo) +{ + OHOS::Utils::UniqueReadGuard lk(this->componentInfoLock_); + SecCompEntity* sc = GetSecurityComponentFromList(caller.uid, scId); + if (sc == nullptr) { + SC_LOG_ERROR(LABEL, "Can not find target component"); + return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; + } + + SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); + std::unique_ptr reportComponentInfo(report); + if (reportComponentInfo == nullptr) { + SC_LOG_ERROR(LABEL, "report component info invalid"); + return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + } + + if (!sc->CompareComponentInfo(reportComponentInfo.get())) { + SC_LOG_ERROR(LABEL, "Report component compare register component failed"); + return SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL; + } + + if (!sc->CheckTouchInfo(touchInfo)) { + SC_LOG_ERROR(LABEL, "touchInfo is invalid"); + return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; + } + + return sc->GrantTempPermission(); +} + +void SecCompManager::DumpSecComp(std::string& dumpStr) const +{ + for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { + dumpStr.append("uid:" + std::to_string(iter->first) + "\n"); + for (auto compIter = iter->second.begin(); compIter != iter->second.end(); compIter ++) { + nlohmann::json json; + compIter->GetComponentInfo()->ToJson(json); + dumpStr.append(" scId:" + std::to_string(compIter->GetScId()) + + ", isGrant:" + std::to_string(compIter->IsGrant()) + ", " + json.dump() + "\n"); + } + } +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h new file mode 100644 index 0000000..143b38c --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_MANAGER_H +#define SECURITY_COMPONENT_MANAGER_H + +#include +#include +#include +#include + +#include "access_token.h" +#include "accesstoken_kit.h" +#include "app_state_observer.h" +#include "nocopyable.h" +#include "rwlock.h" +#include "sec_comp_base.h" +#include "sec_comp_entity.h" +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +struct SecCompCallerInfo { + AccessToken::AccessTokenID tokenId; + int32_t uid; + int32_t pid; +}; + +class SecCompManager { +public: + static SecCompManager& GetInstance(); + virtual ~SecCompManager() = default; + + int32_t RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent, + const SecCompCallerInfo& caller, int32_t& scId); + int32_t UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, + const SecCompCallerInfo& caller); + int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); + int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent, + const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo); + void NotifyProcessBackground(int32_t uid); + void NotifyProcessDied(int32_t uid); + void DumpSecComp(std::string& dumpStr) const; + +private: + SecCompManager(); + int32_t AddProcessComponent(std::vector& componentList, + const SecCompEntity& newEntity); + int32_t AddSecurityComponentToList(int32_t uid, const SecCompEntity& newEntity); + int32_t DeleteSecurityComponentFromList(int32_t uid, int32_t scId); + SecCompEntity* GetSecurityComponentFromList(int32_t uid, int32_t scId); + void ExitSaAfterAllProcessDie(); + + int32_t CreateScId(); + + OHOS::Utils::RWLock componentInfoLock_; + std::mutex scIdMtx_; + std::unordered_map> componentMap_; + int32_t scIdStart_; + + bool Initialize() const; + DISALLOW_COPY_AND_MOVE(SecCompManager); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_MANAGER_H + diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp new file mode 100644 index 0000000..94d084b --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -0,0 +1,228 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_service.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "sec_comp_err.h" +#include "sec_comp_manager.h" +#include "sec_comp_log.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompService"}; +} + +REGISTER_SYSTEM_ABILITY_BY_ID(SecCompService, SA_ID_SECURITY_COMPONENT_SERVICE, true); + +SecCompService::SecCompService(int32_t saId, bool runOnCreate) + : SystemAbility(saId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START) +{ + SC_LOG_INFO(LABEL, "SecCompService()"); +} + +SecCompService::~SecCompService() +{ + SC_LOG_INFO(LABEL, "~SecCompService()"); +} + +void SecCompService::OnStart() +{ + if (state_ == ServiceRunningState::STATE_RUNNING) { + SC_LOG_INFO(LABEL, "SecCompService has already started!"); + return; + } + SC_LOG_INFO(LABEL, "SecCompService is starting"); + if (!RegisterAppStateObserver()) { + SC_LOG_ERROR(LABEL, "Failed to register app state observer!"); + return; + } + state_ = ServiceRunningState::STATE_RUNNING; + bool ret = Publish(this); + if (!ret) { + SC_LOG_ERROR(LABEL, "Failed to publish service!"); + return; + } + SC_LOG_INFO(LABEL, "Congratulations, SecCompService start successfully!"); +} + +void SecCompService::OnStop() +{ + SC_LOG_INFO(LABEL, "Stop service"); + state_ = ServiceRunningState::STATE_NOT_START; + UnregisterAppStateObserver(); + iAppMgr_ = nullptr; + appStateObserver_ = nullptr; +} + +bool SecCompService::RegisterAppStateObserver() +{ + if (appStateObserver_ != nullptr) { + SC_LOG_INFO(LABEL, "AppStateObserver instance already create"); + return true; + } + appStateObserver_ = new (std::nothrow) AppStateObserver(); + if (appStateObserver_ == nullptr) { + SC_LOG_ERROR(LABEL, "Failed to create AppStateObserver instance"); + return false; + } + sptr samgrClient = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrClient == nullptr) { + SC_LOG_ERROR(LABEL, "Failed to get system ability manager"); + appStateObserver_ = nullptr; + return false; + } + iAppMgr_ = iface_cast(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID)); + if (iAppMgr_ == nullptr) { + SC_LOG_ERROR(LABEL, "Failed to get ability manager service"); + appStateObserver_ = nullptr; + return false; + } + + if (iAppMgr_->RegisterApplicationStateObserver(appStateObserver_) != ERR_OK) { + SC_LOG_ERROR(LABEL, "Failed to Register app state observer"); + iAppMgr_ = nullptr; + appStateObserver_ = nullptr; + return false; + } + + std::vector list; + if (iAppMgr_->GetForegroundApplications(list) == ERR_OK) { + for (auto it = list.begin(); it != list.end(); ++it) { + appStateObserver_->AddProcessToForegroundSet(*it); + } + } + SC_LOG_ERROR(LABEL, "Register app state observer success"); + return true; +} + +void SecCompService::UnregisterAppStateObserver() +{ + if (iAppMgr_ != nullptr && appStateObserver_ != nullptr) { + iAppMgr_->UnregisterApplicationStateObserver(appStateObserver_); + } +} + +bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) +{ + AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); + int32_t pid = IPCSkeleton::GetCallingPid(); + int32_t uid = IPCSkeleton::GetCallingUid(); + if (AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) != AccessToken::TOKEN_HAP) { + SC_LOG_ERROR(LABEL, "Get caller tokenId invalid"); + return false; + } + if (!appStateObserver_->IsProcessForeground(uid)) { + SC_LOG_ERROR(LABEL, "caller uid is not in foreground"); + return false; + } + caller.tokenId = tokenId; + caller.pid = pid; + caller.uid = uid; + return true; +} + +int32_t SecCompService::RegisterSecurityComponent(SecCompType type, + const std::string& componentInfo, int32_t& scId) +{ + SecCompCallerInfo caller; + if (!GetCallerInfo(caller)) { + SC_LOG_ERROR(LABEL, "Check caller failed"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + nlohmann::json jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); + if (jsonRes.is_discarded()) { + SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + return SecCompManager::GetInstance().RegisterSecurityComponent(type, jsonRes, caller, scId); +} + +int32_t SecCompService::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) +{ + SecCompCallerInfo caller; + if (!GetCallerInfo(caller)) { + SC_LOG_ERROR(LABEL, "Check caller failed"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + nlohmann::json jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); + if (jsonRes.is_discarded()) { + SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + return SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); +} + +int32_t SecCompService::UnregisterSecurityComponent(int32_t scId) +{ + SecCompCallerInfo caller; + if (!GetCallerInfo(caller)) { + SC_LOG_ERROR(LABEL, "Check caller failed"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + return SecCompManager::GetInstance().UnregisterSecurityComponent(scId, caller); +} + +int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo) +{ + SecCompCallerInfo caller; + if (!GetCallerInfo(caller)) { + SC_LOG_ERROR(LABEL, "Check caller failed"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + nlohmann::json jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); + if (jsonRes.is_discarded()) { + SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + return SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, touchInfo); +} + +int SecCompService::Dump(int fd, const std::vector& args) +{ + if (fd < 0) { + return ERR_INVALID_VALUE; + } + + dprintf(fd, "SecCompService Dump:\n"); + std::string arg0 = ((args.size() == 0)? "" : Str16ToStr8(args.at(0))); + if (arg0.compare("-h") == 0) { + dprintf(fd, "Usage:\n"); + dprintf(fd, " -h: command help\n"); + dprintf(fd, " -a: dump all sec component\n"); + dprintf(fd, " -p: dump foreground processes\n"); + } else if (arg0.compare("-p") == 0) { + std::string dumpStr; + appStateObserver_->DumpProcess(dumpStr); + dprintf(fd, "%s\n", dumpStr.c_str()); + } else if (arg0.compare("-a") == 0 || arg0 == "") { + std::string dumpStr; + SecCompManager::GetInstance().DumpSecComp(dumpStr); + dprintf(fd, "%s\n", dumpStr.c_str()); + } + return ERR_OK; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.h b/services/security_component_service/sa/sa_main/sec_comp_service.h new file mode 100644 index 0000000..5d735be --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_service.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_SERVICE_H +#define SECURITY_COMPONENT_SERVICE_H + +#include +#include +#include "app_state_observer.h" +#include "iremote_object.h" +#include "nocopyable.h" +#include "sec_comp_manager.h" +#include "sec_comp_stub.h" +#include "singleton.h" +#include "system_ability.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; +class SecCompService final : public SystemAbility, public SecCompStub { + DECLARE_DELAYED_SINGLETON(SecCompService); + DECLEAR_SYSTEM_ABILITY(SecCompService); + +public: + SecCompService(int32_t saId, bool runOnCreate); + + void OnStart() override; + void OnStop() override; + + int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId) override; + int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) override; + int32_t UnregisterSecurityComponent(int32_t scId) override; + int32_t ReportSecurityComponentClickEvent(int32_t scId, + const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; + int Dump(int fd, const std::vector& args) override; + +private: + bool Initialize() const; + bool RegisterAppStateObserver(); + void UnregisterAppStateObserver(); + bool GetCallerInfo(SecCompCallerInfo& caller); + + ServiceRunningState state_; + sptr iAppMgr_; + sptr appStateObserver_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_SERVICE_H diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp new file mode 100644 index 0000000..4d8b32f --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -0,0 +1,186 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_stub.h" + +#include "ipc_skeleton.h" +#include "sec_comp_click_event_parcel.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompStub"}; +} // namespace + +int32_t SecCompStub::OnRemoteRequest( + uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + std::u16string descripter = SecCompStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + SC_LOG_ERROR(LABEL, "Deal remote request fail, descriptor is not matched"); + return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; + } + + auto itFunc = requestFuncMap_.find(code); + if (itFunc != requestFuncMap_.end()) { + auto requestFunc = itFunc->second; + if (requestFunc != nullptr) { + return (this->*requestFunc)(data, reply); + } else { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } + return SC_OK; +} + +int32_t SecCompStub::RegisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply) +{ + uint32_t type; + if (!data.ReadUint32(type)) { + SC_LOG_ERROR(LABEL, "Read component type fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (type <= UNKNOWN_SC_TYPE || type >= MAX_SC_TYPE) { + SC_LOG_ERROR(LABEL, "Security component type invalid"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + std::string componentInfo; + if (!data.ReadString(componentInfo)) { + SC_LOG_ERROR(LABEL, "Read component info fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + int32_t scId = INVALID_SC_ID; + int32_t res = this->RegisterSecurityComponent(static_cast(type), componentInfo, scId); + if (!reply.WriteInt32(res)) { + SC_LOG_ERROR(LABEL, "Register security component result fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (!reply.WriteInt32(scId)) { + SC_LOG_ERROR(LABEL, "Register security component result fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + return SC_OK; +} + +int32_t SecCompStub::UpdateSecurityComponentInner(MessageParcel& data, MessageParcel& reply) +{ + int32_t scId; + if (!data.ReadInt32(scId)) { + SC_LOG_ERROR(LABEL, "Read component id fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (scId < 0) { + SC_LOG_ERROR(LABEL, "Security component id invalid"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + std::string componentInfo; + if (!data.ReadString(componentInfo)) { + SC_LOG_ERROR(LABEL, "Read component info fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + int32_t res = this->UpdateSecurityComponent(scId, componentInfo); + if (!reply.WriteInt32(res)) { + SC_LOG_ERROR(LABEL, "Update security component result fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + return res; +} + +int32_t SecCompStub::UnregisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply) +{ + int32_t scId; + if (!data.ReadInt32(scId)) { + SC_LOG_ERROR(LABEL, "Read component id fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (scId < 0) { + SC_LOG_ERROR(LABEL, "Security component id invalid"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + int32_t res = this->UnregisterSecurityComponent(scId); + if (!reply.WriteInt32(res)) { + SC_LOG_ERROR(LABEL, "Unregister security component result fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return SC_OK; +} + +int32_t SecCompStub::ReportSecurityComponentClickEventInner(MessageParcel& data, MessageParcel& reply) +{ + int32_t scId; + if (!data.ReadInt32(scId)) { + SC_LOG_ERROR(LABEL, "Read component id fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (scId < 0) { + SC_LOG_ERROR(LABEL, "Security component id invalid"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + std::string componentInfo; + if (!data.ReadString(componentInfo)) { + SC_LOG_ERROR(LABEL, "Read component info fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + sptr touchInfoParcel = data.ReadParcelable(); + if (touchInfoParcel == nullptr) { + SC_LOG_ERROR(LABEL, "Read touchInfo info fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + int32_t res = this->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfoParcel->touchInfoParams_); + if (!reply.WriteInt32(res)) { + SC_LOG_ERROR(LABEL, "Register security component result fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return SC_OK; +} + +SecCompStub::SecCompStub() +{ + requestFuncMap_[static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT)] = + &SecCompStub::RegisterSecurityComponentInner; + requestFuncMap_[static_cast(ISecCompService::InterfaceCode::UPDATE_SECURITY_COMPONENT)] = + &SecCompStub::UpdateSecurityComponentInner; + requestFuncMap_[static_cast(ISecCompService::InterfaceCode::UNREGISTER_SECURITY_COMPONENT)] = + &SecCompStub::UnregisterSecurityComponentInner; + requestFuncMap_[static_cast(ISecCompService::InterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT)] = + &SecCompStub::ReportSecurityComponentClickEventInner; +} + +SecCompStub::~SecCompStub() +{ + SC_LOG_ERROR(LABEL, "~SecCompStub"); + requestFuncMap_.clear(); + SC_LOG_ERROR(LABEL, "~SecCompStub end"); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.h b/services/security_component_service/sa/sa_main/sec_comp_stub.h new file mode 100644 index 0000000..b858161 --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_STUB_H +#define SECURITY_COMPONENT_STUB_H + +#include +#include "i_sec_comp_service.h" +#include "iremote_stub.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompStub : public IRemoteStub { +public: + SecCompStub(); + virtual ~SecCompStub(); + + int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; + +private: + int32_t RegisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply); + int32_t UpdateSecurityComponentInner(MessageParcel& data, MessageParcel& reply); + int32_t UnregisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply); + int32_t ReportSecurityComponentClickEventInner(MessageParcel& data, MessageParcel& reply); + + using RequestFuncType = int32_t (SecCompStub::*)(MessageParcel& data, MessageParcel& reply); + std::map requestFuncMap_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_STUB_H diff --git a/services/security_component_service/sa/sa_profile/3506.xml b/services/security_component_service/sa/sa_profile/3506.xml new file mode 100644 index 0000000..b41acee --- /dev/null +++ b/services/security_component_service/sa/sa_profile/3506.xml @@ -0,0 +1,24 @@ + + + + security_component_service + + 3506 + libsecurity_component_service.z.so + false + false + 1 + + diff --git a/services/security_component_service/sa/sa_profile/BUILD.gn b/services/security_component_service/sa/sa_profile/BUILD.gn new file mode 100644 index 0000000..3ed788b --- /dev/null +++ b/services/security_component_service/sa/sa_profile/BUILD.gn @@ -0,0 +1,19 @@ +# Copyright (c) 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. + +import("//build/ohos/sa_profile/sa_profile.gni") + +ohos_sa_profile("security_component_sa_profile_standard") { + sources = [ "3506.xml" ] + part_name = "security_component" +} diff --git a/services/security_component_service/sa/security_component_service.cfg b/services/security_component_service/sa/security_component_service.cfg new file mode 100644 index 0000000..3081ece --- /dev/null +++ b/services/security_component_service/sa/security_component_service.cfg @@ -0,0 +1,17 @@ +{ + "services" : [{ + "name" : "security_component_service", + "path" : ["/system/bin/sa_main", "/system/profile/security_component_service.xml"], + "ondemand" : true, + "start-mode" : "condition", + "uid" : "security_component", + "gid" : ["security_component"], + "apl" : "system_basic", + "permission" : [ + "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", + "ohos.permission.REVOKE_SENSITIVE_PERMISSIONS" + ], + "secon" : "u:r:security_component_service:s0" + } + ] +} diff --git a/services/security_component_service/sa/security_component_service.rc b/services/security_component_service/sa/security_component_service.rc new file mode 100644 index 0000000..28c7570 --- /dev/null +++ b/services/security_component_service/sa/security_component_service.rc @@ -0,0 +1,19 @@ +# Copyright (c) 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. + +service security_component_service /system/bin/sa_main /system/profile/security_component.xml + class security_component_service + priority -20 + user security_component + group security_component + seclabel u:r:security_component_service:s0 diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn new file mode 100644 index 0000000..dce6264 --- /dev/null +++ b/services/security_component_service/sa/test/BUILD.gn @@ -0,0 +1,66 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/test.gni") + +ohos_unittest("sec_comp_service_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/include", + "//third_party/json/include", + ] + + sources = [ + "unittest/src/app_state_observer_test.cpp", + "unittest/src/sec_comp_entity_test.cpp", + "unittest/src/sec_comp_info_helper_test.cpp", + "unittest/src/sec_comp_manager_test.cpp", + "unittest/src/sec_comp_service_test.cpp", + "unittest/src/sec_comp_stub_test.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_manager.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "//third_party/googletest:gmock_main", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "access_token:libtoken_setproc", + ] +} + +group("unittest") { + testonly = true + deps = [ ":sec_comp_service_test" ] +} diff --git a/services/security_component_service/sa/test/mock/include/access_token.h b/services/security_component_service/sa/test/mock/include/access_token.h new file mode 100644 index 0000000..1640ba4 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/access_token.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_MOCK_ACCESS_TOKEN_DEF_H +#define SECURITY_COMPONENT_MOCK_ACCESS_TOKEN_DEF_H + +namespace OHOS { +namespace Security { +namespace AccessToken { +typedef unsigned int AccessTokenID; +typedef enum TypeATokenTypeEnum { + TOKEN_INVALID = -1, + TOKEN_HAP = 0, + TOKEN_NATIVE, + TOKEN_SHELL, + TOKEN_TYPE_BUTT, +} ATokenTypeEnum; + +typedef enum TypePermissionFlag { + PERMISSION_DEFAULT_FLAG = 0, + PERMISSION_USER_SET = 1 << 0, + PERMISSION_USER_FIXED = 1 << 1, + PERMISSION_SYSTEM_FIXED = 1 << 2, + PERMISSION_GRANTED_BY_POLICY = 1 << 3, +} PermissionFlag; + +typedef struct { + unsigned int tokenUniqueID : 20; + unsigned int res : 6; + unsigned int dlpFlag : 1; + unsigned int type : 2; + unsigned int version : 3; +} AccessTokenIDInner; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_MOCK_ACCESS_TOKEN_DEF_H \ No newline at end of file diff --git a/services/security_component_service/sa/test/mock/include/accesstoken_kit.h b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h new file mode 100644 index 0000000..bb44771 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H +#define SECURITY_COMPONENT_INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H + +#include +#include "access_token.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class AccessTokenKit { +public: + static int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag) + { + return 0; + }; + + static int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag) + { + return 0; + }; + + static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID) + { + AccessTokenIDInner *idInner = reinterpret_cast(&tokenID); + return static_cast(idInner->type); + }; +}; +} // namespace SECURITY_COMPONENT_INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H +} // namespace Security +} // namespace OHOS +#endif diff --git a/services/security_component_service/sa/test/mock/include/app_mgr_interface.h b/services/security_component_service/sa/test/mock/include/app_mgr_interface.h new file mode 100644 index 0000000..b0a36ec --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/app_mgr_interface.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_MOCK_ABILITY_RUNTIME_APP_MGR_INTERFACE_H +#define SECURITY_COMPONENT_MOCK_ABILITY_RUNTIME_APP_MGR_INTERFACE_H + +#include "iremote_broker.h" +#include "iremote_object.h" +#include "application_state_observer_stub.h" + +namespace OHOS { +namespace AppExecFwk { + +class IAppMgr : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.mock.AppMgr"); + + virtual int32_t RegisterApplicationStateObserver(const sptr &observer, + const std::vector &bundleNameList = {}) = 0; + + virtual int32_t UnregisterApplicationStateObserver(const sptr &observer) = 0; + + virtual int32_t GetForegroundApplications(std::vector &list) = 0; +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // SECURITY_COMPONENT_MOCK_ABILITY_RUNTIME_APP_MGR_INTERFACE_H diff --git a/services/security_component_service/sa/test/mock/include/application_state_observer_stub.h b/services/security_component_service/sa/test/mock/include/application_state_observer_stub.h new file mode 100644 index 0000000..9271160 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/application_state_observer_stub.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_MOCK_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H +#define SECURITY_COMPONENT_MOCK_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H + +#include +#include + +#include "iremote_stub.h" +#include "nocopyable.h" +#include "string_ex.h" + +namespace OHOS { +namespace AppExecFwk { +enum class AppProcessState { + APP_STATE_BEGIN = 0, + APP_STATE_CREATE = APP_STATE_BEGIN, + APP_STATE_READY, + APP_STATE_FOREGROUND, + APP_STATE_FOCUS, + APP_STATE_BACKGROUND, + APP_STATE_TERMINATED, + APP_STATE_END, +}; + +struct ProcessData { + std::string bundleName; + int32_t pid = 0; + int32_t uid = 0; + AppProcessState state; + bool isContinuousTask = false; + bool isKeepAlive = false; + bool isFocused = false; + int32_t requestProcCode = 0; +}; + +struct AppStateData { + std::string bundleName; + int32_t pid = -1; + int32_t uid = 0; + int32_t state = 0; + int32_t accessTokenId = 0; + bool isFocused = false; +}; + +class IApplicationStateObserver : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.IApplicationStateObserver"); + virtual void OnProcessStateChanged(const ProcessData &processData) {} + virtual void OnProcessDied(const ProcessData &processData) = 0; +}; + +class ApplicationStateObserverStub : public IRemoteStub { +public: + ApplicationStateObserverStub() = default; + virtual ~ApplicationStateObserverStub() = default; + DISALLOW_COPY_AND_MOVE(ApplicationStateObserverStub); +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // SECURITY_COMPONENT_MOCK_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H diff --git a/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h b/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h new file mode 100644 index 0000000..e67a1a1 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h @@ -0,0 +1,44 @@ +/* +* Copyright (c) 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 SEC_COMP_MOCK_SYSTEM_ABILITY_IF_MANAGER_CLINET_INCLUDE_H +#define SEC_COMP_MOCK_SYSTEM_ABILITY_IF_MANAGER_CLINET_INCLUDE_H +#include "iremote_broker.h" +#include "iremote_object.h" +#include "iremote_proxy.h" +#include "mock_app_mgr_proxy.h" + +namespace OHOS { +class MockIRemoteObject : public virtual RefBase { +}; + +template inline sptr iface_cast(const sptr &object) +{ + if (object == nullptr) { + return nullptr; + } + const std::u16string descriptor = INTERFACE::GetDescriptor(); + if (descriptor == u"ohos.appexecfwk.mock.AppMgr") { + return static_cast(MockAppMgrProxy::g_MockAppMgrProxy); + } + return nullptr; +} + +class ISystemAbilityManager : public IRemoteBroker { +public: + virtual sptr GetSystemAbility(int32_t systemAbilityId) = 0; + virtual int32_t UnloadSystemAbility(int32_t systemAbilityId) = 0; +}; +} // namespace OHOS +#endif // SEC_COMP_MOCK_SYSTEM_ABILITY_IF_MANAGER_CLINET_INCLUDE_H diff --git a/services/security_component_service/sa/test/mock/include/ipc_mock_skeleton.h b/services/security_component_service/sa/test/mock/include/ipc_mock_skeleton.h new file mode 100644 index 0000000..f1ca97c --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/ipc_mock_skeleton.h @@ -0,0 +1,38 @@ +/* +* Copyright (c) 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 SEC_COMP_MOCK_IPC_SKELETON_INCLUDE_H +#define SEC_COMP_MOCK_IPC_SKELETON_INCLUDE_H +#include + +namespace OHOS { +class IPCSkeleton { +public: + IPCSkeleton() = default; + ~IPCSkeleton() = default; + static uint32_t GetCallingTokenID() + { + return tokenID; + }; + + static pid_t GetCallingPid() + { + return pid; + }; + + static uint32_t tokenID; + static pid_t pid; +}; +} // namespace OHOS +#endif // SEC_COMP_MOCK_IPC_SKELETON_INCLUDE_H diff --git a/services/security_component_service/sa/test/mock/include/iservice_registry.h b/services/security_component_service/sa/test/mock/include/iservice_registry.h new file mode 100644 index 0000000..0556610 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/iservice_registry.h @@ -0,0 +1,30 @@ +/* +* Copyright (c) 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 SEC_COMP_MOCK_SYSTEM_ABILITY_MANAGER_CLINET_INCLUDE_H +#define SEC_COMP_MOCK_SYSTEM_ABILITY_MANAGER_CLINET_INCLUDE_H +#include +#include "if_system_ability_manager.h" + +namespace OHOS { +class SystemAbilityManagerClient { +public: + static SystemAbilityManagerClient& GetInstance(); + SystemAbilityManagerClient() = default; + ~SystemAbilityManagerClient() = default; + + MOCK_METHOD0(GetSystemAbilityManager, sptr()); +}; +} // namespace OHOS +#endif // SEC_COMP_MOCK_SYSTEM_ABILITY_MANAGER_CLINET_INCLUDE_H \ No newline at end of file diff --git a/services/security_component_service/sa/test/mock/include/mock_app_mgr_proxy.h b/services/security_component_service/sa/test/mock/include/mock_app_mgr_proxy.h new file mode 100644 index 0000000..eab8644 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/mock_app_mgr_proxy.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 SECURITY_COMPONET_MOCK_APP_MGR_PROXY_H +#define SECURITY_COMPONET_MOCK_APP_MGR_PROXY_H +#include +#include +#include "app_mgr_interface.h" +#include "iremote_proxy.h" + +namespace OHOS { +class MockAppMgrProxy : public IRemoteProxy { +public: + explicit MockAppMgrProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~MockAppMgrProxy() = default; + + static MockAppMgrProxy* g_MockAppMgrProxy; + + MOCK_METHOD2(RegisterApplicationStateObserver, + int32_t(const sptr&, const std::vector&)); + MOCK_METHOD1(UnregisterApplicationStateObserver, int32_t(const sptr &)); + MOCK_METHOD1(GetForegroundApplications, int32_t(std::vector&)); +}; +} // namespace OHOS +#endif diff --git a/services/security_component_service/sa/test/mock/include/mock_system_ability_proxy.h b/services/security_component_service/sa/test/mock/include/mock_system_ability_proxy.h new file mode 100644 index 0000000..ee833e6 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/mock_system_ability_proxy.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_MOCK_SYSTEM_ABILITY_MANAGER_PROXY +#define SECURITY_COMPONENT_MOCK_SYSTEM_ABILITY_MANAGER_PROXY +#include +#include +#include "if_system_ability_manager.h" + +namespace OHOS { +class SystemAbilityManagerProxy : public IRemoteProxy { +public: + explicit SystemAbilityManagerProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~SystemAbilityManagerProxy() = default; + + MOCK_METHOD1(GetSystemAbility, sptr(int32_t)); + MOCK_METHOD1(UnloadSystemAbility, int32_t(int32_t)); +}; +} // namespace OHOS +#endif diff --git a/services/security_component_service/sa/test/mock/include/system_ability.h b/services/security_component_service/sa/test/mock/include/system_ability.h new file mode 100644 index 0000000..9127f3a --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/system_ability.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 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 MOCK_SECURITY_COMPONENT_SYSTEM_ABILITY_H +#define MOCK_SECURITY_COMPONENT_SYSTEM_ABILITY_H + +#include +#include "if_system_ability_manager.h" +#include "iremote_object.h" + +namespace OHOS { +#define DECLEAR_SYSTEM_ABILITY(className) +#define SA_ID_SECURITY_COMPONENT_SERVICE 1000 +#define REGISTER_SYSTEM_ABILITY_BY_ID(a, b, c) + +class SystemAbility { +public: + static bool MakeAndRegisterAbility(SystemAbility*) + { + return true; + } + + virtual void OnStart() {}; + + virtual void OnStop() {}; + + MOCK_METHOD1(Publish, bool(sptr)); + + SystemAbility(bool runOnCreate = false) {}; + + SystemAbility(const int32_t serviceId, bool runOnCreate = false) {}; + + virtual ~SystemAbility() = default; +}; +} // namespace OHOS +#endif // MOCK_SECURITY_COMPONENT_SYSTEM_ABILITY_H diff --git a/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp b/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp new file mode 100644 index 0000000..d83e9f0 --- /dev/null +++ b/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp @@ -0,0 +1,20 @@ +/* +* Copyright (c) 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. +*/ + +#include "mock_app_mgr_proxy.h" + +namespace OHOS { +MockAppMgrProxy* MockAppMgrProxy::g_MockAppMgrProxy = nullptr; +} diff --git a/services/security_component_service/sa/test/mock/src/mock_ipc_skeleton.cpp b/services/security_component_service/sa/test/mock/src/mock_ipc_skeleton.cpp new file mode 100644 index 0000000..62174e8 --- /dev/null +++ b/services/security_component_service/sa/test/mock/src/mock_ipc_skeleton.cpp @@ -0,0 +1,20 @@ +/* +* Copyright (c) 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. +*/ +#include "ipc_skeleton.h" + +namespace OHOS { +uint32_t IPCSkeleton::tokenID = 0; +pid_t IPCSkeleton::pid = -1; +} // namespace OHOS diff --git a/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp b/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp new file mode 100644 index 0000000..486e0ee --- /dev/null +++ b/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp @@ -0,0 +1,24 @@ + /* + * Copyright (C) 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. + */ + +#include "iservice_registry.h" + +namespace OHOS { +SystemAbilityManagerClient& SystemAbilityManagerClient::GetInstance() +{ + static auto instance = new SystemAbilityManagerClient(); + return *instance; +} +} // namespace OHOS diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp new file mode 100644 index 0000000..5c57ee6 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 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. + */ +#include "app_state_observer_test.h" +#include "sec_comp_log.h" +#include "sec_comp_err.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "AppStateObserverTest"}; + +static constexpr int32_t TEST_UID = 1; +static constexpr int32_t TEST_UID_2 = 2; +} + +void AppStateObserverTest::SetUpTestCase() +{} + +void AppStateObserverTest::TearDownTestCase() +{} + +void AppStateObserverTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); + if (observer_ != nullptr) { + return; + } + + observer_ = std::make_shared(); + ASSERT_NE(nullptr, observer_); +} + +void AppStateObserverTest::TearDown() +{ + observer_ = nullptr; +} + +/** + * @tc.name: IsProcessForeground001 + * @tc.desc: Test is process foreground + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) +{ + ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); + AppExecFwk::AppStateData stateData = { + .uid = TEST_UID + }; + observer_->AddProcessToForegroundSet(stateData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); +} + +/** + * @tc.name: AddProcessToForegroundSet001 + * @tc.desc: Test add process to foreground + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) +{ + AppExecFwk::AppStateData stateData = { + .uid = TEST_UID + }; + observer_->AddProcessToForegroundSet(stateData); + observer_->AddProcessToForegroundSet(stateData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + + AppExecFwk::ProcessData procData = { + .uid = TEST_UID_2 + }; + observer_->AddProcessToForegroundSet(procData); + observer_->AddProcessToForegroundSet(procData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID_2)); +} + +/** + * @tc.name: RemoveProcessFromForegroundSet001 + * @tc.desc: Test remove process from foreground + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level1) +{ + AppExecFwk::ProcessData procData = { + .uid = TEST_UID + }; + observer_->AddProcessToForegroundSet(procData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + observer_->RemoveProcessFromForegroundSet(procData); + observer_->RemoveProcessFromForegroundSet(procData); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); +} + +/** + * @tc.name: OnProcessStateChanged001 + * @tc.desc: Test recieve process state changed + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) +{ + AppExecFwk::ProcessData processData; + processData.state = AppExecFwk::AppProcessState::APP_STATE_CREATE; + observer_->OnProcessStateChanged(processData); + ASSERT_EQ(observer_->foregrandProcList_.size(), static_cast(0)); + + processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; + processData.uid = TEST_UID; + observer_->OnProcessStateChanged(processData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + + processData.state = AppExecFwk::AppProcessState::APP_STATE_BACKGROUND; + processData.uid = TEST_UID; + observer_->OnProcessStateChanged(processData); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); +} + +/** + * @tc.name: OnProcessDied001 + * @tc.desc: Test recieve process state died + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(AppStateObserverTest, OnProcessDied001, TestSize.Level1) +{ + AppExecFwk::ProcessData processData; + processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; + processData.uid = TEST_UID; + observer_->OnProcessStateChanged(processData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + + // if last process died, the sec_comp service will exit. + processData.uid = TEST_UID_2; + observer_->OnProcessStateChanged(processData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID_2)); + + // notify process 1 died + processData.uid = TEST_UID; + observer_->OnProcessDied(processData); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); +} diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.h b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.h new file mode 100644 index 0000000..7195cf2 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 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 APP_STATE_OBSERVER_TEST_H +#define APP_STATE_OBSERVER_TEST_H + +#include +#define private public +#include "app_state_observer.h" +#undef private + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class AppStateObserverTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); + + std::shared_ptr observer_ = nullptr; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // APP_STATE_OBSERVER_TEST_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp new file mode 100644 index 0000000..b254c45 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_entity_test.h" +#include "sec_comp_log.h" +#include "sec_comp_err.h" +#include "sec_comp_tool.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntityTest"}; +static constexpr int32_t TEST_SC_ID = 1; +static constexpr AccessTokenID TEST_TOKEN_ID = 1; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr double TEST_DIFF_COORDINATE = 200.0; +} + +void SecCompEntityTest::SetUpTestCase() +{} + +void SecCompEntityTest::TearDownTestCase() +{} + +void SecCompEntityTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); + if (entity_ != nullptr) { + return; + } + + std::shared_ptr component = std::make_shared(); + ASSERT_NE(nullptr, component); + + entity_ = std::make_shared(component, 1, 1); + ASSERT_NE(nullptr, entity_); +} + +void SecCompEntityTest::TearDown() +{ + entity_ = nullptr; +} + +/** + * @tc.name: RevokeTempPermission001 + * @tc.desc: Test revoke temp permission + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompEntityTest, RevokeTempPermission001, TestSize.Level1) +{ + entity_->isGrant_ = false; + ASSERT_EQ(entity_->RevokeTempPermission(), SC_OK); + + entity_->isGrant_ = true; + entity_->componentInfo_->type_ = UNKNOWN_SC_TYPE; + ASSERT_EQ(entity_->RevokeTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + ASSERT_FALSE(entity_->isGrant_); + + entity_->isGrant_ = true; + entity_->componentInfo_->type_ = LOCATION_COMPONENT; + ASSERT_EQ(entity_->RevokeTempPermission(), SC_OK); + ASSERT_FALSE(entity_->isGrant_); +} + +/** + * @tc.name: GrantTempPermission001 + * @tc.desc: Test grant permission + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level1) +{ + entity_->isGrant_ = true; + ASSERT_EQ(entity_->GrantTempPermission(), SC_OK); + + entity_->isGrant_ = false; + entity_->componentInfo_->type_ = UNKNOWN_SC_TYPE; + ASSERT_EQ(entity_->GrantTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + ASSERT_TRUE(entity_->isGrant_); + + entity_->isGrant_ = false; + entity_->componentInfo_->type_ = LOCATION_COMPONENT; + ASSERT_EQ(entity_->GrantTempPermission(), SC_OK); + ASSERT_TRUE(entity_->isGrant_); +} + +/** + * @tc.name: CheckTouchInfo001 + * @tc.desc: Test touch info + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) +{ + SecCompClickEvent touch = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = 0, + }; + ASSERT_FALSE(entity_->CheckTouchInfo(touch)); + + uint64_t current = GetCurrentTime(); + touch.timestamp = current + 10000L; // 10s + ASSERT_FALSE(entity_->CheckTouchInfo(touch)); + + entity_->componentInfo_->rect_.x_ = TEST_DIFF_COORDINATE; // click event will not hit this rect + entity_->componentInfo_->rect_.y_ = TEST_DIFF_COORDINATE; + entity_->componentInfo_->rect_.width_ = TEST_DIFF_COORDINATE; + entity_->componentInfo_->rect_.height_ = TEST_DIFF_COORDINATE; + touch.timestamp = GetCurrentTime(); + ASSERT_FALSE(entity_->CheckTouchInfo(touch)); + + entity_->componentInfo_->rect_.x_ = TEST_COORDINATE; + entity_->componentInfo_->rect_.y_ = TEST_COORDINATE; + touch.timestamp = GetCurrentTime(); + ASSERT_TRUE(entity_->CheckTouchInfo(touch)); +} + +/** + * @tc.name: IsRectOverlaped001 + * @tc.desc: Test check rect overlap + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompEntityTest, IsRectOverlaped001, TestSize.Level1) +{ + SecCompEntity other(nullptr, TEST_TOKEN_ID, TEST_SC_ID); + ASSERT_TRUE(entity_->IsRectOverlaped(other)); + + entity_->componentInfo_->rect_.x_ = TEST_COORDINATE; + entity_->componentInfo_->rect_.y_ = TEST_COORDINATE; + entity_->componentInfo_->rect_.width_ = TEST_COORDINATE; + entity_->componentInfo_->rect_.height_ = TEST_COORDINATE; + + other.componentInfo_ = std::make_shared(); + ASSERT_NE(other.componentInfo_, nullptr); + other.componentInfo_->rect_.x_ = TEST_COORDINATE; + other.componentInfo_->rect_.y_ = TEST_COORDINATE; + other.componentInfo_->rect_.width_ = TEST_COORDINATE; + other.componentInfo_->rect_.height_ = TEST_COORDINATE; + ASSERT_TRUE(entity_->IsRectOverlaped(other)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.h new file mode 100644 index 0000000..c65cf37 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 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 SEC_COMP_ENTITY_TEST_H +#define SEC_COMP_ENTITY_TEST_H + +#include +#define private public +#include "sec_comp_entity.h" +#undef private + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompEntityTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); + + std::shared_ptr entity_ = nullptr; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_ENTITY_TEST_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp new file mode 100644 index 0000000..5ac58db --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_info_helper_test.h" +#include "location_button.h" +#include "paste_button.h" +#include "save_button.h" +#include "sec_comp_log.h" +#include "sec_comp_err.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelperTest"}; + +static constexpr uint32_t TEST_FONT_SIZE = 100; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr uint32_t TEST_COLOR = 0xffffffff; + +static LocationButton BuildValidLocationComponent() +{ + LocationButton button; + button.fontSize_ = TEST_FONT_SIZE; + button.font_ = FONT_HO_SANS; + button.bgColor_.value = TEST_COLOR; + button.fontColor_.value = TEST_COLOR; + button.icon_ = DEFAULT_ICON; + button.label_ = DEFAULT_LABEL; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.type_ = LOCATION_COMPONENT; + button.buttonType_ = CAPSULE; + return button; +} +} + +void SecCompInfoHelperTest::SetUpTestCase() +{} + +void SecCompInfoHelperTest::TearDownTestCase() +{} + +void SecCompInfoHelperTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); +} + +void SecCompInfoHelperTest::TearDown() +{} + +/** + * @tc.name: ParseComponent001 + * @tc.desc: Test parse component info + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) +{ + nlohmann::json jsonComponent; + ASSERT_EQ(SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent), nullptr); + + LocationButton button = BuildValidLocationComponent(); + button.fontSize_ = 0; + button.ToJson(jsonComponent); + ASSERT_EQ(SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent), nullptr); + ASSERT_NE(SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent), nullptr); + ASSERT_NE(SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent), nullptr); +} + +/** + * @tc.name: CompareSecCompInfo001 + * @tc.desc: Test compare sec comp info + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompInfoHelperTest, CompareSecCompInfo001, TestSize.Level1) +{ + SecCompBase comp1; + ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(&comp1, nullptr)); + ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(nullptr, &comp1)); + + SecCompBase comp2; + comp2.type_ = UNKNOWN_SC_TYPE; + comp1.type_ = LOCATION_COMPONENT; + ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(&comp1, &comp2)); + + comp1.type_ = UNKNOWN_SC_TYPE; + ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(&comp1, &comp2)); + + LocationButton locationButton1 = BuildValidLocationComponent(); + LocationButton locationButton2 = BuildValidLocationComponent(); + ASSERT_TRUE(SecCompInfoHelper::CompareSecCompInfo(&locationButton1, &locationButton2)); + + PasteButton pasteButton; + pasteButton.type_ = PASTE_COMPONENT; + pasteButton.rect_.x_ = TEST_COORDINATE; + pasteButton.rect_.y_ = TEST_COORDINATE; + ASSERT_TRUE(SecCompInfoHelper::CompareSecCompInfo(&pasteButton, &pasteButton)); + + SaveButton saveButton; + saveButton.type_ = SAVE_COMPONENT; + saveButton.rect_.x_ = TEST_COORDINATE; + saveButton.rect_.y_ = TEST_COORDINATE; + ASSERT_TRUE(SecCompInfoHelper::CompareSecCompInfo(&saveButton, &saveButton)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h new file mode 100644 index 0000000..a9d8459 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 SEC_COMP_INFO_HELPER_TEST_H +#define SEC_COMP_INFO_HELPER_TEST_H + +#include +#include "sec_comp_info_helper.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompInfoHelperTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_ENTITY_TEST_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp new file mode 100644 index 0000000..ebd178d --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -0,0 +1,400 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_manager_test.h" +#include "sec_comp_log.h" +#include "location_button.h" +#include "sec_comp_err.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompManagerTest"}; + +static constexpr uint32_t TEST_FONT_SIZE = 100; +static constexpr uint32_t TEST_DIFF_FONT_SIZE = 16; +static constexpr uint32_t TEST_INVALID_FONT_SIZE = 0; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr uint32_t TEST_COLOR = 0xffffffff; + +static constexpr int32_t TEST_UID_1 = 1; +static constexpr int32_t TEST_UID_2 = 2; +static constexpr int32_t TEST_UID_3 = 3; + +static constexpr AccessTokenID TEST_TOKEN_ID_1 = 1; +static constexpr AccessTokenID TEST_TOKEN_ID_2 = 2; +static constexpr int32_t TEST_SC_ID_1 = 1; +static constexpr int32_t TEST_SC_ID_2 = 2; +static constexpr int32_t TEST_INVALID_SC_ID = -1; + +static constexpr int32_t MAX_INT_NUM = 0x7fffffff; +static constexpr int32_t SC_ID_START = 1000; + +static LocationButton BuildInvalidLocationComponent() +{ + LocationButton button; + button.fontSize_ = TEST_INVALID_FONT_SIZE; + button.font_ = FONT_HO_SANS; + button.bgColor_.value = TEST_COLOR; + button.fontColor_.value = TEST_COLOR; + button.icon_ = DEFAULT_ICON; + button.label_ = DEFAULT_LABEL; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.type_ = LOCATION_COMPONENT; + button.buttonType_ = CAPSULE; + return button; +} + +static LocationButton BuildValidLocationComponent() +{ + LocationButton button; + button.fontSize_ = TEST_FONT_SIZE; + button.font_ = FONT_HO_SANS; + button.bgColor_.value = TEST_COLOR; + button.fontColor_.value = TEST_COLOR; + button.icon_ = DEFAULT_ICON; + button.label_ = DEFAULT_LABEL; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.type_ = LOCATION_COMPONENT; + button.buttonType_ = CAPSULE; + return button; +} +} + +void SecCompManagerTest::SetUpTestCase() +{} + +void SecCompManagerTest::TearDownTestCase() +{} + +void SecCompManagerTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); +} + +void SecCompManagerTest::TearDown() +{ + SecCompManager::GetInstance().componentMap_.clear(); +} + + +/** + * @tc.name: CreateScId001 + * @tc.desc: Test create sc id + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) +{ + SecCompManager::GetInstance().scIdStart_ = MAX_INT_NUM; + ASSERT_EQ(SecCompManager::GetInstance().CreateScId(), SC_ID_START); + + SecCompManager::GetInstance().scIdStart_ = SC_ID_START; + ASSERT_EQ(SecCompManager::GetInstance().CreateScId(), SC_ID_START + 1); +} + +/** + * @tc.name: AddProcessComponent001 + * @tc.desc: Test add process component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, AddProcessComponent001, TestSize.Level1) +{ + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(compPtr, nullptr); + compPtr->rect_.x_ = TEST_COORDINATE; + compPtr->rect_.y_ = TEST_COORDINATE; + compPtr->rect_.width_ = TEST_COORDINATE; + compPtr->rect_.height_ = TEST_COORDINATE; + SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + std::vector componentList; + ASSERT_EQ(SecCompManager::GetInstance().AddProcessComponent(componentList, entity), SC_OK); + + std::shared_ptr compPtrNew = std::make_shared(); + ASSERT_NE(compPtrNew, nullptr); + compPtrNew->rect_.x_ = TEST_COORDINATE + 1; // offset 1 pixel + compPtrNew->rect_.y_ = TEST_COORDINATE; + compPtrNew->rect_.width_ = TEST_COORDINATE; + compPtrNew->rect_.height_ = TEST_COORDINATE; + SecCompEntity entityNew(compPtrNew, TEST_TOKEN_ID_2, TEST_SC_ID_2); + ASSERT_EQ(SecCompManager::GetInstance().AddProcessComponent(componentList, entityNew), + SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP); +} + +/** + * @tc.name: AddSecurityComponentToList001 + * @tc.desc: Test add security component to list + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) +{ + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(compPtr, nullptr); + compPtr->rect_.x_ = TEST_COORDINATE; + compPtr->rect_.y_ = TEST_COORDINATE; + compPtr->rect_.width_ = TEST_COORDINATE; + compPtr->rect_.height_ = TEST_COORDINATE; + SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entity), SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entity), + SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP); + + std::shared_ptr compPtrNew = std::make_shared(); + ASSERT_NE(compPtrNew, nullptr); + compPtrNew->rect_.x_ = TEST_COORDINATE * 2; // not overlap + compPtrNew->rect_.y_ = TEST_COORDINATE * 2; // not overlap + compPtrNew->rect_.width_ = TEST_COORDINATE; + compPtrNew->rect_.height_ = TEST_COORDINATE; + SecCompEntity entityNew(compPtrNew, TEST_TOKEN_ID_1, TEST_SC_ID_2); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entityNew), SC_OK); +} + +/** + * @tc.name: DeleteSecurityComponentFromList001 + * @tc.desc: Test delete security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1) +{ + ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), + SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(compPtr, nullptr); + compPtr->rect_.x_ = TEST_COORDINATE; + compPtr->rect_.y_ = TEST_COORDINATE; + compPtr->rect_.width_ = TEST_COORDINATE; + compPtr->rect_.height_ = TEST_COORDINATE; + SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); + + ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_2), + SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + + ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), SC_OK); +} + +/** + * @tc.name: GetSecurityComponentFromList001 + * @tc.desc: Test get security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) +{ + ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(compPtr, nullptr); + compPtr->rect_.x_ = TEST_COORDINATE; + compPtr->rect_.y_ = TEST_COORDINATE; + compPtr->rect_.width_ = TEST_COORDINATE; + compPtr->rect_.height_ = TEST_COORDINATE; + SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_2), nullptr); + ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); +} + +/** + * @tc.name: NotifyProcessBackground001 + * @tc.desc: Test notify process background + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) +{ + SecCompManager::GetInstance().NotifyProcessBackground(TEST_UID_1); + + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(compPtr, nullptr); + compPtr->rect_.x_ = TEST_COORDINATE; + compPtr->rect_.y_ = TEST_COORDINATE; + compPtr->rect_.width_ = TEST_COORDINATE; + compPtr->rect_.height_ = TEST_COORDINATE; + SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); + ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + SecCompManager::GetInstance().NotifyProcessBackground(TEST_UID_1); + ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); +} + +/** + * @tc.name: NotifyProcessDied001 + * @tc.desc: Test notify process died + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) +{ + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(compPtr, nullptr); + compPtr->rect_.x_ = TEST_COORDINATE; + compPtr->rect_.y_ = TEST_COORDINATE; + compPtr->rect_.width_ = TEST_COORDINATE; + compPtr->rect_.height_ = TEST_COORDINATE; + SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); + + std::shared_ptr compPtr2 = std::make_shared(); + ASSERT_NE(compPtr2, nullptr); + compPtr2->rect_.x_ = TEST_COORDINATE * 2; // not overlap + compPtr2->rect_.y_ = TEST_COORDINATE * 2; // not overlap + compPtr2->rect_.width_ = TEST_COORDINATE; + compPtr2->rect_.height_ = TEST_COORDINATE; + SecCompEntity entity2(compPtr2, TEST_TOKEN_ID_1, TEST_SC_ID_2); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_2, entity2), SC_OK); + + SecCompManager::GetInstance().NotifyProcessDied(TEST_UID_3); + ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + + SecCompManager::GetInstance().NotifyProcessDied(TEST_UID_1); + ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); +} + +/** + * @tc.name: RegisterSecurityComponent001 + * @tc.desc: Test register security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) +{ + nlohmann::json jsonInvalid; + LocationButton buttonInvalid = BuildInvalidLocationComponent(); + buttonInvalid.ToJson(jsonInvalid); + int32_t scId; + SecCompCallerInfo caller = { + .tokenId = TEST_TOKEN_ID_1, + .uid = TEST_UID_1 + }; + ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId), + SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); + + nlohmann::json jsonValid; + LocationButton buttonValid = BuildValidLocationComponent(); + buttonValid.ToJson(jsonValid); + + ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId), + SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId), + SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP); + ASSERT_EQ(scId, INVALID_SC_ID); +} + +/** + * @tc.name: UpdateSecurityComponent001 + * @tc.desc: Test update security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) +{ + nlohmann::json jsonValid; + LocationButton buttonValid = BuildValidLocationComponent(); + buttonValid.ToJson(jsonValid); + SecCompCallerInfo caller = { + .tokenId = TEST_TOKEN_ID_1, + .uid = TEST_UID_1 + }; + ASSERT_EQ(SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller), + SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + + int32_t scId; + ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId), + SC_OK); + nlohmann::json jsonInvalid; + LocationButton buttonInvalid = BuildInvalidLocationComponent(); + buttonInvalid.ToJson(jsonInvalid); + ASSERT_EQ(SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonInvalid, caller), + SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); +} + +/** + * @tc.name: UnregisterSecurityComponent001 + * @tc.desc: Test unregister security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) +{ + SecCompCallerInfo caller = { + .tokenId = TEST_TOKEN_ID_1, + .uid = TEST_UID_1 + }; + + ASSERT_EQ(SecCompManager::GetInstance().UnregisterSecurityComponent(TEST_INVALID_SC_ID, caller), + SC_SERVICE_ERROR_VALUE_INVALID); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent001 + * @tc.desc: Test report security component click + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Level1) +{ + SecCompCallerInfo caller = { + .tokenId = TEST_TOKEN_ID_1, + .uid = TEST_UID_1 + }; + SecCompClickEvent touchInfo; + nlohmann::json jsonVaild; + LocationButton buttonValid = BuildValidLocationComponent(); + buttonValid.ToJson(jsonVaild); + ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo), + SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + + int32_t scId; + ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonVaild, caller, scId), + SC_OK); + + nlohmann::json jsonInvalid; + LocationButton buttonInvalid = BuildInvalidLocationComponent(); + buttonInvalid.ToJson(jsonInvalid); + + ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonInvalid, caller, touchInfo), + SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); + + ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonInvalid, caller, touchInfo), + SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); + + nlohmann::json jsonValid1; + LocationButton buttonValid1 = BuildValidLocationComponent(); + buttonValid1.fontSize_ = TEST_DIFF_FONT_SIZE; + buttonValid1.ToJson(jsonValid1); + ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonValid1, caller, touchInfo), + SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL); + + touchInfo.touchX = 500.0; // not hit component + touchInfo.touchY = 500.0; // not hit component + ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonVaild, caller, touchInfo), + SC_SERVICE_ERROR_CLICK_EVENT_INVALID); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.h new file mode 100644 index 0000000..eaf0968 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 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 SEC_COMP_MANAGER_TEST_H +#define SEC_COMP_MANAGER_TEST_H + +#include +#define private public +#include "sec_comp_manager.h" +#undef private + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_MANAGER_TEST_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp new file mode 100644 index 0000000..82355b3 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -0,0 +1,319 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_service_test.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "location_button.h" +#include "mock_system_ability_proxy.h" +#include "mock_app_mgr_proxy.h" +#include "token_setproc.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" +#include "system_ability.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompServiceTest"}; + +static constexpr uint32_t HAP_TOKEN_ID = 537715419; +static constexpr int32_t SA_ID = 3506; +static constexpr uint32_t TEST_FONT_SIZE = 100; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr uint32_t TEST_COLOR = 0xffffffff; +static constexpr int32_t TEST_SC_ID = 1; + +static std::string BuildLocationComponentInfo() +{ + LocationButton button; + button.fontSize_ = TEST_FONT_SIZE; + button.font_ = FONT_HO_SANS; + button.bgColor_.value = TEST_COLOR; + button.fontColor_.value = TEST_COLOR; + button.icon_ = DEFAULT_ICON; + button.label_ = DEFAULT_LABEL; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.type_ = LOCATION_COMPONENT; + button.buttonType_ = CAPSULE; + + nlohmann::json jsonRes; + button.ToJson(jsonRes); + return jsonRes.dump(); +} +} + +void SecCompServiceTest::SetUpTestCase() +{} + +void SecCompServiceTest::TearDownTestCase() +{} + +void SecCompServiceTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); + if (secCompService_ != nullptr) { + return; + } + SecCompService* ptr = new (std::nothrow) SecCompService(SA_ID, true); + secCompService_ = sptr(ptr); + ASSERT_NE(nullptr, secCompService_); + secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); + ASSERT_TRUE(secCompService_->appStateObserver_ != nullptr); +} + +void SecCompServiceTest::TearDown() +{ + if (secCompService_ != nullptr) { + secCompService_->appStateObserver_ = nullptr; + } + secCompService_ = nullptr; +} + +/** + * @tc.name: Onstart001 + * @tc.desc: Test OnStart + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1) +{ + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->appStateObserver_ = nullptr; + secCompService_->OnStart(); + ASSERT_EQ(secCompService_->appStateObserver_, nullptr); + EXPECT_CALL(*secCompService_, Publish(testing::_)).WillOnce(testing::Return(false)); + + secCompService_->state_ = ServiceRunningState::STATE_NOT_START; + secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); + secCompService_->OnStart(); + ASSERT_EQ(secCompService_->state_, ServiceRunningState::STATE_RUNNING); + + secCompService_->OnStop(); + ASSERT_EQ(secCompService_->appStateObserver_, nullptr); +} + +/** + * @tc.name: RegisterAppStateObserver001 + * @tc.desc: Test RegisterAppStateObserver + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) +{ + // GetSystemAbilityManager get failed + secCompService_->appStateObserver_ = nullptr; + SystemAbilityManagerClient& saClient = SystemAbilityManagerClient::GetInstance(); + EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr)); + EXPECT_FALSE(secCompService_->RegisterAppStateObserver()); + + // GetSystemAbility get app mgr failed + secCompService_->appStateObserver_ = nullptr; + SystemAbilityManagerProxy proxy(nullptr); + EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_FALSE(secCompService_->RegisterAppStateObserver()); + + // RegisterApplicationStateObserver failed + secCompService_->appStateObserver_ = nullptr; + EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + MockIRemoteObject object; + EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); + sptr appProxy = new (std::nothrow) MockAppMgrProxy(nullptr); + MockAppMgrProxy::g_MockAppMgrProxy = appProxy; + EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, + RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(-1)); + EXPECT_FALSE(secCompService_->RegisterAppStateObserver()); + + // GetForegroundApplications failed + secCompService_->appStateObserver_ = nullptr; + EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); + EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, + RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0)); + EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, + GetForegroundApplications(testing::_)).WillOnce(testing::Return(-1)); + EXPECT_TRUE(secCompService_->RegisterAppStateObserver()); + EXPECT_EQ(secCompService_->appStateObserver_->foregrandProcList_.size(), static_cast(0)); + + // get one foreground app + secCompService_->appStateObserver_ = nullptr; + EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); + EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, + RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0)); + EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, GetForegroundApplications(testing::_)) + .WillOnce([](std::vector& list) { + AppExecFwk::AppStateData data; + data.uid = 1000; + list.emplace_back(data); + return 0; + }); + EXPECT_TRUE(secCompService_->RegisterAppStateObserver()); + EXPECT_EQ(secCompService_->appStateObserver_->foregrandProcList_.size(), static_cast(1)); + secCompService_->UnregisterAppStateObserver(); +} + +/** + * @tc.name: UnregisterAppStateObserver001 + * @tc.desc: Test RegisterAppStateObserver + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level1) +{ + // GetSystemAbilityManager get failed + secCompService_->appStateObserver_ = nullptr; + sptr appProxy = new (std::nothrow) MockAppMgrProxy(nullptr); + secCompService_->iAppMgr_ = appProxy; + + EXPECT_CALL(*appProxy, UnregisterApplicationStateObserver(testing::_)).Times(testing::Exactly(0)); + secCompService_->UnregisterAppStateObserver(); +} + +/** + * @tc.name: GetCallerInfo001 + * @tc.desc: Test get caller info + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) +{ + // token id is shell + SecCompCallerInfo caller; + EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); + + // set token id to hap token, but uid is not in foreground + AccessTokenID selfTokenId = GetSelfTokenID(); + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); + + // add local uid to foreground. + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + EXPECT_TRUE(secCompService_->GetCallerInfo(caller)); + + SetSelfTokenID(selfTokenId); +} + +/** + * @tc.name: RegisterSecurityComponent001 + * @tc.desc: Test register security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) +{ + // get caller fail + int32_t scId; + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId), + SC_SERVICE_ERROR_VALUE_INVALID); + + // parse component json fail + AccessTokenID selfTokenId = GetSelfTokenID(); + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "{a=", scId), + SC_SERVICE_ERROR_VALUE_INVALID); // wrong json + + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), + SC_OK); + EXPECT_EQ(secCompService_->UpdateSecurityComponent(scId, BuildLocationComponentInfo()), + SC_OK); + struct SecCompClickEvent touch = { + .touchX = 100, + .touchY = 100, + .timestamp = GetCurrentTime() + }; + + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + SC_OK); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); + SetSelfTokenID(selfTokenId); +} + +/** + * @tc.name: UnregisterSecurityComponent001 + * @tc.desc: Test unregister security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) +{ + // get caller fail + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(TEST_SC_ID), SC_SERVICE_ERROR_VALUE_INVALID); +} + +/** + * @tc.name: UpdateSecurityComponent001 + * @tc.desc: Test update security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) +{ + // get caller fail + EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, ""), SC_SERVICE_ERROR_VALUE_INVALID); + + AccessTokenID selfTokenId = GetSelfTokenID(); + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, "{a"), SC_SERVICE_ERROR_VALUE_INVALID); + + SetSelfTokenID(selfTokenId); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent001 + * @tc.desc: Test update security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) +{ + // get caller fail + struct SecCompClickEvent touch = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = GetCurrentTime() + }; + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(TEST_SC_ID, BuildLocationComponentInfo(), touch), + SC_SERVICE_ERROR_VALUE_INVALID); + + AccessTokenID selfTokenId = GetSelfTokenID(); + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(TEST_SC_ID, "{a", touch), + SC_SERVICE_ERROR_VALUE_INVALID); + SetSelfTokenID(selfTokenId); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.h new file mode 100644 index 0000000..4786c06 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 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 SEC_COMP_SERVICE_TEST_H +#define SEC_COMP_SERVICE_TEST_H + +#include +#define private public +#include "sec_comp_service.h" +#undef private + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); + + sptr secCompService_ = nullptr; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_SERVICE_TEST_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp new file mode 100644 index 0000000..069b8e5 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_stub_test.h" +#include "sec_comp_log.h" +#include "sec_comp_err.h" +#include "sec_comp_click_event_parcel.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompStubTest"}; +} + +void SecCompStubTest::SetUpTestCase() +{} + +void SecCompStubTest::TearDownTestCase() +{} + +void SecCompStubTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); + if (stub_ != nullptr) { + return; + } + + stub_ = new (std::nothrow) SecCompStubMock(); + ASSERT_NE(nullptr, stub_); +} + +void SecCompStubTest::TearDown() +{ + stub_ = nullptr; +} + +/** + * @tc.name: OnRemoteRequest001 + * @tc.desc: Test on remote request + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + data.WriteInterfaceToken(u"wrong"); + ASSERT_EQ(stub_->OnRemoteRequest(static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT), + data, reply, option), SC_SERVICE_ERROR_IPC_REQUEST_FAIL); + + data.WriteInterfaceToken(u"ohos.security.ISecCompService"); + ASSERT_EQ(stub_->OnRemoteRequest(static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT), + data, reply, option), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteInterfaceToken(u"ohos.security.ISecCompService"); + ASSERT_EQ(stub_->OnRemoteRequest(0, data, reply, option), 0); +} + +/** + * @tc.name: RegisterSecurityComponentInner001 + * @tc.desc: Test register security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + + ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteUint32(UNKNOWN_SC_TYPE); + ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + + data.WriteUint32(MAX_SC_TYPE); + ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + + data.WriteUint32(LOCATION_COMPONENT); + ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteUint32(LOCATION_COMPONENT); + data.WriteString(""); + ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_OK); +} + +/** + * @tc.name: UpdateSecurityComponentInner001 + * @tc.desc: Test update security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + + ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteInt32(-1); + ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + + data.WriteInt32(1); + ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteInt32(1); + data.WriteString(""); + ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_OK); +} + +/** + * @tc.name: UnregisterSecurityComponentInner001 + * @tc.desc: Test unregister security component + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + ASSERT_EQ(stub_->UnregisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteInt32(-1); + ASSERT_EQ(stub_->UnregisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + + data.WriteInt32(1); + ASSERT_EQ(stub_->UnregisterSecurityComponentInner(data, reply), SC_OK); +} + +/** + * @tc.name: ReportSecurityComponentClickEventInner001 + * @tc.desc: Test report click event + * @tc.type: FUNC + * @tc.require:AR000HO9IN + */ +HWTEST_F(SecCompStubTest, ReportSecurityComponentClickEventInner001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteInt32(-1); + ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + + data.WriteInt32(1); + ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteInt32(1); + data.WriteString(""); + ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + + data.WriteInt32(1); + data.WriteString(""); + SecCompClickEvent touchInfo; + sptr parcel = new (std::nothrow) SecCompClickEventParcel(); + parcel->touchInfoParams_ = touchInfo; + data.WriteParcelable(parcel); + ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_OK); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h new file mode 100644 index 0000000..15a7bdb --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 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 SEC_COMP_STUB_TEST_H +#define SEC_COMP_STUB_TEST_H + +#include +#define private public +#include "sec_comp_stub.h" +#undef private + +namespace OHOS { +namespace Security { +namespace SecurityComponent { + +// stub is abstract class +struct SecCompStubMock : public SecCompStub { +public: + int32_t RegisterSecurityComponent(SecCompType type, + const std::string& componentInfo, int32_t& scId) override + { + return 0; + }; + + int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) override + { + return 0; + }; + + int32_t UnregisterSecurityComponent(int32_t scId) override + { + return 0; + }; + + int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, + const SecCompClickEvent& touchInfo) override + { + return 0; + }; +}; + +class SecCompStubTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); + + sptr stub_ = nullptr; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_STUB_TEST_H -- Gitee From e247fa808643b6760b1e928a4df5b7ca09afffa7 Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 14 Feb 2023 10:23:49 +0800 Subject: [PATCH 03/59] Description:modify code check Match-id-559428f79e96611d3b2102d4f818b4922c6e89e3 --- .../security_component/src/location_button.cpp | 2 +- .../sa/sa_main/sec_comp_entity.h | 2 +- .../sa/test/mock/include/iservice_registry.h | 3 +++ .../sa/test/mock/src/mock_iservice_registry.cpp | 14 ++++++++++++-- .../test/unittest/src/sec_comp_service_test.cpp | 15 +++++++++------ 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/frameworks/security_component/src/location_button.cpp b/frameworks/security_component/src/location_button.cpp index dca93c5..9e7b869 100644 --- a/frameworks/security_component/src/location_button.cpp +++ b/frameworks/security_component/src/location_button.cpp @@ -56,7 +56,7 @@ void LocationButton::FromJson(const nlohmann::json& jsonSrc) } if (jsonSrc.find(JSON_FONT_SIZE) != jsonSrc.end() && jsonSrc.at(JSON_FONT_SIZE).is_number()) { - fontSize_ = jsonSrc.at(JSON_FONT_SIZE).get(); + fontSize_ = jsonSrc.at(JSON_FONT_SIZE).get(); } if (jsonSrc.find(JSON_FONT_COLOR) != jsonSrc.end() && jsonSrc.at(JSON_FONT_COLOR).is_number()) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.h b/services/security_component_service/sa/sa_main/sec_comp_entity.h index d1e6a25..acd1feb 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.h +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.h @@ -65,7 +65,7 @@ public: private: std::shared_ptr componentInfo_; - bool isGrant_; + bool isGrant_ = false; AccessToken::AccessTokenID tokenId_; int32_t scId_; }; diff --git a/services/security_component_service/sa/test/mock/include/iservice_registry.h b/services/security_component_service/sa/test/mock/include/iservice_registry.h index 0556610..8a4aa3d 100644 --- a/services/security_component_service/sa/test/mock/include/iservice_registry.h +++ b/services/security_component_service/sa/test/mock/include/iservice_registry.h @@ -24,6 +24,9 @@ public: SystemAbilityManagerClient() = default; ~SystemAbilityManagerClient() = default; + static SystemAbilityManagerClient* clientInstance; + static SystemAbilityManagerClient defaultInstance; + static std::mutex instanceMtx; MOCK_METHOD0(GetSystemAbilityManager, sptr()); }; } // namespace OHOS diff --git a/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp b/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp index 486e0ee..410bca5 100644 --- a/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp +++ b/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp @@ -16,9 +16,19 @@ #include "iservice_registry.h" namespace OHOS { +SystemAbilityManagerClient* SystemAbilityManagerClient::clientInstance = nullptr; +SystemAbilityManagerClient SystemAbilityManagerClient::defaultInstance; +std::mutex SystemAbilityManagerClient::instanceMtx; + SystemAbilityManagerClient& SystemAbilityManagerClient::GetInstance() { - static auto instance = new SystemAbilityManagerClient(); - return *instance; + std::lock_guard lock(instanceMtx); + if (clientInstance == nullptr) { + clientInstance = new (std::nothrow)SystemAbilityManagerClient(); + if (clientInstance == nullptr) { + return defaultInstance; + } + } + return *clientInstance; } } // namespace OHOS diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 82355b3..2f08748 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -122,19 +122,21 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) { // GetSystemAbilityManager get failed secCompService_->appStateObserver_ = nullptr; - SystemAbilityManagerClient& saClient = SystemAbilityManagerClient::GetInstance(); - EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr)); + std::shared_ptr saClient = std::make_shared(); + ASSERT_NE(saClient, nullptr); + SystemAbilityManagerClient::clientInstance = saClient.get(); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr)); EXPECT_FALSE(secCompService_->RegisterAppStateObserver()); // GetSystemAbility get app mgr failed secCompService_->appStateObserver_ = nullptr; SystemAbilityManagerProxy proxy(nullptr); - EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); EXPECT_FALSE(secCompService_->RegisterAppStateObserver()); // RegisterApplicationStateObserver failed secCompService_->appStateObserver_ = nullptr; - EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); MockIRemoteObject object; EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); sptr appProxy = new (std::nothrow) MockAppMgrProxy(nullptr); @@ -145,7 +147,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) // GetForegroundApplications failed secCompService_->appStateObserver_ = nullptr; - EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0)); @@ -156,7 +158,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) // get one foreground app secCompService_->appStateObserver_ = nullptr; - EXPECT_CALL(saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0)); @@ -170,6 +172,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) EXPECT_TRUE(secCompService_->RegisterAppStateObserver()); EXPECT_EQ(secCompService_->appStateObserver_->foregrandProcList_.size(), static_cast(1)); secCompService_->UnregisterAppStateObserver(); + SystemAbilityManagerClient::clientInstance = nullptr; } /** -- Gitee From ed71939e029a279e7a032a7c2de695efad4c0b5e Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 15 Feb 2023 10:42:59 +0800 Subject: [PATCH 04/59] Description:add unit test compile Match-id-c0f88eee1520c82f86a9c5276459781eb2bd83ab --- bundle.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bundle.json b/bundle.json index 355285c..4f09985 100644 --- a/bundle.json +++ b/bundle.json @@ -58,6 +58,9 @@ "header_base": "//base/security/security_component/interfaces/inner_api/security_component/include" } } + ], + "test": [ + "//base/security/security_component:security_component_build_module_test" ] } } -- Gitee From d51746684ffaae7bdc8a0b7f32f272b037e3db77 Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 27 Feb 2023 21:22:56 +0800 Subject: [PATCH 05/59] Description:add hmac sign Match-id-999482e1ce251a2439947866f24b1fc978977d9f --- BUILD.gn | 2 + frameworks/BUILD.gn | 29 ++- frameworks/common/include/sec_comp_err.h | 4 + .../include/sec_comp_enhance_adapter.h | 68 +++++++ .../src/sec_comp_enhance_adapter.cpp | 140 +++++++++++++++ interfaces/inner_api/enhance_kits/BUILD.gn | 57 ++++++ .../include/sec_comp_enhance_cfg_parcel.h | 37 ++++ .../include/sec_comp_enhance_kit.h | 30 ++++ .../src/sec_comp_enhance_cfg_parcel.cpp | 61 +++++++ .../enhance_kits/src/sec_comp_enhance_kit.cpp | 32 ++++ .../inner_api/enhance_kits/test/BUILD.gn | 49 +++++ .../unittest/src/sec_comp_enhance_test.cpp | 170 ++++++++++++++++++ .../test/unittest/src/sec_comp_enhance_test.h | 38 ++++ .../unittest/src/location_button_test.cpp | 12 +- .../test/unittest/src/sec_comp_base_test.cpp | 12 +- security_component.gni | 2 +- .../security_component_service/sa/BUILD.gn | 2 + .../sa/sa_main/sec_comp_manager.cpp | 25 ++- .../sa/sa_main/sec_comp_manager.h | 1 + .../sa/test/BUILD.gn | 2 + .../unittest/src/app_state_observer_test.cpp | 10 +- .../unittest/src/sec_comp_entity_test.cpp | 8 +- .../src/sec_comp_info_helper_test.cpp | 4 +- .../unittest/src/sec_comp_manager_test.cpp | 22 +-- .../unittest/src/sec_comp_service_test.cpp | 16 +- .../test/unittest/src/sec_comp_stub_test.cpp | 10 +- 26 files changed, 791 insertions(+), 52 deletions(-) create mode 100644 frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h create mode 100644 frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp create mode 100644 interfaces/inner_api/enhance_kits/BUILD.gn create mode 100644 interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h create mode 100644 interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h create mode 100644 interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp create mode 100644 interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp create mode 100644 interfaces/inner_api/enhance_kits/test/BUILD.gn create mode 100644 interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp create mode 100644 interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h diff --git a/BUILD.gn b/BUILD.gn index 2dbefbe..1ebfda6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -18,6 +18,7 @@ group("security_component_build_module") { if (is_standard_system) { deps = [ "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits:libsecurity_component_enhance_sdk", "${sec_comp_root_dir}/services/security_component_service/sa:security_component_service", "${sec_comp_root_dir}/services/security_component_service/sa/sa_profile:security_component_sa_profile_standard", ] @@ -30,6 +31,7 @@ group("security_component_build_module_test") { if (is_standard_system) { deps += [ "${sec_comp_root_dir}/interfaces/inner_api/security_component/test:unittest", + "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits/test:unittest", "${sec_comp_root_dir}/services/security_component_service/sa/test:unittest", ] } diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 08be8de..9542fee 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -20,7 +20,6 @@ ohos_shared_library("libsecurity_component_framework") { output_name = "libsecurity_component_framework" include_dirs = [ - "include", "${sec_comp_root_dir}/frameworks/common/include", "${sec_comp_root_dir}/frameworks/security_component/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", @@ -44,3 +43,31 @@ ohos_shared_library("libsecurity_component_framework") { cflags_cc = [ "-DHILOG_ENABLE" ] } + +ohos_shared_library("libsecurity_component_enhance_adapter") { + subsystem_name = "security" + part_name = "security_component" + output_name = "libsecurity_component_enhance_adapter" + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + ] + + sources = [ + "enhance_adapter/src/sec_comp_enhance_adapter.cpp", + ] + + configs = [ "//base/security/security_component/config:coverage_flags" ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + if (current_cpu == "arm64") { + defines = [ "_ARM64_" ] + } +} diff --git a/frameworks/common/include/sec_comp_err.h b/frameworks/common/include/sec_comp_err.h index e16edc5..8ded864 100644 --- a/frameworks/common/include/sec_comp_err.h +++ b/frameworks/common/include/sec_comp_err.h @@ -33,6 +33,10 @@ enum SCErrCode : int32_t { SC_SERVICE_ERROR_PERMISSION_OPER_FAIL = -59, SC_SERVICE_ERROR_CLICK_EVENT_INVALID = -60, SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL = -61, + + SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE = -100, + SC_ENHANCE_ERROR_VALUE_INVALID = -101, + SC_ENHANCE_ERROR_OPER_FAIL = -102, }; } // namespace SecurityComponent } // namespace Security diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h new file mode 100644 index 0000000..33fdf07 --- /dev/null +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_ENHANCE_ADAPTER_H +#define SECURITY_COMPONENT_ENHANCE_ADAPTER_H +#include +#include "parcel.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +enum EnhanceInterfaceType { + SEC_COMP_ENHANCE_INPUT_INTERFACE = 0, + SEC_COMP_ENHANCE_SRV_INTERFACE = 1, +}; + +class SecCompEnhanceCfgBase { +}; + +class SecCompEnhanceInputInterface { +public: + virtual bool MarshallEnhanceCfg(SecCompEnhanceCfgBase* cfg, Parcel& out) = 0; + virtual SecCompEnhanceCfgBase* UnmarshallEnhanceCfg(Parcel& in) = 0; + virtual int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) = 0; + virtual int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, + uint8_t** enhanceData, uint32_t& enHancedataLen) = 0; +}; + +class SecCompEnhanceServiceInterface { +public: + virtual int32_t EnableInputEnhance() = 0; + virtual int32_t DisableInputEnhance() = 0; +}; + +struct SecCompEnhanceAdapter { + static void InitEnhanceHandler(EnhanceInterfaceType type); + static int32_t MarshallEnhanceCfg(SecCompEnhanceCfgBase* cfg, Parcel& out); + static int32_t UnmarshallEnhanceCfg(Parcel& in, SecCompEnhanceCfgBase*& result); + static int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); + static int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, + uint8_t** enhanceData, uint32_t& enHancedataLen); + + static int32_t EnableInputEnhance(); + static int32_t DisableInputEnhance(); + + static SecCompEnhanceInputInterface* inputHandler; + static bool isEnhanceInputHandlerInit; + + static SecCompEnhanceServiceInterface* srvHandler; + static bool isEnhanceSrvHandlerInit; + + static std::mutex initMtx; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_ENHANCE_ADAPTER_H diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp new file mode 100644 index 0000000..81dd8cf --- /dev/null +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_enhance_adapter.h" +#include +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceAdapter"}; + +#ifdef _ARM64_ +static const std::string LIB_PATH = "/system/lib64/"; +#else +static const std::string LIB_PATH = "/system/lib/"; +#endif +static const std::string ENHANCE_INPUT_INTERFACE_LIB = LIB_PATH + "libsec_comp_input_enhance.z.so"; +static const std::string ENHANCE_SRV_INTERFACE_LIB = LIB_PATH + "libsec_comp_service_enhance.z.so"; +} + +SecCompEnhanceInputInterface* SecCompEnhanceAdapter::inputHandler = nullptr; +bool SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + +SecCompEnhanceServiceInterface* SecCompEnhanceAdapter::srvHandler = nullptr; +bool SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false; + +std::mutex SecCompEnhanceAdapter::initMtx; + +void SecCompEnhanceAdapter::InitEnhanceHandler(EnhanceInterfaceType type) +{ + std::unique_lock lck(initMtx); + std::string libPath = ""; + switch (type) { + case SEC_COMP_ENHANCE_INPUT_INTERFACE: + libPath = ENHANCE_INPUT_INTERFACE_LIB; + isEnhanceInputHandlerInit = true; + break; + case SEC_COMP_ENHANCE_SRV_INTERFACE: + libPath = ENHANCE_SRV_INTERFACE_LIB; + isEnhanceSrvHandlerInit = true; + break; + default: + break; + } + if (dlopen(libPath.c_str(), RTLD_LAZY) == nullptr) { + SC_LOG_ERROR(LABEL, "init enhance lib %{public}s failed", libPath.c_str()); + } else { + SC_LOG_INFO(LABEL, "init enhance lib %{public}s ok", libPath.c_str()); + } +} + +int32_t SecCompEnhanceAdapter::MarshallEnhanceCfg(SecCompEnhanceCfgBase* cfg, Parcel& out) +{ + if (!isEnhanceInputHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); + } + if (inputHandler != nullptr) { + if (!inputHandler->MarshallEnhanceCfg(cfg, out)) { + return SC_ENHANCE_ERROR_OPER_FAIL; + } else { + return SC_OK; + } + } + return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; +} + +int32_t SecCompEnhanceAdapter::UnmarshallEnhanceCfg(Parcel& in, SecCompEnhanceCfgBase*& result) +{ + if (!isEnhanceInputHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); + } + if (inputHandler != nullptr) { + result = inputHandler->UnmarshallEnhanceCfg(in); + return SC_OK; + } + return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; +} + +int32_t SecCompEnhanceAdapter::SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) +{ + if (!isEnhanceInputHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); + } + if (inputHandler != nullptr) { + return inputHandler->SetEnhanceCfg(cfg); + } + return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; +} + +int32_t SecCompEnhanceAdapter::GetPointerEventEnhanceData(void *data, uint32_t dataLen, + uint8_t** enhanceData, uint32_t& enHancedataLen) +{ + if (!isEnhanceInputHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); + } + if (inputHandler != nullptr) { + return inputHandler->GetPointerEventEnhanceData(data, dataLen, enhanceData, enHancedataLen); + } + return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; +} + +int32_t SecCompEnhanceAdapter::EnableInputEnhance() +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + return srvHandler->EnableInputEnhance(); + } + return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; +} + +int32_t SecCompEnhanceAdapter::DisableInputEnhance() +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + return srvHandler->DisableInputEnhance(); + } + return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn new file mode 100644 index 0000000..8e53707 --- /dev/null +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -0,0 +1,57 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/ohos.gni") + +config("sec_comp_enhance_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits/include" + ] +} + +ohos_shared_library("libsecurity_component_enhance_sdk") { + subsystem_name = "security" + part_name = "security_component" + output_name = "libsecurity_component_enhance_sdk" + + public_configs = [ ":sec_comp_enhance_config" ] + + include_dirs = [ + "include", + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + ] + + sources = [ + "src/sec_comp_enhance_cfg_parcel.cpp", + "src/sec_comp_enhance_kit.cpp" + ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter" + ] + + configs = [ "//base/security/security_component/config:coverage_flags" ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] +} diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h new file mode 100644 index 0000000..08f5eee --- /dev/null +++ b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_ENHANCE_CFG_PARCEL_H +#define SECURITY_COMPONENT_ENHANCE_CFG_PARCEL_H +#include "parcel.h" +#include "sec_comp_enhance_adapter.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +struct SecCompEnhanceCfgParcel final : public Parcelable { + SecCompEnhanceCfgParcel(); + + ~SecCompEnhanceCfgParcel() override; + + bool Marshalling(Parcel& out) const override; + + static SecCompEnhanceCfgParcel* Unmarshalling(Parcel& in); + + SecCompEnhanceCfgBase* cfg_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_ENHANCE_CFG_PARCEL_H \ No newline at end of file diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h new file mode 100644 index 0000000..78fdb3b --- /dev/null +++ b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_ENHANCE_KITS_H +#define SECURITY_COMPONENT_ENHANCE_KITS_H +#include "sec_comp_enhance_adapter.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +struct SecCompEnhanceKit { + static int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); + static int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, + uint8_t** enhanceData, uint32_t& enHancedataLen); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_ENHANCE_KITS_H \ No newline at end of file diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp new file mode 100644 index 0000000..343e6bc --- /dev/null +++ b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_enhance_cfg_parcel.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceCfgParcel"}; +} + +SecCompEnhanceCfgParcel::SecCompEnhanceCfgParcel() +{ + cfg_ = nullptr; +} + +SecCompEnhanceCfgParcel::~SecCompEnhanceCfgParcel() +{ + if (cfg_ != nullptr) { + delete cfg_; + } +} + +bool SecCompEnhanceCfgParcel::Marshalling(Parcel& out) const +{ + return (SecCompEnhanceAdapter::MarshallEnhanceCfg(cfg_, out) != SC_OK); +} + +SecCompEnhanceCfgParcel* SecCompEnhanceCfgParcel::Unmarshalling(Parcel& in) +{ + auto* SecCompCfgParcel = new (std::nothrow) SecCompEnhanceCfgParcel(); + if (SecCompCfgParcel == nullptr) { + SC_LOG_ERROR(LABEL, "New SecCompCfgParcel failed"); + return nullptr; + } + + if (SecCompEnhanceAdapter::UnmarshallEnhanceCfg(in, SecCompCfgParcel->cfg_) != SC_OK) { + SC_LOG_ERROR(LABEL, "Unmarshall failed"); + delete SecCompCfgParcel; + return nullptr; + } + return SecCompCfgParcel; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp new file mode 100644 index 0000000..99eb415 --- /dev/null +++ b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_enhance_kit.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +int32_t SecCompEnhanceKit::SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) +{ + return SecCompEnhanceAdapter::SetEnhanceCfg(cfg); +} + +int32_t SecCompEnhanceKit::GetPointerEventEnhanceData(void *data, uint32_t dataLen, + uint8_t** enhanceData, uint32_t& enHancedataLen) +{ + return SecCompEnhanceAdapter::GetPointerEventEnhanceData(data, dataLen, enhanceData, enHancedataLen); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn new file mode 100644 index 0000000..15e0e1a --- /dev/null +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 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. + +import("//base/security/security_component/security_component.gni") +import("//build/test.gni") + +ohos_unittest("sec_comp_enhance_sdk_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "unittest/src", + ] + + sources = [ + "unittest/src/sec_comp_enhance_test.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", + "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits:libsecurity_component_enhance_sdk", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + if (current_cpu == "arm64") { + defines = [ "_ARM64_" ] + } +} + +group("unittest") { + testonly = true + deps = [ ":sec_comp_enhance_sdk_test" ] +} diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp new file mode 100644 index 0000000..49c5649 --- /dev/null +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_enhance_test.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include + +using namespace testing::ext; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceTest"}; +static bool g_inputEnhanceExist = false; +static bool g_srvEnhanceExist = false; +#ifdef _ARM64_ +static const std::string LIB_PATH = "/system/lib64/"; +#else +static const std::string LIB_PATH = "/system/lib/"; +#endif +static const std::string ENHANCE_INPUT_INTERFACE_LIB = LIB_PATH + "libsec_comp_input_enhance.z.so"; +static const std::string ENHANCE_SRV_INTERFACE_LIB = LIB_PATH + "libsec_comp_service_enhance.z.so"; +} // namespace + +void SecCompEnhanceTest::SetUpTestCase() +{ + if (access(ENHANCE_INPUT_INTERFACE_LIB.c_str(), F_OK) == 0) { + g_inputEnhanceExist = true; + } + + if (access(ENHANCE_SRV_INTERFACE_LIB.c_str(), F_OK) == 0) { + g_srvEnhanceExist = true; + } + + SC_LOG_INFO(LABEL, "SetUpTestCase."); +} + +void SecCompEnhanceTest::TearDownTestCase() +{ + SC_LOG_INFO(LABEL, "TearDownTestCase."); +} + +void SecCompEnhanceTest::SetUp() +{ + SC_LOG_INFO(LABEL, "SetUp ok."); +} + +void SecCompEnhanceTest::TearDown() +{ + SC_LOG_INFO(LABEL, "TearDown."); +} + +/** + * @tc.name: SetEnhanceCfg001 + * @tc.desc: test SetEnhanceCfg + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, SetEnhanceCfg001, TestSize.Level1) +{ + uint8_t cfgData[16] = { 0 }; + int32_t result = SecCompEnhanceKit::SetEnhanceCfg(reinterpret_cast(&cfgData)); + if (g_inputEnhanceExist) { + EXPECT_EQ(result, SC_OK); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} + +/** + * @tc.name: GetPoniterEventEnhanceData001 + * @tc.desc: test GetPoniterEventEnhanceData + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, GetPoniterEventEnhanceData001, TestSize.Level1) +{ + uint8_t originData[16] = { 0 }; + uint32_t dataLen = 16; + uint8_t* enhanceData = nullptr; + uint32_t enHancedataLen = 0; + + int32_t result = SecCompEnhanceKit::GetPointerEventEnhanceData(originData, dataLen, &enhanceData, enHancedataLen); + if (g_inputEnhanceExist) { + EXPECT_EQ(result, SC_OK); + EXPECT_EQ(enHancedataLen, static_cast(0)); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} + +/** + * @tc.name: MarshallEnhanceCfg001 + * @tc.desc: test MarshallEnhanceCfg + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, MarshallEnhanceCfg001, TestSize.Level1) +{ + OHOS::Parcel out; + int32_t result = SecCompEnhanceAdapter::MarshallEnhanceCfg(nullptr, out); + if (g_inputEnhanceExist) { + EXPECT_EQ(result, SC_ENHANCE_ERROR_OPER_FAIL); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} + +/** + * @tc.name: UnmarshallEnhanceCfg001 + * @tc.desc: test UnmarshallEnhanceCfg + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, UnmarshallEnhanceCfg001, TestSize.Level1) +{ + OHOS::Parcel in; + SecCompEnhanceCfgBase* cfg = nullptr; + int32_t result = SecCompEnhanceAdapter::UnmarshallEnhanceCfg(in, cfg); + if (g_inputEnhanceExist) { + EXPECT_EQ(result, SC_OK); + EXPECT_EQ(cfg, nullptr); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} + +/** + * @tc.name: EnableInputEnhance001 + * @tc.desc: test UnmarshallEnhanceCfg + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, EnableInputEnhance001, TestSize.Level1) +{ + int32_t result = SecCompEnhanceAdapter::EnableInputEnhance(); + if (g_srvEnhanceExist) { + EXPECT_EQ(result, SC_OK); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} + +/** + * @tc.name: DisableInputEnhance001 + * @tc.desc: test DisableInputEnhance + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, DisableInputEnhance001, TestSize.Level1) +{ + int32_t result = SecCompEnhanceAdapter::DisableInputEnhance(); + if (g_srvEnhanceExist) { + EXPECT_EQ(result, SC_OK); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h new file mode 100644 index 0000000..deff0d4 --- /dev/null +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_ENHANCE_KITS_TEST +#define SECURITY_COMPONENT_ENHANCE_KITS_TEST + +#include +#include "sec_comp_enhance_adapter.h" +#include "sec_comp_enhance_kit.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompEnhanceTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_ENHANCE_KITS_TEST \ No newline at end of file diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 69995b9..2e59ae2 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -80,7 +80,7 @@ void LocationButtonTest::TearDown() * @tc.name: FromJson001 * @tc.desc: Test location button from json success * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { @@ -110,7 +110,7 @@ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) * @tc.name: FromJson002 * @tc.desc: Test empty location button from json * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) { @@ -124,7 +124,7 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) * @tc.name: FromJson003 * @tc.desc: Test location button from wrong type params json * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) { @@ -158,7 +158,7 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) * @tc.name: FromJson004 * @tc.desc: Test location button from wrong value params json * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) { @@ -188,7 +188,7 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) * @tc.name: IsValid001 * @tc.desc: Test check location button invalid * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(LocationButtonTest, IsValid001, TestSize.Level1) { @@ -223,7 +223,7 @@ HWTEST_F(LocationButtonTest, IsValid001, TestSize.Level1) * @tc.name: CompareLocationButton001 * @tc.desc: Test compare location button * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) { diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp index 64dc1f1..4222269 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp @@ -54,7 +54,7 @@ void SecCompBaseTest::TearDown() * @tc.name: FromJson001 * @tc.desc: Test SecCompBase from json success * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompBaseTest, FromJson001, TestSize.Level1) { @@ -77,7 +77,7 @@ HWTEST_F(SecCompBaseTest, FromJson001, TestSize.Level1) * @tc.name: FromJson002 * @tc.desc: Test empty SecCompBase from json * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompBaseTest, FromJson002, TestSize.Level1) { @@ -91,7 +91,7 @@ HWTEST_F(SecCompBaseTest, FromJson002, TestSize.Level1) * @tc.name: FromJson003 * @tc.desc: Test location button from wrong type json * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompBaseTest, FromJson003, TestSize.Level1) { @@ -110,7 +110,7 @@ HWTEST_F(SecCompBaseTest, FromJson003, TestSize.Level1) * @tc.name: FromJson004 * @tc.desc: Test location button from wrong rect json * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompBaseTest, FromJson004, TestSize.Level1) { @@ -164,7 +164,7 @@ HWTEST_F(SecCompBaseTest, FromJson004, TestSize.Level1) * @tc.name: IsValid001 * @tc.desc: Test SecCompBase invalid * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompBaseTest, IsValid001, TestSize.Level1) { @@ -181,7 +181,7 @@ HWTEST_F(SecCompBaseTest, IsValid001, TestSize.Level1) * @tc.name: Compare001 * @tc.desc: Test SecCompBase compare * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompBaseTest, Compare001, TestSize.Level1) { diff --git a/security_component.gni b/security_component.gni index 8e38353..310d1c6 100644 --- a/security_component.gni +++ b/security_component.gni @@ -11,4 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -sec_comp_root_dir = "//base/security/security_component" +sec_comp_root_dir = "//base/security/security_component" \ No newline at end of file diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 7453c49..829b527 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -28,6 +28,7 @@ ohos_shared_library("security_component_service") { include_dirs = [ "sa_main", "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", "${sec_comp_root_dir}/frameworks/security_component/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", ] @@ -46,6 +47,7 @@ ohos_shared_library("security_component_service") { deps = [ ":security_component_service.rc", + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", ] diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index b8e963c..53020fe 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -15,11 +15,12 @@ #include "sec_comp_manager.h" +#include "i_sec_comp_service.h" +#include "iservice_registry.h" +#include "sec_comp_enhance_adapter.h" #include "sec_comp_err.h" #include "sec_comp_info_helper.h" #include "sec_comp_log.h" -#include "iservice_registry.h" -#include "i_sec_comp_service.h" namespace OHOS { namespace Security { @@ -33,6 +34,7 @@ static constexpr int32_t MAX_INT_NUM = 0x7fffffff; SecCompManager::SecCompManager() { scIdStart_ = SC_ID_START; + scCount_ = 0; SC_LOG_INFO(LABEL, "SecCompManager()"); } @@ -64,6 +66,10 @@ int32_t SecCompManager::AddProcessComponent(std::vector& componen } componentList.emplace_back(newEntity); + if (scCount_ == 0) { + SecCompEnhanceAdapter::EnableInputEnhance(); + } + scCount_++; return SC_OK; } @@ -94,6 +100,7 @@ int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t uid, int32_t scI if (it->GetScId() == scId) { it->RevokeTempPermission(); list.erase(it); + scCount_--; return SC_OK; } } @@ -128,8 +135,12 @@ void SecCompManager::NotifyProcessBackground(int32_t uid) std::vector& list = iter->second; for (auto it = list.begin(); it != list.end(); ++it) { it->RevokeTempPermission(); + scCount_--; } list.clear(); + if (scCount_ <= 0) { + SecCompEnhanceAdapter::DisableInputEnhance(); + } } void SecCompManager::NotifyProcessDied(int32_t uid) @@ -143,8 +154,12 @@ void SecCompManager::NotifyProcessDied(int32_t uid) std::vector& list = iter->second; for (auto it = list.begin(); it != list.end(); ++it) { it->RevokeTempPermission(); + scCount_--; } list.clear(); + if (scCount_ <= 0) { + SecCompEnhanceAdapter::DisableInputEnhance(); + } componentMap_.erase(uid); ExitSaAfterAllProcessDie(); } @@ -214,7 +229,11 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC SC_LOG_ERROR(LABEL, "ScId is invalid"); return SC_SERVICE_ERROR_VALUE_INVALID; } - return DeleteSecurityComponentFromList(caller.uid, scId); + int32_t res = DeleteSecurityComponentFromList(caller.uid, scId); + if (res == SC_OK && scCount_ <= 0) { + SecCompEnhanceAdapter::DisableInputEnhance(); + } + return res; } int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index 143b38c..d02dffa 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -69,6 +69,7 @@ private: OHOS::Utils::RWLock componentInfoLock_; std::mutex scIdMtx_; std::unordered_map> componentMap_; + int32_t scCount_; int32_t scIdStart_; bool Initialize() const; diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index dce6264..78db19d 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -21,6 +21,7 @@ ohos_unittest("sec_comp_service_test") { include_dirs = [ "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", "${sec_comp_root_dir}/frameworks/security_component/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", @@ -48,6 +49,7 @@ ohos_unittest("sec_comp_service_test") { cflags_cc = [ "-DHILOG_ENABLE" ] deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", "//third_party/googletest:gmock_main", ] diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp index 5c57ee6..36d9db2 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -54,7 +54,7 @@ void AppStateObserverTest::TearDown() * @tc.name: IsProcessForeground001 * @tc.desc: Test is process foreground * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) { @@ -70,7 +70,7 @@ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) * @tc.name: AddProcessToForegroundSet001 * @tc.desc: Test add process to foreground * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) { @@ -93,7 +93,7 @@ HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) * @tc.name: RemoveProcessFromForegroundSet001 * @tc.desc: Test remove process from foreground * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level1) { @@ -111,7 +111,7 @@ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level * @tc.name: OnProcessStateChanged001 * @tc.desc: Test recieve process state changed * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) { @@ -135,7 +135,7 @@ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) * @tc.name: OnProcessDied001 * @tc.desc: Test recieve process state died * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(AppStateObserverTest, OnProcessDied001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index b254c45..e4a2609 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -60,7 +60,7 @@ void SecCompEntityTest::TearDown() * @tc.name: RevokeTempPermission001 * @tc.desc: Test revoke temp permission * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompEntityTest, RevokeTempPermission001, TestSize.Level1) { @@ -82,7 +82,7 @@ HWTEST_F(SecCompEntityTest, RevokeTempPermission001, TestSize.Level1) * @tc.name: GrantTempPermission001 * @tc.desc: Test grant permission * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level1) { @@ -104,7 +104,7 @@ HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level1) * @tc.name: CheckTouchInfo001 * @tc.desc: Test touch info * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) { @@ -136,7 +136,7 @@ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) * @tc.name: IsRectOverlaped001 * @tc.desc: Test check rect overlap * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompEntityTest, IsRectOverlaped001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 5ac58db..ce83267 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -69,7 +69,7 @@ void SecCompInfoHelperTest::TearDown() * @tc.name: ParseComponent001 * @tc.desc: Test parse component info * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) { @@ -88,7 +88,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) * @tc.name: CompareSecCompInfo001 * @tc.desc: Test compare sec comp info * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompInfoHelperTest, CompareSecCompInfo001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index ebd178d..9c16099 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -104,7 +104,7 @@ void SecCompManagerTest::TearDown() * @tc.name: CreateScId001 * @tc.desc: Test create sc id * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) { @@ -119,7 +119,7 @@ HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) * @tc.name: AddProcessComponent001 * @tc.desc: Test add process component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, AddProcessComponent001, TestSize.Level1) { @@ -148,7 +148,7 @@ HWTEST_F(SecCompManagerTest, AddProcessComponent001, TestSize.Level1) * @tc.name: AddSecurityComponentToList001 * @tc.desc: Test add security component to list * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) { @@ -178,7 +178,7 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) * @tc.name: DeleteSecurityComponentFromList001 * @tc.desc: Test delete security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1) { @@ -204,7 +204,7 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 * @tc.name: GetSecurityComponentFromList001 * @tc.desc: Test get security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) { @@ -226,7 +226,7 @@ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) * @tc.name: NotifyProcessBackground001 * @tc.desc: Test notify process background * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) { @@ -249,7 +249,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) * @tc.name: NotifyProcessDied001 * @tc.desc: Test notify process died * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) { @@ -282,7 +282,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) * @tc.name: RegisterSecurityComponent001 * @tc.desc: Test register security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) { @@ -312,7 +312,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) * @tc.name: UpdateSecurityComponent001 * @tc.desc: Test update security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) { @@ -340,7 +340,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) * @tc.name: UnregisterSecurityComponent001 * @tc.desc: Test unregister security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) { @@ -357,7 +357,7 @@ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) * @tc.name: ReportSecurityComponentClickEvent001 * @tc.desc: Test report security component click * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 2f08748..35e45d1 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -93,7 +93,7 @@ void SecCompServiceTest::TearDown() * @tc.name: Onstart001 * @tc.desc: Test OnStart * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1) { @@ -116,7 +116,7 @@ HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1) * @tc.name: RegisterAppStateObserver001 * @tc.desc: Test RegisterAppStateObserver * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) { @@ -179,7 +179,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) * @tc.name: UnregisterAppStateObserver001 * @tc.desc: Test RegisterAppStateObserver * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level1) { @@ -196,7 +196,7 @@ HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level1) * @tc.name: GetCallerInfo001 * @tc.desc: Test get caller info * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) { @@ -223,7 +223,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) * @tc.name: RegisterSecurityComponent001 * @tc.desc: Test register security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) { @@ -263,7 +263,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) * @tc.name: UnregisterSecurityComponent001 * @tc.desc: Test unregister security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) { @@ -275,7 +275,7 @@ HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) * @tc.name: UpdateSecurityComponent001 * @tc.desc: Test update security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) { @@ -297,7 +297,7 @@ HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) * @tc.name: ReportSecurityComponentClickEvent001 * @tc.desc: Test update security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp index 069b8e5..cb36e3c 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp @@ -53,7 +53,7 @@ void SecCompStubTest::TearDown() * @tc.name: OnRemoteRequest001 * @tc.desc: Test on remote request * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) { @@ -77,7 +77,7 @@ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) * @tc.name: RegisterSecurityComponentInner001 * @tc.desc: Test register security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level1) { @@ -104,7 +104,7 @@ HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level1) * @tc.name: UpdateSecurityComponentInner001 * @tc.desc: Test update security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level1) { @@ -128,7 +128,7 @@ HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level1) * @tc.name: UnregisterSecurityComponentInner001 * @tc.desc: Test unregister security component * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level1) { @@ -147,7 +147,7 @@ HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level1) * @tc.name: ReportSecurityComponentClickEventInner001 * @tc.desc: Test report click event * @tc.type: FUNC - * @tc.require:AR000HO9IN + * @tc.require: */ HWTEST_F(SecCompStubTest, ReportSecurityComponentClickEventInner001, TestSize.Level1) { -- Gitee From e15f08634ad5f7565eb2b05019756f16641475de Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 21 Mar 2023 15:40:21 +0800 Subject: [PATCH 06/59] Description:modify handle unknown code request Match-id-cca4eb8024af0723841d02f9b015cf4c0ded4498 --- .../security_component_service/sa/sa_main/sec_comp_stub.cpp | 4 +--- .../sa/test/unittest/src/sec_comp_stub_test.cpp | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index 4d8b32f..4790b7b 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -41,11 +41,9 @@ int32_t SecCompStub::OnRemoteRequest( auto requestFunc = itFunc->second; if (requestFunc != nullptr) { return (this->*requestFunc)(data, reply); - } else { - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } } - return SC_OK; + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } int32_t SecCompStub::RegisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply) diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp index cb36e3c..88f565c 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp @@ -70,7 +70,7 @@ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) data, reply, option), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); data.WriteInterfaceToken(u"ohos.security.ISecCompService"); - ASSERT_EQ(stub_->OnRemoteRequest(0, data, reply, option), 0); + ASSERT_EQ(stub_->OnRemoteRequest(0, data, reply, option), 305); } /** -- Gitee From beecb9998d2058a0c73b27d2b30608d37df0f64a Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 29 Mar 2023 20:55:53 +0800 Subject: [PATCH 07/59] Description:modify code check Match-id-d3f33c58f94cefdabc00ecf261aa1b4cc3178acf --- .../enhance_adapter/include/sec_comp_enhance_adapter.h | 4 ++-- frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp | 2 +- .../inner_api/enhance_kits/include/sec_comp_enhance_kit.h | 2 +- .../inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp | 2 +- .../test/unittest/src/sec_comp_enhance_test.cpp | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index 33fdf07..c3defd6 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -34,7 +34,7 @@ public: virtual SecCompEnhanceCfgBase* UnmarshallEnhanceCfg(Parcel& in) = 0; virtual int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) = 0; virtual int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, - uint8_t** enhanceData, uint32_t& enHancedataLen) = 0; + uint8_t* enhanceData, uint32_t& enHancedataLen) = 0; }; class SecCompEnhanceServiceInterface { @@ -49,7 +49,7 @@ struct SecCompEnhanceAdapter { static int32_t UnmarshallEnhanceCfg(Parcel& in, SecCompEnhanceCfgBase*& result); static int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); static int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, - uint8_t** enhanceData, uint32_t& enHancedataLen); + uint8_t* enhanceData, uint32_t& enHancedataLen); static int32_t EnableInputEnhance(); static int32_t DisableInputEnhance(); diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index 81dd8cf..57fcdbf 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -103,7 +103,7 @@ int32_t SecCompEnhanceAdapter::SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) } int32_t SecCompEnhanceAdapter::GetPointerEventEnhanceData(void *data, uint32_t dataLen, - uint8_t** enhanceData, uint32_t& enHancedataLen) + uint8_t* enhanceData, uint32_t& enHancedataLen) { if (!isEnhanceInputHandlerInit) { InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h index 78fdb3b..5c7ee04 100644 --- a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h +++ b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h @@ -22,7 +22,7 @@ namespace SecurityComponent { struct SecCompEnhanceKit { static int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); static int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, - uint8_t** enhanceData, uint32_t& enHancedataLen); + uint8_t* enhanceData, uint32_t& enHancedataLen); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp index 99eb415..2a88edb 100644 --- a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp +++ b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp @@ -23,7 +23,7 @@ int32_t SecCompEnhanceKit::SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) } int32_t SecCompEnhanceKit::GetPointerEventEnhanceData(void *data, uint32_t dataLen, - uint8_t** enhanceData, uint32_t& enHancedataLen) + uint8_t* enhanceData, uint32_t& enHancedataLen) { return SecCompEnhanceAdapter::GetPointerEventEnhanceData(data, dataLen, enhanceData, enHancedataLen); } diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index 49c5649..000aa1d 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -32,6 +32,7 @@ static const std::string LIB_PATH = "/system/lib/"; #endif static const std::string ENHANCE_INPUT_INTERFACE_LIB = LIB_PATH + "libsec_comp_input_enhance.z.so"; static const std::string ENHANCE_SRV_INTERFACE_LIB = LIB_PATH + "libsec_comp_service_enhance.z.so"; +static constexpr uint32_t MAX_HMAC_SIZE = 64; } // namespace void SecCompEnhanceTest::SetUpTestCase() @@ -90,12 +91,11 @@ HWTEST_F(SecCompEnhanceTest, GetPoniterEventEnhanceData001, TestSize.Level1) uint8_t originData[16] = { 0 }; uint32_t dataLen = 16; uint8_t* enhanceData = nullptr; - uint32_t enHancedataLen = 0; + uint32_t enHancedataLen = MAX_HMAC_SIZE; - int32_t result = SecCompEnhanceKit::GetPointerEventEnhanceData(originData, dataLen, &enhanceData, enHancedataLen); + int32_t result = SecCompEnhanceKit::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen); if (g_inputEnhanceExist) { EXPECT_EQ(result, SC_OK); - EXPECT_EQ(enHancedataLen, static_cast(0)); } else { EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); } -- Gitee From 576798a2478a553508e40772ca6c1732f6a3c7eb Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 25 Apr 2023 09:09:27 +0800 Subject: [PATCH 08/59] Description:support locationbutton Match-id-89501ff9fcb7d04f965c28cec95bacfe6ccf6714 --- bundle.json | 31 +- frameworks/BUILD.gn | 17 +- frameworks/common/include/sec_comp_tool.h | 4 +- frameworks/common/src/sec_comp_tool.cpp | 121 +++- .../src/location_button.cpp | 108 ++-- .../security_component/src/sec_comp_base.cpp | 274 ++++++-- .../src/sec_comp_click_event_parcel.cpp | 4 +- .../security_component/src/sec_comp_rect.cpp | 45 -- .../include/location_button.h | 52 +- .../security_component/include/paste_button.h | 5 - .../security_component/include/save_button.h | 5 - .../include/sec_comp_base.h | 61 +- .../include/sec_comp_click_event.h | 37 -- .../include/sec_comp_info.h | 96 ++- .../include/sec_comp_pattern.h | 92 --- .../include/sec_comp_rect.h | 50 -- .../unittest/src/location_button_test.cpp | 271 ++++---- .../test/unittest/src/sec_comp_base_test.cpp | 312 +++++++-- .../test/unittest/src/sec_comp_kit_test.cpp | 38 +- .../security_component_service/sa/BUILD.gn | 13 + .../sa/sa_main/app_state_observer.cpp | 1 + .../sa/sa_main/sec_comp_entity.cpp | 21 +- .../sa/sa_main/sec_comp_entity.h | 19 +- .../sa/sa_main/sec_comp_info_helper.cpp | 158 ++++- .../sa/sa_main/sec_comp_info_helper.h | 7 +- .../sa/sa_main/sec_comp_manager.cpp | 95 +-- .../sa/sa_main/sec_comp_manager.h | 3 +- .../sa/test/BUILD.gn | 15 +- .../sa/test/mock/include/access_token.h | 2 + .../unittest/src/sec_comp_entity_test.cpp | 33 +- .../src/sec_comp_info_helper_test.cpp | 609 ++++++++++++++++-- .../unittest/src/sec_comp_manager_test.cpp | 112 ++-- .../unittest/src/sec_comp_service_test.cpp | 33 +- 33 files changed, 1917 insertions(+), 827 deletions(-) delete mode 100644 frameworks/security_component/src/sec_comp_rect.cpp delete mode 100644 interfaces/inner_api/security_component/include/sec_comp_click_event.h delete mode 100644 interfaces/inner_api/security_component/include/sec_comp_pattern.h delete mode 100644 interfaces/inner_api/security_component/include/sec_comp_rect.h diff --git a/bundle.json b/bundle.json index 4f09985..7c32811 100644 --- a/bundle.json +++ b/bundle.json @@ -1,7 +1,7 @@ { "name": "@openharmony/security_component", "description": "security_component", - "version": "3.1.0", + "version": "4.0.0", "license": "Apache License 2.0", "publishAs": "code-segment", "segment": { @@ -57,6 +57,35 @@ ], "header_base": "//base/security/security_component/interfaces/inner_api/security_component/include" } + }, + { + "name": "//base/security/security_component/frameworks:libsecurity_component_framework", + "header": { + "header_files": [ + "location_button.h", + "paste_button.h", + "save_button.h" + ], + "header_base": "//base/security/security_component/interfaces/inner_api/security_component/include" + } + }, + { + "name": "//base/security/security_component/frameworks:libsecurity_component_enhance_adapter", + "header": { + "header_files": [ + "sec_comp_enhance_adapter.h" + ], + "header_base": "//base/security/security_component/frameworks/enhance_adapter/include" + } + }, + { + "name": "//base/security/security_component/services/security_component_service/sa:security_component_service", + "header": { + "header_files": [ + "sec_comp_manager.h" + ], + "header_base": "//base/security/security_component/services/security_component_service/sa/sa_main" + } } ], "test": [ diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 9542fee..5ae3f7f 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -14,6 +14,13 @@ import("//base/security/security_component/security_component.gni") import("//build/ohos.gni") +config("libsecurity_component_framework_config") { + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] +} + ohos_shared_library("libsecurity_component_framework") { subsystem_name = "security" part_name = "security_component" @@ -31,10 +38,10 @@ ohos_shared_library("libsecurity_component_framework") { "security_component/src/location_button.cpp", "security_component/src/sec_comp_base.cpp", "security_component/src/sec_comp_click_event_parcel.cpp", - "security_component/src/sec_comp_rect.cpp", ] configs = [ "//base/security/security_component/config:coverage_flags" ] + public_configs = [ ":libsecurity_component_framework_config" ] external_deps = [ "c_utils:utils", @@ -44,6 +51,13 @@ ohos_shared_library("libsecurity_component_framework") { cflags_cc = [ "-DHILOG_ENABLE" ] } +config("libsecurity_component_enhance_adapter_config") { + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + ] +} + ohos_shared_library("libsecurity_component_enhance_adapter") { subsystem_name = "security" part_name = "security_component" @@ -59,6 +73,7 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { ] configs = [ "//base/security/security_component/config:coverage_flags" ] + public_configs = [ ":libsecurity_component_enhance_adapter_config" ] external_deps = [ "c_utils:utils", diff --git a/frameworks/common/include/sec_comp_tool.h b/frameworks/common/include/sec_comp_tool.h index 1ee6108..98aa805 100644 --- a/frameworks/common/include/sec_comp_tool.h +++ b/frameworks/common/include/sec_comp_tool.h @@ -16,11 +16,13 @@ #define SECURITY_COMPONENT_TOOL_H #include +#include "sec_comp_info.h" namespace OHOS { namespace Security { namespace SecurityComponent { - uint64_t GetCurrentTime(); + bool IsColorSimilar(const SecCompColor& color1, const SecCompColor& color2); + bool IsColorTransparent(const SecCompColor& color); } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/frameworks/common/src/sec_comp_tool.cpp b/frameworks/common/src/sec_comp_tool.cpp index 24a273b..4885970 100644 --- a/frameworks/common/src/sec_comp_tool.cpp +++ b/frameworks/common/src/sec_comp_tool.cpp @@ -12,26 +12,129 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "sec_comp_tool.h" +#include #include -#include +#include "sec_comp_log.h" namespace OHOS { namespace Security { namespace SecurityComponent { namespace { -static constexpr uint32_t MS_PER_SECOND = 1000; -static constexpr uint32_t NS_PER_MILLISECOND = 1000000; +static constexpr double PI_CIRCLE = 3.1415926F; +static constexpr double MIN_HSV_DISTANCE = 25.0F; +static constexpr double MAX_RGB_VALUE = 255.0F; +static constexpr uint8_t MAX_TRANSPARENT = 0x99; // 60% +static constexpr double ZERO_DOUBLE = 0.0F; +static constexpr double THIRTY_ANGLE = 30.0F; +static constexpr double SIXTY_ANGLE = 60.0F; +static constexpr double ONE_HUNDRED_TWEENTY_ANGLE = 120.0F; +static constexpr double ONE_HUNDRED_EIGHTY_ANGLE = 180.0F; +static constexpr double TWO_HUNDREDS_FORTY_ANGLE = 240.0F; +static constexpr double THREE_HUNDREDS_SIXTY_ANGLE = 360.0F; +static constexpr double DEFAULT_R = 100.0F; + +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompTool"}; } -uint64_t GetCurrentTime() +static inline double MaxValue(double a, double b, double c) { - struct timespec current = {0}; - int ret = clock_gettime(CLOCK_REALTIME, ¤t); - if (ret != 0) { - return 0; + return ((a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)); +} + +static inline double MinValue(double a, double b, double c) +{ + return ((a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c)); +} + +struct HsvColor { + double h; + double s; + double v; +}; + +static HsvColor RgbToHsv(uint8_t r, uint8_t g, uint8_t b) +{ + HsvColor hsv; + double red = static_cast(r) / MAX_RGB_VALUE; + double green = static_cast(g) / MAX_RGB_VALUE; + double blue = static_cast(b) / MAX_RGB_VALUE; + float max = MaxValue(red, green, blue); + float min = MinValue(red, green, blue); + float delta = max - min; + if (max == min) { + hsv.h = ZERO_DOUBLE; + } else if (max == red) { + hsv.h = fmod((SIXTY_ANGLE * ((green - blue) / delta) + THREE_HUNDREDS_SIXTY_ANGLE), + THREE_HUNDREDS_SIXTY_ANGLE); + } else if (max == green) { + hsv.h = fmod((SIXTY_ANGLE * ((blue - red) / delta) + ONE_HUNDRED_TWEENTY_ANGLE), THREE_HUNDREDS_SIXTY_ANGLE); + } else if (max == blue) { + hsv.h = fmod((SIXTY_ANGLE * ((red - green) / delta) + TWO_HUNDREDS_FORTY_ANGLE), THREE_HUNDREDS_SIXTY_ANGLE); } - return static_cast(current.tv_sec) * MS_PER_SECOND + current.tv_nsec / NS_PER_MILLISECOND; + + if (max == 0) { + hsv.s = ZERO_DOUBLE; + } else { + hsv.s = delta / max; + } + + hsv.v = max; + return hsv; +} + +static inline double GetHsvDisX(HsvColor& hsv, double r) +{ + return r * hsv.v * hsv.s * cos(hsv.h / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); +} + +static inline double GetHsvDisY(HsvColor& hsv, double r) +{ + return r * hsv.v * hsv.s * sin(hsv.h / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); +} + +static inline double GetHsvDisZ(HsvColor& hsv, double h) +{ + return h * (1 - hsv.v); +} + +static double HsvDistance(HsvColor& hsv1, HsvColor& hsv2) +{ + double rDef = DEFAULT_R; + double angle = THIRTY_ANGLE; + double h = rDef * cos(angle / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); + double r = rDef * sin(angle / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); + + double x1 = GetHsvDisX(hsv1, r); + double y1 = GetHsvDisY(hsv1, r); + double z1 = GetHsvDisZ(hsv1, h); + + double x2 = GetHsvDisX(hsv2, r); + double y2 = GetHsvDisY(hsv2, r); + double z2 = GetHsvDisZ(hsv2, h); + + double dx = x1 - x2; + double dy = y1 - y2; + double dz = z1 - z2; + + return sqrt((dx * dx) + (dy * dy) + (dz * dz)); +} + +bool IsColorSimilar(const SecCompColor& color1, const SecCompColor& color2) +{ + HsvColor hsv1 = RgbToHsv(color1.argb.red, color1.argb.green, color1.argb.blue); + HsvColor hsv2 = RgbToHsv(color2.argb.red, color2.argb.green, color2.argb.blue); + + double distance = HsvDistance(hsv1, hsv2); + SC_LOG_INFO(LABEL, "Compare color %{public}x %{public}x distance %{public}f", + color1.value, color2.value, distance); + return (distance < MIN_HSV_DISTANCE); +} + +bool IsColorTransparent(const SecCompColor& color) +{ + return ((color.argb.alpha != 0) && (color.argb.alpha < MAX_TRANSPARENT)); } } // namespace SecurityComponent } // namespace Security diff --git a/frameworks/security_component/src/location_button.cpp b/frameworks/security_component/src/location_button.cpp index 9e7b869..201dc46 100644 --- a/frameworks/security_component/src/location_button.cpp +++ b/frameworks/security_component/src/location_button.cpp @@ -14,98 +14,88 @@ */ #include "location_button.h" +#include #include "sec_comp_err.h" #include "sec_comp_log.h" +#include "sec_comp_tool.h" namespace OHOS { namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButton"}; -static const std::string JSON_FONT_FAMILY = "font"; -static const std::string JSON_LABEL_TYPE = "label"; -static const std::string JSON_ICON = "icon"; -static const std::string JSON_FONT_SIZE = "fontSize"; -static const std::string JSON_FONT_COLOR = "fontColor"; -static const std::string JSON_BG_COLOR = "bgColor"; -static const std::string JSON_BUTTON_TYPE = "buttonType"; +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; } -void LocationButton::FromJson(const nlohmann::json& jsonSrc) +bool LocationButton::ParseStyle(const nlohmann::json& json, const std::string& tag) { - SecCompBase::FromJson(jsonSrc); - if (jsonSrc.find(JSON_FONT_FAMILY) != jsonSrc.end() && jsonSrc.at(JSON_FONT_FAMILY).is_number()) { - int32_t font = jsonSrc.at(JSON_FONT_FAMILY).get(); - if (IsComponentFontFamilyValid(font)) { - font_ = static_cast(font); - } - } - - if (jsonSrc.find(JSON_LABEL_TYPE) != jsonSrc.end() && jsonSrc.at(JSON_LABEL_TYPE).is_number()) { - int32_t label = jsonSrc.at(JSON_LABEL_TYPE).get(); - if (IsComponentLabelValid(label)) { - label_ = static_cast(label); - } + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; } - - if (jsonSrc.find(JSON_ICON) != jsonSrc.end() && jsonSrc.at(JSON_ICON).is_number()) { - int32_t icon = jsonSrc.at(JSON_ICON).get(); - if (IsComponentIconValid(icon)) { - icon_ = static_cast(icon); - } + auto jsonStyle = json.at(tag); + if (!ParseTypeValue(jsonStyle, JSON_TEXT_TAG, text_, + LocationDesc::UNKNOWN_TEXT, LocationDesc::MAX_LABEL_TYPE)) { + return false; } - if (jsonSrc.find(JSON_FONT_SIZE) != jsonSrc.end() && jsonSrc.at(JSON_FONT_SIZE).is_number()) { - fontSize_ = jsonSrc.at(JSON_FONT_SIZE).get(); + if (!ParseTypeValue(jsonStyle, JSON_ICON_TAG, icon_, + LocationIcon::UNKNOWN_ICON, LocationIcon::MAX_ICON_TYPE)) { + return false; } - if (jsonSrc.find(JSON_FONT_COLOR) != jsonSrc.end() && jsonSrc.at(JSON_FONT_COLOR).is_number()) { - fontColor_.value = jsonSrc.at(JSON_FONT_COLOR).get(); + if (!ParseTypeValue(jsonStyle, JSON_BG_TAG, bg_, + LocationBackground::UNKNOWN_BG, LocationBackground::MAX_BG_TYPE)) { + return false; } - if (jsonSrc.find(JSON_BG_COLOR) != jsonSrc.end() && jsonSrc.at(JSON_BG_COLOR).is_number()) { - bgColor_.value = jsonSrc.at(JSON_BG_COLOR).get(); - } + return true; +} - if (jsonSrc.find(JSON_BUTTON_TYPE) != jsonSrc.end() && jsonSrc.at(JSON_BUTTON_TYPE).is_number()) { - int32_t type = jsonSrc.at(JSON_BUTTON_TYPE).get(); - if (IsComponentButtonTypeValid(type)) { - buttonType_ = static_cast(type); - } +bool LocationButton::FromJson(const nlohmann::json& jsonSrc) +{ + SC_LOG_DEBUG(LABEL, "LocationButton info %{public}s.", jsonSrc.dump().c_str()); + SecCompBase::FromJson(jsonSrc); + if (!ParseStyle(jsonSrc, JSON_STYLE_TAG)) { + SC_LOG_ERROR(LABEL, "Parse style %{public}s.", jsonSrc.dump().c_str()); + return false; } + return true; } void LocationButton::ToJson(nlohmann::json& jsonRes) const { SecCompBase::ToJson(jsonRes); - jsonRes[JSON_FONT_FAMILY] = font_; - jsonRes[JSON_LABEL_TYPE] = label_; - jsonRes[JSON_ICON] = icon_; - jsonRes[JSON_FONT_SIZE] = fontSize_; - jsonRes[JSON_FONT_COLOR] = fontColor_.value; - jsonRes[JSON_BG_COLOR] = bgColor_.value; - jsonRes[JSON_BUTTON_TYPE] = buttonType_; + jsonRes[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, text_ }, + { JSON_ICON_TAG, icon_ }, + { JSON_BG_TAG, bg_ }, + }; } -bool LocationButton::IsValid(void) const +std::string LocationButton::ToJsonStr() const { - bool locationValid = IsComponentFontFamilyValid(font_) && IsComponentLabelValid(label_) && - IsComponentIconValid(icon_) && IsComponentFontSizeValid(fontSize_) && - IsComponentButtonTypeValid(buttonType_); - - return locationValid && SecCompBase::IsValid(); + nlohmann::json json; + ToJson(json); + return json.dump(); } -bool LocationButton::operator==(const LocationButton& other) const +bool LocationButton::CompareComponentBasicInfo(SecCompBase *other) const { - if (font_ != other.font_ || label_ != other.label_ || icon_ != other.icon_ || - fontSize_ != other.fontSize_ || fontColor_.value != other.fontColor_.value || - bgColor_.value != other.bgColor_.value || buttonType_ != other.buttonType_) { - SC_LOG_INFO(LABEL, "LocationButton is not equal"); + if (!SecCompBase::CompareComponentBasicInfo(other)) { + SC_LOG_ERROR(LABEL, "SecComp base not equal."); return false; } - - return SecCompBase::operator==(other); + LocationButton* otherLocationButton = reinterpret_cast(other); + if (otherLocationButton == nullptr) { + SC_LOG_ERROR(LABEL, "other is not location button."); + return false; + } + return (icon_ == otherLocationButton->icon_) && (text_ == otherLocationButton->text_) && + (bg_ == otherLocationButton->bg_); } } // namespace SecurityComponent } // namespace Security diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index 99ac1ae..38817bd 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -28,68 +28,205 @@ static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; static const std::string JSON_RECT_WIDTH = "width"; static const std::string JSON_RECT_HEIGHT = "height"; + +static const std::string JSON_SIZE_TAG = "size"; +static const std::string JSON_FONT_SIZE_TAG = "fontSize"; +static const std::string JSON_ICON_SIZE_TAG = "iconSize"; +static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; +static const std::string JSON_PADDING_LEFT_TAG = "left"; +static const std::string JSON_PADDING_TOP_TAG = "top"; +static const std::string JSON_PADDING_RIGHT_TAG = "right"; +static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_RECT_WIDTH_TAG = "width"; +static const std::string JSON_RECT_HEIGHT_TAG = "height"; + +static const std::string JSON_COLORS_TAG = "colors"; +static const std::string JSON_FONT_COLOR_TAG = "fontColor"; +static const std::string JSON_ICON_COLOR_TAG = "iconColor"; +static const std::string JSON_BG_COLOR_TAG = "bgColor"; + +static const std::string JSON_BORDER_TAG = "border"; +static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; +static const std::string JSON_PARENT_TAG = "parent"; +static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; +} + +bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_number_float()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; + } + + res = json.at(tag).get(); + return true; +} + +bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; + } + + res.value = json.at(tag).get(); + return true; +} + +bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool& res) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; + } + + res = json.at(tag).get(); + return true; +} + +bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; + } + + auto jsonPadding = json.at(tag); + if (!ParseDimension(jsonPadding, JSON_PADDING_TOP_TAG, res.top)) { + return false; + } + if (!ParseDimension(jsonPadding, JSON_PADDING_RIGHT_TAG, res.right)) { + return false; + } + if (!ParseDimension(jsonPadding, JSON_PADDING_BOTTOM_TAG, res.bottom)) { + return false; + } + if (!ParseDimension(jsonPadding, JSON_PADDING_LEFT_TAG, res.left)) { + return false; + } + return true; +} + +bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; + } + auto jsonColors = json.at(tag); + if (!ParseColor(jsonColors, JSON_FONT_COLOR_TAG, fontColor_)) { + return false; + } + if (!ParseColor(jsonColors, JSON_ICON_COLOR_TAG, iconColor_)) { + return false; + } + if (!ParseColor(jsonColors, JSON_BG_COLOR_TAG, bgColor_)) { + return false; + } + return true; +} + +bool SecCompBase::ParseBorders(const nlohmann::json& json, const std::string& tag) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; + } + auto jsonBorder = json.at(tag); + return ParseDimension(jsonBorder, JSON_BORDER_WIDTH_TAG, borderWidth_); } -void from_json(const nlohmann::json& j, SecCompRect& p) +bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) { - SecCompRect res; - if (j.is_discarded()) { - SC_LOG_ERROR(LABEL, "component info invalid"); - return ; + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; } - if (j.find(JSON_RECT_X) != j.end() && j.at(JSON_RECT_X).is_number_float()) { - res.x_ = j.at(JSON_RECT_X).get(); - } else { - SC_LOG_ERROR(LABEL, "x is invalid"); - return; + auto jsonSize = json.at(tag); + if (!ParseDimension(jsonSize, JSON_FONT_SIZE_TAG, fontSize_)) { + return false; } - if (j.find(JSON_RECT_Y) != j.end() && j.at(JSON_RECT_Y).is_number_float()) { - res.y_ = j.at(JSON_RECT_Y).get(); - } else { - SC_LOG_ERROR(LABEL, "y is invalid."); - return; + if (!ParseDimension(jsonSize, JSON_ICON_SIZE_TAG, iconSize_)) { + return false; } - if (j.find(JSON_RECT_WIDTH) != j.end() && j.at(JSON_RECT_WIDTH).is_number_float()) { - res.width_ = j.at(JSON_RECT_WIDTH).get(); - } else { - SC_LOG_ERROR(LABEL, "width is invalid."); - return; + if (!ParseDimension(jsonSize, JSON_TEXT_ICON_PADDING_TAG, textIconPadding_)) { + return false; } - if (j.find(JSON_RECT_HEIGHT) != j.end() && j.at(JSON_RECT_HEIGHT).is_number_float()) { - res.height_ = j.at(JSON_RECT_HEIGHT).get(); - } else { - SC_LOG_ERROR(LABEL, "height is invalid."); - return; + if (!ParsePadding(jsonSize, JSON_PADDING_SIZE_TAG, padding_)) { + return false; } - p = res; + return true; } -void SecCompBase::FromJson(const nlohmann::json& jsonSrc) +bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag) { - if (jsonSrc.find(JSON_SC_TYPE) != jsonSrc.end() && jsonSrc.at(JSON_SC_TYPE).is_number()) { - int32_t type = jsonSrc.at(JSON_SC_TYPE).get(); - if (IsComponentTypeValid(type)) { - type_ = static_cast(type); - } else { - SC_LOG_ERROR(LABEL, "type is invalid."); - return; - } - } else { - SC_LOG_ERROR(LABEL, "get type fail."); - return; - } - - if (jsonSrc.find(JSON_RECT) != jsonSrc.end() && jsonSrc.at(JSON_RECT).is_object()) { - rect_ = jsonSrc.at(JSON_RECT).get(); - } else { - SC_LOG_ERROR(LABEL, "rect is invalid."); - return; + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; } + auto jsonParent = json.at(tag); + return ParseBool(jsonParent, JSON_PARENT_EFFECT_TAG, parentEffect_); +} + +bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + return false; + } + + auto jsonSize = json.at(tag); + if (!ParseDimension(jsonSize, JSON_RECT_X, rect_.x_)) { + return false; + } + + if (!ParseDimension(jsonSize, JSON_RECT_Y, rect_.y_)) { + return false; + } + + if (!ParseDimension(jsonSize, JSON_RECT_WIDTH, rect_.width_)) { + return false; + } + + if (!ParseDimension(jsonSize, JSON_RECT_HEIGHT, rect_.height_)) { + return false; + } + + return true; +} + +bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) +{ + if (!ParseTypeValue(jsonSrc, JSON_SC_TYPE, type_, + SecCompType::UNKNOWN_SC_TYPE, SecCompType::MAX_SC_TYPE)) { + return false; + } + + if (!ParseRect(jsonSrc, JSON_RECT)) { + return false; + } + if (!ParseSize(jsonSrc, JSON_SIZE_TAG)) { + return false; + } + + if (!ParseColors(jsonSrc, JSON_COLORS_TAG)) { + return false; + } + + if (!ParseBorders(jsonSrc, JSON_BORDER_TAG)) { + return false; + } + if (!ParseParent(jsonSrc, JSON_PARENT_TAG)) { + return false; + } + + return true; } void SecCompBase::ToJson(nlohmann::json& jsonRes) const @@ -101,21 +238,48 @@ void SecCompBase::ToJson(nlohmann::json& jsonRes) const {JSON_RECT_WIDTH, rect_.width_}, {JSON_RECT_HEIGHT, rect_.height_} }; -} + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, padding_.top }, + { JSON_PADDING_RIGHT_TAG, padding_.right }, + { JSON_PADDING_BOTTOM_TAG, padding_.bottom }, + { JSON_PADDING_LEFT_TAG, padding_.left }, + }; -bool SecCompBase::IsValid(void) const -{ - return IsComponentTypeValid(type_) && rect_.IsValid(); + jsonRes[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, fontSize_ }, + { JSON_ICON_SIZE_TAG, iconSize_ }, + { JSON_TEXT_ICON_PADDING_TAG, textIconPadding_ }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonRes[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, fontColor_.value }, + { JSON_ICON_COLOR_TAG, iconColor_.value }, + { JSON_BG_COLOR_TAG, bgColor_.value } + }; + + jsonRes[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, borderWidth_ }, + }; + jsonRes[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, parentEffect_ }, + }; } -bool SecCompBase::operator==(const SecCompBase& other) const +bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other) const { - bool result = (type_ == other.type_ && rect_.x_ == other.rect_.x_ && rect_.y_ == other.rect_.y_ && - rect_.width_ == other.rect_.width_ && rect_.height_ == other.rect_.height_); - if (!result) { - SC_LOG_INFO(LABEL, "SecCompBase is not equal"); + if (other == nullptr) { + SC_LOG_ERROR(LABEL, "other is invalid."); + return false; } - return result; + + auto leftValue = std::tie(type_, fontSize_, iconSize_, textIconPadding_, padding_.top, padding_.right, + padding_.bottom, padding_.left, fontColor_.value, bgColor_.value, iconColor_.value, borderWidth_); + auto rightValue = std::tie(other->type_, other->fontSize_, other->iconSize_, other->textIconPadding_, + other->padding_.top, other->padding_.right, other->padding_.bottom, other->padding_.left, + other->fontColor_.value, other->bgColor_.value, other->iconColor_.value, other->borderWidth_); + + return (leftValue == rightValue); } } // namespace base } // namespace Security diff --git a/frameworks/security_component/src/sec_comp_click_event_parcel.cpp b/frameworks/security_component/src/sec_comp_click_event_parcel.cpp index 8b7f290..5fc9a42 100644 --- a/frameworks/security_component/src/sec_comp_click_event_parcel.cpp +++ b/frameworks/security_component/src/sec_comp_click_event_parcel.cpp @@ -26,7 +26,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { bool SecCompClickEventParcel::Marshalling(Parcel& out) const { - if (!(out.WriteFloat(this->touchInfoParams_.touchX)) || !(out.WriteFloat(this->touchInfoParams_.touchY))) { + if (!(out.WriteDouble(this->touchInfoParams_.touchX)) || !(out.WriteDouble(this->touchInfoParams_.touchY))) { SC_LOG_ERROR(LABEL, "Write touch xy pointer fail"); return false; } @@ -59,7 +59,7 @@ SecCompClickEventParcel* SecCompClickEventParcel::Unmarshalling(Parcel& in) } SecCompClickEvent touchInfo; - if (!in.ReadFloat(touchInfo.touchX) || !in.ReadFloat(touchInfo.touchY)) { + if (!in.ReadDouble(touchInfo.touchX) || !in.ReadDouble(touchInfo.touchY)) { SC_LOG_ERROR(LABEL, "Read touch xy porinter fail"); delete touchInfoParcel; return nullptr; diff --git a/frameworks/security_component/src/sec_comp_rect.cpp b/frameworks/security_component/src/sec_comp_rect.cpp deleted file mode 100644 index ccb9cad..0000000 --- a/frameworks/security_component/src/sec_comp_rect.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 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. - */ - -#include "sec_comp_rect.h" - -namespace OHOS { -namespace Security { -namespace SecurityComponent { -bool SecCompRect::IsValid() const -{ - return GreatNotEqual(x_, 0.0) && GreatNotEqual(y_, 0.0) && - GreatNotEqual(width_, 0.0) && GreatNotEqual(height_, 0.0); -} - -bool SecCompRect::IsRectOverlaped(const SecCompRect& other) const -{ - double xLow = other.x_; - double xHigh = other.x_ + other.width_ ; - double yLow = other.y_ ; - double yHigh = other.y_ + other.height_; - - return (!(GreatOrEqual(xLow, (x_ + width_)) || GreatOrEqual(x_, xHigh))) && - (!(GreatOrEqual(y_, yHigh) || GreatOrEqual(yLow, (y_ + height_)))); -} - -bool SecCompRect::IsInRect(double x, double y) const -{ - return (GreatOrEqual(x, x_) && GreatOrEqual((x_ + width_), x) && - GreatOrEqual(y, y_) && GreatOrEqual((y_ + height_), y)); -} -} // namespace SecurityComponent -} // namespace Security -} // namespace OHOS diff --git a/interfaces/inner_api/security_component/include/location_button.h b/interfaces/inner_api/security_component/include/location_button.h index 00d155c..301d03a 100644 --- a/interfaces/inner_api/security_component/include/location_button.h +++ b/interfaces/inner_api/security_component/include/location_button.h @@ -23,20 +23,52 @@ namespace OHOS { namespace Security { namespace SecurityComponent { +enum class LocationDesc : int32_t { + UNKNOWN_TEXT = -2, + NO_TEXT = -1, + CURRENT_LOCATION = 0, + ADD_LOCATION = 4, + SELECT_LOCATION = 1, + SHARE_LOCATION = 2, + SEND_LOCATION = 3, + LOCATING = 5, + LOCATION = 6, + SEND_CURRENT_LOCATION = 7, + RELOCATION = 8, + PUNCH_IN = 9, + CURRENT_POSITION = 10, + MAX_LABEL_TYPE +}; + +enum class LocationIcon { + UNKNOWN_ICON = -2, + NO_ICON = -1, + FILLED_ICON = 0, + LINE_ICON = 1, + MAX_ICON_TYPE +}; + +enum class LocationBackground { + UNKNOWN_BG = -2, + NO_BG_TYPE = -1, + CAPSULE = 0, + CIRCLE = 1, + NORMAL = 2, + MAX_BG_TYPE +}; + class LocationButton : public SecCompBase { public: - SecCompFontFamily font_ = UNKNOWN_FONT_FAMILY_TYPE; - SecCompLabel label_ = UNKNOWN_LABEL; - SecCompIcon icon_ = UNKNOWN_ICON; - uint32_t fontSize_ = 0; - SecCompColor fontColor_; - SecCompColor bgColor_; - SecCompButtonType buttonType_ = UNKNOWN_BUTTON_TYPE; + LocationDesc text_ = LocationDesc::UNKNOWN_TEXT; + LocationIcon icon_ = LocationIcon::UNKNOWN_ICON; + LocationBackground bg_ = LocationBackground::UNKNOWN_BG; - virtual void FromJson(const nlohmann::json& jsonSrc) override; + virtual bool FromJson(const nlohmann::json& jsonSrc) override; virtual void ToJson(nlohmann::json& jsonRes) const override; - virtual bool IsValid(void) const override; - bool operator==(const LocationButton& other) const; + virtual std::string ToJsonStr(void) const; + virtual bool CompareComponentBasicInfo(SecCompBase *other) const override; +private: + bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/paste_button.h b/interfaces/inner_api/security_component/include/paste_button.h index 91a4465..c6b89af 100644 --- a/interfaces/inner_api/security_component/include/paste_button.h +++ b/interfaces/inner_api/security_component/include/paste_button.h @@ -23,11 +23,6 @@ namespace OHOS { namespace Security { namespace SecurityComponent { class PasteButton : public SecCompBase { -public: - bool operator==(const PasteButton& other) const - { - return SecCompBase::operator==(other); - }; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h index 53e5752..7c87437 100644 --- a/interfaces/inner_api/security_component/include/save_button.h +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -24,11 +24,6 @@ namespace OHOS { namespace Security { namespace SecurityComponent { class SaveButton : public SecCompBase { -public: - bool operator==(const SaveButton& other) const - { - return SecCompBase::operator==(other); - }; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index a29ce44..146d141 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_base.h +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -21,18 +21,69 @@ namespace OHOS { namespace Security { namespace SecurityComponent { +template +bool ParseTypeValue(const nlohmann::json& json, const std::string& tag, + T& type, const T& min, const T& max) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) { + return false; + } + + int32_t value = json.at(tag).get(); + if ((value <= static_cast(min)) || (value >= static_cast(max))) { + return false; + } + type = static_cast(value); + return true; +} + class SecCompBase { public: SecCompBase() = default; virtual ~SecCompBase() = default; + virtual bool FromJson(const nlohmann::json& jsonSrc); + virtual void ToJson(nlohmann::json& jsonRes) const; + virtual bool CompareComponentBasicInfo(SecCompBase *other) const; + void SetValid(bool valid) + { + isValid_ = valid; + }; + + bool GetValid() + { + return isValid_; + }; + + // size + DimensionT fontSize_ = DEFAULT_DIMENSION; + DimensionT iconSize_ = DEFAULT_DIMENSION; + PaddingSize padding_; + DimensionT textIconPadding_ = DEFAULT_DIMENSION; + + // color + SecCompColor fontColor_; + SecCompColor iconColor_; + SecCompColor bgColor_; + + // border + DimensionT borderWidth_ = DEFAULT_DIMENSION; + + // parent effect + bool parentEffect_ = false; SecCompType type_ = UNKNOWN_SC_TYPE; SecCompRect rect_; - - virtual void FromJson(const nlohmann::json& jsonSrc); - virtual void ToJson(nlohmann::json& jsonRes) const; - virtual bool IsValid(void) const; - bool operator==(const SecCompBase& other) const; + bool isValid_ = false; +private: + bool ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res); + bool ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res); + bool ParseBool(const nlohmann::json& json, const std::string& tag, bool& res); + bool ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res); + bool ParseColors(const nlohmann::json& json, const std::string& tag); + bool ParseBorders(const nlohmann::json& json, const std::string& tag); + bool ParseSize(const nlohmann::json& json, const std::string& tag); + bool ParseParent(const nlohmann::json& json, const std::string& tag); + bool ParseRect(const nlohmann::json& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_click_event.h b/interfaces/inner_api/security_component/include/sec_comp_click_event.h deleted file mode 100644 index b084cc8..0000000 --- a/interfaces/inner_api/security_component/include/sec_comp_click_event.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 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 SECURITY_COMPONENT_CLICK_EVENT_H -#define SECURITY_COMPONENT_CLICK_EVENT_H -namespace OHOS { -namespace Security { -namespace SecurityComponent { -static constexpr uint32_t MAX_EXTRA_SIZE = 0x1000; - -struct ExtraInfo { - uint32_t dataSize; - uint8_t* data; -}; - -struct SecCompClickEvent { - float touchX; - float touchY; - uint64_t timestamp; - ExtraInfo extraInfo; -}; -} // namespace SecurityComponent -} // namespace Security -} // namespace OHOS -#endif // SECURITY_COMPONENT_CLICK_EVENT_H - diff --git a/interfaces/inner_api/security_component/include/sec_comp_info.h b/interfaces/inner_api/security_component/include/sec_comp_info.h index 3f1540f..9a3c9fc 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_info.h +++ b/interfaces/inner_api/security_component/include/sec_comp_info.h @@ -16,13 +16,103 @@ #ifndef SECURITY_COMPONENT_INFO_H #define SECURITY_COMPONENT_INFO_H -#include "sec_comp_rect.h" -#include "sec_comp_pattern.h" -#include "sec_comp_click_event.h" namespace OHOS { namespace Security { namespace SecurityComponent { static constexpr int32_t INVALID_SC_ID = -1; +using DimensionT = double; // unit is vp +static constexpr DimensionT DEFAULT_DIMENSION = 0.0f; +static constexpr DimensionT MIN_FONT_SIZE = 12.0f; +static constexpr DimensionT MIN_ICON_SIZE = 12.0f; +static constexpr DimensionT MIN_PADDING_SIZE = 0.0f; +static constexpr DimensionT MIN_PADDING_WITHOUT_BG = 4.0f; +static constexpr uint32_t MAX_EXTRA_SIZE = 0x1000; + +struct PaddingSize { + DimensionT top = DEFAULT_DIMENSION; + DimensionT right = DEFAULT_DIMENSION; + DimensionT bottom = DEFAULT_DIMENSION; + DimensionT left = DEFAULT_DIMENSION; +}; + +enum SecCompType { + UNKNOWN_SC_TYPE = 0, + LOCATION_COMPONENT, + PASTE_COMPONENT, + SAVE_COMPONENT, + MAX_SC_TYPE +}; + +union SecCompColor { + struct { + uint8_t blue; + uint8_t green; + uint8_t red; + uint8_t alpha; + } argb; + uint32_t value; +}; + +inline bool IsComponentTypeValid(int32_t type) +{ + if (!(type > UNKNOWN_SC_TYPE) || !(type < MAX_SC_TYPE)) { + return false; + } + return true; +} + +inline bool GreatOrEqual(double left, double right) +{ + constexpr double epsilon = -0.001f; + return (left - right) > epsilon; +} + +inline bool GreatNotEqual(double left, double right) +{ + constexpr double epsilon = 0.001f; + return (left - right) > epsilon; +} + +inline bool IsEqual(double left, double right) +{ + constexpr double epsilon = 0.001f; + if (left > right) { + return (left - right) < epsilon; + } else if (right > left) { + return (right - left) < epsilon; + } else { + return true; + } +} + +class SecCompRect { +public: + SecCompRect() = default; + ~SecCompRect() = default; + + bool IsInRect(double x, double y) const + { + return (GreatOrEqual(x, x_) && GreatOrEqual((x_ + width_), x) && + GreatOrEqual(y, y_) && GreatOrEqual((y_ + height_), y)); + }; + + DimensionT x_ = 0.0F; + DimensionT y_ = 0.0F; + DimensionT width_ = 0.0F; + DimensionT height_ = 0.0F; +}; + +struct ExtraInfo { + uint32_t dataSize; + uint8_t* data; +}; + +struct SecCompClickEvent { + double touchX; + double touchY; + uint64_t timestamp; + ExtraInfo extraInfo; +}; } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/include/sec_comp_pattern.h b/interfaces/inner_api/security_component/include/sec_comp_pattern.h deleted file mode 100644 index 96b5675..0000000 --- a/interfaces/inner_api/security_component/include/sec_comp_pattern.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 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 SECURITY_COMPONENT_PATTERN_H -#define SECURITY_COMPONENT_PATTERN_H - -static constexpr int32_t MIN_FONT_SIZE = 10; -enum SecCompType { - UNKNOWN_SC_TYPE = 0, - LOCATION_COMPONENT, - PASTE_COMPONENT, - SAVE_COMPONENT, - MAX_SC_TYPE -}; - -enum SecCompFontFamily { - UNKNOWN_FONT_FAMILY_TYPE = 0, - FONT_HO_SANS, // default font family - MAX_FONT_FAMILY_TYPE -}; - -enum SecCompLabel { - UNKNOWN_LABEL = 0, - DEFAULT_LABEL, - MAX_LABEL_TYPE -}; - -enum SecCompIcon { - UNKNOWN_ICON = 0, - DEFAULT_ICON, - MAX_ICON_TYPE -}; - -union SecCompColor { - struct { - uint8_t blue; - uint8_t green; - uint8_t red; - uint8_t alpha; - } argb; - uint32_t value; -}; - -enum SecCompButtonType { - UNKNOWN_BUTTON_TYPE = 0, - CAPSULE, - CIRCLE, - NORMAL, - MAX_BUTTON_TYPE -}; - -static inline bool IsComponentTypeValid(int32_t type) -{ - return type > UNKNOWN_SC_TYPE && type < MAX_SC_TYPE; -} - -static inline bool IsComponentFontFamilyValid(int32_t font) -{ - return font > UNKNOWN_FONT_FAMILY_TYPE && font < MAX_FONT_FAMILY_TYPE; -} - -static inline bool IsComponentLabelValid(int32_t label) -{ - return label > UNKNOWN_LABEL && label < MAX_LABEL_TYPE; -} - -static inline bool IsComponentIconValid(int32_t icon) -{ - return icon > UNKNOWN_ICON && icon < MAX_ICON_TYPE; -} - -static inline bool IsComponentButtonTypeValid(int32_t buttonType) -{ - return buttonType > UNKNOWN_BUTTON_TYPE && buttonType < MAX_BUTTON_TYPE; -} - -static inline bool IsComponentFontSizeValid(int32_t fontSize) -{ - return fontSize > MIN_FONT_SIZE; -} -#endif // SECURITY_COMPONENT_PATTERN_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_rect.h b/interfaces/inner_api/security_component/include/sec_comp_rect.h deleted file mode 100644 index e865216..0000000 --- a/interfaces/inner_api/security_component/include/sec_comp_rect.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 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 SECURITY_COMPONENT_RECT_H -#define SECURITY_COMPONENT_RECT_H -namespace OHOS { -namespace Security { -namespace SecurityComponent { -inline bool GreatOrEqual(double left, double right) -{ - constexpr double epsilon = -0.001f; - return (left - right) > epsilon; -} - -inline bool GreatNotEqual(double left, double right) -{ - constexpr double epsilon = 0.001f; - return (left - right) > epsilon; -} - -class SecCompRect { -public: - SecCompRect(double x, double y, double width, double height) : x_(x), y_(y), width_(width), height_(height) {}; - SecCompRect() = default; - ~SecCompRect() = default; - - bool IsValid() const; - bool IsRectOverlaped(const SecCompRect& other) const; - bool IsInRect(double x, double y) const; - - double x_ = 0.0; - double y_ = 0.0; - double width_ = 0.0; - double height_ = 0.0; -}; -} // namespace SecurityComponent -} // namespace Security -} // namespace OHOS -#endif // SECURITY_COMPONENT_INFO_H diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 2e59ae2..0d1b3ee 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -25,13 +25,11 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButtonTest"}; -static const std::string JSON_FONT_FAMILY = "font"; -static const std::string JSON_LABEL_TYPE = "label"; -static const std::string JSON_ICON = "icon"; -static const std::string JSON_FONT_SIZE = "fontSize"; -static const std::string JSON_FONT_COLOR = "fontColor"; -static const std::string JSON_BG_COLOR = "bgColor"; -static const std::string JSON_BUTTON_TYPE = "buttonType"; +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; + static const std::string JSON_RECT = "rect"; static const std::string JSON_SC_TYPE = "type"; static const std::string JSON_RECT_X = "x"; @@ -39,26 +37,76 @@ static const std::string JSON_RECT_Y = "y"; static const std::string JSON_RECT_WIDTH = "width"; static const std::string JSON_RECT_HEIGHT = "height"; -static constexpr uint32_t TEST_FONT_SIZE = 100; -static constexpr uint32_t TEST_INVALID_FONT_SIZE = 0; +static const std::string JSON_SIZE_TAG = "size"; +static const std::string JSON_FONT_SIZE_TAG = "fontSize"; +static const std::string JSON_ICON_SIZE_TAG = "iconSize"; +static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; +static const std::string JSON_PADDING_LEFT_TAG = "left"; +static const std::string JSON_PADDING_TOP_TAG = "top"; +static const std::string JSON_PADDING_RIGHT_TAG = "right"; +static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_RECT_WIDTH_TAG = "width"; +static const std::string JSON_RECT_HEIGHT_TAG = "height"; + +static const std::string JSON_COLORS_TAG = "colors"; +static const std::string JSON_FONT_COLOR_TAG = "fontColor"; +static const std::string JSON_ICON_COLOR_TAG = "iconColor"; +static const std::string JSON_BG_COLOR_TAG = "bgColor"; + +static const std::string JSON_BORDER_TAG = "border"; +static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; +static const std::string JSON_PARENT_TAG = "parent"; +static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; + +static const std::string WRONG_TYPE = "wrongType"; +static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; -static constexpr uint32_t TEST_COLOR = 0xffffffff; -static constexpr uint32_t TEST_DIFF_COLOR = 0; +static constexpr double TEST_DIMENSION = 100.0; +static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; +static constexpr uint32_t TEST_COLOR_2 = 0xff0000; +static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; -static void BuildLocationComponentInfo(LocationButton& button) +static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { - button.fontSize_ = TEST_FONT_SIZE; - button.font_ = FONT_HO_SANS; - button.bgColor_.value = TEST_COLOR; - button.fontColor_.value = TEST_COLOR; - button.icon_ = DEFAULT_ICON; - button.label_ = DEFAULT_LABEL; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.type_ = LOCATION_COMPONENT; - button.buttonType_ = CAPSULE; + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + + jsonComponent[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; } } @@ -85,25 +133,9 @@ void LocationButtonTest::TearDown() HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { nlohmann::json jsonComponent; - - jsonComponent[JSON_FONT_FAMILY] = FONT_HO_SANS; - jsonComponent[JSON_LABEL_TYPE] = DEFAULT_LABEL; - jsonComponent[JSON_ICON] = DEFAULT_ICON; - jsonComponent[JSON_FONT_SIZE] = TEST_FONT_SIZE; - jsonComponent[JSON_FONT_COLOR] = TEST_COLOR; - jsonComponent[JSON_BG_COLOR] = TEST_COLOR; - jsonComponent[JSON_BUTTON_TYPE] = CAPSULE; - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} - }; - + BuildLocationComponentInfo(jsonComponent); LocationButton button; - button.FromJson(jsonComponent); - ASSERT_TRUE(button.IsValid()); + ASSERT_TRUE(button.FromJson(jsonComponent)); } /** @@ -116,8 +148,7 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton button; - button.FromJson(jsonComponent); - ASSERT_FALSE(button.IsValid()); + ASSERT_FALSE(button.FromJson(jsonComponent)); } /** @@ -129,29 +160,30 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) { nlohmann::json jsonComponent; - jsonComponent[JSON_FONT_FAMILY] = "wrongType"; - jsonComponent[JSON_LABEL_TYPE] = "wrongType"; - jsonComponent[JSON_ICON] = "wrongType"; - jsonComponent[JSON_FONT_SIZE] = "wrongType"; - jsonComponent[JSON_FONT_COLOR] = "wrongType"; - jsonComponent[JSON_BG_COLOR] = "wrongType"; - jsonComponent[JSON_BUTTON_TYPE] = "wrongType"; - jsonComponent[JSON_BUTTON_TYPE] = "wrongType"; - jsonComponent[JSON_SC_TYPE] = "wrongType"; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} + BuildLocationComponentInfo(jsonComponent); + LocationButton button; + ASSERT_TRUE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, WRONG_TYPE }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, }; + ASSERT_FALSE(button.FromJson(jsonComponent)); - LocationButton button; - button.FromJson(jsonComponent); - ASSERT_FALSE(button.IsValid()); - ASSERT_EQ(button.rect_.x_, 0); - ASSERT_EQ(button.rect_.y_, 0); - ASSERT_EQ(button.rect_.width_, 0); - ASSERT_EQ(button.rect_.height_, 0); + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, WRONG_TYPE }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, WRONG_TYPE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); } /** @@ -162,61 +194,32 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) { - nlohmann::json jsonComponent; - jsonComponent[JSON_FONT_FAMILY] = 0; - jsonComponent[JSON_LABEL_TYPE] = 0; - jsonComponent[JSON_ICON] = 0; - jsonComponent[JSON_FONT_SIZE] = 0; - jsonComponent[JSON_FONT_COLOR] = 0; - jsonComponent[JSON_BG_COLOR] = 0; - jsonComponent[JSON_BUTTON_TYPE] = 0; - jsonComponent[JSON_BUTTON_TYPE] = 0; - jsonComponent[JSON_SC_TYPE] = 0; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} - }; - - LocationButton button; - button.FromJson(jsonComponent); - ASSERT_FALSE(button.IsValid()); -} - -/** - * @tc.name: IsValid001 - * @tc.desc: Test check location button invalid - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(LocationButtonTest, IsValid001, TestSize.Level1) -{ +nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); LocationButton button; - BuildLocationComponentInfo(button); - button.font_ = UNKNOWN_FONT_FAMILY_TYPE; - ASSERT_FALSE(button.IsValid()); - button.font_ = FONT_HO_SANS; + ASSERT_TRUE(button.FromJson(jsonComponent)); - button.label_ = UNKNOWN_LABEL; - ASSERT_FALSE(button.IsValid()); - button.label_ = DEFAULT_LABEL; - - button.icon_ = UNKNOWN_ICON; - ASSERT_FALSE(button.IsValid()); - button.icon_ = DEFAULT_ICON; - - button.fontSize_ = TEST_INVALID_FONT_SIZE; - ASSERT_FALSE(button.IsValid()); - button.fontSize_ = TEST_FONT_SIZE; + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::UNKNOWN_TEXT }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); - button.buttonType_ = UNKNOWN_BUTTON_TYPE; - ASSERT_FALSE(button.IsValid()); - button.buttonType_ = CAPSULE; + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::UNKNOWN_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); - button.type_ = UNKNOWN_SC_TYPE; - ASSERT_FALSE(button.IsValid()); + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::UNKNOWN_BG }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); } /** @@ -228,34 +231,26 @@ HWTEST_F(LocationButtonTest, IsValid001, TestSize.Level1) HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) { LocationButton button1; - BuildLocationComponentInfo(button1); - LocationButton button2; - BuildLocationComponentInfo(button2); - button1.font_ = UNKNOWN_FONT_FAMILY_TYPE; - ASSERT_FALSE(button1 == button2); - button1.font_ = FONT_HO_SANS; - - button1.label_ = UNKNOWN_LABEL; - ASSERT_FALSE(button1 == button2); - button1.label_ = DEFAULT_LABEL; + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); - button1.icon_ = UNKNOWN_ICON; - ASSERT_FALSE(button1 == button2); - button1.icon_ = DEFAULT_ICON; + ASSERT_TRUE(button1.FromJson(jsonComponent)); + ASSERT_TRUE(button2.FromJson(jsonComponent)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); - button1.fontSize_ = TEST_INVALID_FONT_SIZE; - ASSERT_FALSE(button1 == button2); - button1.fontSize_ = TEST_FONT_SIZE; + button1.text_ = LocationDesc::UNKNOWN_TEXT; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.text_ = LocationDesc::SELECT_LOCATION; - button1.fontColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(button1 == button2); - button1.fontColor_.value = TEST_COLOR; + button1.icon_ = LocationIcon::UNKNOWN_ICON; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.icon_ = LocationIcon::LINE_ICON; - button1.bgColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(button1 == button2); - button1.bgColor_.value = TEST_COLOR; + button1.bg_ = LocationBackground::UNKNOWN_BG; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.bg_ = LocationBackground::CIRCLE; - ASSERT_TRUE(button1 == button2); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp index 4222269..0bc7bab 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp @@ -31,9 +31,74 @@ static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; static const std::string JSON_RECT_WIDTH = "width"; static const std::string JSON_RECT_HEIGHT = "height"; + +static const std::string JSON_SIZE_TAG = "size"; +static const std::string JSON_FONT_SIZE_TAG = "fontSize"; +static const std::string JSON_ICON_SIZE_TAG = "iconSize"; +static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; +static const std::string JSON_PADDING_LEFT_TAG = "left"; +static const std::string JSON_PADDING_TOP_TAG = "top"; +static const std::string JSON_PADDING_RIGHT_TAG = "right"; +static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_RECT_WIDTH_TAG = "width"; +static const std::string JSON_RECT_HEIGHT_TAG = "height"; + +static const std::string JSON_COLORS_TAG = "colors"; +static const std::string JSON_FONT_COLOR_TAG = "fontColor"; +static const std::string JSON_ICON_COLOR_TAG = "iconColor"; +static const std::string JSON_BG_COLOR_TAG = "bgColor"; + +static const std::string JSON_BORDER_TAG = "border"; +static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; +static const std::string JSON_PARENT_TAG = "parent"; +static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; + +static const std::string WRONG_TYPE = "wrongType"; +static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; -static constexpr double WRONG_COORDINATE = -100.0; -static constexpr double TEST_DIFF_COORDINATE = 0.0; +static constexpr double TEST_DIMENSION = 100.0; +static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; +static constexpr uint32_t TEST_COLOR_2 = 0xff0000; +static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; +static constexpr uint32_t TEST_DIFF_COLOR = 0; + +static void BuildSecCompBaseInfo(nlohmann::json& jsonComponent) +{ + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + + jsonComponent[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, false }, + }; +} } void SecCompBaseTest::SetUpTestCase() @@ -59,18 +124,9 @@ void SecCompBaseTest::TearDown() HWTEST_F(SecCompBaseTest, FromJson001, TestSize.Level1) { nlohmann::json jsonComponent; - - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} - }; - + BuildSecCompBaseInfo(jsonComponent); SecCompBase comp; - comp.FromJson(jsonComponent); - ASSERT_TRUE(comp.IsValid()); + ASSERT_TRUE(comp.FromJson(jsonComponent)); } /** @@ -83,8 +139,7 @@ HWTEST_F(SecCompBaseTest, FromJson002, TestSize.Level1) { nlohmann::json jsonComponent; SecCompBase comp; - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_FALSE(comp.FromJson(jsonComponent)); } /** @@ -96,14 +151,15 @@ HWTEST_F(SecCompBaseTest, FromJson002, TestSize.Level1) HWTEST_F(SecCompBaseTest, FromJson003, TestSize.Level1) { nlohmann::json jsonComponent; + BuildSecCompBaseInfo(jsonComponent); SecCompBase comp; - jsonComponent[JSON_SC_TYPE] = "wrongType"; - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_SC_TYPE] = WRONG_TYPE; + ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JSON_SC_TYPE] = 0; - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_FALSE(comp.FromJson(jsonComponent)); } /** @@ -119,62 +175,168 @@ HWTEST_F(SecCompBaseTest, FromJson004, TestSize.Level1) jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); jsonComponent[JSON_RECT] = wrongJson; - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + BuildSecCompBaseInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, "wrongType"}, + {JSON_RECT_X, WRONG_TYPE}, {JSON_RECT_Y, TEST_COORDINATE}, {JSON_RECT_WIDTH, TEST_COORDINATE}, {JSON_RECT_HEIGHT, TEST_COORDINATE} }; - - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, "wrongType"}, + {JSON_RECT_Y, WRONG_TYPE}, {JSON_RECT_WIDTH, TEST_COORDINATE}, {JSON_RECT_HEIGHT, TEST_COORDINATE} }; - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE}, {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, "wrongType"}, + {JSON_RECT_WIDTH, WRONG_TYPE}, {JSON_RECT_HEIGHT, TEST_COORDINATE} }; - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE}, {JSON_RECT_Y, TEST_COORDINATE}, {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, "wrongType"} + {JSON_RECT_HEIGHT, WRONG_TYPE} }; - comp.FromJson(jsonComponent); - ASSERT_FALSE(comp.IsValid()); + ASSERT_FALSE(comp.FromJson(jsonComponent)); } /** - * @tc.name: IsValid001 - * @tc.desc: Test SecCompBase invalid + * @tc.name: FromJson005 + * @tc.desc: Test location button from wrong size json * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SecCompBaseTest, IsValid001, TestSize.Level1) +HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) { + nlohmann::json jsonComponent; SecCompBase comp; - comp.type_ = UNKNOWN_SC_TYPE; - ASSERT_FALSE(comp.IsValid()); + BuildSecCompBaseInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, WRONG_TYPE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, WRONG_TYPE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); - comp.type_ = LOCATION_COMPONENT; - comp.rect_.x_ = WRONG_COORDINATE; - ASSERT_FALSE(comp.IsValid()); + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, WRONG_TYPE }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, WRONG_TYPE }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,WRONG_TYPE }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, WRONG_TYPE }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson006 + * @tc.desc: Test location button from wrong border and parent json + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompBaseTest, FromJson006, TestSize.Level1) +{ + nlohmann::json jsonComponent; + SecCompBase comp; + BuildSecCompBaseInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, WRONG_TYPE }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + BuildSecCompBaseInfo(jsonComponent); + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); } /** @@ -185,31 +347,59 @@ HWTEST_F(SecCompBaseTest, IsValid001, TestSize.Level1) */ HWTEST_F(SecCompBaseTest, Compare001, TestSize.Level1) { + nlohmann::json jsonComponent; SecCompBase comp1; - comp1.type_ = LOCATION_COMPONENT; - comp1.rect_.x_ = TEST_COORDINATE; - comp1.rect_.y_ = TEST_COORDINATE; - comp1.rect_.width_ = TEST_COORDINATE; - comp1.rect_.height_ = TEST_COORDINATE; + BuildSecCompBaseInfo(jsonComponent); + ASSERT_TRUE(comp1.FromJson(jsonComponent)); SecCompBase comp2 = comp1; comp1.type_ = SAVE_COMPONENT; - ASSERT_FALSE(comp1 == comp2); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); comp1.type_ = LOCATION_COMPONENT; - comp1.rect_.x_ = TEST_DIFF_COORDINATE; - ASSERT_FALSE(comp1 == comp2); - comp1.rect_.x_ = TEST_COORDINATE; + comp1.fontSize_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.fontSize_ = TEST_SIZE; + + comp1.iconSize_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.iconSize_ = TEST_SIZE; + + comp1.padding_.top = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.top = TEST_DIMENSION; + + comp1.padding_.right = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.right = TEST_DIMENSION; + + comp1.padding_.bottom = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.bottom = TEST_DIMENSION; + + comp1.padding_.left = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.left = TEST_DIMENSION; + + comp1.textIconPadding_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.textIconPadding_ = TEST_SIZE; + + comp1.borderWidth_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.borderWidth_ = TEST_SIZE; + + comp1.fontColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.fontColor_.value = TEST_COLOR_2; - comp1.rect_.y_ = TEST_DIFF_COORDINATE; - ASSERT_FALSE(comp1 == comp2); - comp1.rect_.y_ = TEST_COORDINATE; + comp1.bgColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.bgColor_.value = TEST_COLOR_1; - comp1.rect_.width_ = TEST_DIFF_COORDINATE; - ASSERT_FALSE(comp1 == comp2); - comp1.rect_.width_ = TEST_COORDINATE; + comp1.iconColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.iconColor_.value = TEST_COLOR_3; - comp1.rect_.height_ = TEST_DIFF_COORDINATE; - ASSERT_FALSE(comp1 == comp2); - comp1.rect_.height_ = TEST_COORDINATE; + ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 1821e8b..9bcdc9c 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -25,8 +25,9 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompKitTest"}; -static constexpr uint32_t TEST_FONT_SIZE = 100; +static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; +static constexpr double TEST_DIMENSION = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffffff; } // namespace @@ -58,22 +59,27 @@ void SecCompKitTest::TearDown() */ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) { - LocationButton button; - button.fontSize_ = TEST_FONT_SIZE; - button.font_ = FONT_HO_SANS; - button.bgColor_.value = TEST_COLOR; - button.fontColor_.value = TEST_COLOR; - button.icon_ = DEFAULT_ICON; - button.label_ = DEFAULT_LABEL; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.type_ = LOCATION_COMPONENT; - button.buttonType_ = CAPSULE; + SecCompBase comp; + comp.fontSize_ = TEST_SIZE; + comp.iconSize_ = TEST_SIZE; + comp.padding_.top = TEST_DIMENSION; + comp.padding_.right = TEST_DIMENSION; + comp.padding_.bottom = TEST_DIMENSION; + comp.padding_.left = TEST_DIMENSION; + comp.textIconPadding_ = TEST_SIZE; + comp.bgColor_.value = TEST_COLOR; + comp.fontColor_.value = TEST_COLOR; + comp.iconColor_.value = TEST_COLOR; + comp.borderWidth_ = TEST_SIZE; + comp.parentEffect_ = true; + comp.type_ = LOCATION_COMPONENT; + comp.rect_.x_ = TEST_COORDINATE; + comp.rect_.y_ = TEST_COORDINATE; + comp.rect_.width_ = TEST_COORDINATE; + comp.rect_.height_ = TEST_COORDINATE; nlohmann::json jsonRes; - button.ToJson(jsonRes); + comp.ToJson(jsonRes); int32_t scId = -1; ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonRes.dump(), scId)); ASSERT_EQ(-1, scId); @@ -82,7 +88,7 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) struct SecCompClickEvent touch = { .touchX = TEST_COORDINATE, .touchY = TEST_COORDINATE, - .timestamp = GetCurrentTime() + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonRes.dump(), touch)); EXPECT_NE(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 829b527..e5591ba 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -14,6 +14,16 @@ import("//base/security/security_component/security_component.gni") import("//build/ohos.gni") +config("security_component_service_config") { + include_dirs = [ + "sa_main", + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] +} + ohos_prebuilt_etc("security_component_service.rc") { source = "security_component_service.cfg" relative_install_dir = "init" @@ -49,9 +59,11 @@ ohos_shared_library("security_component_service") { ":security_component_service.rc", "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client" ] configs = [ "//base/security/security_component/config:coverage_flags" ] + public_configs = [ ":security_component_service_config" ] external_deps = [ "ability_runtime:app_manager", @@ -61,5 +73,6 @@ ohos_shared_library("security_component_service") { "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "window_manager:libdm", ] } diff --git a/services/security_component_service/sa/sa_main/app_state_observer.cpp b/services/security_component_service/sa/sa_main/app_state_observer.cpp index 000c0d7..ede34eb 100644 --- a/services/security_component_service/sa/sa_main/app_state_observer.cpp +++ b/services/security_component_service/sa/sa_main/app_state_observer.cpp @@ -90,6 +90,7 @@ void AppStateObserver::OnProcessStateChanged(const AppExecFwk::ProcessData &proc { if (processData.state == AppExecFwk::AppProcessState::APP_STATE_FOREGROUND) { AddProcessToForegroundSet(processData); + SecCompManager::GetInstance().NotifyProcessForeground(processData.uid); } else if (processData.state == AppExecFwk::AppProcessState::APP_STATE_BACKGROUND) { RemoveProcessFromForegroundSet(processData); SecCompManager::GetInstance().NotifyProcessBackground(processData.uid); diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index 70c7294..07bed0a 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -13,17 +13,17 @@ * limitations under the License. */ #include "sec_comp_entity.h" +#include #include "sec_comp_err.h" #include "sec_comp_info_helper.h" #include "sec_comp_log.h" -#include "sec_comp_tool.h" namespace OHOS { namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntity"}; -static constexpr uint64_t MAX_TOUCH_INTERVAL = 1000L; // 1000ms +static constexpr uint64_t MAX_TOUCH_INTERVAL = 1000000000L; // 1000ms } int32_t SecCompEntity::RevokeTempPermission() @@ -46,14 +46,14 @@ int32_t SecCompEntity::GrantTempPermission() return SecCompInfoHelper::GrantTempPermission(tokenId_, componentInfo_); } -bool SecCompEntity::CompareComponentInfo(const SecCompBase* other) const +bool SecCompEntity::CompareComponentBasicInfo(SecCompBase* other) const { - return SecCompInfoHelper::CompareSecCompInfo(componentInfo_.get(), other); + return componentInfo_->CompareComponentBasicInfo(other); } bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const { - uint64_t current = GetCurrentTime(); + auto current = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); if (touchInfo.timestamp < current - MAX_TOUCH_INTERVAL || touchInfo.timestamp > current) { SC_LOG_ERROR(LABEL, "touch timestamp invalid"); return false; @@ -65,17 +65,6 @@ bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const } return true; } - -bool SecCompEntity::IsRectOverlaped(const SecCompEntity& other) const -{ - std::shared_ptr otherInfo = other.GetComponentInfo(); - if (otherInfo == nullptr) { - SC_LOG_ERROR(LABEL, "component info is null"); - return true; - } - - return componentInfo_->rect_.IsRectOverlaped(otherInfo->rect_); -} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.h b/services/security_component_service/sa/sa_main/sec_comp_entity.h index acd1feb..7b4de17 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.h +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.h @@ -59,8 +59,22 @@ public: componentInfo_ = com; }; - bool CompareComponentInfo(const SecCompBase* other) const; - bool IsRectOverlaped(const SecCompEntity& other) const; + std::shared_ptr GetComponentInfo() + { + return componentInfo_; + }; + + bool GetEffective() const + { + return isEffective_; + }; + + void SetEffective(bool effective) + { + isEffective_ = effective; + }; + + bool CompareComponentBasicInfo(SecCompBase* other) const; bool CheckTouchInfo(const SecCompClickEvent& touchInfo) const; private: @@ -68,6 +82,7 @@ private: bool isGrant_ = false; AccessToken::AccessTokenID tokenId_; int32_t scId_; + bool isEffective_ = false; }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 855a0d7..8b36fa9 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -15,17 +15,24 @@ #include "sec_comp_info_helper.h" +#include "display.h" +#include "display_info.h" +#include "display_manager.h" #include "location_button.h" #include "paste_button.h" #include "save_button.h" #include "sec_comp_err.h" #include "sec_comp_log.h" +#include "sec_comp_tool.h" namespace OHOS { namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelper"}; +static constexpr double MAX_RECT_PERCENT = 0.1F; // 10% +static constexpr double ZERO_OFFSET = 0.0F; +static std::mutex g_renderLock; } SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const nlohmann::json& jsonComponent) @@ -45,41 +52,158 @@ SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const nlohmann: SC_LOG_ERROR(LABEL, "Parse component type unknown"); break; } - if (comp != nullptr && !comp->IsValid()) { - delete comp; - return nullptr; + if (comp == nullptr) { + SC_LOG_ERROR(LABEL, "Parse component failed"); + return comp; } + + comp->SetValid(CheckComponentValid(comp)); return comp; } -bool SecCompInfoHelper::CompareSecCompInfo(const SecCompBase* comA, const SecCompBase* comB) +static bool GetScreenSize(double& width, double& height) +{ + sptr display = + OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync(); + if (display == nullptr) { + SC_LOG_ERROR(LABEL, "Get display manager failed"); + return false; + } + + auto info = display->GetDisplayInfo(); + if (info == nullptr) { + SC_LOG_ERROR(LABEL, "Get display info failed"); + return false; + } + + width = static_cast(info->GetWidth()); + height = static_cast(info->GetHeight()); + SC_LOG_DEBUG(LABEL, "display manager Screen width %{public}f height %{public}f", + width, height); + return true; +} + +static bool CheckRectValid(const SecCompRect& rect) +{ + double curScreenWidth = 0.0F; + double curScreenHeight = 0.0F; + if (!GetScreenSize(curScreenWidth, curScreenHeight)) { + SC_LOG_ERROR(LABEL, "Get screen size is invalid"); + return false; + } + + if (GreatNotEqual(ZERO_OFFSET, rect.x_) || GreatNotEqual(ZERO_OFFSET, rect.y_) || + GreatNotEqual(rect.x_, curScreenWidth) || GreatNotEqual(rect.y_, curScreenHeight)) { + SC_LOG_ERROR(LABEL, "start point is out of screen"); + return false; + } + + if (GreatOrEqual((rect.x_ + rect.width_), curScreenWidth) || + GreatOrEqual((rect.y_ + rect.height_), curScreenHeight)) { + SC_LOG_ERROR(LABEL, "rect is out of screen"); + return false; + } + + // check rect > 10% + if (GreatOrEqual((rect.width_ * rect.height_), (curScreenWidth * curScreenHeight * MAX_RECT_PERCENT))) { + SC_LOG_ERROR(LABEL, "rect area is too large"); + return false; + } + + return true; +} + +static bool CheckSecCompBase(const SecCompBase* comp) +{ + if (!CheckRectValid(comp->rect_)) { + SC_LOG_INFO(LABEL, "check component rect failed."); + return false; + } + + if (comp->parentEffect_) { + SC_LOG_ERROR(LABEL, "parentEffect is active, security component invalid."); + return false; + } + + if ((comp->fontSize_ < MIN_FONT_SIZE) || (comp->iconSize_ < MIN_ICON_SIZE) || + (comp->padding_.top < MIN_PADDING_SIZE) || (comp->padding_.right < MIN_PADDING_SIZE) || + (comp->padding_.bottom < MIN_PADDING_SIZE) || (comp->padding_.left < MIN_PADDING_SIZE) || + (comp->textIconPadding_ < MIN_PADDING_SIZE)) { + SC_LOG_ERROR(LABEL, "size is invalid."); + return false; + } + + if (IsColorTransparent(comp->bgColor_) || IsColorTransparent(comp->fontColor_) || + IsColorTransparent(comp->iconColor_)) { + SC_LOG_ERROR(LABEL, "bgColor or fontColor or iconColor is too transparent."); + return false; + } + + return true; +} + +static bool CheckLocationButton(const LocationButton* comp) +{ + if ((comp->text_ == LocationDesc::NO_TEXT) && (comp->icon_ == LocationIcon::NO_ICON)) { + SC_LOG_INFO(LABEL, "both text and icon do not exist."); + return false; + } + + if (comp->bg_ != LocationBackground::NO_BG_TYPE && + (IsColorSimilar(comp->fontColor_, comp->bgColor_) || IsColorSimilar(comp->iconColor_, comp->bgColor_))) { + SC_LOG_INFO(LABEL, "fontColor or iconColor is similar whith backgroundColor."); + return false; + } + + if (comp->bg_ == LocationBackground::NO_BG_TYPE && + ((comp->padding_.top != MIN_PADDING_WITHOUT_BG) || (comp->padding_.right != MIN_PADDING_WITHOUT_BG) || + (comp->padding_.bottom != MIN_PADDING_WITHOUT_BG) || (comp->padding_.left != MIN_PADDING_WITHOUT_BG))) { + SC_LOG_INFO(LABEL, "padding can not change without background."); + return false; + } + + return true; +} + +static bool CheckPasteButton(const PasteButton* comp) { - if (comA == nullptr || comB == nullptr) { - SC_LOG_ERROR(LABEL, "Security component info is null"); + return false; +} + +static bool CheckSaveButton(const SaveButton* comp) +{ + return false; +} + +bool SecCompInfoHelper::CheckComponentValid(const SecCompBase* comp) +{ + if ((comp == nullptr) || !IsComponentTypeValid(comp->type_)) { + SC_LOG_INFO(LABEL, "comp is null or type is invalid."); return false; } - if (comA->type_ != comB->type_) { - SC_LOG_ERROR(LABEL, "Security component type is not equal"); + if (!CheckSecCompBase(comp)) { + SC_LOG_INFO(LABEL, "SecComp base is invalid."); return false; } - bool res = false; - switch (comA->type_) { + bool valid = false; + switch (comp->type_) { case LOCATION_COMPONENT: - res = *reinterpret_cast(comA) == *reinterpret_cast(comB); + valid = CheckLocationButton(reinterpret_cast(comp)); break; case PASTE_COMPONENT: - res = *reinterpret_cast(comA) == *reinterpret_cast(comB); + valid = CheckPasteButton(reinterpret_cast(comp)); break; case SAVE_COMPONENT: - res = *reinterpret_cast(comA) == *reinterpret_cast(comB); + valid = CheckSaveButton(reinterpret_cast(comp)); break; default: - SC_LOG_ERROR(LABEL, "Compare component component type unknown"); + SC_LOG_ERROR(LABEL, "Parse component type unknown"); break; } - return res; + + return valid; } int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID tokenId, @@ -93,7 +217,7 @@ int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID token switch (type) { case LOCATION_COMPONENT: return AccessToken::AccessTokenKit::RevokePermission(tokenId, "ohos.permission.LOCATION", - AccessToken::PermissionFlag::PERMISSION_DEFAULT_FLAG); + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); default: SC_LOG_ERROR(LABEL, "revoke component type unknown"); break; @@ -112,7 +236,7 @@ int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenI switch (type) { case LOCATION_COMPONENT: return AccessToken::AccessTokenKit::GrantPermission(tokenId, "ohos.permission.LOCATION", - AccessToken::PermissionFlag::PERMISSION_DEFAULT_FLAG); + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); default: SC_LOG_ERROR(LABEL, "Parse component type unknown"); break; diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h index 05d8cae..17879dd 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h @@ -27,8 +27,9 @@ template T* ConstructComponent(const nlohmann::json& jsonComponent) { T *componentPtr = new (std::nothrow)T(); - if (componentPtr != nullptr) { - componentPtr->FromJson(jsonComponent); + if ((componentPtr != nullptr) && !componentPtr->FromJson(jsonComponent)) { + delete componentPtr; + return nullptr; } return componentPtr; } @@ -36,11 +37,11 @@ T* ConstructComponent(const nlohmann::json& jsonComponent) class SecCompInfoHelper { public: static SecCompBase* ParseComponent(SecCompType type, const nlohmann::json& jsonComponent); - static bool CompareSecCompInfo(const SecCompBase* comA, const SecCompBase* comB); static int32_t RevokeTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo); static int32_t GrantTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo); + static bool CheckComponentValid(const SecCompBase* comp); }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 53020fe..e911305 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -34,7 +34,7 @@ static constexpr int32_t MAX_INT_NUM = 0x7fffffff; SecCompManager::SecCompManager() { scIdStart_ = SC_ID_START; - scCount_ = 0; + scValidCount_ = 0; SC_LOG_INFO(LABEL, "SecCompManager()"); } @@ -58,18 +58,11 @@ int32_t SecCompManager::CreateScId() int32_t SecCompManager::AddProcessComponent(std::vector& componentList, const SecCompEntity& newEntity) { - for (auto it = componentList.begin(); it != componentList.end(); ++it) { - if (it->IsRectOverlaped(newEntity)) { - SC_LOG_ERROR(LABEL, "Components overlap"); - return SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP; - } - } - componentList.emplace_back(newEntity); - if (scCount_ == 0) { + if (scValidCount_ == 0) { SecCompEnhanceAdapter::EnableInputEnhance(); } - scCount_++; + scValidCount_++; return SC_OK; } @@ -99,8 +92,11 @@ int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t uid, int32_t scI for (auto it = list.begin(); it != list.end(); ++it) { if (it->GetScId() == scId) { it->RevokeTempPermission(); + if (it->GetEffective()) { + scValidCount_--; + } + list.erase(it); - scCount_--; return SC_OK; } } @@ -123,6 +119,25 @@ SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t uid, int32_t return nullptr; } +void SecCompManager::NotifyProcessForeground(int32_t uid) +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + auto iter = componentMap_.find(uid); + if (iter == componentMap_.end()) { + return; + } + + SC_LOG_INFO(LABEL, "App uid %{public}d to foreground", uid); + std::vector& list = iter->second; + for (auto it = list.begin(); it != list.end(); ++it) { + it->SetEffective(true); + scValidCount_++; + } + if (!list.empty()) { + SecCompEnhanceAdapter::EnableInputEnhance(); + } +} + void SecCompManager::NotifyProcessBackground(int32_t uid) { OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); @@ -131,14 +146,14 @@ void SecCompManager::NotifyProcessBackground(int32_t uid) return; } - SC_LOG_INFO(LABEL, "uid %{public}d to background", uid); + SC_LOG_INFO(LABEL, "App uid %{public}d to background", uid); std::vector& list = iter->second; for (auto it = list.begin(); it != list.end(); ++it) { it->RevokeTempPermission(); - scCount_--; + it->SetEffective(false); + scValidCount_--; } - list.clear(); - if (scCount_ <= 0) { + if (scValidCount_ <= 0) { SecCompEnhanceAdapter::DisableInputEnhance(); } } @@ -150,14 +165,16 @@ void SecCompManager::NotifyProcessDied(int32_t uid) if (iter == componentMap_.end()) { return; } - SC_LOG_INFO(LABEL, "uid %{public}d died", uid); + SC_LOG_INFO(LABEL, "App uid %{public}d died", uid); std::vector& list = iter->second; for (auto it = list.begin(); it != list.end(); ++it) { it->RevokeTempPermission(); - scCount_--; + if (it->GetEffective()) { + scValidCount_--; + } } list.clear(); - if (scCount_ <= 0) { + if (scValidCount_ <= 0) { SecCompEnhanceAdapter::DisableInputEnhance(); } componentMap_.erase(uid); @@ -166,20 +183,21 @@ void SecCompManager::NotifyProcessDied(int32_t uid) void SecCompManager::ExitSaAfterAllProcessDie() { - if (componentMap_.empty()) { - SC_LOG_INFO(LABEL, "All processes using security component died, start sa exit"); - auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityMgr == nullptr) { - SC_LOG_ERROR(LABEL, "Failed to get SystemAbilityManager."); - return; - } - int32_t ret = systemAbilityMgr->UnloadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE); - if (ret != SC_OK) { - SC_LOG_ERROR(LABEL, "Failed to UnloadSystemAbility service! errcode=%{public}d", ret); - return; - } - SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); + if (!componentMap_.empty()) { + return; } + SC_LOG_INFO(LABEL, "All processes using security component died, start sa exit"); + auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + SC_LOG_ERROR(LABEL, "Failed to get SystemAbilityManager."); + return; + } + int32_t ret = systemAbilityMgr->UnloadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE); + if (ret != SC_OK) { + SC_LOG_ERROR(LABEL, "Failed to UnloadSystemAbility service! errcode=%{public}d", ret); + return; + } + SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); } int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, @@ -194,6 +212,7 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, int32_t registerId = CreateScId(); SecCompEntity entity(component, caller.tokenId, registerId); + entity.SetEffective(true); int32_t ret = AddSecurityComponentToList(caller.uid, entity); if (ret == SC_OK) { scId = registerId; @@ -230,7 +249,7 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t res = DeleteSecurityComponentFromList(caller.uid, scId); - if (res == SC_OK && scCount_ <= 0) { + if (scValidCount_ <= 0) { SecCompEnhanceAdapter::DisableInputEnhance(); } return res; @@ -247,17 +266,21 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, } SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); - std::unique_ptr reportComponentInfo(report); - if (reportComponentInfo == nullptr) { + std::shared_ptr reportComponentInfo(report); + if ((reportComponentInfo == nullptr) || !reportComponentInfo->GetValid()) { SC_LOG_ERROR(LABEL, "report component info invalid"); return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } - if (!sc->CompareComponentInfo(reportComponentInfo.get())) { - SC_LOG_ERROR(LABEL, "Report component compare register component failed"); + // BasicInfo is the attributes of security component defined in ets, such as fontsize. + // we only compare these because they can be obtained during registration/update, + // but other infos such as position may not be notified because changs occur at the parent node. + if (!sc->CompareComponentBasicInfo(reportComponentInfo.get())) { + SC_LOG_ERROR(LABEL, "Report component basic compare register component failed"); return SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL; } + sc->SetComponentInfo(reportComponentInfo); if (!sc->CheckTouchInfo(touchInfo)) { SC_LOG_ERROR(LABEL, "touchInfo is invalid"); return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index d02dffa..5f4488c 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -51,6 +51,7 @@ public: int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo); + void NotifyProcessForeground(int32_t uid); void NotifyProcessBackground(int32_t uid); void NotifyProcessDied(int32_t uid); void DumpSecComp(std::string& dumpStr) const; @@ -69,7 +70,7 @@ private: OHOS::Utils::RWLock componentInfoLock_; std::mutex scIdMtx_; std::unordered_map> componentMap_; - int32_t scCount_; + int32_t scValidCount_; int32_t scIdStart_; bool Initialize() const; diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 78db19d..4abac30 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -30,12 +30,6 @@ ohos_unittest("sec_comp_service_test") { ] sources = [ - "unittest/src/app_state_observer_test.cpp", - "unittest/src/sec_comp_entity_test.cpp", - "unittest/src/sec_comp_info_helper_test.cpp", - "unittest/src/sec_comp_manager_test.cpp", - "unittest/src/sec_comp_service_test.cpp", - "unittest/src/sec_comp_stub_test.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", @@ -44,6 +38,11 @@ ohos_unittest("sec_comp_service_test") { "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", + "unittest/src/app_state_observer_test.cpp", + "unittest/src/sec_comp_entity_test.cpp", + "unittest/src/sec_comp_manager_test.cpp", + "unittest/src/sec_comp_service_test.cpp", + "unittest/src/sec_comp_stub_test.cpp", ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -51,14 +50,16 @@ ohos_unittest("sec_comp_service_test") { deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client", "//third_party/googletest:gmock_main", ] external_deps = [ + "access_token:libtoken_setproc", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", - "access_token:libtoken_setproc", + "window_manager:libdm", ] } diff --git a/services/security_component_service/sa/test/mock/include/access_token.h b/services/security_component_service/sa/test/mock/include/access_token.h index 1640ba4..a2314f9 100644 --- a/services/security_component_service/sa/test/mock/include/access_token.h +++ b/services/security_component_service/sa/test/mock/include/access_token.h @@ -33,6 +33,8 @@ typedef enum TypePermissionFlag { PERMISSION_USER_FIXED = 1 << 1, PERMISSION_SYSTEM_FIXED = 1 << 2, PERMISSION_GRANTED_BY_POLICY = 1 << 3, + PERMISSION_COMPONENT_SET = 1 << 4, + PERMISSION_POLICY_FIXED = 1 << 5, } PermissionFlag; typedef struct { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index e4a2609..579eca1 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -25,8 +25,6 @@ using namespace OHOS::Security::AccessToken; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntityTest"}; -static constexpr int32_t TEST_SC_ID = 1; -static constexpr AccessTokenID TEST_TOKEN_ID = 1; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIFF_COORDINATE = 200.0; } @@ -115,7 +113,7 @@ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) }; ASSERT_FALSE(entity_->CheckTouchInfo(touch)); - uint64_t current = GetCurrentTime(); + uint64_t current = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); touch.timestamp = current + 10000L; // 10s ASSERT_FALSE(entity_->CheckTouchInfo(touch)); @@ -123,36 +121,11 @@ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) entity_->componentInfo_->rect_.y_ = TEST_DIFF_COORDINATE; entity_->componentInfo_->rect_.width_ = TEST_DIFF_COORDINATE; entity_->componentInfo_->rect_.height_ = TEST_DIFF_COORDINATE; - touch.timestamp = GetCurrentTime(); + touch.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); ASSERT_FALSE(entity_->CheckTouchInfo(touch)); entity_->componentInfo_->rect_.x_ = TEST_COORDINATE; entity_->componentInfo_->rect_.y_ = TEST_COORDINATE; - touch.timestamp = GetCurrentTime(); + touch.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); ASSERT_TRUE(entity_->CheckTouchInfo(touch)); } - -/** - * @tc.name: IsRectOverlaped001 - * @tc.desc: Test check rect overlap - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompEntityTest, IsRectOverlaped001, TestSize.Level1) -{ - SecCompEntity other(nullptr, TEST_TOKEN_ID, TEST_SC_ID); - ASSERT_TRUE(entity_->IsRectOverlaped(other)); - - entity_->componentInfo_->rect_.x_ = TEST_COORDINATE; - entity_->componentInfo_->rect_.y_ = TEST_COORDINATE; - entity_->componentInfo_->rect_.width_ = TEST_COORDINATE; - entity_->componentInfo_->rect_.height_ = TEST_COORDINATE; - - other.componentInfo_ = std::make_shared(); - ASSERT_NE(other.componentInfo_, nullptr); - other.componentInfo_->rect_.x_ = TEST_COORDINATE; - other.componentInfo_->rect_.y_ = TEST_COORDINATE; - other.componentInfo_->rect_.width_ = TEST_COORDINATE; - other.componentInfo_->rect_.height_ = TEST_COORDINATE; - ASSERT_TRUE(entity_->IsRectOverlaped(other)); -} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index ce83267..e080eb8 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -19,6 +19,9 @@ #include "save_button.h" #include "sec_comp_log.h" #include "sec_comp_err.h" +#include "display.h" +#include "display_info.h" +#include "display_manager.h" using namespace testing::ext; using namespace OHOS; @@ -27,32 +30,120 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelperTest"}; +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; -static constexpr uint32_t TEST_FONT_SIZE = 100; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr uint32_t TEST_COLOR = 0xffffffff; +static const std::string JSON_RECT = "rect"; +static const std::string JSON_SC_TYPE = "type"; +static const std::string JSON_RECT_X = "x"; +static const std::string JSON_RECT_Y = "y"; +static const std::string JSON_RECT_WIDTH = "width"; +static const std::string JSON_RECT_HEIGHT = "height"; -static LocationButton BuildValidLocationComponent() +static const std::string JSON_SIZE_TAG = "size"; +static const std::string JSON_FONT_SIZE_TAG = "fontSize"; +static const std::string JSON_ICON_SIZE_TAG = "iconSize"; +static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; +static const std::string JSON_PADDING_LEFT_TAG = "left"; +static const std::string JSON_PADDING_TOP_TAG = "top"; +static const std::string JSON_PADDING_RIGHT_TAG = "right"; +static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_RECT_WIDTH_TAG = "width"; +static const std::string JSON_RECT_HEIGHT_TAG = "height"; + +static const std::string JSON_COLORS_TAG = "colors"; +static const std::string JSON_FONT_COLOR_TAG = "fontColor"; +static const std::string JSON_ICON_COLOR_TAG = "iconColor"; +static const std::string JSON_BG_COLOR_TAG = "bgColor"; + +static const std::string JSON_BORDER_TAG = "border"; +static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; +static const std::string JSON_PARENT_TAG = "parent"; +static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; + +static constexpr float TEST_SIZE = 100.0; +static constexpr double TEST_DIMENSION = 100.0; +static constexpr double TEST_INVALID_DIMENSION = -100.0; +static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; +static constexpr uint32_t TEST_COLOR_2 = 0xff0000; +static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; +static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; +static constexpr double ZERO_OFFSET = 0.0F; +static double CUR_SCREEN_WIDTH = 0.0F; +static double CUR_SCREEN_HEIGHT = 0.0F; +static double TEST_WIDTH = 0.0F; +static double TEST_HEIGHT = 0.0F; +static uint32_t QUARTER = 4; +static bool GetScreenSize() +{ + sptr display = + OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync(); + if (display == nullptr) { + return false; + } + + auto info = display->GetDisplayInfo(); + if (info == nullptr) { + return false; + } + + CUR_SCREEN_WIDTH = static_cast(info->GetWidth()); + CUR_SCREEN_HEIGHT = static_cast(info->GetHeight()); + return true; +} + +static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { - LocationButton button; - button.fontSize_ = TEST_FONT_SIZE; - button.font_ = FONT_HO_SANS; - button.bgColor_.value = TEST_COLOR; - button.fontColor_.value = TEST_COLOR; - button.icon_ = DEFAULT_ICON; - button.label_ = DEFAULT_LABEL; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.type_ = LOCATION_COMPONENT; - button.buttonType_ = CAPSULE; - return button; + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_Y, TEST_HEIGHT }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + + jsonComponent[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; } } void SecCompInfoHelperTest::SetUpTestCase() -{} +{ + ASSERT_TRUE(GetScreenSize()); + TEST_WIDTH = (ZERO_OFFSET + CUR_SCREEN_WIDTH) / QUARTER; + TEST_HEIGHT = (ZERO_OFFSET + CUR_SCREEN_HEIGHT) / QUARTER; +} void SecCompInfoHelperTest::TearDownTestCase() {} @@ -67,56 +158,464 @@ void SecCompInfoHelperTest::TearDown() /** * @tc.name: ParseComponent001 - * @tc.desc: Test parse component info + * @tc.desc: Test parse component info success * @tc.type: FUNC * @tc.require: */ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_NE(comp, nullptr); + ASSERT_TRUE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent002 + * @tc.desc: Test parse component info with empty json + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) { nlohmann::json jsonComponent; ASSERT_EQ(SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent), nullptr); - LocationButton button = BuildValidLocationComponent(); - button.fontSize_ = 0; - button.ToJson(jsonComponent); ASSERT_EQ(SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent), nullptr); - ASSERT_NE(SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent), nullptr); - ASSERT_NE(SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent), nullptr); + ASSERT_EQ(SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent), nullptr); + ASSERT_EQ(SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent), nullptr); } /** - * @tc.name: CompareSecCompInfo001 - * @tc.desc: Test compare sec comp info + * @tc.name: ParseComponent003 + * @tc.desc: Test parse component info with invalid type * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SecCompInfoHelperTest, CompareSecCompInfo001, TestSize.Level1) +HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) { - SecCompBase comp1; - ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(&comp1, nullptr)); - ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(nullptr, &comp1)); - - SecCompBase comp2; - comp2.type_ = UNKNOWN_SC_TYPE; - comp1.type_ = LOCATION_COMPONENT; - ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(&comp1, &comp2)); - - comp1.type_ = UNKNOWN_SC_TYPE; - ASSERT_FALSE(SecCompInfoHelper::CompareSecCompInfo(&comp1, &comp2)); - - LocationButton locationButton1 = BuildValidLocationComponent(); - LocationButton locationButton2 = BuildValidLocationComponent(); - ASSERT_TRUE(SecCompInfoHelper::CompareSecCompInfo(&locationButton1, &locationButton2)); - - PasteButton pasteButton; - pasteButton.type_ = PASTE_COMPONENT; - pasteButton.rect_.x_ = TEST_COORDINATE; - pasteButton.rect_.y_ = TEST_COORDINATE; - ASSERT_TRUE(SecCompInfoHelper::CompareSecCompInfo(&pasteButton, &pasteButton)); - - SaveButton saveButton; - saveButton.type_ = SAVE_COMPONENT; - saveButton.rect_.x_ = TEST_COORDINATE; - saveButton.rect_.y_ = TEST_COORDINATE; - ASSERT_TRUE(SecCompInfoHelper::CompareSecCompInfo(&saveButton, &saveButton)); + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + jsonComponent[JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent004 + * @tc.desc: Test parse component info with invalid rect + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_INVALID_DIMENSION }, + {JSON_RECT_Y, TEST_HEIGHT }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_Y, TEST_INVALID_DIMENSION }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, CUR_SCREEN_WIDTH }, + {JSON_RECT_Y, TEST_HEIGHT }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_Y, CUR_SCREEN_HEIGHT }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_Y, TEST_HEIGHT }, + {JSON_RECT_WIDTH, CUR_SCREEN_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_Y, TEST_HEIGHT }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, CUR_SCREEN_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, CUR_SCREEN_WIDTH - TEST_WIDTH }, + {JSON_RECT_Y, TEST_HEIGHT }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_Y, CUR_SCREEN_HEIGHT - TEST_HEIGHT }, + {JSON_RECT_WIDTH, TEST_WIDTH }, + {JSON_RECT_HEIGHT, TEST_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_Y, TEST_HEIGHT }, + {JSON_RECT_WIDTH, CUR_SCREEN_WIDTH * CUR_SCREEN_HEIGHT }, + {JSON_RECT_HEIGHT, CUR_SCREEN_WIDTH * CUR_SCREEN_HEIGHT } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent005 + * @tc.desc: Test parse component info with parentEffect active + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, true }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent006 + * @tc.desc: Test parse component info with invalid size + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_INVALID_DIMENSION }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_INVALID_DIMENSION }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_INVALID_DIMENSION }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_INVALID_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_INVALID_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_INVALID_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_INVALID_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent007 + * @tc.desc: Test parse component info with invalid color + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_INVALID }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_INVALID }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_INVALID } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent008 + * @tc.desc: Test parse component info with invalid style + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::NO_TEXT }, + { JSON_ICON_TAG, LocationIcon::NO_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_1 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::CIRCLE }, + }; + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_1 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::NO_BG_TYPE }, + }; + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_1 }, + { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::NO_BG_TYPE }, + }; + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 9c16099..d71b176 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -27,18 +27,20 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompManagerTest"}; -static constexpr uint32_t TEST_FONT_SIZE = 100; -static constexpr uint32_t TEST_DIFF_FONT_SIZE = 16; -static constexpr uint32_t TEST_INVALID_FONT_SIZE = 0; +static constexpr double TEST_SIZE = 100.0; +static constexpr uint32_t TEST_DIFF_SIZE = 16; +static constexpr uint32_t TEST_INVALID_SIZE = 0; static constexpr double TEST_COORDINATE = 100.0; -static constexpr uint32_t TEST_COLOR = 0xffffffff; +static constexpr uint32_t TEST_COLOR = 0xffffff; +static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; +static constexpr uint32_t TEST_COLOR_2 = 0xff0000; +static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; static constexpr int32_t TEST_UID_1 = 1; static constexpr int32_t TEST_UID_2 = 2; static constexpr int32_t TEST_UID_3 = 3; -static constexpr AccessTokenID TEST_TOKEN_ID_1 = 1; -static constexpr AccessTokenID TEST_TOKEN_ID_2 = 2; +static constexpr AccessTokenID TEST_TOKEN_ID = 1; static constexpr int32_t TEST_SC_ID_1 = 1; static constexpr int32_t TEST_SC_ID_2 = 2; static constexpr int32_t TEST_INVALID_SC_ID = -1; @@ -49,36 +51,50 @@ static constexpr int32_t SC_ID_START = 1000; static LocationButton BuildInvalidLocationComponent() { LocationButton button; - button.fontSize_ = TEST_INVALID_FONT_SIZE; - button.font_ = FONT_HO_SANS; - button.bgColor_.value = TEST_COLOR; + button.fontSize_ = TEST_INVALID_SIZE; + button.iconSize_ = TEST_INVALID_SIZE; + button.padding_.top = TEST_INVALID_SIZE; + button.padding_.right = TEST_INVALID_SIZE; + button.padding_.bottom = TEST_INVALID_SIZE; + button.padding_.left = TEST_INVALID_SIZE; + button.textIconPadding_ = TEST_INVALID_SIZE; button.fontColor_.value = TEST_COLOR; - button.icon_ = DEFAULT_ICON; - button.label_ = DEFAULT_LABEL; + button.iconColor_.value = TEST_COLOR; + button.bgColor_.value = TEST_COLOR; + button.borderWidth_ = TEST_INVALID_SIZE; + button.type_ = LOCATION_COMPONENT; button.rect_.x_ = TEST_COORDINATE; button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; - button.type_ = LOCATION_COMPONENT; - button.buttonType_ = CAPSULE; + button.text_ = LocationDesc::UNKNOWN_TEXT; + button.icon_ = LocationIcon::UNKNOWN_ICON; + button.bg_ = LocationBackground::UNKNOWN_BG; return button; } static LocationButton BuildValidLocationComponent() { LocationButton button; - button.fontSize_ = TEST_FONT_SIZE; - button.font_ = FONT_HO_SANS; - button.bgColor_.value = TEST_COLOR; - button.fontColor_.value = TEST_COLOR; - button.icon_ = DEFAULT_ICON; - button.label_ = DEFAULT_LABEL; + button.fontSize_ = TEST_SIZE; + button.iconSize_ = TEST_SIZE; + button.padding_.top = TEST_SIZE; + button.padding_.right = TEST_SIZE; + button.padding_.bottom = TEST_SIZE; + button.padding_.left = TEST_SIZE; + button.textIconPadding_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_1; + button.iconColor_.value = TEST_COLOR_2; + button.bgColor_.value = TEST_COLOR_3; + button.borderWidth_ = TEST_SIZE; + button.type_ = LOCATION_COMPONENT; button.rect_.x_ = TEST_COORDINATE; button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; - button.type_ = LOCATION_COMPONENT; - button.buttonType_ = CAPSULE; + button.text_ = LocationDesc::SELECT_LOCATION; + button.icon_ = LocationIcon::LINE_ICON; + button.bg_ = LocationBackground::CIRCLE; return button; } } @@ -129,19 +145,9 @@ HWTEST_F(SecCompManagerTest, AddProcessComponent001, TestSize.Level1) compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); std::vector componentList; ASSERT_EQ(SecCompManager::GetInstance().AddProcessComponent(componentList, entity), SC_OK); - - std::shared_ptr compPtrNew = std::make_shared(); - ASSERT_NE(compPtrNew, nullptr); - compPtrNew->rect_.x_ = TEST_COORDINATE + 1; // offset 1 pixel - compPtrNew->rect_.y_ = TEST_COORDINATE; - compPtrNew->rect_.width_ = TEST_COORDINATE; - compPtrNew->rect_.height_ = TEST_COORDINATE; - SecCompEntity entityNew(compPtrNew, TEST_TOKEN_ID_2, TEST_SC_ID_2); - ASSERT_EQ(SecCompManager::GetInstance().AddProcessComponent(componentList, entityNew), - SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP); } /** @@ -158,11 +164,9 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entity), SC_OK); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entity), - SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP); std::shared_ptr compPtrNew = std::make_shared(); ASSERT_NE(compPtrNew, nullptr); @@ -170,7 +174,7 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) compPtrNew->rect_.y_ = TEST_COORDINATE * 2; // not overlap compPtrNew->rect_.width_ = TEST_COORDINATE; compPtrNew->rect_.height_ = TEST_COORDINATE; - SecCompEntity entityNew(compPtrNew, TEST_TOKEN_ID_1, TEST_SC_ID_2); + SecCompEntity entityNew(compPtrNew, TEST_TOKEN_ID, TEST_SC_ID_2); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entityNew), SC_OK); } @@ -191,7 +195,7 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_2), @@ -216,7 +220,7 @@ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_2), nullptr); ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); @@ -238,11 +242,16 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); - ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1); + ASSERT_NE(component, nullptr); + SecCompManager::GetInstance().NotifyProcessForeground(TEST_UID_1); + ASSERT_TRUE(component->isEffective_); + SecCompManager::GetInstance().NotifyProcessBackground(TEST_UID_1); - ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + ASSERT_NE(component, nullptr); + ASSERT_FALSE(component->isEffective_); } /** @@ -259,7 +268,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID_1, TEST_SC_ID_1); + SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); std::shared_ptr compPtr2 = std::make_shared(); @@ -268,7 +277,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) compPtr2->rect_.y_ = TEST_COORDINATE * 2; // not overlap compPtr2->rect_.width_ = TEST_COORDINATE; compPtr2->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity2(compPtr2, TEST_TOKEN_ID_1, TEST_SC_ID_2); + SecCompEntity entity2(compPtr2, TEST_TOKEN_ID, TEST_SC_ID_2); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_2, entity2), SC_OK); SecCompManager::GetInstance().NotifyProcessDied(TEST_UID_3); @@ -291,7 +300,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) buttonInvalid.ToJson(jsonInvalid); int32_t scId; SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID_1, + .tokenId = TEST_TOKEN_ID, .uid = TEST_UID_1 }; ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId), @@ -301,11 +310,8 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); - ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId), - SC_OK); - ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId), - SC_SERVICE_ERROR_COMPONENT_RECT_OVERLAP); - ASSERT_EQ(scId, INVALID_SC_ID); + ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent( + LOCATION_COMPONENT, jsonValid, caller, scId), SC_OK); } /** @@ -320,7 +326,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID_1, + .tokenId = TEST_TOKEN_ID, .uid = TEST_UID_1 }; ASSERT_EQ(SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller), @@ -345,7 +351,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) { SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID_1, + .tokenId = TEST_TOKEN_ID, .uid = TEST_UID_1 }; @@ -362,7 +368,7 @@ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID_1, + .tokenId = TEST_TOKEN_ID, .uid = TEST_UID_1 }; SecCompClickEvent touchInfo; @@ -388,7 +394,7 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve nlohmann::json jsonValid1; LocationButton buttonValid1 = BuildValidLocationComponent(); - buttonValid1.fontSize_ = TEST_DIFF_FONT_SIZE; + buttonValid1.fontSize_ = TEST_DIFF_SIZE; buttonValid1.ToJson(jsonValid1); ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonValid1, caller, touchInfo), SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL); diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 35e45d1..5a42581 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -35,26 +35,35 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr int32_t SA_ID = 3506; -static constexpr uint32_t TEST_FONT_SIZE = 100; static constexpr double TEST_COORDINATE = 100.0; -static constexpr uint32_t TEST_COLOR = 0xffffffff; static constexpr int32_t TEST_SC_ID = 1; +static constexpr double TEST_SIZE = 100.0; +static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; +static constexpr uint32_t TEST_COLOR_2 = 0xff0000; +static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; static std::string BuildLocationComponentInfo() { LocationButton button; - button.fontSize_ = TEST_FONT_SIZE; - button.font_ = FONT_HO_SANS; - button.bgColor_.value = TEST_COLOR; - button.fontColor_.value = TEST_COLOR; - button.icon_ = DEFAULT_ICON; - button.label_ = DEFAULT_LABEL; + button.fontSize_ = TEST_SIZE; + button.iconSize_ = TEST_SIZE; + button.padding_.top = TEST_SIZE; + button.padding_.right = TEST_SIZE; + button.padding_.bottom = TEST_SIZE; + button.padding_.left = TEST_SIZE; + button.textIconPadding_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_1; + button.iconColor_.value = TEST_COLOR_2; + button.bgColor_.value = TEST_COLOR_3; + button.borderWidth_ = TEST_SIZE; + button.type_ = LOCATION_COMPONENT; button.rect_.x_ = TEST_COORDINATE; button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; - button.type_ = LOCATION_COMPONENT; - button.buttonType_ = CAPSULE; + button.text_ = LocationDesc::SELECT_LOCATION; + button.icon_ = LocationIcon::LINE_ICON; + button.bg_ = LocationBackground::CIRCLE; nlohmann::json jsonRes; button.ToJson(jsonRes); @@ -250,7 +259,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) struct SecCompClickEvent touch = { .touchX = 100, .touchY = 100, - .timestamp = GetCurrentTime() + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), @@ -305,7 +314,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve struct SecCompClickEvent touch = { .touchX = TEST_COORDINATE, .touchY = TEST_COORDINATE, - .timestamp = GetCurrentTime() + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(TEST_SC_ID, BuildLocationComponentInfo(), touch), SC_SERVICE_ERROR_VALUE_INVALID); -- Gitee From bc37e670a4ec9af2cdc093b6cacf6c4aa2e8ac6f Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 28 Apr 2023 12:05:33 +0800 Subject: [PATCH 09/59] Description: . Match-id-56d6f32e771b23fb03e8943d03d388f1134676ef --- .../unittest/src/location_button_test.cpp | 2 +- .../test/unittest/src/sec_comp_base_test.cpp | 48 +++- .../sa/test/BUILD.gn | 1 + .../src/sec_comp_info_helper_test.cpp | 226 ++++++++++++------ 4 files changed, 197 insertions(+), 80 deletions(-) diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 0d1b3ee..cf69443 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -79,7 +79,7 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp index 0bc7bab..8df03a1 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp @@ -75,7 +75,7 @@ static void BuildSecCompBaseInfo(nlohmann::json& jsonComponent) nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -229,7 +229,7 @@ HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -256,11 +256,25 @@ HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) { JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); +} - jsonPadding = nlohmann::json { +/** + * @tc.name: FromJson006 + * @tc.desc: Test location button from wrong size json + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompBaseTest, FromJson006, TestSize.Level1) +{ + nlohmann::json jsonComponent; + SecCompBase comp; + BuildSecCompBaseInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, WRONG_TYPE }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; jsonComponent[JSON_SIZE_TAG] = nlohmann::json { @@ -274,7 +288,7 @@ HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, WRONG_TYPE }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; jsonComponent[JSON_SIZE_TAG] = nlohmann::json { @@ -284,11 +298,25 @@ HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) { JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); +} - jsonPadding = nlohmann::json { +/** + * @tc.name: FromJson007 + * @tc.desc: Test location button from wrong size json + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompBaseTest, FromJson007, TestSize.Level1) +{ + nlohmann::json jsonComponent; + SecCompBase comp; + BuildSecCompBaseInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,WRONG_TYPE }, + { JSON_PADDING_BOTTOM_TAG, WRONG_TYPE }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; jsonComponent[JSON_SIZE_TAG] = nlohmann::json { @@ -302,7 +330,7 @@ HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, WRONG_TYPE }, }; jsonComponent[JSON_SIZE_TAG] = nlohmann::json { @@ -315,12 +343,12 @@ HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) } /** - * @tc.name: FromJson006 + * @tc.name: FromJson008 * @tc.desc: Test location button from wrong border and parent json * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SecCompBaseTest, FromJson006, TestSize.Level1) +HWTEST_F(SecCompBaseTest, FromJson008, TestSize.Level1) { nlohmann::json jsonComponent; SecCompBase comp; diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 4abac30..53d3236 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -40,6 +40,7 @@ ohos_unittest("sec_comp_service_test") { "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", "unittest/src/app_state_observer_test.cpp", "unittest/src/sec_comp_entity_test.cpp", + "unittest/src/sec_comp_info_helper_test.cpp", "unittest/src/sec_comp_manager_test.cpp", "unittest/src/sec_comp_service_test.cpp", "unittest/src/sec_comp_stub_test.cpp", diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index e080eb8..9b441ad 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -71,12 +71,13 @@ static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; static constexpr uint32_t TEST_COLOR_2 = 0xff0000; static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; +static constexpr uint32_t QUARTER = 4; static constexpr double ZERO_OFFSET = 0.0F; -static double CUR_SCREEN_WIDTH = 0.0F; -static double CUR_SCREEN_HEIGHT = 0.0F; -static double TEST_WIDTH = 0.0F; -static double TEST_HEIGHT = 0.0F; -static uint32_t QUARTER = 4; +static double g_curScreenWidth = 0.0F; +static double g_curScreenHeight = 0.0F; +static double g_testWidth = 0.0F; +static double g_testHeight = 0.0F; + static bool GetScreenSize() { sptr display = @@ -90,8 +91,8 @@ static bool GetScreenSize() return false; } - CUR_SCREEN_WIDTH = static_cast(info->GetWidth()); - CUR_SCREEN_HEIGHT = static_cast(info->GetHeight()); + g_curScreenWidth = static_cast(info->GetWidth()); + g_curScreenHeight = static_cast(info->GetHeight()); return true; } @@ -99,15 +100,15 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_WIDTH }, - {JSON_RECT_Y, TEST_HEIGHT }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_X, g_testWidth }, + {JSON_RECT_Y, g_testHeight }, + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -141,8 +142,8 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) void SecCompInfoHelperTest::SetUpTestCase() { ASSERT_TRUE(GetScreenSize()); - TEST_WIDTH = (ZERO_OFFSET + CUR_SCREEN_WIDTH) / QUARTER; - TEST_HEIGHT = (ZERO_OFFSET + CUR_SCREEN_HEIGHT) / QUARTER; + g_testWidth = (ZERO_OFFSET + g_curScreenWidth) / QUARTER; + g_testHeight = (ZERO_OFFSET + g_curScreenHeight) / QUARTER; } void SecCompInfoHelperTest::TearDownTestCase() @@ -216,99 +217,112 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); - SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, TEST_INVALID_DIMENSION }, - {JSON_RECT_Y, TEST_HEIGHT }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_Y, g_testHeight }, + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_WIDTH }, + {JSON_RECT_X, g_testWidth }, {JSON_RECT_Y, TEST_INVALID_DIMENSION }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, CUR_SCREEN_WIDTH }, - {JSON_RECT_Y, TEST_HEIGHT }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_X, g_curScreenWidth }, + {JSON_RECT_Y, g_testHeight }, + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_WIDTH }, - {JSON_RECT_Y, CUR_SCREEN_HEIGHT }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_X, g_testWidth }, + {JSON_RECT_Y, g_curScreenHeight }, + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent005 + * @tc.desc: Test parse component info with invalid rect + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_WIDTH }, - {JSON_RECT_Y, TEST_HEIGHT }, - {JSON_RECT_WIDTH, CUR_SCREEN_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_X, g_testWidth }, + {JSON_RECT_Y, g_testHeight }, + {JSON_RECT_WIDTH, g_curScreenWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_WIDTH }, - {JSON_RECT_Y, TEST_HEIGHT }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, CUR_SCREEN_HEIGHT } + {JSON_RECT_X, g_testWidth }, + {JSON_RECT_Y, g_testHeight }, + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_curScreenHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, CUR_SCREEN_WIDTH - TEST_WIDTH }, - {JSON_RECT_Y, TEST_HEIGHT }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_X, g_curScreenWidth - g_testWidth }, + {JSON_RECT_Y, g_testHeight }, + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_WIDTH }, - {JSON_RECT_Y, CUR_SCREEN_HEIGHT - TEST_HEIGHT }, - {JSON_RECT_WIDTH, TEST_WIDTH }, - {JSON_RECT_HEIGHT, TEST_HEIGHT } + {JSON_RECT_X, g_testWidth }, + {JSON_RECT_Y, g_curScreenHeight - g_testHeight }, + {JSON_RECT_WIDTH, g_testWidth }, + {JSON_RECT_HEIGHT, g_testHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_WIDTH }, - {JSON_RECT_Y, TEST_HEIGHT }, - {JSON_RECT_WIDTH, CUR_SCREEN_WIDTH * CUR_SCREEN_HEIGHT }, - {JSON_RECT_HEIGHT, CUR_SCREEN_WIDTH * CUR_SCREEN_HEIGHT } + {JSON_RECT_X, g_testWidth }, + {JSON_RECT_Y, g_testHeight }, + {JSON_RECT_WIDTH, g_curScreenWidth * g_curScreenHeight }, + {JSON_RECT_HEIGHT, g_curScreenWidth * g_curScreenHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } /** - * @tc.name: ParseComponent005 + * @tc.name: ParseComponent006 * @tc.desc: Test parse component info with parentEffect active * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) +HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); @@ -324,12 +338,12 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) } /** - * @tc.name: ParseComponent006 + * @tc.name: ParseComponent007 * @tc.desc: Test parse component info with invalid size * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) +HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); @@ -340,7 +354,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -356,7 +370,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -372,7 +386,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -384,11 +398,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); +} - jsonPadding = nlohmann::json { +/** + * @tc.name: ParseComponent008 + * @tc.desc: Test parse component info with invalid size + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_INVALID_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -404,7 +432,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_INVALID_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -416,11 +444,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); +} - jsonPadding = nlohmann::json { +/** + * @tc.name: ParseComponent009 + * @tc.desc: Test parse component info with invalid size + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_INVALID_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_INVALID_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; @@ -436,7 +478,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG,TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, { JSON_PADDING_LEFT_TAG, TEST_INVALID_DIMENSION }, }; @@ -451,16 +493,15 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) } /** - * @tc.name: ParseComponent007 + * @tc.name: ParseComponent010 * @tc.desc: Test parse component info with invalid color * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) +HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); - SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -490,16 +531,15 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) } /** - * @tc.name: ParseComponent008 + * @tc.name: ParseComponent011 * @tc.desc: Test parse component info with invalid style * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) +HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); - SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -510,6 +550,20 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent012 + * @tc.desc: Test parse component info with invalid style + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, @@ -549,6 +603,20 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); +} + +/** + * @tc.name: ParseComponent013 + * @tc.desc: Test parse component info with invalid style + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, @@ -586,8 +654,28 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); +} - jsonPadding = nlohmann::json { +/** + * @tc.name: ParseComponent014 + * @tc.desc: Test parse component info with invalid style + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, LocationBackground::NO_BG_TYPE }, + }; + + nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, { JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, -- Gitee From c3693f7e669a2b414494421a9dcaf779da9d9cbb Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 28 Apr 2023 18:18:04 +0800 Subject: [PATCH 10/59] Description: . Match-id-66dc37bb2f8a8bfb0f5c338a22f341265e2cf504 --- BUILD.gn | 15 ++++++----- frameworks/BUILD.gn | 25 +++++++++---------- interfaces/inner_api/enhance_kits/BUILD.gn | 10 ++++---- .../inner_api/enhance_kits/test/BUILD.gn | 11 +++----- .../inner_api/security_component/BUILD.gn | 10 +++----- .../security_component/test/BUILD.gn | 5 ++-- security_component.gni | 2 +- .../security_component_service/sa/BUILD.gn | 8 +++--- .../sa/test/BUILD.gn | 6 +++-- 9 files changed, 45 insertions(+), 47 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 1ebfda6..40b1ede 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -11,16 +11,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/ohos.gni") group("security_component_build_module") { if (is_standard_system) { deps = [ - "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", - "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits:libsecurity_component_enhance_sdk", - "${sec_comp_root_dir}/services/security_component_service/sa:security_component_service", - "${sec_comp_root_dir}/services/security_component_service/sa/sa_profile:security_component_sa_profile_standard", + "interfaces/inner_api/enhance_kits:libsecurity_component_enhance_sdk", + "interfaces/inner_api/security_component:libsecurity_component_sdk", + "services/security_component_service/sa:security_component_service", + "services/security_component_service/sa/sa_profile:security_component_sa_profile_standard", ] } } @@ -30,9 +29,9 @@ group("security_component_build_module_test") { deps = [] if (is_standard_system) { deps += [ - "${sec_comp_root_dir}/interfaces/inner_api/security_component/test:unittest", - "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits/test:unittest", - "${sec_comp_root_dir}/services/security_component_service/sa/test:unittest", + "interfaces/inner_api/enhance_kits/test:unittest", + "interfaces/inner_api/security_component/test:unittest", + "services/security_component_service/sa/test:unittest", ] } } diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 5ae3f7f..918a900 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -11,12 +11,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/ohos.gni") +sec_comp_root_dir = ".." + config("libsecurity_component_framework_config") { include_dirs = [ - "${sec_comp_root_dir}/frameworks/common/include", + "common/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", ] } @@ -27,8 +28,8 @@ ohos_shared_library("libsecurity_component_framework") { output_name = "libsecurity_component_framework" include_dirs = [ - "${sec_comp_root_dir}/frameworks/common/include", - "${sec_comp_root_dir}/frameworks/security_component/include", + "common/include", + "security_component/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", "//third_party/json/include", ] @@ -40,7 +41,7 @@ ohos_shared_library("libsecurity_component_framework") { "security_component/src/sec_comp_click_event_parcel.cpp", ] - configs = [ "//base/security/security_component/config:coverage_flags" ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] public_configs = [ ":libsecurity_component_framework_config" ] external_deps = [ @@ -53,8 +54,8 @@ ohos_shared_library("libsecurity_component_framework") { config("libsecurity_component_enhance_adapter_config") { include_dirs = [ - "${sec_comp_root_dir}/frameworks/common/include", - "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + "common/include", + "enhance_adapter/include", ] } @@ -64,15 +65,13 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { output_name = "libsecurity_component_enhance_adapter" include_dirs = [ - "${sec_comp_root_dir}/frameworks/common/include", - "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + "common/include", + "enhance_adapter/include", ] - sources = [ - "enhance_adapter/src/sec_comp_enhance_adapter.cpp", - ] + sources = [ "enhance_adapter/src/sec_comp_enhance_adapter.cpp" ] - configs = [ "//base/security/security_component/config:coverage_flags" ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] public_configs = [ ":libsecurity_component_enhance_adapter_config" ] external_deps = [ diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index 8e53707..03f7fad 100644 --- a/interfaces/inner_api/enhance_kits/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -11,16 +11,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/ohos.gni") +sec_comp_root_dir = "../../.." + config("sec_comp_enhance_config") { visibility = [ ":*" ] include_dirs = [ "include", "${sec_comp_root_dir}/frameworks/common/include", "${sec_comp_root_dir}/frameworks/enhance_adapter/include", - "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits/include" ] } @@ -39,14 +39,14 @@ ohos_shared_library("libsecurity_component_enhance_sdk") { sources = [ "src/sec_comp_enhance_cfg_parcel.cpp", - "src/sec_comp_enhance_kit.cpp" + "src/sec_comp_enhance_kit.cpp", ] deps = [ - "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter" + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", ] - configs = [ "//base/security/security_component/config:coverage_flags" ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] external_deps = [ "c_utils:utils", diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn index 15e0e1a..5707e41 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -11,21 +11,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/test.gni") +sec_comp_root_dir = "../../../.." + ohos_unittest("sec_comp_enhance_sdk_test") { subsystem_name = "security" part_name = "security_component" module_out_path = part_name + "/" + part_name - include_dirs = [ - "unittest/src", - ] + include_dirs = [ "unittest/src" ] - sources = [ - "unittest/src/sec_comp_enhance_test.cpp", - ] + sources = [ "unittest/src/sec_comp_enhance_test.cpp" ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 2178b5c..d2b1aed 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -11,9 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/ohos.gni") +sec_comp_root_dir = "../../.." + config("sec_comp_config") { visibility = [ ":*" ] include_dirs = [ @@ -34,7 +35,6 @@ ohos_shared_library("libsecurity_component_sdk") { "include", "${sec_comp_root_dir}/frameworks/common/include", "${sec_comp_root_dir}/frameworks/security_component/include", - "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", ] sources = [ @@ -45,11 +45,9 @@ ohos_shared_library("libsecurity_component_sdk") { "src/sec_comp_proxy.cpp", ] - deps = [ - "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", - ] + deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_framework" ] - configs = [ "//base/security/security_component/config:coverage_flags" ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] external_deps = [ "c_utils:utils", diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index 74747a5..e4c6594 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -11,9 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/test.gni") +sec_comp_root_dir = "../../../.." + ohos_unittest("sec_comp_sdk_test") { subsystem_name = "security" part_name = "security_component" @@ -26,9 +27,9 @@ ohos_unittest("sec_comp_sdk_test") { ] sources = [ + "unittest/src/location_button_test.cpp", "unittest/src/sec_comp_base_test.cpp", "unittest/src/sec_comp_kit_test.cpp", - "unittest/src/location_button_test.cpp", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/security_component.gni b/security_component.gni index 310d1c6..6a705be 100644 --- a/security_component.gni +++ b/security_component.gni @@ -11,4 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -sec_comp_root_dir = "//base/security/security_component" \ No newline at end of file +foundation_graphic_2d = "//foundation/graphic/graphic_2d" \ No newline at end of file diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index e5591ba..2e9721e 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -11,8 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/ohos.gni") +import("../../../security_component.gni") + +sec_comp_root_dir = "../../.." config("security_component_service_config") { include_dirs = [ @@ -57,12 +59,12 @@ ohos_shared_library("security_component_service") { deps = [ ":security_component_service.rc", + "${foundation_graphic_2d}/rosen/modules/render_service_client:librender_service_client", "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", - "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client" ] - configs = [ "//base/security/security_component/config:coverage_flags" ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] public_configs = [ ":security_component_service_config" ] external_deps = [ diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 53d3236..108517a 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -11,8 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//base/security/security_component/security_component.gni") import("//build/test.gni") +import("../../../../security_component.gni") + +sec_comp_root_dir = "../../../.." ohos_unittest("sec_comp_service_test") { subsystem_name = "security" @@ -49,9 +51,9 @@ ohos_unittest("sec_comp_service_test") { cflags_cc = [ "-DHILOG_ENABLE" ] deps = [ + "${foundation_graphic_2d}/rosen/modules/render_service_client:librender_service_client", "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", - "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client", "//third_party/googletest:gmock_main", ] -- Gitee From c4e96b74ce3f34ee12722d89c58517a35b85ab8d Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 8 May 2023 07:04:13 +0800 Subject: [PATCH 11/59] Description:modify check font and icon size Match-id-a42983635eafcc78b056c79d411d164fc56debf4 --- .../sa/sa_main/sec_comp_info_helper.cpp | 11 +++++++++-- .../sa/sa_main/sec_comp_manager.cpp | 8 -------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 8b36fa9..e233f7f 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -125,8 +125,7 @@ static bool CheckSecCompBase(const SecCompBase* comp) return false; } - if ((comp->fontSize_ < MIN_FONT_SIZE) || (comp->iconSize_ < MIN_ICON_SIZE) || - (comp->padding_.top < MIN_PADDING_SIZE) || (comp->padding_.right < MIN_PADDING_SIZE) || + if ((comp->padding_.top < MIN_PADDING_SIZE) || (comp->padding_.right < MIN_PADDING_SIZE) || (comp->padding_.bottom < MIN_PADDING_SIZE) || (comp->padding_.left < MIN_PADDING_SIZE) || (comp->textIconPadding_ < MIN_PADDING_SIZE)) { SC_LOG_ERROR(LABEL, "size is invalid."); @@ -148,6 +147,14 @@ static bool CheckLocationButton(const LocationButton* comp) SC_LOG_INFO(LABEL, "both text and icon do not exist."); return false; } + if ((comp->text_ != LocationDesc::NO_TEXT) && (comp->fontSize_ < MIN_FONT_SIZE)) { + SC_LOG_INFO(LABEL, "font size invalid."); + return false; + } + if ((comp->icon_ != LocationIcon::NO_ICON) &&(comp->iconSize_ < MIN_ICON_SIZE)) { + SC_LOG_INFO(LABEL, "icon size invalid."); + return false; + } if (comp->bg_ != LocationBackground::NO_BG_TYPE && (IsColorSimilar(comp->fontColor_, comp->bgColor_) || IsColorSimilar(comp->iconColor_, comp->bgColor_))) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index e911305..178bf35 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -272,14 +272,6 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } - // BasicInfo is the attributes of security component defined in ets, such as fontsize. - // we only compare these because they can be obtained during registration/update, - // but other infos such as position may not be notified because changs occur at the parent node. - if (!sc->CompareComponentBasicInfo(reportComponentInfo.get())) { - SC_LOG_ERROR(LABEL, "Report component basic compare register component failed"); - return SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL; - } - sc->SetComponentInfo(reportComponentInfo); if (!sc->CheckTouchInfo(touchInfo)) { SC_LOG_ERROR(LABEL, "touchInfo is invalid"); -- Gitee From 765b73c12f67d023fe56188d06f17374c17f08cf Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 10 May 2023 11:58:55 +0800 Subject: [PATCH 12/59] Description:Component-based rectification Match-id-a460a8210028f2c3e6cfe706d987b845d97866a5 --- bundle.json | 19 ++++++------------- ohos.build | 17 ----------------- 2 files changed, 6 insertions(+), 30 deletions(-) delete mode 100644 ohos.build diff --git a/bundle.json b/bundle.json index 7c32811..2c3e02b 100644 --- a/bundle.json +++ b/bundle.json @@ -23,25 +23,18 @@ "ram": "5102KB", "deps": { "components": [ - "aafwk_standard", - "ability_base", "ability_runtime", - "appexecfwk_standard", + "access_token", + "c_utils", "hiviewdfx_hilog_native", - "hisysevent_native", - "ipc_core", + "ipc", "safwk", "samgr", - "c_utils", - "samgr_proxy", - "samgr", - "system_ability_fwk", - "utils_base" + "window_manager" ], "third_party": [ - "cJSON", - "sqlite", - "mbedtls" + "cjson", + "googletest" ] }, "build": { diff --git a/ohos.build b/ohos.build deleted file mode 100644 index 0429522..0000000 --- a/ohos.build +++ /dev/null @@ -1,17 +0,0 @@ -{ - "subsystem": "security", - "parts": { - "security_component": { - "variants": [ - "phone", - "wearable" - ], - "module_list": [ - "//base/security/security_component:security_component_build_module" - ], - "test_list": [ - "//base/security/security_component:security_component_build_module_test" - ] - } - } -} -- Gitee From 8fcc21353c60017572f44e8f02a342db54d020fa Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 11 May 2023 22:33:48 +0800 Subject: [PATCH 13/59] Description: change bundle.json Match-id-0faeff70b5512a79f8b0747113b107b070b969db --- bundle.json | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bundle.json b/bundle.json index 2c3e02b..b50cf92 100644 --- a/bundle.json +++ b/bundle.json @@ -51,6 +51,15 @@ "header_base": "//base/security/security_component/interfaces/inner_api/security_component/include" } }, + { + "name": "//base/security/security_component/interfaces/inner_api/enhance_kits:libsecurity_component_enhance_sdk", + "header": { + "header_files": [ + "sec_comp_enhance_kit.h" + ], + "header_base": "//base/security/security_component/interfaces/inner_api/enhance_kits/include" + } + }, { "name": "//base/security/security_component/frameworks:libsecurity_component_framework", "header": { -- Gitee From 316ba3cc4965229a922e6b1fc7a2c288f521e770 Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 12 May 2023 15:52:29 +0800 Subject: [PATCH 14/59] Description:modify ut test failed Match-id-508f62cb1dc652faa47e44283100ab35aabed6fb --- .../sa/test/unittest/src/sec_comp_manager_test.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index d71b176..67a07b2 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -28,7 +28,6 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompManagerTest"}; static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_DIFF_SIZE = 16; static constexpr uint32_t TEST_INVALID_SIZE = 0; static constexpr double TEST_COORDINATE = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffff; @@ -392,13 +391,6 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonInvalid, caller, touchInfo), SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); - nlohmann::json jsonValid1; - LocationButton buttonValid1 = BuildValidLocationComponent(); - buttonValid1.fontSize_ = TEST_DIFF_SIZE; - buttonValid1.ToJson(jsonValid1); - ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonValid1, caller, touchInfo), - SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL); - touchInfo.touchX = 500.0; // not hit component touchInfo.touchY = 500.0; // not hit component ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonVaild, caller, touchInfo), -- Gitee From 9ec9f87ca410106ec7afcc49c94d39b927f8bff0 Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 23 May 2023 11:02:47 +0800 Subject: [PATCH 15/59] Description: Change deps to external_deps Match-id-d97f41d7702f87c0e53cfed58ca44b6f6105b283 --- security_component.gni | 2 -- services/security_component_service/sa/BUILD.gn | 2 +- services/security_component_service/sa/test/BUILD.gn | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/security_component.gni b/security_component.gni index 6a705be..4d0f814 100644 --- a/security_component.gni +++ b/security_component.gni @@ -10,5 +10,3 @@ # 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. - -foundation_graphic_2d = "//foundation/graphic/graphic_2d" \ No newline at end of file diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 2e9721e..3279925 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -59,7 +59,6 @@ ohos_shared_library("security_component_service") { deps = [ ":security_component_service.rc", - "${foundation_graphic_2d}/rosen/modules/render_service_client:librender_service_client", "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", ] @@ -71,6 +70,7 @@ ohos_shared_library("security_component_service") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "c_utils:utils", + "graphic_standard:librender_service_client", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 108517a..9ec1f4f 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -51,7 +51,6 @@ ohos_unittest("sec_comp_service_test") { cflags_cc = [ "-DHILOG_ENABLE" ] deps = [ - "${foundation_graphic_2d}/rosen/modules/render_service_client:librender_service_client", "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", "//third_party/googletest:gmock_main", @@ -60,6 +59,7 @@ ohos_unittest("sec_comp_service_test") { external_deps = [ "access_token:libtoken_setproc", "c_utils:utils", + "graphic_standard:librender_service_client", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "window_manager:libdm", -- Gitee From c13a24e9e947475632d0054b95f69359cf1aa3ff Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 25 May 2023 22:00:43 +0800 Subject: [PATCH 16/59] Description:add depends to bundle.json Match-id-6c90c77a0ffbf7cdc97c653b5b729fcfb350594a --- bundle.json | 1 + 1 file changed, 1 insertion(+) diff --git a/bundle.json b/bundle.json index b50cf92..343332c 100644 --- a/bundle.json +++ b/bundle.json @@ -27,6 +27,7 @@ "access_token", "c_utils", "hiviewdfx_hilog_native", + "graphic_standard", "ipc", "safwk", "samgr", -- Gitee From 26d9b60992e7b2611a20d30082f23f2b0ed9c20e Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 31 May 2023 16:21:06 +0800 Subject: [PATCH 17/59] Description: cppcheck Match-id-29412c5d90bcf044cdb22ab00051b0287fb5d277 --- frameworks/common/src/sec_comp_tool.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/common/src/sec_comp_tool.cpp b/frameworks/common/src/sec_comp_tool.cpp index 4885970..19639c2 100644 --- a/frameworks/common/src/sec_comp_tool.cpp +++ b/frameworks/common/src/sec_comp_tool.cpp @@ -84,17 +84,17 @@ static HsvColor RgbToHsv(uint8_t r, uint8_t g, uint8_t b) return hsv; } -static inline double GetHsvDisX(HsvColor& hsv, double r) +static inline double GetHsvDisX(const HsvColor& hsv, double r) { return r * hsv.v * hsv.s * cos(hsv.h / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); } -static inline double GetHsvDisY(HsvColor& hsv, double r) +static inline double GetHsvDisY(const HsvColor& hsv, double r) { return r * hsv.v * hsv.s * sin(hsv.h / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); } -static inline double GetHsvDisZ(HsvColor& hsv, double h) +static inline double GetHsvDisZ(const HsvColor& hsv, double h) { return h * (1 - hsv.v); } -- Gitee From 8bcbe17a3f2a7bfc36926afeea0a02476a2510bf Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 29 May 2023 18:32:16 +0800 Subject: [PATCH 18/59] Description:replace xml with json Match-id-2cfeb9ed0b95ea76e100081141dc774c46348d91 --- .../sa/sa_profile/3506.json | 12 ++++++++++ .../sa/sa_profile/3506.xml | 24 ------------------- .../sa/sa_profile/BUILD.gn | 2 +- .../sa/security_component_service.cfg | 2 +- .../sa/security_component_service.rc | 2 +- 5 files changed, 15 insertions(+), 27 deletions(-) create mode 100644 services/security_component_service/sa/sa_profile/3506.json delete mode 100644 services/security_component_service/sa/sa_profile/3506.xml diff --git a/services/security_component_service/sa/sa_profile/3506.json b/services/security_component_service/sa/sa_profile/3506.json new file mode 100644 index 0000000..83650d4 --- /dev/null +++ b/services/security_component_service/sa/sa_profile/3506.json @@ -0,0 +1,12 @@ +{ + "process": "security_component_service", + "systemability": [ + { + "name": 3506, + "libpath": "libsecurity_component_service.z.so", + "run-on-create": false, + "distributed": false, + "dump_level": 1 + } + ] +} \ No newline at end of file diff --git a/services/security_component_service/sa/sa_profile/3506.xml b/services/security_component_service/sa/sa_profile/3506.xml deleted file mode 100644 index b41acee..0000000 --- a/services/security_component_service/sa/sa_profile/3506.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - security_component_service - - 3506 - libsecurity_component_service.z.so - false - false - 1 - - diff --git a/services/security_component_service/sa/sa_profile/BUILD.gn b/services/security_component_service/sa/sa_profile/BUILD.gn index 3ed788b..dc0c6d9 100644 --- a/services/security_component_service/sa/sa_profile/BUILD.gn +++ b/services/security_component_service/sa/sa_profile/BUILD.gn @@ -14,6 +14,6 @@ import("//build/ohos/sa_profile/sa_profile.gni") ohos_sa_profile("security_component_sa_profile_standard") { - sources = [ "3506.xml" ] + sources = [ "3506.json" ] part_name = "security_component" } diff --git a/services/security_component_service/sa/security_component_service.cfg b/services/security_component_service/sa/security_component_service.cfg index 3081ece..818b6d5 100644 --- a/services/security_component_service/sa/security_component_service.cfg +++ b/services/security_component_service/sa/security_component_service.cfg @@ -1,7 +1,7 @@ { "services" : [{ "name" : "security_component_service", - "path" : ["/system/bin/sa_main", "/system/profile/security_component_service.xml"], + "path" : ["/system/bin/sa_main", "/system/profile/security_component_service.json"], "ondemand" : true, "start-mode" : "condition", "uid" : "security_component", diff --git a/services/security_component_service/sa/security_component_service.rc b/services/security_component_service/sa/security_component_service.rc index 28c7570..68b6fbc 100644 --- a/services/security_component_service/sa/security_component_service.rc +++ b/services/security_component_service/sa/security_component_service.rc @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -service security_component_service /system/bin/sa_main /system/profile/security_component.xml +service security_component_service /system/bin/sa_main /system/profile/security_component.json class security_component_service priority -20 user security_component -- Gitee From 2ff49de6ff122117ce190b55804dd8c3b3f7093b Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 31 May 2023 19:11:00 +0800 Subject: [PATCH 19/59] Description: save & paste button Match-id-2df172b824e4dc0cc7810603614b24a24d250e56 --- bundle.json | 1 + frameworks/BUILD.gn | 2 + .../src/location_button.cpp | 51 +- .../security_component/src/paste_button.cpp | 61 +++ .../security_component/src/save_button.cpp | 61 +++ .../security_component/src/sec_comp_base.cpp | 69 ++- .../inner_api/enhance_kits/test/BUILD.gn | 1 + .../unittest/src/sec_comp_enhance_test.cpp | 17 + .../test/unittest/src/sec_comp_enhance_test.h | 1 + .../inner_api/security_component/BUILD.gn | 1 + .../include/i_sec_comp_service.h | 3 + .../include/location_button.h | 19 +- .../security_component/include/paste_button.h | 20 + .../security_component/include/save_button.h | 33 +- .../include/sec_comp_base.h | 37 +- .../include/sec_comp_client.h | 1 + .../include/sec_comp_info.h | 1 + .../security_component/include/sec_comp_kit.h | 3 + .../include/sec_comp_proxy.h | 1 + .../src/sec_comp_client.cpp | 11 + .../security_component/src/sec_comp_kit.cpp | 10 + .../security_component/src/sec_comp_proxy.cpp | 35 ++ .../security_component/test/BUILD.gn | 6 +- .../unittest/src/location_button_test.cpp | 447 ++++++++++++++++-- ...mp_base_test.cpp => paste_button_test.cpp} | 352 ++++---------- .../test/unittest/src/paste_button_test.h | 38 ++ .../test/unittest/src/save_button_test.cpp | 283 +++++++++++ ...ec_comp_base_test.h => save_button_test.h} | 11 +- .../test/unittest/src/sec_comp_kit_test.cpp | 4 +- .../security_component_service/sa/BUILD.gn | 3 + .../sa/sa_main/sec_comp_entity.cpp | 4 - .../sa/sa_main/sec_comp_entity.h | 6 +- .../sa/sa_main/sec_comp_info_helper.cpp | 106 ++--- .../sa/sa_main/sec_comp_info_helper.h | 1 + .../sa/sa_main/sec_comp_manager.cpp | 14 + .../sa/sa_main/sec_comp_manager.h | 8 +- .../sa/sa_main/sec_comp_perm_manager.cpp | 137 ++++++ .../sa/sa_main/sec_comp_perm_manager.h | 60 +++ .../sa/sa_main/sec_comp_service.cpp | 14 + .../sa/sa_main/sec_comp_service.h | 1 + .../sa/sa_main/sec_comp_stub.cpp | 23 + .../sa/sa_main/sec_comp_stub.h | 2 +- .../sa/sa_main/sec_event_handler.cpp | 41 ++ .../sa/sa_main/sec_event_handler.h | 39 ++ .../sa/test/BUILD.gn | 4 + .../unittest/src/app_state_observer_test.cpp | 28 +- .../unittest/src/sec_comp_entity_test.cpp | 39 +- .../src/sec_comp_info_helper_test.cpp | 158 +++++-- .../unittest/src/sec_comp_manager_test.cpp | 70 +-- .../unittest/src/sec_comp_service_test.cpp | 220 +++++++-- .../test/unittest/src/sec_comp_stub_test.cpp | 10 +- .../sa/test/unittest/src/sec_comp_stub_test.h | 5 + 52 files changed, 1979 insertions(+), 594 deletions(-) create mode 100644 frameworks/security_component/src/paste_button.cpp create mode 100644 frameworks/security_component/src/save_button.cpp rename interfaces/inner_api/security_component/test/unittest/src/{sec_comp_base_test.cpp => paste_button_test.cpp} (37%) create mode 100644 interfaces/inner_api/security_component/test/unittest/src/paste_button_test.h create mode 100644 interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp rename interfaces/inner_api/security_component/test/unittest/src/{sec_comp_base_test.h => save_button_test.h} (83%) create mode 100644 services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp create mode 100644 services/security_component_service/sa/sa_main/sec_comp_perm_manager.h create mode 100644 services/security_component_service/sa/sa_main/sec_event_handler.cpp create mode 100644 services/security_component_service/sa/sa_main/sec_event_handler.h diff --git a/bundle.json b/bundle.json index 343332c..5535487 100644 --- a/bundle.json +++ b/bundle.json @@ -26,6 +26,7 @@ "ability_runtime", "access_token", "c_utils", + "eventhandler", "hiviewdfx_hilog_native", "graphic_standard", "ipc", diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 918a900..5d423aa 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -37,6 +37,8 @@ ohos_shared_library("libsecurity_component_framework") { sources = [ "common/src/sec_comp_tool.cpp", "security_component/src/location_button.cpp", + "security_component/src/paste_button.cpp", + "security_component/src/save_button.cpp", "security_component/src/sec_comp_base.cpp", "security_component/src/sec_comp_click_event_parcel.cpp", ] diff --git a/frameworks/security_component/src/location_button.cpp b/frameworks/security_component/src/location_button.cpp index 201dc46..14f9a56 100644 --- a/frameworks/security_component/src/location_button.cpp +++ b/frameworks/security_component/src/location_button.cpp @@ -30,59 +30,18 @@ static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; } -bool LocationButton::ParseStyle(const nlohmann::json& json, const std::string& tag) +bool LocationButton::IsParamValid() { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + if ((static_cast(text_) <= LocationDesc::UNKNOWN_TEXT) || + (static_cast(text_) >= LocationDesc::MAX_LABEL_TYPE) || + (static_cast(icon_) <= LocationIcon::UNKNOWN_ICON) || + (static_cast(icon_) >= LocationIcon::MAX_ICON_TYPE)) { return false; } - auto jsonStyle = json.at(tag); - if (!ParseTypeValue(jsonStyle, JSON_TEXT_TAG, text_, - LocationDesc::UNKNOWN_TEXT, LocationDesc::MAX_LABEL_TYPE)) { - return false; - } - - if (!ParseTypeValue(jsonStyle, JSON_ICON_TAG, icon_, - LocationIcon::UNKNOWN_ICON, LocationIcon::MAX_ICON_TYPE)) { - return false; - } - - if (!ParseTypeValue(jsonStyle, JSON_BG_TAG, bg_, - LocationBackground::UNKNOWN_BG, LocationBackground::MAX_BG_TYPE)) { - return false; - } - - return true; -} -bool LocationButton::FromJson(const nlohmann::json& jsonSrc) -{ - SC_LOG_DEBUG(LABEL, "LocationButton info %{public}s.", jsonSrc.dump().c_str()); - SecCompBase::FromJson(jsonSrc); - if (!ParseStyle(jsonSrc, JSON_STYLE_TAG)) { - SC_LOG_ERROR(LABEL, "Parse style %{public}s.", jsonSrc.dump().c_str()); - return false; - } return true; } -void LocationButton::ToJson(nlohmann::json& jsonRes) const -{ - SecCompBase::ToJson(jsonRes); - jsonRes[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, text_ }, - { JSON_ICON_TAG, icon_ }, - { JSON_BG_TAG, bg_ }, - }; -} - -std::string LocationButton::ToJsonStr() const -{ - nlohmann::json json; - ToJson(json); - return json.dump(); -} - bool LocationButton::CompareComponentBasicInfo(SecCompBase *other) const { if (!SecCompBase::CompareComponentBasicInfo(other)) { diff --git a/frameworks/security_component/src/paste_button.cpp b/frameworks/security_component/src/paste_button.cpp new file mode 100644 index 0000000..ea20e3f --- /dev/null +++ b/frameworks/security_component/src/paste_button.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 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. + */ + +#include "paste_button.h" +#include +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "PasteButton"}; +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; +} + +bool PasteButton::IsParamValid() +{ + if ((static_cast(text_) <= PasteDesc::UNKNOWN_TEXT) || + (static_cast(text_) >= PasteDesc::MAX_LABEL_TYPE) || + (static_cast(icon_) <= PasteIcon::UNKNOWN_ICON) || + (static_cast(icon_) >= PasteIcon::MAX_ICON_TYPE)) { + return false; + } + + return true; +} + +bool PasteButton::CompareComponentBasicInfo(SecCompBase *other) const +{ + if (!SecCompBase::CompareComponentBasicInfo(other)) { + SC_LOG_ERROR(LABEL, "SecComp base not equal."); + return false; + } + PasteButton* otherPasteButton = reinterpret_cast(other); + if (otherPasteButton == nullptr) { + SC_LOG_ERROR(LABEL, "other is not paste button."); + return false; + } + return (icon_ == otherPasteButton->icon_) && (text_ == otherPasteButton->text_) && + (bg_ == otherPasteButton->bg_); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/frameworks/security_component/src/save_button.cpp b/frameworks/security_component/src/save_button.cpp new file mode 100644 index 0000000..7c7a5ca --- /dev/null +++ b/frameworks/security_component/src/save_button.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 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. + */ + +#include "save_button.h" +#include +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SaveButton"}; +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; +} + +bool SaveButton::IsParamValid() +{ + if ((static_cast(text_) <= SaveDesc::UNKNOWN_TEXT) || + (static_cast(text_) >= SaveDesc::MAX_LABEL_TYPE) || + (static_cast(icon_) <= SaveIcon::UNKNOWN_ICON) || + (static_cast(icon_) >= SaveIcon::MAX_ICON_TYPE)) { + return false; + } + + return true; +} + +bool SaveButton::CompareComponentBasicInfo(SecCompBase *other) const +{ + if (!SecCompBase::CompareComponentBasicInfo(other)) { + SC_LOG_ERROR(LABEL, "SecComp base not equal."); + return false; + } + SaveButton* otherSaveButton = reinterpret_cast(other); + if (otherSaveButton == nullptr) { + SC_LOG_ERROR(LABEL, "other is not save button."); + return false; + } + return (icon_ == otherSaveButton->icon_) && (text_ == otherSaveButton->text_) && + (bg_ == otherSaveButton->bg_); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index 38817bd..b1ac4d5 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -37,7 +37,7 @@ static const std::string JSON_PADDING_LEFT_TAG = "left"; static const std::string JSON_PADDING_TOP_TAG = "top"; static const std::string JSON_PADDING_RIGHT_TAG = "right"; static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; static const std::string JSON_RECT_WIDTH_TAG = "width"; static const std::string JSON_RECT_HEIGHT_TAG = "height"; @@ -50,6 +50,11 @@ static const std::string JSON_BORDER_TAG = "border"; static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; static const std::string JSON_PARENT_TAG = "parent"; static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; + +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; } bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res) @@ -153,7 +158,7 @@ bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) return false; } - if (!ParseDimension(jsonSize, JSON_TEXT_ICON_PADDING_TAG, textIconPadding_)) { + if (!ParseDimension(jsonSize, JSON_TEXT_ICON_PADDING_TAG, textIconSpace_)) { return false; } @@ -203,10 +208,17 @@ bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag) bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) { - if (!ParseTypeValue(jsonSrc, JSON_SC_TYPE, type_, - SecCompType::UNKNOWN_SC_TYPE, SecCompType::MAX_SC_TYPE)) { + SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonSrc.dump().c_str()); + if ((jsonSrc.find(JSON_SC_TYPE) == jsonSrc.end()) || + !jsonSrc.at(JSON_SC_TYPE).is_number()) { + return false; + } + int32_t value = jsonSrc.at(JSON_SC_TYPE).get(); + if ((value <= static_cast(SecCompType::UNKNOWN_SC_TYPE)) || + (value >= static_cast(SecCompType::MAX_SC_TYPE))) { return false; } + type_ = static_cast(value); if (!ParseRect(jsonSrc, JSON_RECT)) { return false; @@ -226,6 +238,10 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) return false; } + if (!ParseStyle(jsonSrc, JSON_STYLE_TAG)) { + return false; + } + return true; } @@ -248,7 +264,7 @@ void SecCompBase::ToJson(nlohmann::json& jsonRes) const jsonRes[JSON_SIZE_TAG] = nlohmann::json { { JSON_FONT_SIZE_TAG, fontSize_ }, { JSON_ICON_SIZE_TAG, iconSize_ }, - { JSON_TEXT_ICON_PADDING_TAG, textIconPadding_ }, + { JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ }, { JSON_PADDING_SIZE_TAG, jsonPadding }, }; @@ -264,6 +280,19 @@ void SecCompBase::ToJson(nlohmann::json& jsonRes) const jsonRes[JSON_PARENT_TAG] = nlohmann::json { { JSON_PARENT_EFFECT_TAG, parentEffect_ }, }; + + jsonRes[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, text_ }, + { JSON_ICON_TAG, icon_ }, + { JSON_BG_TAG, bg_ }, + }; +} + +std::string SecCompBase::ToJsonStr() const +{ + nlohmann::json json; + ToJson(json); + return json.dump(); } bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other) const @@ -273,14 +302,40 @@ bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other) const return false; } - auto leftValue = std::tie(type_, fontSize_, iconSize_, textIconPadding_, padding_.top, padding_.right, + auto leftValue = std::tie(type_, fontSize_, iconSize_, textIconSpace_, padding_.top, padding_.right, padding_.bottom, padding_.left, fontColor_.value, bgColor_.value, iconColor_.value, borderWidth_); - auto rightValue = std::tie(other->type_, other->fontSize_, other->iconSize_, other->textIconPadding_, + auto rightValue = std::tie(other->type_, other->fontSize_, other->iconSize_, other->textIconSpace_, other->padding_.top, other->padding_.right, other->padding_.bottom, other->padding_.left, other->fontColor_.value, other->bgColor_.value, other->iconColor_.value, other->borderWidth_); return (leftValue == rightValue); } + +bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag) +{ + if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + SC_LOG_ERROR(LABEL, "has no %{public}s.", tag.c_str()); + return false; + } + auto jsonStyle = json.at(tag); + if (!(jsonStyle.at(JSON_TEXT_TAG).is_number() && jsonStyle.at(JSON_ICON_TAG).is_number() && + jsonStyle.at(JSON_BG_TAG).is_number())) { + SC_LOG_ERROR(LABEL, "%{public}s is not number.", tag.c_str()); + return false; + } + text_ = jsonStyle.at(JSON_TEXT_TAG).get(); + icon_ = jsonStyle.at(JSON_ICON_TAG).get(); + if (!(IsParamValid())) { + return false; + } + + bg_ = static_cast(jsonStyle.at(JSON_BG_TAG).get()); + if ((bg_ <= SecCompBackground::UNKNOWN_BG) || (bg_ >= SecCompBackground::MAX_BG_TYPE)) { + return false; + } + + return true; +} } // namespace base } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn index 5707e41..778e37a 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -24,6 +24,7 @@ ohos_unittest("sec_comp_enhance_sdk_test") { sources = [ "unittest/src/sec_comp_enhance_test.cpp" ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] deps = [ diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index 000aa1d..0961c8f 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -118,6 +118,23 @@ HWTEST_F(SecCompEnhanceTest, MarshallEnhanceCfg001, TestSize.Level1) } } +/** + * @tc.name: SecCompEnhanceCfgParcel001 + * @tc.desc: test Marshalling/Unmarshalling + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, SecCompEnhanceCfgParcel001, TestSize.Level1) +{ + SecCompEnhanceCfgParcel secCompEnhanceCfgParcel; + OHOS::Parcel out; + bool result = secCompEnhanceCfgParcel.Marshalling(out); + EXPECT_TRUE(result); + + OHOS::Parcel in; + EXPECT_NE(nullptr, secCompEnhanceCfgParcel.Unmarshalling(in)); +} + /** * @tc.name: UnmarshallEnhanceCfg001 * @tc.desc: test UnmarshallEnhanceCfg diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h index deff0d4..18cc9f5 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h @@ -17,6 +17,7 @@ #include #include "sec_comp_enhance_adapter.h" +#include "sec_comp_enhance_cfg_parcel.h" #include "sec_comp_enhance_kit.h" namespace OHOS { diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index d2b1aed..9177db3 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("libsecurity_component_sdk") { configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] external_deps = [ + "access_token:libaccesstoken_sdk", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/interfaces/inner_api/security_component/include/i_sec_comp_service.h b/interfaces/inner_api/security_component/include/i_sec_comp_service.h index 4ca8e97..4fb43fc 100644 --- a/interfaces/inner_api/security_component/include/i_sec_comp_service.h +++ b/interfaces/inner_api/security_component/include/i_sec_comp_service.h @@ -16,6 +16,7 @@ #define I_SECURITY_COMPONENT_SERVICE_H #include +#include "access_token.h" #include "iremote_broker.h" #include "sec_comp_info.h" @@ -34,6 +35,7 @@ public: virtual int32_t UnregisterSecurityComponent(int32_t scId) = 0; virtual int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) = 0; + virtual bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) = 0; enum class InterfaceCode { REGISTER_SECURITY_COMPONENT = 0xff01, @@ -41,6 +43,7 @@ public: UNREGISTER_SECURITY_COMPONENT = 0xff03, REPORT_SECURITY_COMPONENT_CLICK_EVENT = 0xff04, GET_SECURITY_COMPONENT_ENHANCE_OBJECT = 0xff05, + VERIFY_TEMP_SAVE_PERMISSION = 0xff06, }; }; } // namespace SecurityComponent diff --git a/interfaces/inner_api/security_component/include/location_button.h b/interfaces/inner_api/security_component/include/location_button.h index 301d03a..f55e984 100644 --- a/interfaces/inner_api/security_component/include/location_button.h +++ b/interfaces/inner_api/security_component/include/location_button.h @@ -40,7 +40,7 @@ enum class LocationDesc : int32_t { MAX_LABEL_TYPE }; -enum class LocationIcon { +enum class LocationIcon : int32_t { UNKNOWN_ICON = -2, NO_ICON = -1, FILLED_ICON = 0, @@ -48,24 +48,9 @@ enum class LocationIcon { MAX_ICON_TYPE }; -enum class LocationBackground { - UNKNOWN_BG = -2, - NO_BG_TYPE = -1, - CAPSULE = 0, - CIRCLE = 1, - NORMAL = 2, - MAX_BG_TYPE -}; - class LocationButton : public SecCompBase { public: - LocationDesc text_ = LocationDesc::UNKNOWN_TEXT; - LocationIcon icon_ = LocationIcon::UNKNOWN_ICON; - LocationBackground bg_ = LocationBackground::UNKNOWN_BG; - - virtual bool FromJson(const nlohmann::json& jsonSrc) override; - virtual void ToJson(nlohmann::json& jsonRes) const override; - virtual std::string ToJsonStr(void) const; + virtual bool IsParamValid() override; virtual bool CompareComponentBasicInfo(SecCompBase *other) const override; private: bool ParseStyle(const nlohmann::json& json, const std::string& tag); diff --git a/interfaces/inner_api/security_component/include/paste_button.h b/interfaces/inner_api/security_component/include/paste_button.h index c6b89af..d096d40 100644 --- a/interfaces/inner_api/security_component/include/paste_button.h +++ b/interfaces/inner_api/security_component/include/paste_button.h @@ -22,7 +22,27 @@ namespace OHOS { namespace Security { namespace SecurityComponent { +enum class PasteDesc : int32_t { + UNKNOWN_TEXT = -2, + NO_TEXT = -1, + PASTE = 0, + MAX_LABEL_TYPE +}; + +enum class PasteIcon : int32_t { + UNKNOWN_ICON = -2, + NO_ICON = -1, + FILLED_ICON = 0, + LINE_ICON = 1, + MAX_ICON_TYPE +}; + class PasteButton : public SecCompBase { +public: + virtual bool IsParamValid() override; + virtual bool CompareComponentBasicInfo(SecCompBase *other) const override; +private: + bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h index 7c87437..ee1e46e 100644 --- a/interfaces/inner_api/security_component/include/save_button.h +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef SECURITY_COMPONENT_SAVE_BUTTON_H -#define SECURITY_COMPONENT_SAVE_BUTTON_H +#ifndef I_SECURITY_COMPONENT_SAVE_BUTTON_H +#define I_SECURITY_COMPONENT_SAVE_BUTTON_H #include #include "nlohmann/json.hpp" @@ -23,9 +23,36 @@ namespace OHOS { namespace Security { namespace SecurityComponent { +enum class SaveDesc : int32_t { + UNKNOWN_TEXT = -2, + NO_TEXT = -1, + DOWNLOAD = 0, + DOWNLOAD_FILES, + SAVE, + SAVE_IMAGES, + SAVE_FILES, + DOWNLOAD_AND_SHARE, + RECEIVE, + CONTINUE_TO_RECEIVE, + MAX_LABEL_TYPE +}; + +enum class SaveIcon : int32_t { + UNKNOWN_ICON = -2, + NO_ICON = -1, + FILLED_ICON = 0, + LINE_ICON = 1, + MAX_ICON_TYPE +}; + class SaveButton : public SecCompBase { +public: + virtual bool IsParamValid() override; + virtual bool CompareComponentBasicInfo(SecCompBase *other) const override; +private: + bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // SECURITY_COMPONENT_SAVE_BUTTON_H +#endif // I_SECURITY_COMPONENT_SAVE_BUTTON_H diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index 146d141..17c558e 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_base.h +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -21,28 +21,22 @@ namespace OHOS { namespace Security { namespace SecurityComponent { -template -bool ParseTypeValue(const nlohmann::json& json, const std::string& tag, - T& type, const T& min, const T& max) -{ - if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) { - return false; - } - - int32_t value = json.at(tag).get(); - if ((value <= static_cast(min)) || (value >= static_cast(max))) { - return false; - } - type = static_cast(value); - return true; -} +enum class SecCompBackground { + UNKNOWN_BG = -2, + NO_BG_TYPE = -1, + CAPSULE = 0, + CIRCLE = 1, + NORMAL = 2, + MAX_BG_TYPE +}; class SecCompBase { public: SecCompBase() = default; virtual ~SecCompBase() = default; - virtual bool FromJson(const nlohmann::json& jsonSrc); - virtual void ToJson(nlohmann::json& jsonRes) const; + bool FromJson(const nlohmann::json& jsonSrc); + void ToJson(nlohmann::json& jsonRes) const; + std::string ToJsonStr(void) const; virtual bool CompareComponentBasicInfo(SecCompBase *other) const; void SetValid(bool valid) { @@ -58,7 +52,7 @@ public: DimensionT fontSize_ = DEFAULT_DIMENSION; DimensionT iconSize_ = DEFAULT_DIMENSION; PaddingSize padding_; - DimensionT textIconPadding_ = DEFAULT_DIMENSION; + DimensionT textIconSpace_ = DEFAULT_DIMENSION; // color SecCompColor fontColor_; @@ -74,6 +68,12 @@ public: SecCompType type_ = UNKNOWN_SC_TYPE; SecCompRect rect_; bool isValid_ = false; + + int32_t text_ = UNKNOWN_TYPE; + int32_t icon_ = UNKNOWN_TYPE; + SecCompBackground bg_ = SecCompBackground::UNKNOWN_BG; +protected: + virtual bool IsParamValid() = 0; private: bool ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res); bool ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res); @@ -84,6 +84,7 @@ private: bool ParseSize(const nlohmann::json& json, const std::string& tag); bool ParseParent(const nlohmann::json& json, const std::string& tag); bool ParseRect(const nlohmann::json& json, const std::string& tag); + bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_client.h b/interfaces/inner_api/security_component/include/sec_comp_client.h index d30f1ea..0b17497 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_client.h +++ b/interfaces/inner_api/security_component/include/sec_comp_client.h @@ -35,6 +35,7 @@ public: int32_t UnregisterSecurityComponent(int32_t scId); int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo); + bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); void FinishStartSASuccess(const sptr& remoteObject); void FinishStartSAFail(); diff --git a/interfaces/inner_api/security_component/include/sec_comp_info.h b/interfaces/inner_api/security_component/include/sec_comp_info.h index 9a3c9fc..5d49e10 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_info.h +++ b/interfaces/inner_api/security_component/include/sec_comp_info.h @@ -27,6 +27,7 @@ static constexpr DimensionT MIN_ICON_SIZE = 12.0f; static constexpr DimensionT MIN_PADDING_SIZE = 0.0f; static constexpr DimensionT MIN_PADDING_WITHOUT_BG = 4.0f; static constexpr uint32_t MAX_EXTRA_SIZE = 0x1000; +static constexpr int32_t UNKNOWN_TYPE = -2; struct PaddingSize { DimensionT top = DEFAULT_DIMENSION; diff --git a/interfaces/inner_api/security_component/include/sec_comp_kit.h b/interfaces/inner_api/security_component/include/sec_comp_kit.h index e281412..2e8977e 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_kit.h +++ b/interfaces/inner_api/security_component/include/sec_comp_kit.h @@ -16,18 +16,21 @@ #define INTERFACES_INNER_API_SECURITY_COMPONENT_KIT_H #include +#include "accesstoken_kit.h" #include "sec_comp_info.h" namespace OHOS { namespace Security { namespace SecurityComponent { class SecCompKit { + public: static int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId); static int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); static int32_t UnregisterSecurityComponent(int32_t scId); static int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo); + static bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_proxy.h b/interfaces/inner_api/security_component/include/sec_comp_proxy.h index 4b91ed7..fbea5b3 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_proxy.h +++ b/interfaces/inner_api/security_component/include/sec_comp_proxy.h @@ -31,6 +31,7 @@ public: int32_t UnregisterSecurityComponent(int32_t scId) override; int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; + bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) override; private: static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp index 5a805ae..77b80b0 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -94,6 +94,17 @@ int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, return proxy->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo); } +bool SecCompClient::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + SC_LOG_ERROR(LABEL, "Proxy is null"); + return false; + } + + return proxy->ReduceAfterVerifySavePermission(tokenId); +} + bool SecCompClient::StartLoadSecCompSa() { { diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp index 7728c15..2d25705 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -61,6 +61,16 @@ int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, } return res; } + +bool SecCompKit::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) +{ + bool res = + SecCompClient::GetInstance().ReduceAfterVerifySavePermission(tokenId); + if (!res) { + SC_LOG_ERROR(LABEL, "verify temp save permission, error: %{public}d", res); + } + return res; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp index e7271b0..0c8d229 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp @@ -199,6 +199,41 @@ int32_t SecCompProxy::ReportSecurityComponentClickEvent(int32_t scId, } return res; } + +bool SecCompProxy::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { + SC_LOG_ERROR(LABEL, "Write descriptor fail"); + return false; + } + + if (!data.WriteUint32(tokenId)) { + SC_LOG_ERROR(LABEL, "Write Uint32 fail"); + return false; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + SC_LOG_ERROR(LABEL, "Remote service is null"); + return false; + } + int32_t requestResult = remote->SendRequest( + static_cast(ISecCompService::InterfaceCode::VERIFY_TEMP_SAVE_PERMISSION), + data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); + return false; + } + bool res; + if (!reply.ReadBool(res)) { + SC_LOG_ERROR(LABEL, "Read bool fail"); + return false; + } + return res; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index e4c6594..deb25c8 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -28,10 +28,11 @@ ohos_unittest("sec_comp_sdk_test") { sources = [ "unittest/src/location_button_test.cpp", - "unittest/src/sec_comp_base_test.cpp", + "unittest/src/paste_button_test.cpp", + "unittest/src/save_button_test.cpp", "unittest/src/sec_comp_kit_test.cpp", ] - + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] deps = [ @@ -40,6 +41,7 @@ ohos_unittest("sec_comp_sdk_test") { ] external_deps = [ + "access_token:libaccesstoken_sdk", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index cf69443..227420f 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -45,9 +45,10 @@ static const std::string JSON_PADDING_LEFT_TAG = "left"; static const std::string JSON_PADDING_TOP_TAG = "top"; static const std::string JSON_PADDING_RIGHT_TAG = "right"; static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; static const std::string JSON_RECT_WIDTH_TAG = "width"; static const std::string JSON_RECT_HEIGHT_TAG = "height"; +static const std::string JSON_INVALID_TAG = "test"; static const std::string JSON_COLORS_TAG = "colors"; static const std::string JSON_FONT_COLOR_TAG = "fontColor"; @@ -63,9 +64,12 @@ static const std::string WRONG_TYPE = "wrongType"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; -static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; -static constexpr uint32_t TEST_COLOR_2 = 0xff0000; -static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint32_t TEST_DIFF_COLOR = 0; +static constexpr int32_t UNKNOWN_TEXT = -2; +static constexpr int32_t UNKNOWN_ICON = -2; static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { @@ -91,9 +95,9 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) }; jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; jsonComponent[JSON_BORDER_TAG] = nlohmann::json { @@ -105,7 +109,7 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } } @@ -126,38 +130,263 @@ void LocationButtonTest::TearDown() /** * @tc.name: FromJson001 - * @tc.desc: Test location button from json success + * @tc.desc: Test LocationButton from json success * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); - LocationButton button; - ASSERT_TRUE(button.FromJson(jsonComponent)); + LocationButton comp; + ASSERT_TRUE(comp.FromJson(jsonComponent)); } /** * @tc.name: FromJson002 - * @tc.desc: Test empty location button from json + * @tc.desc: Test empty LocationButton from json * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) { nlohmann::json jsonComponent; - LocationButton button; - ASSERT_FALSE(button.FromJson(jsonComponent)); + LocationButton comp; + ASSERT_FALSE(comp.FromJson(jsonComponent)); } /** * @tc.name: FromJson003 - * @tc.desc: Test location button from wrong type params json + * @tc.desc: Test location button from wrong type json * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + LocationButton comp; + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_SC_TYPE] = WRONG_TYPE; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_SC_TYPE] = 0; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson004 + * @tc.desc: Test location button from wrong rect json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp; + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); + jsonComponent[JSON_RECT] = wrongJson; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, WRONG_TYPE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, WRONG_TYPE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, WRONG_TYPE}, + {JSON_RECT_HEIGHT, TEST_COORDINATE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE}, + {JSON_RECT_Y, TEST_COORDINATE}, + {JSON_RECT_WIDTH, TEST_COORDINATE}, + {JSON_RECT_HEIGHT, WRONG_TYPE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson005 + * @tc.desc: Test location button from wrong size json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp; + BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, WRONG_TYPE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, WRONG_TYPE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson006 + * @tc.desc: Test location button from wrong size json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp; + BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, WRONG_TYPE }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, WRONG_TYPE }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson007 + * @tc.desc: Test location button from wrong size json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp; + BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, WRONG_TYPE }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, WRONG_TYPE }, + }; + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson008 + * @tc.desc: Test location button from wrong border and parent json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp; + BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + jsonComponent[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, WRONG_TYPE }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + BuildLocationComponentInfo(jsonComponent); + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson009 + * @tc.desc: Test location button from wrong type params json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); @@ -167,14 +396,14 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, WRONG_TYPE }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, WRONG_TYPE }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); @@ -187,12 +416,47 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) } /** - * @tc.name: FromJson004 + * @tc.name: FromJson010 + * @tc.desc: Test location button from wrong type params json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + LocationButton button; + ASSERT_TRUE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, WRONG_TYPE }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, WRONG_TYPE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, WRONG_TYPE } + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson011 * @tc.desc: Test location button from wrong value params json * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); @@ -203,30 +467,82 @@ nlohmann::json jsonComponent; jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::UNKNOWN_TEXT }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::UNKNOWN_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::UNKNOWN_BG }, + { JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::MAX_LABEL_TYPE }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::MAX_ICON_TYPE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); +} + +/** + * @tc.name: ToJsonStr001 + * @tc.desc: Test ToJsonStr normal branch + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + LocationButton button; + + ASSERT_TRUE(button.FromJson(jsonComponent)); + ASSERT_EQ(jsonComponent.dump(), button.ToJsonStr()); +} + +/** + * @tc.name: CompareComponentBasicInfo001 + * @tc.desc: Test CompareComponentBasicInfo other is null + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + LocationButton button; + + ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr)); } /** * @tc.name: CompareLocationButton001 * @tc.desc: Test compare location button * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) { @@ -240,17 +556,82 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) ASSERT_TRUE(button2.FromJson(jsonComponent)); ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); - button1.text_ = LocationDesc::UNKNOWN_TEXT; + button1.text_ = UNKNOWN_TEXT; ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); - button1.text_ = LocationDesc::SELECT_LOCATION; + button1.text_ = static_cast(LocationDesc::SELECT_LOCATION); - button1.icon_ = LocationIcon::UNKNOWN_ICON; + button1.icon_ = UNKNOWN_ICON; ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); - button1.icon_ = LocationIcon::LINE_ICON; + button1.icon_ = static_cast(LocationIcon::LINE_ICON); - button1.bg_ = LocationBackground::UNKNOWN_BG; + button1.bg_ = SecCompBackground::UNKNOWN_BG; ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); - button1.bg_ = LocationBackground::CIRCLE; + button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); } + +/** + * @tc.name: CompareLocationButton002 + * @tc.desc: Test LocationButton compare + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp1; + BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp1.FromJson(jsonComponent)); + LocationButton comp2 = comp1; + + comp1.type_ = SAVE_COMPONENT; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.type_ = LOCATION_COMPONENT; + + comp1.fontSize_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.fontSize_ = TEST_SIZE; + + comp1.iconSize_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.iconSize_ = TEST_SIZE; + + comp1.padding_.top = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.top = TEST_DIMENSION; + + comp1.padding_.right = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.right = TEST_DIMENSION; + + comp1.padding_.bottom = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.bottom = TEST_DIMENSION; + + comp1.padding_.left = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.left = TEST_DIMENSION; + + comp1.textIconSpace_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.textIconSpace_ = TEST_SIZE; + + comp1.borderWidth_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.borderWidth_ = TEST_SIZE; + + comp1.fontColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.fontColor_.value = TEST_COLOR_RED; + + comp1.bgColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.bgColor_.value = TEST_COLOR_YELLOW; + + comp1.iconColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.iconColor_.value = TEST_COLOR_BLUE; + + ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); +} \ No newline at end of file diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp similarity index 37% rename from interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp rename to interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index 8df03a1..eb54474 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "sec_comp_base_test.h" +#include "paste_button_test.h" #include -#include "sec_comp_log.h" #include "sec_comp_err.h" +#include "sec_comp_log.h" using namespace testing::ext; using namespace OHOS; @@ -23,7 +23,12 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompBaseTest"}; + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "PasteButtonTest"}; + +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; static const std::string JSON_RECT = "rect"; static const std::string JSON_SC_TYPE = "type"; @@ -40,7 +45,7 @@ static const std::string JSON_PADDING_LEFT_TAG = "left"; static const std::string JSON_PADDING_TOP_TAG = "top"; static const std::string JSON_PADDING_RIGHT_TAG = "right"; static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; static const std::string JSON_RECT_WIDTH_TAG = "width"; static const std::string JSON_RECT_HEIGHT_TAG = "height"; @@ -58,14 +63,16 @@ static const std::string WRONG_TYPE = "wrongType"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; -static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; -static constexpr uint32_t TEST_COLOR_2 = 0xff0000; -static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; static constexpr uint32_t TEST_DIFF_COLOR = 0; +static constexpr int32_t UNKNOWN_TEXT = -2; +static constexpr int32_t UNKNOWN_ICON = -2; -static void BuildSecCompBaseInfo(nlohmann::json& jsonComponent) +static void BuildPasteComponentInfo(nlohmann::json& jsonComponent) { - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_SC_TYPE] = PASTE_COMPONENT; jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE }, {JSON_RECT_Y, TEST_COORDINATE }, @@ -87,9 +94,9 @@ static void BuildSecCompBaseInfo(nlohmann::json& jsonComponent) }; jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; jsonComponent[JSON_BORDER_TAG] = nlohmann::json { @@ -98,292 +105,135 @@ static void BuildSecCompBaseInfo(nlohmann::json& jsonComponent) jsonComponent[JSON_PARENT_TAG] = nlohmann::json { { JSON_PARENT_EFFECT_TAG, false }, }; + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, PasteDesc::PASTE }, + { JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; } } -void SecCompBaseTest::SetUpTestCase() +void PasteButtonTest::SetUpTestCase() {} -void SecCompBaseTest::TearDownTestCase() +void PasteButtonTest::TearDownTestCase() {} -void SecCompBaseTest::SetUp() +void PasteButtonTest::SetUp() { SC_LOG_INFO(LABEL, "setup"); } -void SecCompBaseTest::TearDown() +void PasteButtonTest::TearDown() {} /** - * @tc.name: FromJson001 - * @tc.desc: Test SecCompBase from json success - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompBaseTest, FromJson001, TestSize.Level1) -{ - nlohmann::json jsonComponent; - BuildSecCompBaseInfo(jsonComponent); - SecCompBase comp; - ASSERT_TRUE(comp.FromJson(jsonComponent)); -} - -/** - * @tc.name: FromJson002 - * @tc.desc: Test empty SecCompBase from json + * @tc.name: IsParamValid001 + * @tc.desc: Test paste button from wrong value params json * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9JB */ -HWTEST_F(SecCompBaseTest, FromJson002, TestSize.Level1) +HWTEST_F(PasteButtonTest, FromJson010, TestSize.Level1) { - nlohmann::json jsonComponent; - SecCompBase comp; - ASSERT_FALSE(comp.FromJson(jsonComponent)); -} - -/** - * @tc.name: FromJson003 - * @tc.desc: Test location button from wrong type json - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompBaseTest, FromJson003, TestSize.Level1) -{ - nlohmann::json jsonComponent; - BuildSecCompBaseInfo(jsonComponent); - SecCompBase comp; - ASSERT_TRUE(comp.FromJson(jsonComponent)); - - jsonComponent[JSON_SC_TYPE] = WRONG_TYPE; - ASSERT_FALSE(comp.FromJson(jsonComponent)); - - jsonComponent[JSON_SC_TYPE] = 0; - ASSERT_FALSE(comp.FromJson(jsonComponent)); -} - -/** - * @tc.name: FromJson004 - * @tc.desc: Test location button from wrong rect json - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompBaseTest, FromJson004, TestSize.Level1) -{ - nlohmann::json jsonComponent; - SecCompBase comp; - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; - nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); - jsonComponent[JSON_RECT] = wrongJson; - ASSERT_FALSE(comp.FromJson(jsonComponent)); - - BuildSecCompBaseInfo(jsonComponent); - ASSERT_TRUE(comp.FromJson(jsonComponent)); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, WRONG_TYPE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); +nlohmann::json jsonComponent; + BuildPasteComponentInfo(jsonComponent); + PasteButton button; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, WRONG_TYPE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + ASSERT_TRUE(button.FromJson(jsonComponent)); - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, WRONG_TYPE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, PasteDesc::UNKNOWN_TEXT }, + { JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, WRONG_TYPE} + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, PasteDesc::PASTE}, + { JSON_ICON_TAG, PasteIcon::UNKNOWN_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); -} - -/** - * @tc.name: FromJson005 - * @tc.desc: Test location button from wrong size json - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompBaseTest, FromJson005, TestSize.Level1) -{ - nlohmann::json jsonComponent; - SecCompBase comp; - BuildSecCompBaseInfo(jsonComponent); - ASSERT_TRUE(comp.FromJson(jsonComponent)); + ASSERT_FALSE(button.FromJson(jsonComponent)); - nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, PasteDesc::PASTE }, + { JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, }; + ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, WRONG_TYPE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, PasteDesc::MAX_LABEL_TYPE }, + { JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, WRONG_TYPE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, PasteDesc::PASTE }, + { JSON_ICON_TAG, PasteIcon::MAX_ICON_TYPE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, PasteDesc::PASTE }, + { JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + ASSERT_FALSE(button.FromJson(jsonComponent)); } /** - * @tc.name: FromJson006 - * @tc.desc: Test location button from wrong size json + * @tc.name: ComparePasteButton001 + * @tc.desc: Test compare paste button * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9JB */ -HWTEST_F(SecCompBaseTest, FromJson006, TestSize.Level1) +HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) { - nlohmann::json jsonComponent; - SecCompBase comp; - BuildSecCompBaseInfo(jsonComponent); - ASSERT_TRUE(comp.FromJson(jsonComponent)); + PasteButton button1; + PasteButton button2; - nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, WRONG_TYPE }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, - }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); - - jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, WRONG_TYPE }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, - }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); -} - -/** - * @tc.name: FromJson007 - * @tc.desc: Test location button from wrong size json - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompBaseTest, FromJson007, TestSize.Level1) -{ nlohmann::json jsonComponent; - SecCompBase comp; - BuildSecCompBaseInfo(jsonComponent); - ASSERT_TRUE(comp.FromJson(jsonComponent)); + BuildPasteComponentInfo(jsonComponent); - nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, WRONG_TYPE }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, - }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + ASSERT_TRUE(button1.FromJson(jsonComponent)); + ASSERT_TRUE(button2.FromJson(jsonComponent)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); - jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, WRONG_TYPE }, - }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); -} + button1.text_ = UNKNOWN_TEXT; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.text_ = static_cast(PasteDesc::PASTE); -/** - * @tc.name: FromJson008 - * @tc.desc: Test location button from wrong border and parent json - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompBaseTest, FromJson008, TestSize.Level1) -{ - nlohmann::json jsonComponent; - SecCompBase comp; - BuildSecCompBaseInfo(jsonComponent); - ASSERT_TRUE(comp.FromJson(jsonComponent)); + button1.icon_ = UNKNOWN_ICON; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.icon_ = static_cast(PasteIcon::LINE_ICON); - jsonComponent[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, WRONG_TYPE }, - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + button1.bg_ = SecCompBackground::UNKNOWN_BG; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.bg_ = SecCompBackground::CIRCLE; - BuildSecCompBaseInfo(jsonComponent); - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, - }; - ASSERT_FALSE(comp.FromJson(jsonComponent)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); } /** - * @tc.name: Compare001 - * @tc.desc: Test SecCompBase compare + * @tc.name: ComparePasteButton002 + * @tc.desc: Test PasteButton compare * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9JB */ -HWTEST_F(SecCompBaseTest, Compare001, TestSize.Level1) +HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) { nlohmann::json jsonComponent; - SecCompBase comp1; - BuildSecCompBaseInfo(jsonComponent); + PasteButton comp1; + BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); - SecCompBase comp2 = comp1; + PasteButton comp2 = comp1; - comp1.type_ = SAVE_COMPONENT; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); comp1.type_ = LOCATION_COMPONENT; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.type_ = PASTE_COMPONENT; comp1.fontSize_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); @@ -409,9 +259,9 @@ HWTEST_F(SecCompBaseTest, Compare001, TestSize.Level1) ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); comp1.padding_.left = TEST_DIMENSION; - comp1.textIconPadding_ = DEFAULT_DIMENSION; + comp1.textIconSpace_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); - comp1.textIconPadding_ = TEST_SIZE; + comp1.textIconSpace_ = TEST_SIZE; comp1.borderWidth_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); @@ -419,15 +269,15 @@ HWTEST_F(SecCompBaseTest, Compare001, TestSize.Level1) comp1.fontColor_.value = TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); - comp1.fontColor_.value = TEST_COLOR_2; + comp1.fontColor_.value = TEST_COLOR_RED; comp1.bgColor_.value = TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); - comp1.bgColor_.value = TEST_COLOR_1; + comp1.bgColor_.value = TEST_COLOR_YELLOW; comp1.iconColor_.value = TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); - comp1.iconColor_.value = TEST_COLOR_3; + comp1.iconColor_.value = TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); -} +} \ No newline at end of file diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.h b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.h new file mode 100644 index 0000000..6e73d91 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 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 SEC_COMP_PASTE_BUTTON_TEST_H +#define SEC_COMP_PASTE_BUTTON_TEST_H + +#include +#include "paste_button.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class PasteButtonTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_PASTE_BUTTON_TEST_H diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp new file mode 100644 index 0000000..541e85f --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -0,0 +1,283 @@ +/* + * Copyright (c) 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. + */ +#include "save_button_test.h" +#include +#include "sec_comp_log.h" +#include "sec_comp_err.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SaveButtonTest"}; + +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; + +static const std::string JSON_RECT = "rect"; +static const std::string JSON_SC_TYPE = "type"; +static const std::string JSON_RECT_X = "x"; +static const std::string JSON_RECT_Y = "y"; +static const std::string JSON_RECT_WIDTH = "width"; +static const std::string JSON_RECT_HEIGHT = "height"; + +static const std::string JSON_SIZE_TAG = "size"; +static const std::string JSON_FONT_SIZE_TAG = "fontSize"; +static const std::string JSON_ICON_SIZE_TAG = "iconSize"; +static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; +static const std::string JSON_PADDING_LEFT_TAG = "left"; +static const std::string JSON_PADDING_TOP_TAG = "top"; +static const std::string JSON_PADDING_RIGHT_TAG = "right"; +static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; +static const std::string JSON_RECT_WIDTH_TAG = "width"; +static const std::string JSON_RECT_HEIGHT_TAG = "height"; + +static const std::string JSON_COLORS_TAG = "colors"; +static const std::string JSON_FONT_COLOR_TAG = "fontColor"; +static const std::string JSON_ICON_COLOR_TAG = "iconColor"; +static const std::string JSON_BG_COLOR_TAG = "bgColor"; + +static const std::string JSON_BORDER_TAG = "border"; +static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; +static const std::string JSON_PARENT_TAG = "parent"; +static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; + +static const std::string WRONG_TYPE = "wrongType"; +static constexpr float TEST_SIZE = 100.0; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr double TEST_DIMENSION = 100.0; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint32_t TEST_DIFF_COLOR = 0; +static constexpr int32_t UNKNOWN_TEXT = -2; +static constexpr int32_t UNKNOWN_ICON = -2; + +static void BuildSaveComponentInfo(nlohmann::json& jsonComponent) +{ + jsonComponent[JSON_SC_TYPE] = SAVE_COMPONENT; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + + jsonComponent[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; +} +} + +void SaveButtonTest::SetUpTestCase() +{} + +void SaveButtonTest::TearDownTestCase() +{} + +void SaveButtonTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); +} + +void SaveButtonTest::TearDown() +{} + +/** + * @tc.name: IsParamValid001 + * @tc.desc: Test save button from wrong value params json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SaveButtonTest, FromJson010, TestSize.Level1) +{ +nlohmann::json jsonComponent; + BuildSaveComponentInfo(jsonComponent); + SaveButton button; + + ASSERT_TRUE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, SaveDesc::UNKNOWN_TEXT }, + { JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, SaveDesc::DOWNLOAD}, + { JSON_ICON_TAG, SaveIcon::UNKNOWN_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, SaveDesc::MAX_LABEL_TYPE }, + { JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JSON_ICON_TAG, SaveIcon::MAX_ICON_TYPE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); + + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, + }; + ASSERT_FALSE(button.FromJson(jsonComponent)); +} + +/** + * @tc.name: CompareSaveButton001 + * @tc.desc: Test compare save button + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) +{ + SaveButton button1; + SaveButton button2; + + nlohmann::json jsonComponent; + BuildSaveComponentInfo(jsonComponent); + + ASSERT_TRUE(button1.FromJson(jsonComponent)); + ASSERT_TRUE(button2.FromJson(jsonComponent)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); + + button1.text_ = UNKNOWN_TEXT; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.text_ = static_cast(SaveDesc::DOWNLOAD); + + button1.icon_ = UNKNOWN_ICON; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.icon_ = static_cast(SaveIcon::LINE_ICON); + + button1.bg_ = SecCompBackground::UNKNOWN_BG; + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + button1.bg_ = SecCompBackground::CIRCLE; + + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); +} + +/** + * @tc.name: CompareSaveButton002 + * @tc.desc: Test SaveButton compare + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) +{ + nlohmann::json jsonComponent; + SaveButton comp1; + BuildSaveComponentInfo(jsonComponent); + ASSERT_TRUE(comp1.FromJson(jsonComponent)); + SaveButton comp2 = comp1; + + comp1.type_ = PASTE_COMPONENT; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.type_ = SAVE_COMPONENT; + + comp1.fontSize_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.fontSize_ = TEST_SIZE; + + comp1.iconSize_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.iconSize_ = TEST_SIZE; + + comp1.padding_.top = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.top = TEST_DIMENSION; + + comp1.padding_.right = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.right = TEST_DIMENSION; + + comp1.padding_.bottom = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.bottom = TEST_DIMENSION; + + comp1.padding_.left = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.padding_.left = TEST_DIMENSION; + + comp1.textIconSpace_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.textIconSpace_ = TEST_SIZE; + + comp1.borderWidth_ = DEFAULT_DIMENSION; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.borderWidth_ = TEST_SIZE; + + comp1.fontColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.fontColor_.value = TEST_COLOR_RED; + + comp1.bgColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.bgColor_.value = TEST_COLOR_YELLOW; + + comp1.iconColor_.value = TEST_DIFF_COLOR; + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + comp1.iconColor_.value = TEST_COLOR_BLUE; + + ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); +} \ No newline at end of file diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.h b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.h similarity index 83% rename from interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.h rename to interfaces/inner_api/security_component/test/unittest/src/save_button_test.h index 78a9c6a..34c9f6a 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_base_test.h +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.h @@ -12,16 +12,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef SEC_COMP_BASE_TEST_H -#define SEC_COMP_BASE_TEST_H + +#ifndef SEC_COMP_SAVE_BUTTON_TEST_H +#define SEC_COMP_SAVE_BUTTON_TEST_H #include -#include "sec_comp_base.h" +#include "save_button.h" namespace OHOS { namespace Security { namespace SecurityComponent { -class SecCompBaseTest : public testing::Test { +class SaveButtonTest : public testing::Test { public: static void SetUpTestCase(); @@ -34,4 +35,4 @@ public: } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // SEC_COMP_BASE_TEST_H +#endif // SEC_COMP_SAVE_BUTTON_TEST_H diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 9bcdc9c..4aa76c8 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -59,14 +59,14 @@ void SecCompKitTest::TearDown() */ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) { - SecCompBase comp; + LocationButton comp; comp.fontSize_ = TEST_SIZE; comp.iconSize_ = TEST_SIZE; comp.padding_.top = TEST_DIMENSION; comp.padding_.right = TEST_DIMENSION; comp.padding_.bottom = TEST_DIMENSION; comp.padding_.left = TEST_DIMENSION; - comp.textIconPadding_ = TEST_SIZE; + comp.textIconSpace_ = TEST_SIZE; comp.bgColor_.value = TEST_COLOR; comp.fontColor_.value = TEST_COLOR; comp.iconColor_.value = TEST_COLOR; diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 3279925..10b6b42 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -50,8 +50,10 @@ ohos_shared_library("security_component_service") { "sa_main/sec_comp_entity.cpp", "sa_main/sec_comp_info_helper.cpp", "sa_main/sec_comp_manager.cpp", + "sa_main/sec_comp_perm_manager.cpp", "sa_main/sec_comp_service.cpp", "sa_main/sec_comp_stub.cpp", + "sa_main/sec_event_handler.cpp", ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -70,6 +72,7 @@ ohos_shared_library("security_component_service") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "c_utils:utils", + "eventhandler:libeventhandler", "graphic_standard:librender_service_client", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index 07bed0a..44e22f2 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -38,10 +38,6 @@ int32_t SecCompEntity::RevokeTempPermission() int32_t SecCompEntity::GrantTempPermission() { - if (isGrant_) { - SC_LOG_ERROR(LABEL, "security component is already granted"); - return SC_OK; - } isGrant_ = true; return SecCompInfoHelper::GrantTempPermission(tokenId_, componentInfo_); } diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.h b/services/security_component_service/sa/sa_main/sec_comp_entity.h index 7b4de17..912ea8a 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.h +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.h @@ -17,9 +17,10 @@ #define SECURITY_COMPONENT_ENTITY_H #include -#include "access_token.h" +#include "accesstoken_kit.h" #include "sec_comp_base.h" #include "sec_comp_info.h" +#include "sec_comp_perm_manager.h" namespace OHOS { namespace Security { @@ -27,7 +28,8 @@ namespace SecurityComponent { class SecCompEntity { public: SecCompEntity(std::shared_ptr component, - AccessToken::AccessTokenID token, int32_t scId) : componentInfo_(component), tokenId_(token), scId_(scId) {}; + AccessToken::AccessTokenID token, int32_t scId) : componentInfo_(component), + tokenId_(token), scId_(scId) {}; ~SecCompEntity() = default; int32_t RevokeTempPermission(); int32_t GrantTempPermission(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index e233f7f..d931534 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -23,6 +23,7 @@ #include "save_button.h" #include "sec_comp_err.h" #include "sec_comp_log.h" +#include "sec_comp_service.h" #include "sec_comp_tool.h" namespace OHOS { @@ -113,75 +114,67 @@ static bool CheckRectValid(const SecCompRect& rect) return true; } -static bool CheckSecCompBase(const SecCompBase* comp) +static bool CheckSecCompBaseButton(const SecCompBase* comp) { - if (!CheckRectValid(comp->rect_)) { - SC_LOG_INFO(LABEL, "check component rect failed."); + if ((comp->text_ < 0) && (comp->icon_ < 0)) { + SC_LOG_INFO(LABEL, "both text and icon do not exist."); return false; } - - if (comp->parentEffect_) { - SC_LOG_ERROR(LABEL, "parentEffect is active, security component invalid."); + if ((comp->text_ >= 0) && comp->fontSize_ < MIN_FONT_SIZE) { + SC_LOG_INFO(LABEL, "font size invalid."); + return false; + } + if ((comp->icon_ >= 0) && comp->iconSize_ < MIN_ICON_SIZE) { + SC_LOG_INFO(LABEL, "icon size invalid."); return false; } - if ((comp->padding_.top < MIN_PADDING_SIZE) || (comp->padding_.right < MIN_PADDING_SIZE) || - (comp->padding_.bottom < MIN_PADDING_SIZE) || (comp->padding_.left < MIN_PADDING_SIZE) || - (comp->textIconPadding_ < MIN_PADDING_SIZE)) { - SC_LOG_ERROR(LABEL, "size is invalid."); + if (comp->bg_ != SecCompBackground::NO_BG_TYPE && + (IsColorSimilar(comp->fontColor_, comp->bgColor_) || IsColorSimilar(comp->iconColor_, comp->bgColor_))) { + SC_LOG_INFO(LABEL, "fontColor or iconColor is similar whith backgroundColor."); return false; } - if (IsColorTransparent(comp->bgColor_) || IsColorTransparent(comp->fontColor_) || - IsColorTransparent(comp->iconColor_)) { - SC_LOG_ERROR(LABEL, "bgColor or fontColor or iconColor is too transparent."); + if (comp->bg_ == SecCompBackground::NO_BG_TYPE && + ((comp->padding_.top != MIN_PADDING_WITHOUT_BG) || (comp->padding_.right != MIN_PADDING_WITHOUT_BG) || + (comp->padding_.bottom != MIN_PADDING_WITHOUT_BG) || (comp->padding_.left != MIN_PADDING_WITHOUT_BG))) { + SC_LOG_INFO(LABEL, "padding can not change without background."); return false; } return true; } -static bool CheckLocationButton(const LocationButton* comp) +static bool CheckSecCompBase(const SecCompBase* comp) { - if ((comp->text_ == LocationDesc::NO_TEXT) && (comp->icon_ == LocationIcon::NO_ICON)) { - SC_LOG_INFO(LABEL, "both text and icon do not exist."); + if (!CheckRectValid(comp->rect_)) { + SC_LOG_INFO(LABEL, "check component rect failed."); return false; } - if ((comp->text_ != LocationDesc::NO_TEXT) && (comp->fontSize_ < MIN_FONT_SIZE)) { - SC_LOG_INFO(LABEL, "font size invalid."); + + if (comp->parentEffect_) { + SC_LOG_ERROR(LABEL, "parentEffect is active, security component invalid."); return false; } - if ((comp->icon_ != LocationIcon::NO_ICON) &&(comp->iconSize_ < MIN_ICON_SIZE)) { - SC_LOG_INFO(LABEL, "icon size invalid."); + + if ((comp->padding_.top < MIN_PADDING_SIZE) || (comp->padding_.right < MIN_PADDING_SIZE) || + (comp->padding_.bottom < MIN_PADDING_SIZE) || (comp->padding_.left < MIN_PADDING_SIZE) || + (comp->textIconSpace_ < MIN_PADDING_SIZE)) { + SC_LOG_ERROR(LABEL, "size is invalid."); return false; } - if (comp->bg_ != LocationBackground::NO_BG_TYPE && - (IsColorSimilar(comp->fontColor_, comp->bgColor_) || IsColorSimilar(comp->iconColor_, comp->bgColor_))) { - SC_LOG_INFO(LABEL, "fontColor or iconColor is similar whith backgroundColor."); + if (IsColorTransparent(comp->bgColor_) || IsColorTransparent(comp->fontColor_) || + IsColorTransparent(comp->iconColor_)) { + SC_LOG_ERROR(LABEL, "bgColor or fontColor or iconColor is too transparent."); return false; } - - if (comp->bg_ == LocationBackground::NO_BG_TYPE && - ((comp->padding_.top != MIN_PADDING_WITHOUT_BG) || (comp->padding_.right != MIN_PADDING_WITHOUT_BG) || - (comp->padding_.bottom != MIN_PADDING_WITHOUT_BG) || (comp->padding_.left != MIN_PADDING_WITHOUT_BG))) { - SC_LOG_INFO(LABEL, "padding can not change without background."); + if (!CheckSecCompBaseButton(comp)) { return false; } - return true; } -static bool CheckPasteButton(const PasteButton* comp) -{ - return false; -} - -static bool CheckSaveButton(const SaveButton* comp) -{ - return false; -} - bool SecCompInfoHelper::CheckComponentValid(const SecCompBase* comp) { if ((comp == nullptr) || !IsComponentTypeValid(comp->type_)) { @@ -194,23 +187,7 @@ bool SecCompInfoHelper::CheckComponentValid(const SecCompBase* comp) return false; } - bool valid = false; - switch (comp->type_) { - case LOCATION_COMPONENT: - valid = CheckLocationButton(reinterpret_cast(comp)); - break; - case PASTE_COMPONENT: - valid = CheckPasteButton(reinterpret_cast(comp)); - break; - case SAVE_COMPONENT: - valid = CheckSaveButton(reinterpret_cast(comp)); - break; - default: - SC_LOG_ERROR(LABEL, "Parse component type unknown"); - break; - } - - return valid; + return true; } int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID tokenId, @@ -220,11 +197,15 @@ int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID token SC_LOG_ERROR(LABEL, "revoke component is null"); return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } + SecCompType type = componentInfo->type_; switch (type) { case LOCATION_COMPONENT: - return AccessToken::AccessTokenKit::RevokePermission(tokenId, "ohos.permission.LOCATION", + SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + case PASTE_COMPONENT: + SC_LOG_DEBUG(LABEL, "revoke paste permission"); + return SC_OK; default: SC_LOG_ERROR(LABEL, "revoke component type unknown"); break; @@ -235,15 +216,22 @@ int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID token int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo) { - if (componentInfo == nullptr) { + if ((tokenId <= 0) || (componentInfo == nullptr)) { SC_LOG_ERROR(LABEL, "revoke component is null"); return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } + SecCompType type = componentInfo->type_; switch (type) { case LOCATION_COMPONENT: - return AccessToken::AccessTokenKit::GrantPermission(tokenId, "ohos.permission.LOCATION", + SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + case PASTE_COMPONENT: + SC_LOG_DEBUG(LABEL, "grant paste permission"); + return SC_OK; + case SAVE_COMPONENT: + SC_LOG_DEBUG(LABEL, "grant save permission"); + return SecCompPermManager::GetInstance().GrantTempSavePermission(tokenId); default: SC_LOG_ERROR(LABEL, "Parse component type unknown"); break; diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h index 17879dd..64d9cd3 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h @@ -19,6 +19,7 @@ #include "nlohmann/json.hpp" #include "sec_comp_base.h" #include "sec_comp_info.h" +#include "sec_comp_perm_manager.h" namespace OHOS { namespace Security { diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 178bf35..52a0f84 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -281,6 +281,14 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, return sc->GrantTempPermission(); } +bool SecCompManager::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) +{ + if (SecCompPermManager::GetInstance().RevokeTempSavePermission(tokenId) == SC_OK) { + return true; + } + return false; +} + void SecCompManager::DumpSecComp(std::string& dumpStr) const { for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { @@ -293,6 +301,12 @@ void SecCompManager::DumpSecComp(std::string& dumpStr) const } } } + +bool SecCompManager::Initialize() +{ + SC_LOG_DEBUG(LABEL, "Initialize!!"); + return SecCompPermManager::GetInstance().InitEventHandler(); +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index 5f4488c..501094a 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -21,7 +21,6 @@ #include #include -#include "access_token.h" #include "accesstoken_kit.h" #include "app_state_observer.h" #include "nocopyable.h" @@ -48,13 +47,15 @@ public: const SecCompCallerInfo& caller, int32_t& scId); int32_t UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller); - int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); + int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo); + bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); void NotifyProcessForeground(int32_t uid); void NotifyProcessBackground(int32_t uid); void NotifyProcessDied(int32_t uid); void DumpSecComp(std::string& dumpStr) const; + bool Initialize(); private: SecCompManager(); @@ -72,12 +73,9 @@ private: std::unordered_map> componentMap_; int32_t scValidCount_; int32_t scIdStart_; - - bool Initialize() const; DISALLOW_COPY_AND_MOVE(SecCompManager); }; } // namespace SecurityComponent } // namespace Security } // namespace OHOS #endif // SECURITY_COMPONENT_MANAGER_H - diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp new file mode 100644 index 0000000..57c6a23 --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_comp_perm_manager.h" + +#include "location_button.h" +#include "paste_button.h" +#include "save_button.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include "sec_comp_service.h" +#include "sec_comp_tool.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompPermManager"}; +static const int32_t WAIT_MILLISECONDS = 5 * 1000; +} + +SecCompPermManager& SecCompPermManager::GetInstance() +{ + static SecCompPermManager instance; + return instance; +} + +bool SecCompPermManager::DelayRevokePermission(AccessToken::AccessTokenID tokenId, const std::string& taskName) +{ + if (secHandler_ == nullptr) { + SC_LOG_ERROR(LABEL, "fail to get EventHandler"); + return false; + } + + std::function delayed = ([tokenId]() { + SC_LOG_DEBUG(LABEL, "delay revoke save permission"); + SecCompPermManager::GetInstance().RevokeTempSavePermission(tokenId); + }); + + SC_LOG_DEBUG(LABEL, "revoke save permission after %{public}d ms", WAIT_MILLISECONDS); + secHandler_->ProxyPostTask(delayed, taskName, WAIT_MILLISECONDS); + return true; +} + +bool SecCompPermManager::RevokeSavePermissionTask(const std::string& taskName) +{ + if (secHandler_ == nullptr) { + SC_LOG_ERROR(LABEL, "fail to get EventHandler"); + return false; + } + + SC_LOG_DEBUG(LABEL, "revoke save permission task name:%{public}s", taskName.c_str()); + secHandler_->ProxyRemoveTask(taskName); + return true; +} + +int32_t SecCompPermManager::GrantLocationPermission(AccessToken::AccessTokenID tokenId, + const std::string& permissionName, int flag) +{ + return AccessToken::AccessTokenKit::GrantPermission(tokenId, "ohos.permission.LOCATION", + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); +} + +int32_t SecCompPermManager::RevokeLocationPermission(AccessToken::AccessTokenID tokenId, + const std::string& permissionName, int flag) +{ + return AccessToken::AccessTokenKit::RevokePermission(tokenId, "ohos.permission.LOCATION", + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); +} + +int32_t SecCompPermManager::GrantTempSavePermission(AccessToken::AccessTokenID tokenId) +{ + auto current = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + std::string taskName = std::to_string(tokenId) + std::to_string(current); + if (!DelayRevokePermission(tokenId, taskName)) { + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + std::lock_guard lock(mutex_); + taskQue_.push_back(taskName); + applySaveCountMap_[tokenId]++; + SC_LOG_DEBUG(LABEL, "tokenId: %{public}d current permission apply counts is: %{public}d.", + tokenId, applySaveCountMap_[tokenId]); + return SC_OK; +} + +int32_t SecCompPermManager::RevokeTempSavePermission(AccessToken::AccessTokenID tokenId) +{ + std::lock_guard lock(mutex_); + auto iter = applySaveCountMap_.find(tokenId); + if (iter == applySaveCountMap_.end() || applySaveCountMap_[tokenId] == 0) { + SC_LOG_ERROR(LABEL, "This hap has no permissions to save files."); + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + std::string taskName = taskQue_.front(); + if (!RevokeSavePermissionTask(taskName)) { + return false; + } + taskQue_.pop_front(); + SC_LOG_DEBUG(LABEL, "tokenId: %{public}d current permission apply counts is: %{public}d.", + tokenId, applySaveCountMap_[tokenId]); + if ((--applySaveCountMap_[tokenId]) == 0) { + applySaveCountMap_.erase(tokenId); + } + return SC_OK; +} + +bool SecCompPermManager::InitEventHandler() +{ + secRunner_ = AppExecFwk::EventRunner::Create(true); + if (!secRunner_) { + SC_LOG_ERROR(LABEL, "failed to create a recvRunner."); + return false; + } + + secHandler_ = std::make_shared(secRunner_); + if (secHandler_ == nullptr) { + SC_LOG_ERROR(LABEL, "failed to create secHandler_."); + return false; + } + return true; +} + +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h new file mode 100644 index 0000000..314b0d1 --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_PERMISSION_MANAGER_H +#define SECURITY_COMPONENT_PERMISSION_MANAGER_H + +#include +#include + +#include "accesstoken_kit.h" +#include "rwlock.h" +#include "sec_comp_base.h" +#include "sec_event_handler.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompPermManager { +public: + SecCompPermManager() = default; + virtual ~SecCompPermManager() = default; + static SecCompPermManager& GetInstance(); + + int32_t GrantLocationPermission(AccessToken::AccessTokenID tokenId, + const std::string& permissionName, int flag); + int32_t RevokeLocationPermission(AccessToken::AccessTokenID tokenId, + const std::string& permissionName, int flag); + + int32_t GrantTempSavePermission(AccessToken::AccessTokenID tokenId); + int32_t RevokeTempSavePermission(AccessToken::AccessTokenID tokenId); + + bool InitEventHandler(); + std::shared_ptr GetSecEventHandler() const; + +private: + bool DelayRevokePermission(AccessToken::AccessTokenID tokenId, const std::string& taskName); + bool RevokeSavePermissionTask(const std::string& taskName); + + std::unordered_map applySaveCountMap_; + std::deque taskQue_; + std::mutex mutex_; + std::shared_ptr secRunner_; + std::shared_ptr secHandler_; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_PERMISSION_MANAGER_H diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index 94d084b..d0815fc 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -58,6 +58,10 @@ void SecCompService::OnStart() SC_LOG_ERROR(LABEL, "Failed to publish service!"); return; } + if (!Initialize()) { + SC_LOG_ERROR(LABEL, "Failed to initialize"); + return; + } SC_LOG_INFO(LABEL, "Congratulations, SecCompService start successfully!"); } @@ -199,6 +203,11 @@ int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, return SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, touchInfo); } +bool SecCompService::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) +{ + return SecCompManager::GetInstance().ReduceAfterVerifySavePermission(tokenId); +} + int SecCompService::Dump(int fd, const std::vector& args) { if (fd < 0) { @@ -223,6 +232,11 @@ int SecCompService::Dump(int fd, const std::vector& args) } return ERR_OK; } + +bool SecCompService::Initialize() const +{ + return SecCompManager::GetInstance().Initialize(); +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.h b/services/security_component_service/sa/sa_main/sec_comp_service.h index 5d735be..8b347e0 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.h +++ b/services/security_component_service/sa/sa_main/sec_comp_service.h @@ -45,6 +45,7 @@ public: int32_t UnregisterSecurityComponent(int32_t scId) override; int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; + bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) override; int Dump(int fd, const std::vector& args) override; private: diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index 4790b7b..a11b575 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -161,6 +161,27 @@ int32_t SecCompStub::ReportSecurityComponentClickEventInner(MessageParcel& data, return SC_OK; } +int32_t SecCompStub::ReduceAfterVerifySavePermissionInner(MessageParcel& data, MessageParcel& reply) +{ + uint32_t tokenId; + if (!data.ReadUint32(tokenId)) { + SC_LOG_ERROR(LABEL, "Read component id fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + if (tokenId <= 0) { + SC_LOG_ERROR(LABEL, "AccessTokenId invalid"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + bool res = this->ReduceAfterVerifySavePermission(tokenId); + if (!reply.WriteBool(res)) { + SC_LOG_ERROR(LABEL, "verify temp save permission result fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return SC_OK; +} + SecCompStub::SecCompStub() { requestFuncMap_[static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT)] = @@ -171,6 +192,8 @@ SecCompStub::SecCompStub() &SecCompStub::UnregisterSecurityComponentInner; requestFuncMap_[static_cast(ISecCompService::InterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT)] = &SecCompStub::ReportSecurityComponentClickEventInner; + requestFuncMap_[static_cast(ISecCompService::InterfaceCode::VERIFY_TEMP_SAVE_PERMISSION)] = + &SecCompStub::ReduceAfterVerifySavePermissionInner; } SecCompStub::~SecCompStub() diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.h b/services/security_component_service/sa/sa_main/sec_comp_stub.h index b858161..9f7205a 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.h +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.h @@ -35,7 +35,7 @@ private: int32_t UpdateSecurityComponentInner(MessageParcel& data, MessageParcel& reply); int32_t UnregisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply); int32_t ReportSecurityComponentClickEventInner(MessageParcel& data, MessageParcel& reply); - + int32_t ReduceAfterVerifySavePermissionInner(MessageParcel& data, MessageParcel& reply); using RequestFuncType = int32_t (SecCompStub::*)(MessageParcel& data, MessageParcel& reply); std::map requestFuncMap_; }; diff --git a/services/security_component_service/sa/sa_main/sec_event_handler.cpp b/services/security_component_service/sa/sa_main/sec_event_handler.cpp new file mode 100644 index 0000000..c60b00a --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_event_handler.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 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. + */ + +#include "sec_event_handler.h" + +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +SecEventHandler::SecEventHandler( + const std::shared_ptr& runner) + : AppExecFwk::EventHandler(runner) {} + +SecEventHandler::~SecEventHandler() = default; + +bool SecEventHandler::ProxyPostTask( + const Callback &callback, const std::string &name, int64_t delayTime) +{ + return AppExecFwk::EventHandler::PostTask(callback, name, delayTime); +} + +void SecEventHandler::ProxyRemoveTask(const std::string &name) +{ + AppExecFwk::EventHandler::RemoveTask(name); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/services/security_component_service/sa/sa_main/sec_event_handler.h b/services/security_component_service/sa/sa_main/sec_event_handler.h new file mode 100644 index 0000000..e3c0fff --- /dev/null +++ b/services/security_component_service/sa/sa_main/sec_event_handler.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 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 SEC_EVENT_HANDLER_H +#define SEC_EVENT_HANDLER_H + +#include + +#include "event_handler.h" +#include "event_runner.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecEventHandler : public AppExecFwk::EventHandler { +public: + explicit SecEventHandler(const std::shared_ptr& runner); + ~SecEventHandler() override; + + bool ProxyPostTask(const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0); + + void ProxyRemoveTask(const std::string &name); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_EVENT_HANDLER_H diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 9ec1f4f..8d402e5 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -34,10 +34,12 @@ ohos_unittest("sec_comp_service_test") { sources = [ "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", "unittest/src/app_state_observer_test.cpp", @@ -48,6 +50,7 @@ ohos_unittest("sec_comp_service_test") { "unittest/src/sec_comp_stub_test.cpp", ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] deps = [ @@ -59,6 +62,7 @@ ohos_unittest("sec_comp_service_test") { external_deps = [ "access_token:libtoken_setproc", "c_utils:utils", + "eventhandler:libeventhandler", "graphic_standard:librender_service_client", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp index 36d9db2..420015b 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -54,7 +54,7 @@ void AppStateObserverTest::TearDown() * @tc.name: IsProcessForeground001 * @tc.desc: Test is process foreground * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) { @@ -70,7 +70,7 @@ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) * @tc.name: AddProcessToForegroundSet001 * @tc.desc: Test add process to foreground * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) { @@ -93,7 +93,7 @@ HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) * @tc.name: RemoveProcessFromForegroundSet001 * @tc.desc: Test remove process from foreground * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level1) { @@ -107,11 +107,29 @@ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); } +/** + * @tc.name: RemoveProcessFromForegroundSet002 + * @tc.desc: Test remove process with different uid + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet002, TestSize.Level1) +{ + AppExecFwk::ProcessData procData = { + .uid = TEST_UID + }; + observer_->AddProcessToForegroundSet(procData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + procData.uid = TEST_UID_2; + observer_->RemoveProcessFromForegroundSet(procData); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); +} + /** * @tc.name: OnProcessStateChanged001 * @tc.desc: Test recieve process state changed * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) { @@ -135,7 +153,7 @@ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) * @tc.name: OnProcessDied001 * @tc.desc: Test recieve process state died * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(AppStateObserverTest, OnProcessDied001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 579eca1..ec2935d 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -14,6 +14,9 @@ */ #include "sec_comp_entity_test.h" #include "sec_comp_log.h" +#include "location_button.h" +#include "paste_button.h" +#include "save_button.h" #include "sec_comp_err.h" #include "sec_comp_tool.h" @@ -42,7 +45,7 @@ void SecCompEntityTest::SetUp() return; } - std::shared_ptr component = std::make_shared(); + std::shared_ptr component = std::make_shared(); ASSERT_NE(nullptr, component); entity_ = std::make_shared(component, 1, 1); @@ -58,7 +61,7 @@ void SecCompEntityTest::TearDown() * @tc.name: RevokeTempPermission001 * @tc.desc: Test revoke temp permission * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompEntityTest, RevokeTempPermission001, TestSize.Level1) { @@ -78,15 +81,12 @@ HWTEST_F(SecCompEntityTest, RevokeTempPermission001, TestSize.Level1) /** * @tc.name: GrantTempPermission001 - * @tc.desc: Test grant permission + * @tc.desc: Test grant location permission * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level1) { - entity_->isGrant_ = true; - ASSERT_EQ(entity_->GrantTempPermission(), SC_OK); - entity_->isGrant_ = false; entity_->componentInfo_->type_ = UNKNOWN_SC_TYPE; ASSERT_EQ(entity_->GrantTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); @@ -96,13 +96,36 @@ HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level1) entity_->componentInfo_->type_ = LOCATION_COMPONENT; ASSERT_EQ(entity_->GrantTempPermission(), SC_OK); ASSERT_TRUE(entity_->isGrant_); + + entity_->isGrant_ = false; + entity_->componentInfo_->type_ = PASTE_COMPONENT; + ASSERT_EQ(entity_->GrantTempPermission(), SC_OK); + ASSERT_TRUE(entity_->isGrant_); +} + +/** + * @tc.name: GrantTempPermission002 + * @tc.desc: Test grant paste permission with invalid tokenId. + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompEntityTest, GrantTempPermission002, TestSize.Level1) +{ + std::shared_ptr pasteComponent = std::make_shared(); + ASSERT_NE(nullptr, pasteComponent); + + entity_ = std::make_shared(pasteComponent, 0, 1); + ASSERT_NE(nullptr, entity_); + + ASSERT_EQ(entity_->GrantTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + ASSERT_EQ(entity_->RevokeTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); } /** * @tc.name: CheckTouchInfo001 * @tc.desc: Test touch info * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 9b441ad..3dc44f3 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -50,7 +50,7 @@ static const std::string JSON_PADDING_LEFT_TAG = "left"; static const std::string JSON_PADDING_TOP_TAG = "top"; static const std::string JSON_PADDING_RIGHT_TAG = "right"; static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconPadding"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; static const std::string JSON_RECT_WIDTH_TAG = "width"; static const std::string JSON_RECT_HEIGHT_TAG = "height"; @@ -67,10 +67,12 @@ static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_DIMENSION = 100.0; static constexpr double TEST_INVALID_DIMENSION = -100.0; -static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; -static constexpr uint32_t TEST_COLOR_2 = 0xff0000; -static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; +static constexpr uint32_t TEST_COLOR_BLACK = 0x000000; +static constexpr uint32_t TEST_COLOR_WHITE = 0xffffff; static constexpr uint32_t QUARTER = 4; static constexpr double ZERO_OFFSET = 0.0F; static double g_curScreenWidth = 0.0F; @@ -120,9 +122,9 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) }; jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; jsonComponent[JSON_BORDER_TAG] = nlohmann::json { @@ -134,7 +136,7 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } } @@ -161,7 +163,7 @@ void SecCompInfoHelperTest::TearDown() * @tc.name: ParseComponent001 * @tc.desc: Test parse component info success * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) { @@ -176,7 +178,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) * @tc.name: ParseComponent002 * @tc.desc: Test parse component info with empty json * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) { @@ -192,7 +194,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) * @tc.name: ParseComponent003 * @tc.desc: Test parse component info with invalid type * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) { @@ -204,14 +206,14 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) jsonComponent[JSON_SC_TYPE] = UNKNOWN_SC_TYPE; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); + ASSERT_EQ(comp, nullptr); } /** * @tc.name: ParseComponent004 * @tc.desc: Test parse component info with invalid rect * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) { @@ -239,7 +241,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_curScreenWidth }, + {JSON_RECT_X, g_curScreenWidth + 1 }, {JSON_RECT_Y, g_testHeight }, {JSON_RECT_WIDTH, g_testWidth }, {JSON_RECT_HEIGHT, g_testHeight } @@ -249,7 +251,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, g_curScreenHeight }, + {JSON_RECT_Y, g_curScreenHeight + 1 }, {JSON_RECT_WIDTH, g_testWidth }, {JSON_RECT_HEIGHT, g_testHeight } }; @@ -261,7 +263,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) * @tc.name: ParseComponent005 * @tc.desc: Test parse component info with invalid rect * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) { @@ -309,8 +311,8 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, g_testWidth }, {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_curScreenWidth * g_curScreenHeight }, - {JSON_RECT_HEIGHT, g_curScreenWidth * g_curScreenHeight } + {JSON_RECT_WIDTH, g_curScreenWidth }, + {JSON_RECT_HEIGHT, g_curScreenHeight } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -320,7 +322,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) * @tc.name: ParseComponent006 * @tc.desc: Test parse component info with parentEffect active * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) { @@ -341,7 +343,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) * @tc.name: ParseComponent007 * @tc.desc: Test parse component info with invalid size * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) { @@ -404,7 +406,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) * @tc.name: ParseComponent008 * @tc.desc: Test parse component info with invalid size * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) { @@ -450,7 +452,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) * @tc.name: ParseComponent009 * @tc.desc: Test parse component info with invalid size * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) { @@ -496,7 +498,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) * @tc.name: ParseComponent010 * @tc.desc: Test parse component info with invalid color * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) { @@ -507,23 +509,23 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) jsonComponent[JSON_COLORS_TAG] = nlohmann::json { { JSON_FONT_COLOR_TAG, TEST_COLOR_INVALID }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, { JSON_ICON_COLOR_TAG, TEST_COLOR_INVALID }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, { JSON_BG_COLOR_TAG, TEST_COLOR_INVALID } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -534,7 +536,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) * @tc.name: ParseComponent011 * @tc.desc: Test parse component info with invalid style * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) { @@ -546,7 +548,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::NO_TEXT }, { JSON_ICON_TAG, LocationIcon::NO_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -556,7 +558,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) * @tc.name: ParseComponent012 * @tc.desc: Test parse component info with invalid style * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { @@ -568,12 +570,12 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_1 }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_3 }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_FONT_COLOR_TAG, TEST_COLOR_YELLOW }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -581,12 +583,12 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::CIRCLE }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, }; jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_1 }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -594,12 +596,12 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::NO_BG_TYPE }, + { JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_2 }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_1 }, - { JSON_BG_COLOR_TAG, TEST_COLOR_1 } + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -609,7 +611,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) * @tc.name: ParseComponent013 * @tc.desc: Test parse component info with invalid style * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) { @@ -621,7 +623,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::NO_BG_TYPE }, + { JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, @@ -660,7 +662,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) * @tc.name: ParseComponent014 * @tc.desc: Test parse component info with invalid style * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) { @@ -672,7 +674,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) jsonComponent[JSON_STYLE_TAG] = nlohmann::json { { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, LocationBackground::NO_BG_TYPE }, + { JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; nlohmann::json jsonPadding = nlohmann::json { @@ -707,3 +709,63 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } + +/** + * @tc.name: CheckComponentValid001 + * @tc.desc: Test CheckComponentValid with invalid color + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLACK }, + { JSON_BG_COLOR_TAG, TEST_COLOR_WHITE } + }; + ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); +} + +/** + * @tc.name: CheckComponentValid002 + * @tc.desc: Test CheckComponentValid with invalid text or icon + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + comp->text_ = static_cast(LocationDesc::UNKNOWN_TEXT); + ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + + comp->text_ = static_cast(LocationDesc::SELECT_LOCATION); + comp->icon_ = static_cast(LocationIcon::UNKNOWN_ICON); + ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + + comp->text_ = static_cast(LocationDesc::UNKNOWN_TEXT); + ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); +} + +/** + * @tc.name: CheckComponentValid003 + * @tc.desc: Test CheckComponentValid with invalid type + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + comp->type_ = SecCompType::UNKNOWN_SC_TYPE; + ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); + + comp->type_ = SecCompType::MAX_SC_TYPE; + ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); +} \ No newline at end of file diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 67a07b2..a145957 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -16,6 +16,7 @@ #include "sec_comp_manager_test.h" #include "sec_comp_log.h" #include "location_button.h" +#include "save_button.h" #include "sec_comp_err.h" using namespace testing::ext; @@ -31,14 +32,17 @@ static constexpr double TEST_SIZE = 100.0; static constexpr uint32_t TEST_INVALID_SIZE = 0; static constexpr double TEST_COORDINATE = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffff; -static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; -static constexpr uint32_t TEST_COLOR_2 = 0xff0000; -static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; static constexpr int32_t TEST_UID_1 = 1; static constexpr int32_t TEST_UID_2 = 2; static constexpr int32_t TEST_UID_3 = 3; +static constexpr int32_t UNKNOWN_TEXT = -2; +static constexpr int32_t UNKNOWN_ICON = -2; + static constexpr AccessTokenID TEST_TOKEN_ID = 1; static constexpr int32_t TEST_SC_ID_1 = 1; static constexpr int32_t TEST_SC_ID_2 = 2; @@ -56,7 +60,7 @@ static LocationButton BuildInvalidLocationComponent() button.padding_.right = TEST_INVALID_SIZE; button.padding_.bottom = TEST_INVALID_SIZE; button.padding_.left = TEST_INVALID_SIZE; - button.textIconPadding_ = TEST_INVALID_SIZE; + button.textIconSpace_ = TEST_INVALID_SIZE; button.fontColor_.value = TEST_COLOR; button.iconColor_.value = TEST_COLOR; button.bgColor_.value = TEST_COLOR; @@ -66,9 +70,9 @@ static LocationButton BuildInvalidLocationComponent() button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; - button.text_ = LocationDesc::UNKNOWN_TEXT; - button.icon_ = LocationIcon::UNKNOWN_ICON; - button.bg_ = LocationBackground::UNKNOWN_BG; + button.text_ = UNKNOWN_TEXT; + button.icon_ = UNKNOWN_ICON; + button.bg_ = SecCompBackground::UNKNOWN_BG; return button; } @@ -81,19 +85,19 @@ static LocationButton BuildValidLocationComponent() button.padding_.right = TEST_SIZE; button.padding_.bottom = TEST_SIZE; button.padding_.left = TEST_SIZE; - button.textIconPadding_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_1; - button.iconColor_.value = TEST_COLOR_2; - button.bgColor_.value = TEST_COLOR_3; + button.textIconSpace_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_YELLOW; + button.iconColor_.value = TEST_COLOR_RED; + button.bgColor_.value = TEST_COLOR_BLUE; button.borderWidth_ = TEST_SIZE; button.type_ = LOCATION_COMPONENT; button.rect_.x_ = TEST_COORDINATE; button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; - button.text_ = LocationDesc::SELECT_LOCATION; - button.icon_ = LocationIcon::LINE_ICON; - button.bg_ = LocationBackground::CIRCLE; + button.text_ = static_cast(LocationDesc::SELECT_LOCATION); + button.icon_ = static_cast(LocationIcon::LINE_ICON); + button.bg_ = SecCompBackground::CIRCLE; return button; } } @@ -119,7 +123,7 @@ void SecCompManagerTest::TearDown() * @tc.name: CreateScId001 * @tc.desc: Test create sc id * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) { @@ -134,11 +138,11 @@ HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) * @tc.name: AddProcessComponent001 * @tc.desc: Test add process component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, AddProcessComponent001, TestSize.Level1) { - std::shared_ptr compPtr = std::make_shared(); + std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; @@ -153,11 +157,11 @@ HWTEST_F(SecCompManagerTest, AddProcessComponent001, TestSize.Level1) * @tc.name: AddSecurityComponentToList001 * @tc.desc: Test add security component to list * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) { - std::shared_ptr compPtr = std::make_shared(); + std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; @@ -167,7 +171,7 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entity), SC_OK); - std::shared_ptr compPtrNew = std::make_shared(); + std::shared_ptr compPtrNew = std::make_shared(); ASSERT_NE(compPtrNew, nullptr); compPtrNew->rect_.x_ = TEST_COORDINATE * 2; // not overlap compPtrNew->rect_.y_ = TEST_COORDINATE * 2; // not overlap @@ -181,14 +185,14 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) * @tc.name: DeleteSecurityComponentFromList001 * @tc.desc: Test delete security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1) { ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); - std::shared_ptr compPtr = std::make_shared(); + std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; @@ -207,13 +211,13 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 * @tc.name: GetSecurityComponentFromList001 * @tc.desc: Test get security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) { ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); - std::shared_ptr compPtr = std::make_shared(); + std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; @@ -229,13 +233,13 @@ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) * @tc.name: NotifyProcessBackground001 * @tc.desc: Test notify process background * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) { SecCompManager::GetInstance().NotifyProcessBackground(TEST_UID_1); - std::shared_ptr compPtr = std::make_shared(); + std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; @@ -257,11 +261,11 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) * @tc.name: NotifyProcessDied001 * @tc.desc: Test notify process died * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) { - std::shared_ptr compPtr = std::make_shared(); + std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; @@ -270,7 +274,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); - std::shared_ptr compPtr2 = std::make_shared(); + std::shared_ptr compPtr2 = std::make_shared(); ASSERT_NE(compPtr2, nullptr); compPtr2->rect_.x_ = TEST_COORDINATE * 2; // not overlap compPtr2->rect_.y_ = TEST_COORDINATE * 2; // not overlap @@ -290,7 +294,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) * @tc.name: RegisterSecurityComponent001 * @tc.desc: Test register security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) { @@ -317,7 +321,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) * @tc.name: UpdateSecurityComponent001 * @tc.desc: Test update security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) { @@ -345,7 +349,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) * @tc.name: UnregisterSecurityComponent001 * @tc.desc: Test unregister security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) { @@ -362,7 +366,7 @@ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) * @tc.name: ReportSecurityComponentClickEvent001 * @tc.desc: Test report security component click * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 5a42581..87f0cbd 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -18,11 +18,13 @@ #include "location_button.h" #include "mock_system_ability_proxy.h" #include "mock_app_mgr_proxy.h" -#include "token_setproc.h" +#include "paste_button.h" +#include "save_button.h" #include "sec_comp_err.h" #include "sec_comp_log.h" #include "sec_comp_tool.h" #include "system_ability.h" +#include "token_setproc.h" using namespace testing::ext; using namespace OHOS; @@ -38,9 +40,10 @@ static constexpr int32_t SA_ID = 3506; static constexpr double TEST_COORDINATE = 100.0; static constexpr int32_t TEST_SC_ID = 1; static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_COLOR_1 = 0x7fff00; -static constexpr uint32_t TEST_COLOR_2 = 0xff0000; -static constexpr uint32_t TEST_COLOR_3 = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static AccessTokenID g_selfTokenId = 0; static std::string BuildLocationComponentInfo() { @@ -51,19 +54,47 @@ static std::string BuildLocationComponentInfo() button.padding_.right = TEST_SIZE; button.padding_.bottom = TEST_SIZE; button.padding_.left = TEST_SIZE; - button.textIconPadding_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_1; - button.iconColor_.value = TEST_COLOR_2; - button.bgColor_.value = TEST_COLOR_3; + button.textIconSpace_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_YELLOW; + button.iconColor_.value = TEST_COLOR_RED; + button.bgColor_.value = TEST_COLOR_BLUE; button.borderWidth_ = TEST_SIZE; button.type_ = LOCATION_COMPONENT; button.rect_.x_ = TEST_COORDINATE; button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; - button.text_ = LocationDesc::SELECT_LOCATION; - button.icon_ = LocationIcon::LINE_ICON; - button.bg_ = LocationBackground::CIRCLE; + button.text_ = static_cast(LocationDesc::SELECT_LOCATION); + button.icon_ = static_cast(LocationIcon::LINE_ICON); + button.bg_ = SecCompBackground::CIRCLE; + + nlohmann::json jsonRes; + button.ToJson(jsonRes); + return jsonRes.dump(); +} + +static std::string BuildSaveComponentInfo() +{ + SaveButton button; + button.fontSize_ = TEST_SIZE; + button.iconSize_ = TEST_SIZE; + button.padding_.top = TEST_SIZE; + button.padding_.right = TEST_SIZE; + button.padding_.bottom = TEST_SIZE; + button.padding_.left = TEST_SIZE; + button.textIconSpace_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_YELLOW; + button.iconColor_.value = TEST_COLOR_RED; + button.bgColor_.value = TEST_COLOR_BLUE; + button.borderWidth_ = TEST_SIZE; + button.type_ = SAVE_COMPONENT; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.text_ = static_cast(SaveDesc::DOWNLOAD); + button.icon_ = static_cast(SaveIcon::LINE_ICON); + button.bg_ = SecCompBackground::CIRCLE; nlohmann::json jsonRes; button.ToJson(jsonRes); @@ -88,6 +119,7 @@ void SecCompServiceTest::SetUp() ASSERT_NE(nullptr, secCompService_); secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); ASSERT_TRUE(secCompService_->appStateObserver_ != nullptr); + g_selfTokenId = GetSelfTokenID(); } void SecCompServiceTest::TearDown() @@ -96,13 +128,14 @@ void SecCompServiceTest::TearDown() secCompService_->appStateObserver_ = nullptr; } secCompService_ = nullptr; + EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId)); } /** * @tc.name: Onstart001 * @tc.desc: Test OnStart * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1) { @@ -125,7 +158,7 @@ HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1) * @tc.name: RegisterAppStateObserver001 * @tc.desc: Test RegisterAppStateObserver * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) { @@ -188,7 +221,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) * @tc.name: UnregisterAppStateObserver001 * @tc.desc: Test RegisterAppStateObserver * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level1) { @@ -205,7 +238,7 @@ HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level1) * @tc.name: GetCallerInfo001 * @tc.desc: Test get caller info * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) { @@ -214,25 +247,22 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); // set token id to hap token, but uid is not in foreground - AccessTokenID selfTokenId = GetSelfTokenID(); - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); - EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); + EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); // add local uid to foreground. AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); EXPECT_TRUE(secCompService_->GetCallerInfo(caller)); - - SetSelfTokenID(selfTokenId); } /** * @tc.name: RegisterSecurityComponent001 * @tc.desc: Test register security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) { @@ -242,7 +272,6 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) SC_SERVICE_ERROR_VALUE_INVALID); // parse component json fail - AccessTokenID selfTokenId = GetSelfTokenID(); ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); AppExecFwk::AppStateData stateData = { .uid = getuid() @@ -265,14 +294,13 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), SC_OK); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); - SetSelfTokenID(selfTokenId); } /** * @tc.name: UnregisterSecurityComponent001 * @tc.desc: Test unregister security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) { @@ -284,48 +312,160 @@ HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) * @tc.name: UpdateSecurityComponent001 * @tc.desc: Test update security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) { // get caller fail EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, ""), SC_SERVICE_ERROR_VALUE_INVALID); - AccessTokenID selfTokenId = GetSelfTokenID(); ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, "{a"), SC_SERVICE_ERROR_VALUE_INVALID); - - SetSelfTokenID(selfTokenId); } /** * @tc.name: ReportSecurityComponentClickEvent001 - * @tc.desc: Test update security component + * @tc.desc: Test register security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { - // get caller fail - struct SecCompClickEvent touch = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touchInfo = { + .touchX = 100, + .touchY = 100, .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(TEST_SC_ID, BuildLocationComponentInfo(), touch), - SC_SERVICE_ERROR_VALUE_INVALID); - AccessTokenID selfTokenId = GetSelfTokenID(); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo)); + + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(6); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent002 + * @tc.desc: Test report security component click with save button + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Level1) +{ + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(TEST_SC_ID, "{a", touch), - SC_SERVICE_ERROR_VALUE_INVALID); - SetSelfTokenID(selfTokenId); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touchInfo = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) + }; + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + sleep(3); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.desc: Test report security component click twice with save button + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +{ + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touchInfo = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) + }; + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(6); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp index 88f565c..9f0ff9e 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp @@ -53,7 +53,7 @@ void SecCompStubTest::TearDown() * @tc.name: OnRemoteRequest001 * @tc.desc: Test on remote request * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) { @@ -77,7 +77,7 @@ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) * @tc.name: RegisterSecurityComponentInner001 * @tc.desc: Test register security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level1) { @@ -104,7 +104,7 @@ HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level1) * @tc.name: UpdateSecurityComponentInner001 * @tc.desc: Test update security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level1) { @@ -128,7 +128,7 @@ HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level1) * @tc.name: UnregisterSecurityComponentInner001 * @tc.desc: Test unregister security component * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level1) { @@ -147,7 +147,7 @@ HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level1) * @tc.name: ReportSecurityComponentClickEventInner001 * @tc.desc: Test report click event * @tc.type: FUNC - * @tc.require: + * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompStubTest, ReportSecurityComponentClickEventInner001, TestSize.Level1) { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h index 15a7bdb..23e7000 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h @@ -49,6 +49,11 @@ public: { return 0; }; + + bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) override + { + return true; + } }; class SecCompStubTest : public testing::Test { -- Gitee From fe3d7e36c5ca1eb703696c27d13dbf8432131eba Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 6 Jun 2023 16:47:42 +0800 Subject: [PATCH 20/59] Description:add caller check Match-id-6bd635ce1a16a4a0bde7e3976d4bbce280f969fe --- frameworks/common/include/sec_comp_err.h | 1 + .../inner_api/security_component/BUILD.gn | 2 + .../include/sec_comp_caller_authorization.h | 43 ++++++++++++ .../security_component/include/sec_comp_kit.h | 1 + .../include/sec_comp_ui_register.h | 33 +++++++++ .../src/sec_comp_caller_authorization.cpp | 68 +++++++++++++++++++ .../security_component/src/sec_comp_kit.cpp | 25 +++++++ .../src/sec_comp_ui_register.cpp | 35 ++++++++++ .../test/unittest/src/sec_comp_kit_test.cpp | 49 +++++++++++++ 9 files changed, 257 insertions(+) create mode 100644 interfaces/inner_api/security_component/include/sec_comp_caller_authorization.h create mode 100644 interfaces/inner_api/security_component/include/sec_comp_ui_register.h create mode 100644 interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp create mode 100644 interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp diff --git a/frameworks/common/include/sec_comp_err.h b/frameworks/common/include/sec_comp_err.h index 8ded864..675e47d 100644 --- a/frameworks/common/include/sec_comp_err.h +++ b/frameworks/common/include/sec_comp_err.h @@ -33,6 +33,7 @@ enum SCErrCode : int32_t { SC_SERVICE_ERROR_PERMISSION_OPER_FAIL = -59, SC_SERVICE_ERROR_CLICK_EVENT_INVALID = -60, SC_SERVICE_ERROR_COMPONENT_INFO_NOT_EQUAL = -61, + SC_SERVICE_ERROR_CALLER_INVALID = -62, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE = -100, SC_ENHANCE_ERROR_VALUE_INVALID = -101, diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 9177db3..a441175 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -38,11 +38,13 @@ ohos_shared_library("libsecurity_component_sdk") { ] sources = [ + "src/sec_comp_caller_authorization.cpp", "src/sec_comp_client.cpp", "src/sec_comp_death_recipient.cpp", "src/sec_comp_kit.cpp", "src/sec_comp_load_callback.cpp", "src/sec_comp_proxy.cpp", + "src/sec_comp_ui_register.cpp" ] deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_framework" ] diff --git a/interfaces/inner_api/security_component/include/sec_comp_caller_authorization.h b/interfaces/inner_api/security_component/include/sec_comp_caller_authorization.h new file mode 100644 index 0000000..d1564a0 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_caller_authorization.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 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 INTERFACES_INNER_API_SECURITY_COMPONENT_CALLER_AYTHORIZATION_H +#define INTERFACES_INNER_API_SECURITY_COMPONENT_CALLER_AYTHORIZATION_H + +#include +#include +#include "nocopyable.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompCallerAuthorization { +public: + static SecCompCallerAuthorization& GetInstance(); + virtual ~SecCompCallerAuthorization() = default; + void RegisterSecCompKitCaller(std::vector& callerList); + bool IsKitCaller(uintptr_t callerAddr); + +private: + SecCompCallerAuthorization() = default; + std::vector kitCallerList_; + bool isInit_ = false; + DISALLOW_COPY_AND_MOVE(SecCompCallerAuthorization); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // INTERFACES_INNER_API_SECURITY_COMPONENT_CALLER_AYTHORIZATION_H + diff --git a/interfaces/inner_api/security_component/include/sec_comp_kit.h b/interfaces/inner_api/security_component/include/sec_comp_kit.h index 2e8977e..d0ab068 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_kit.h +++ b/interfaces/inner_api/security_component/include/sec_comp_kit.h @@ -18,6 +18,7 @@ #include #include "accesstoken_kit.h" #include "sec_comp_info.h" +#include "sec_comp_ui_register.h" namespace OHOS { namespace Security { diff --git a/interfaces/inner_api/security_component/include/sec_comp_ui_register.h b/interfaces/inner_api/security_component/include/sec_comp_ui_register.h new file mode 100644 index 0000000..ab8efc9 --- /dev/null +++ b/interfaces/inner_api/security_component/include/sec_comp_ui_register.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 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 INTERFACES_INNER_API_SECURITY_COMPONENT_UI_REGISTER_H +#define INTERFACES_INNER_API_SECURITY_COMPONENT_UI_REGISTER_H + +#include +#include + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompUiRegister { +public: + SecCompUiRegister(std::vector& callerList); + virtual ~SecCompUiRegister() = default; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // INTERFACES_INNER_API_SECURITY_COMPONENT_UI_REGISTER_H + diff --git a/interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp b/interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp new file mode 100644 index 0000000..def9e78 --- /dev/null +++ b/interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_caller_authorization.h" + +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { + +namespace { +static constexpr int32_t MAX_FUNC_ASM_SIZE = 0x250; +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompCallerAuthorization"}; +static constexpr size_t MAX_CALLER_SIZE = 10; +} + +void SecCompCallerAuthorization::RegisterSecCompKitCaller(std::vector& callerList) +{ + if (isInit_) { + SC_LOG_ERROR(LABEL, "can not init repeatly"); + return; + } + + isInit_ = true; + if ((callerList.size() == 0) || (callerList.size() > MAX_CALLER_SIZE)) { + SC_LOG_ERROR(LABEL, "caller size is invalid"); + return; + } + + kitCallerList_ = callerList; +} + +bool SecCompCallerAuthorization::IsKitCaller(uintptr_t callerAddr) +{ + if (!isInit_) { + SC_LOG_INFO(LABEL, "caller authorization has not init"); + return true; + } + for (size_t i = 0; i < kitCallerList_.size(); i++) { + if ((callerAddr > kitCallerList_[i]) && (callerAddr < kitCallerList_[i] + MAX_FUNC_ASM_SIZE)) { + return true; + } + } + return false; +} + +SecCompCallerAuthorization& SecCompCallerAuthorization::GetInstance() +{ + static SecCompCallerAuthorization instance; + return instance; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp index 2d25705..a34adbc 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_kit.h" +#include "sec_comp_caller_authorization.h" #include "sec_comp_client.h" #include "sec_comp_log.h" @@ -26,6 +27,12 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId) { + if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( + reinterpret_cast(__builtin_return_address(0)))) { + SC_LOG_ERROR(LABEL, "register security component fail, caller invalid"); + return SC_SERVICE_ERROR_CALLER_INVALID; + } + int32_t res = SecCompClient::GetInstance().RegisterSecurityComponent(type, componentInfo, scId); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "register security component fail, error: %{public}d", res); @@ -35,6 +42,12 @@ int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) { + if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( + reinterpret_cast(__builtin_return_address(0)))) { + SC_LOG_ERROR(LABEL, "update security component fail, caller invalid"); + return SC_SERVICE_ERROR_CALLER_INVALID; + } + int32_t res = SecCompClient::GetInstance().UpdateSecurityComponent(scId, componentInfo); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "update security component fail, error: %{public}d", res); @@ -44,6 +57,12 @@ int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, const std::string& com int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) { + if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( + reinterpret_cast(__builtin_return_address(0)))) { + SC_LOG_ERROR(LABEL, "unregister security component fail, caller invalid"); + return SC_SERVICE_ERROR_CALLER_INVALID; + } + int32_t res = SecCompClient::GetInstance().UnregisterSecurityComponent(scId); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "unregister security component fail, error: %{public}d", res); @@ -54,6 +73,12 @@ int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) { + if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( + reinterpret_cast(__builtin_return_address(0)))) { + SC_LOG_ERROR(LABEL, "report click event fail, caller invalid"); + return SC_SERVICE_ERROR_CALLER_INVALID; + } + int32_t res = SecCompClient::GetInstance().ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo); if (res != SC_OK) { diff --git a/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp b/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp new file mode 100644 index 0000000..e2a9bf4 --- /dev/null +++ b/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_ui_register.h" +#include "sec_comp_caller_authorization.h" +#include "sec_comp_log.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompUiRegister"}; +} // namespace + +SecCompUiRegister::SecCompUiRegister(std::vector& callerList) +{ + SC_LOG_INFO(LABEL, "Init"); + SecCompCallerAuthorization::GetInstance().RegisterSecCompKitCaller(callerList); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 4aa76c8..6b1aab6 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -14,6 +14,9 @@ */ #include "sec_comp_kit_test.h" #include "location_button.h" +#define private public +#include "sec_comp_caller_authorization.h" +#undef private #include "sec_comp_err.h" #include "sec_comp_info.h" #include "sec_comp_log.h" @@ -29,6 +32,19 @@ static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffffff; + +static bool TestInCallerCheckList() +{ + int32_t scId = -1; + struct SecCompClickEvent touch; + if ((SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId) == SC_SERVICE_ERROR_CALLER_INVALID) || + (SecCompKit::UpdateSecurityComponent(scId, "") == SC_SERVICE_ERROR_CALLER_INVALID) || + (SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch) == SC_SERVICE_ERROR_CALLER_INVALID) || + (SecCompKit::UnregisterSecurityComponent(scId) == SC_SERVICE_ERROR_CALLER_INVALID)) { + return false; + } + return true; +} } // namespace void SecCompKitTest::SetUpTestCase() @@ -51,6 +67,21 @@ void SecCompKitTest::TearDown() SC_LOG_INFO(LABEL, "TearDown."); } +namespace { +static bool TestInCallerNotCheckList() +{ + int32_t scId = -1; + struct SecCompClickEvent touch; + if ((SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId) != SC_SERVICE_ERROR_CALLER_INVALID) || + (SecCompKit::UpdateSecurityComponent(scId, "") != SC_SERVICE_ERROR_CALLER_INVALID) || + (SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch) != SC_SERVICE_ERROR_CALLER_INVALID) || + (SecCompKit::UnregisterSecurityComponent(scId) != SC_SERVICE_ERROR_CALLER_INVALID)) { + return false; + } + return true; +} +} + /** * @tc.name: ExceptCall001 * @tc.desc: do kit except call. @@ -93,3 +124,21 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonRes.dump(), touch)); EXPECT_NE(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } + +/** + * @tc.name: ExceptCall001 + * @tc.desc: do kit except call. + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1) +{ + std::vector callerList = { + reinterpret_cast(TestInCallerCheckList), + }; + SecCompUiRegister registerCallback(callerList); + TestInCallerCheckList(); + TestInCallerNotCheckList(); + + SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); +} -- Gitee From 42c7b92d290941df53bc6d22c7d79b4339ad408d Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 9 Jun 2023 14:23:46 +0800 Subject: [PATCH 21/59] Description:add caller check unittest Match-id-3cd2ec8e44b4858513288f55dd104bc4553cc495 --- .../test/unittest/src/sec_comp_kit_test.cpp | 84 +++++++++++++------ 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 6b1aab6..699a523 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -32,18 +32,36 @@ static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffffff; +static constexpr size_t MAX_CALLER_SIZE = 10; -static bool TestInCallerCheckList() +static void TestInCallerNotCheckList() { int32_t scId = -1; struct SecCompClickEvent touch; - if ((SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId) == SC_SERVICE_ERROR_CALLER_INVALID) || - (SecCompKit::UpdateSecurityComponent(scId, "") == SC_SERVICE_ERROR_CALLER_INVALID) || - (SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch) == SC_SERVICE_ERROR_CALLER_INVALID) || - (SecCompKit::UnregisterSecurityComponent(scId) == SC_SERVICE_ERROR_CALLER_INVALID)) { - return false; - } - return true; + int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId); + int updateRes = SecCompKit::UpdateSecurityComponent(scId, ""); + int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch); + int unregistRes = SecCompKit::UnregisterSecurityComponent(scId); + + EXPECT_EQ(registerRes, SC_SERVICE_ERROR_CALLER_INVALID); + EXPECT_EQ(updateRes, SC_SERVICE_ERROR_CALLER_INVALID); + EXPECT_EQ(reportRes, SC_SERVICE_ERROR_CALLER_INVALID); + EXPECT_EQ(unregistRes, SC_SERVICE_ERROR_CALLER_INVALID); +} + +static void TestInCallerCheckList() +{ + int32_t scId = -1; + struct SecCompClickEvent touch; + int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId); + int updateRes = SecCompKit::UpdateSecurityComponent(scId, ""); + int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch); + int unregistRes = SecCompKit::UnregisterSecurityComponent(scId); + + EXPECT_NE(registerRes, SC_SERVICE_ERROR_CALLER_INVALID); + EXPECT_NE(updateRes, SC_SERVICE_ERROR_CALLER_INVALID); + EXPECT_NE(reportRes, SC_SERVICE_ERROR_CALLER_INVALID); + EXPECT_NE(unregistRes, SC_SERVICE_ERROR_CALLER_INVALID); } } // namespace @@ -67,21 +85,6 @@ void SecCompKitTest::TearDown() SC_LOG_INFO(LABEL, "TearDown."); } -namespace { -static bool TestInCallerNotCheckList() -{ - int32_t scId = -1; - struct SecCompClickEvent touch; - if ((SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId) != SC_SERVICE_ERROR_CALLER_INVALID) || - (SecCompKit::UpdateSecurityComponent(scId, "") != SC_SERVICE_ERROR_CALLER_INVALID) || - (SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch) != SC_SERVICE_ERROR_CALLER_INVALID) || - (SecCompKit::UnregisterSecurityComponent(scId) != SC_SERVICE_ERROR_CALLER_INVALID)) { - return false; - } - return true; -} -} - /** * @tc.name: ExceptCall001 * @tc.desc: do kit except call. @@ -127,9 +130,9 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) /** * @tc.name: ExceptCall001 - * @tc.desc: do kit except call. + * @tc.desc: test caller check. * @tc.type: FUNC - * @tc.require: AR000HO9IN + * @tc.require: AR000HO9JS */ HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1) { @@ -140,5 +143,36 @@ HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1) TestInCallerCheckList(); TestInCallerNotCheckList(); + // prohibit init caller list repeately + std::vector callerList1 = { + reinterpret_cast(TestInCallerNotCheckList), + }; + SecCompUiRegister registerCallback1(callerList1); + TestInCallerNotCheckList(); SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); + SecCompCallerAuthorization::GetInstance().isInit_ = false; } + +/** + * @tc.name: ExceptCall002 + * @tc.desc: test invalid caller register. + * @tc.type: FUNC + * @tc.require: AR000HO9JS + */ +HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) +{ + std::vector callerList; + SecCompUiRegister registerCallback(callerList); + TestInCallerNotCheckList(); + SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); + SecCompCallerAuthorization::GetInstance().isInit_ = false; + + for (size_t i = 0; i < MAX_CALLER_SIZE + 1; i++) { + callerList.emplace_back(reinterpret_cast(TestInCallerNotCheckList)); + } + SecCompUiRegister registerCallback2(callerList); + TestInCallerNotCheckList(); + SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); + SecCompCallerAuthorization::GetInstance().isInit_ = false; +} + -- Gitee From da151a07f63b03430c8ba9b2cf07c1086e81b03f Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 12 Jun 2023 13:10:59 +0800 Subject: [PATCH 22/59] Description: LOCATION_COMPONENT Match-id-deba044f185d647f3564dd027908b53dde175d95 --- .../sa/sa_main/sec_comp_info_helper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index d931534..089a928 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -201,7 +201,7 @@ int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID token SecCompType type = componentInfo->type_; switch (type) { case LOCATION_COMPONENT: - SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, "ohos.permission.LOCATION", + return SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); case PASTE_COMPONENT: SC_LOG_DEBUG(LABEL, "revoke paste permission"); @@ -224,7 +224,7 @@ int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenI SecCompType type = componentInfo->type_; switch (type) { case LOCATION_COMPONENT: - SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", + return SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); case PASTE_COMPONENT: SC_LOG_DEBUG(LABEL, "grant paste permission"); -- Gitee From 94ec5d0f0f73e18d1bbb9b1f0c8c6fc9b238d99d Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 16 Jun 2023 11:36:07 +0800 Subject: [PATCH 23/59] Description: grant APPROXIMATELY_LOCATION permission Match-id-31f2fceace14ff49043cff7254e809576fc53571 --- .../sa/sa_main/sec_comp_info_helper.cpp | 29 ++++++++++++++++--- .../sa/sa_main/sec_comp_perm_manager.cpp | 8 +++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 089a928..049f349 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -201,8 +201,16 @@ int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID token SecCompType type = componentInfo->type_; switch (type) { case LOCATION_COMPONENT: - return SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, "ohos.permission.LOCATION", - AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + { + int32_t locationRes = SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, + "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + int32_t approxLocationRes = SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, + "ohos.permission.APPROXIMATELY_LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + if ((locationRes != SC_OK) || (approxLocationRes != SC_OK)) { + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + return SC_OK; + } case PASTE_COMPONENT: SC_LOG_DEBUG(LABEL, "revoke paste permission"); return SC_OK; @@ -224,8 +232,21 @@ int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenI SecCompType type = componentInfo->type_; switch (type) { case LOCATION_COMPONENT: - return SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", - AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + { + int32_t res = SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, + "ohos.permission.APPROXIMATELY_LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + if (res != SC_OK) { + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + res = SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + if (res != SC_OK) { + SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, + "ohos.permission.APPROXIMATELY_LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + return SC_OK; + } case PASTE_COMPONENT: SC_LOG_DEBUG(LABEL, "grant paste permission"); return SC_OK; diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index 57c6a23..a48fd9f 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -69,15 +69,19 @@ bool SecCompPermManager::RevokeSavePermissionTask(const std::string& taskName) int32_t SecCompPermManager::GrantLocationPermission(AccessToken::AccessTokenID tokenId, const std::string& permissionName, int flag) { - return AccessToken::AccessTokenKit::GrantPermission(tokenId, "ohos.permission.LOCATION", + int32_t res = AccessToken::AccessTokenKit::GrantPermission(tokenId, permissionName, AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + SC_LOG_INFO(LABEL, "grant permission: %{public}s, tokenId:%{public}d", permissionName.c_str(), tokenId); + return res; } int32_t SecCompPermManager::RevokeLocationPermission(AccessToken::AccessTokenID tokenId, const std::string& permissionName, int flag) { - return AccessToken::AccessTokenKit::RevokePermission(tokenId, "ohos.permission.LOCATION", + int32_t res = AccessToken::AccessTokenKit::RevokePermission(tokenId, permissionName, AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + SC_LOG_INFO(LABEL, "revoke permission: %{public}s, tokenId:%{public}d", permissionName.c_str(), tokenId); + return res; } int32_t SecCompPermManager::GrantTempSavePermission(AccessToken::AccessTokenID tokenId) -- Gitee From 1c285ee7ddfd767f10895d8b2ab26d8171f9a2bc Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 19 Jun 2023 11:53:00 +0800 Subject: [PATCH 24/59] =?UTF-8?q?Description:=20DFX=20=E9=83=A8=E4=BB=B6?= =?UTF-8?q?=E5=8C=96=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-2c3c0cffc0ae1b919864b7866d6e92fc6fca56b4 --- bundle.json | 2 +- frameworks/BUILD.gn | 4 ++-- interfaces/inner_api/enhance_kits/BUILD.gn | 2 +- interfaces/inner_api/enhance_kits/test/BUILD.gn | 2 +- interfaces/inner_api/security_component/BUILD.gn | 2 +- interfaces/inner_api/security_component/test/BUILD.gn | 2 +- services/security_component_service/sa/BUILD.gn | 2 +- services/security_component_service/sa/test/BUILD.gn | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bundle.json b/bundle.json index 5535487..54a8bd8 100644 --- a/bundle.json +++ b/bundle.json @@ -27,7 +27,7 @@ "access_token", "c_utils", "eventhandler", - "hiviewdfx_hilog_native", + "hilog", "graphic_standard", "ipc", "safwk", diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 5d423aa..3b19b45 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -48,7 +48,7 @@ ohos_shared_library("libsecurity_component_framework") { external_deps = [ "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -78,7 +78,7 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { external_deps = [ "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index 03f7fad..7ed3906 100644 --- a/interfaces/inner_api/enhance_kits/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -50,7 +50,7 @@ ohos_shared_library("libsecurity_component_enhance_sdk") { external_deps = [ "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn index 778e37a..6bf644b 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -34,7 +34,7 @@ ohos_unittest("sec_comp_enhance_sdk_test") { external_deps = [ "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", ] if (current_cpu == "arm64") { defines = [ "_ARM64_" ] diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index a441175..774ae8b 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -54,7 +54,7 @@ ohos_shared_library("libsecurity_component_sdk") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", "ipc:ipc_core", "samgr:samgr_proxy", ] diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index deb25c8..28a2fc9 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -43,7 +43,7 @@ ohos_unittest("sec_comp_sdk_test") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", "ipc:ipc_core", ] } diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 10b6b42..613f6d6 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -74,7 +74,7 @@ ohos_shared_library("security_component_service") { "c_utils:utils", "eventhandler:libeventhandler", "graphic_standard:librender_service_client", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 8d402e5..93fdabe 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -64,7 +64,7 @@ ohos_unittest("sec_comp_service_test") { "c_utils:utils", "eventhandler:libeventhandler", "graphic_standard:librender_service_client", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", "ipc:ipc_core", "window_manager:libdm", ] -- Gitee From b517eb8744545e45e3ac941f2a82360b59c60e2a Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 20 Jun 2023 11:30:43 +0800 Subject: [PATCH 25/59] =?UTF-8?q?Description:=20=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E6=8E=A7=E4=BB=B6HMAC=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-cb3b278b79cb446dfe960ffb90a3de08a9c598f4 --- .../sa/sa_main/sec_comp_info_helper.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 049f349..eca822b 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -241,8 +241,9 @@ int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenI res = SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); if (res != SC_OK) { - SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, - "ohos.permission.APPROXIMATELY_LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + SecCompPermManager::GetInstance().RevokeLocationPermission( + tokenId, "ohos.permission.APPROXIMATELY_LOCATION", + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } return SC_OK; -- Gitee From 31f889563cfba7cf6e1e19ecb1979a07c2a706b8 Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 20 Jun 2023 20:06:41 +0800 Subject: [PATCH 26/59] Description:add check window rect Match-id-0cfd9863babe7b83bee645900c5b1f80ae31c309 --- .../security_component/src/sec_comp_base.cpp | 24 +++-- .../include/sec_comp_base.h | 3 +- .../unittest/src/location_button_test.cpp | 11 ++- .../test/unittest/src/paste_button_test.cpp | 9 +- .../test/unittest/src/save_button_test.cpp | 9 +- .../sa/sa_main/sec_comp_info_helper.cpp | 15 ++- .../src/sec_comp_info_helper_test.cpp | 97 ++++++++++++++++++- .../unittest/src/sec_comp_manager_test.cpp | 8 ++ .../unittest/src/sec_comp_service_test.cpp | 8 ++ 9 files changed, 169 insertions(+), 15 deletions(-) diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index b1ac4d5..d66cee0 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -28,6 +28,7 @@ static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; static const std::string JSON_RECT_WIDTH = "width"; static const std::string JSON_RECT_HEIGHT = "height"; +static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SIZE_TAG = "size"; static const std::string JSON_FONT_SIZE_TAG = "fontSize"; @@ -179,7 +180,7 @@ bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag return ParseBool(jsonParent, JSON_PARENT_EFFECT_TAG, parentEffect_); } -bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); @@ -187,19 +188,19 @@ bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag) } auto jsonSize = json.at(tag); - if (!ParseDimension(jsonSize, JSON_RECT_X, rect_.x_)) { + if (!ParseDimension(jsonSize, JSON_RECT_X, rect.x_)) { return false; } - if (!ParseDimension(jsonSize, JSON_RECT_Y, rect_.y_)) { + if (!ParseDimension(jsonSize, JSON_RECT_Y, rect.y_)) { return false; } - if (!ParseDimension(jsonSize, JSON_RECT_WIDTH, rect_.width_)) { + if (!ParseDimension(jsonSize, JSON_RECT_WIDTH, rect.width_)) { return false; } - if (!ParseDimension(jsonSize, JSON_RECT_HEIGHT, rect_.height_)) { + if (!ParseDimension(jsonSize, JSON_RECT_HEIGHT, rect.height_)) { return false; } @@ -220,7 +221,10 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) } type_ = static_cast(value); - if (!ParseRect(jsonSrc, JSON_RECT)) { + if (!ParseRect(jsonSrc, JSON_RECT, rect_)) { + return false; + } + if (!ParseRect(jsonSrc, JSON_WINDOW_RECT, windowRect_)) { return false; } if (!ParseSize(jsonSrc, JSON_SIZE_TAG)) { @@ -248,12 +252,18 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) void SecCompBase::ToJson(nlohmann::json& jsonRes) const { jsonRes[JSON_SC_TYPE] = type_; - jsonRes[JSON_RECT] = nlohmann::json { + jsonRes[JSON_RECT] = nlohmann::json { {JSON_RECT_X, rect_.x_}, {JSON_RECT_Y, rect_.y_}, {JSON_RECT_WIDTH, rect_.width_}, {JSON_RECT_HEIGHT, rect_.height_} }; + jsonRes[JSON_WINDOW_RECT] = nlohmann::json { + {JSON_RECT_X, windowRect_.x_}, + {JSON_RECT_Y, windowRect_.y_}, + {JSON_RECT_WIDTH, windowRect_.width_}, + {JSON_RECT_HEIGHT, windowRect_.height_} + }; nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, padding_.top }, { JSON_PADDING_RIGHT_TAG, padding_.right }, diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index 17c558e..92c743f 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_base.h +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -67,6 +67,7 @@ public: SecCompType type_ = UNKNOWN_SC_TYPE; SecCompRect rect_; + SecCompRect windowRect_; bool isValid_ = false; int32_t text_ = UNKNOWN_TYPE; @@ -83,7 +84,7 @@ private: bool ParseBorders(const nlohmann::json& json, const std::string& tag); bool ParseSize(const nlohmann::json& json, const std::string& tag); bool ParseParent(const nlohmann::json& json, const std::string& tag); - bool ParseRect(const nlohmann::json& json, const std::string& tag); + bool ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect); bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; } // namespace SecurityComponent diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 227420f..52d7b26 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -31,6 +31,7 @@ static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; static const std::string JSON_RECT = "rect"; +static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; @@ -74,7 +75,13 @@ static constexpr int32_t UNKNOWN_ICON = -2; static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JSON_RECT] = nlohmann::json { + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE }, {JSON_RECT_Y, TEST_COORDINATE }, {JSON_RECT_WIDTH, TEST_COORDINATE }, @@ -634,4 +641,4 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) comp1.iconColor_.value = TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); -} \ No newline at end of file +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index eb54474..fc293a5 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -31,6 +31,7 @@ static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; static const std::string JSON_RECT = "rect"; +static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; @@ -73,7 +74,13 @@ static constexpr int32_t UNKNOWN_ICON = -2; static void BuildPasteComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JSON_SC_TYPE] = PASTE_COMPONENT; - jsonComponent[JSON_RECT] = nlohmann::json { + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE }, {JSON_RECT_Y, TEST_COORDINATE }, {JSON_RECT_WIDTH, TEST_COORDINATE }, diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index 541e85f..403e3f6 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -31,6 +31,7 @@ static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; static const std::string JSON_RECT = "rect"; +static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; @@ -73,7 +74,13 @@ static constexpr int32_t UNKNOWN_ICON = -2; static void BuildSaveComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JSON_SC_TYPE] = SAVE_COMPONENT; - jsonComponent[JSON_RECT] = nlohmann::json { + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE }, {JSON_RECT_Y, TEST_COORDINATE }, {JSON_RECT_WIDTH, TEST_COORDINATE }, diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index eca822b..d234fa2 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -84,7 +84,7 @@ static bool GetScreenSize(double& width, double& height) return true; } -static bool CheckRectValid(const SecCompRect& rect) +static bool CheckRectValid(const SecCompRect& rect, const SecCompRect& windowRect) { double curScreenWidth = 0.0F; double curScreenHeight = 0.0F; @@ -93,6 +93,11 @@ static bool CheckRectValid(const SecCompRect& rect) return false; } + if (GreatOrEqual(0.0, rect.width_) || GreatOrEqual(0.0, rect.height_)) { + SC_LOG_ERROR(LABEL, "width or height is <= 0"); + return false; + } + if (GreatNotEqual(ZERO_OFFSET, rect.x_) || GreatNotEqual(ZERO_OFFSET, rect.y_) || GreatNotEqual(rect.x_, curScreenWidth) || GreatNotEqual(rect.y_, curScreenHeight)) { SC_LOG_ERROR(LABEL, "start point is out of screen"); @@ -105,6 +110,12 @@ static bool CheckRectValid(const SecCompRect& rect) return false; } + if (GreatNotEqual(windowRect.x_, rect.x_) || GreatNotEqual(windowRect.y_, rect.y_) || + GreatNotEqual(rect.width_, windowRect.width_) || GreatNotEqual(rect.height_, windowRect.height_)) { + SC_LOG_ERROR(LABEL, "rect is out of window"); + return false; + } + // check rect > 10% if (GreatOrEqual((rect.width_ * rect.height_), (curScreenWidth * curScreenHeight * MAX_RECT_PERCENT))) { SC_LOG_ERROR(LABEL, "rect area is too large"); @@ -147,7 +158,7 @@ static bool CheckSecCompBaseButton(const SecCompBase* comp) static bool CheckSecCompBase(const SecCompBase* comp) { - if (!CheckRectValid(comp->rect_)) { + if (!CheckRectValid(comp->rect_, comp->windowRect_)) { SC_LOG_INFO(LABEL, "check component rect failed."); return false; } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 3dc44f3..12d1bcd 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -36,6 +36,7 @@ static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; static const std::string JSON_RECT = "rect"; +static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; @@ -107,6 +108,12 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) {JSON_RECT_WIDTH, g_testWidth }, {JSON_RECT_HEIGHT, g_testHeight } }; + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { + { JSON_RECT_X, g_testWidth }, + { JSON_RECT_Y, g_testHeight }, + { JSON_RECT_WIDTH, g_testWidth }, + { JSON_RECT_HEIGHT, g_testHeight } + }; nlohmann::json jsonPadding = nlohmann::json { { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, @@ -710,6 +717,94 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) ASSERT_FALSE(comp->GetValid()); } +/** + * @tc.name: ParseComponent015 + * @tc.desc: Test parse component info with invalid rect + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + jsonComponent[JSON_RECT] = nlohmann::json { + { JSON_RECT_X, g_testWidth }, + { JSON_RECT_Y, g_testHeight }, + { JSON_RECT_WIDTH, TEST_INVALID_DIMENSION }, + { JSON_RECT_HEIGHT, g_testHeight } + }; + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_NE(comp, nullptr); + EXPECT_FALSE(comp->GetValid()); + delete comp; + + jsonComponent[JSON_RECT] = nlohmann::json { + { JSON_RECT_X, g_testWidth }, + { JSON_RECT_Y, g_testHeight }, + { JSON_RECT_WIDTH, g_testWidth }, + { JSON_RECT_HEIGHT, TEST_INVALID_DIMENSION } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_NE(comp, nullptr); + EXPECT_FALSE(comp->GetValid()); + delete comp; +} + +/** + * @tc.name: ParseComponent016 + * @tc.desc: Test parse component info with windowRect invalid + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent016, TestSize.Level1) +{ + nlohmann::json jsonComponent; + BuildLocationComponentInfo(jsonComponent); + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { + { JSON_RECT_X, g_testWidth }, + { JSON_RECT_Y, g_testHeight + 1 }, + { JSON_RECT_WIDTH, g_testWidth }, + { JSON_RECT_HEIGHT, g_testHeight } + }; + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_NE(comp, nullptr); + EXPECT_FALSE(comp->GetValid()); + delete comp; + + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { + { JSON_RECT_X, g_testWidth + 1 }, + { JSON_RECT_Y, g_testHeight }, + { JSON_RECT_WIDTH, g_testWidth }, + { JSON_RECT_HEIGHT, g_testHeight } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_NE(comp, nullptr); + EXPECT_FALSE(comp->GetValid()); + delete comp; + + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { + { JSON_RECT_X, g_testWidth }, + { JSON_RECT_Y, g_testHeight }, + { JSON_RECT_WIDTH, g_testWidth - 1 }, + { JSON_RECT_HEIGHT, g_testHeight } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_NE(comp, nullptr); + EXPECT_FALSE(comp->GetValid()); + delete comp; + + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { + { JSON_RECT_X, g_testWidth }, + { JSON_RECT_Y, g_testHeight }, + { JSON_RECT_WIDTH, g_testWidth }, + { JSON_RECT_HEIGHT, g_testHeight - 1 } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_NE(comp, nullptr); + EXPECT_FALSE(comp->GetValid()); + delete comp; +} + /** * @tc.name: CheckComponentValid001 * @tc.desc: Test CheckComponentValid with invalid color @@ -768,4 +863,4 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) comp->type_ = SecCompType::MAX_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); -} \ No newline at end of file +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index a145957..974a900 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -70,6 +70,10 @@ static LocationButton BuildInvalidLocationComponent() button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; + button.windowRect_.x_ = TEST_COORDINATE; + button.windowRect_.y_ = TEST_COORDINATE; + button.windowRect_.width_ = TEST_COORDINATE; + button.windowRect_.height_ = TEST_COORDINATE; button.text_ = UNKNOWN_TEXT; button.icon_ = UNKNOWN_ICON; button.bg_ = SecCompBackground::UNKNOWN_BG; @@ -95,6 +99,10 @@ static LocationButton BuildValidLocationComponent() button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; + button.windowRect_.x_ = TEST_COORDINATE; + button.windowRect_.y_ = TEST_COORDINATE; + button.windowRect_.width_ = TEST_COORDINATE; + button.windowRect_.height_ = TEST_COORDINATE; button.text_ = static_cast(LocationDesc::SELECT_LOCATION); button.icon_ = static_cast(LocationIcon::LINE_ICON); button.bg_ = SecCompBackground::CIRCLE; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 87f0cbd..6c15a7b 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -64,6 +64,10 @@ static std::string BuildLocationComponentInfo() button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; + button.windowRect_.x_ = TEST_COORDINATE; + button.windowRect_.y_ = TEST_COORDINATE; + button.windowRect_.width_ = TEST_COORDINATE; + button.windowRect_.height_ = TEST_COORDINATE; button.text_ = static_cast(LocationDesc::SELECT_LOCATION); button.icon_ = static_cast(LocationIcon::LINE_ICON); button.bg_ = SecCompBackground::CIRCLE; @@ -92,6 +96,10 @@ static std::string BuildSaveComponentInfo() button.rect_.y_ = TEST_COORDINATE; button.rect_.width_ = TEST_COORDINATE; button.rect_.height_ = TEST_COORDINATE; + button.windowRect_.x_ = TEST_COORDINATE; + button.windowRect_.y_ = TEST_COORDINATE; + button.windowRect_.width_ = TEST_COORDINATE; + button.windowRect_.height_ = TEST_COORDINATE; button.text_ = static_cast(SaveDesc::DOWNLOAD); button.icon_ = static_cast(SaveIcon::LINE_ICON); button.bg_ = SecCompBackground::CIRCLE; -- Gitee From 2c3107018c5c1c3fccb57beee1eba7a2fcafa156 Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 30 Jun 2023 00:05:49 +0800 Subject: [PATCH 27/59] =?UTF-8?q?Description:=20=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E6=B8=85=E9=9B=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-f2fa8f3929a795f7c6462b03daa0a335789ea9dd --- frameworks/common/src/sec_comp_tool.cpp | 2 +- .../sa/sa_main/sec_comp_perm_manager.cpp | 4 ---- .../security_component_service/sa/sa_main/sec_comp_stub.cpp | 2 +- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/frameworks/common/src/sec_comp_tool.cpp b/frameworks/common/src/sec_comp_tool.cpp index 19639c2..79a0f1d 100644 --- a/frameworks/common/src/sec_comp_tool.cpp +++ b/frameworks/common/src/sec_comp_tool.cpp @@ -99,7 +99,7 @@ static inline double GetHsvDisZ(const HsvColor& hsv, double h) return h * (1 - hsv.v); } -static double HsvDistance(HsvColor& hsv1, HsvColor& hsv2) +static double HsvDistance(const HsvColor& hsv1, const HsvColor& hsv2) { double rDef = DEFAULT_R; double angle = THIRTY_ANGLE; diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index a48fd9f..e42f730 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -129,10 +129,6 @@ bool SecCompPermManager::InitEventHandler() } secHandler_ = std::make_shared(secRunner_); - if (secHandler_ == nullptr) { - SC_LOG_ERROR(LABEL, "failed to create secHandler_."); - return false; - } return true; } diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index a11b575..101377f 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -169,7 +169,7 @@ int32_t SecCompStub::ReduceAfterVerifySavePermissionInner(MessageParcel& data, M return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; } - if (tokenId <= 0) { + if (tokenId == 0) { SC_LOG_ERROR(LABEL, "AccessTokenId invalid"); return SC_SERVICE_ERROR_VALUE_INVALID; } -- Gitee From fe57c7646822025a41e1f38143c420bc48b16eac Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 3 Jul 2023 11:14:05 +0800 Subject: [PATCH 28/59] Description:modify interfaceCode Match-id-5bc0867999b42fa43534117d91ac48f9d6dbb9f1 --- .../include/i_sec_comp_service.h | 10 +----- ...ity_component_service_ipc_interface_code.h | 35 +++++++++++++++++++ .../security_component/src/sec_comp_proxy.cpp | 11 +++--- .../sa/sa_main/sec_comp_stub.cpp | 11 +++--- .../test/unittest/src/sec_comp_stub_test.cpp | 6 ++-- 5 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h diff --git a/interfaces/inner_api/security_component/include/i_sec_comp_service.h b/interfaces/inner_api/security_component/include/i_sec_comp_service.h index 4fb43fc..377a475 100644 --- a/interfaces/inner_api/security_component/include/i_sec_comp_service.h +++ b/interfaces/inner_api/security_component/include/i_sec_comp_service.h @@ -19,6 +19,7 @@ #include "access_token.h" #include "iremote_broker.h" #include "sec_comp_info.h" +#include "security_component_service_ipc_interface_code.h" namespace OHOS { namespace Security { @@ -36,15 +37,6 @@ public: virtual int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) = 0; virtual bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) = 0; - - enum class InterfaceCode { - REGISTER_SECURITY_COMPONENT = 0xff01, - UPDATE_SECURITY_COMPONENT = 0xff02, - UNREGISTER_SECURITY_COMPONENT = 0xff03, - REPORT_SECURITY_COMPONENT_CLICK_EVENT = 0xff04, - GET_SECURITY_COMPONENT_ENHANCE_OBJECT = 0xff05, - VERIFY_TEMP_SAVE_PERMISSION = 0xff06, - }; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h new file mode 100644 index 0000000..ae8d92c --- /dev/null +++ b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_IPC_INTERFACE_CODE_H +#define SECURITY_COMPONENT_IPC_INTERFACE_CODE_H + +/* SAID: 3506 */ +namespace OHOS { +namespace Security { +namespace SecurityComponent { +enum SecurityComponentServiceInterfaceCode { + REGISTER_SECURITY_COMPONENT = 0, + UPDATE_SECURITY_COMPONENT, + UNREGISTER_SECURITY_COMPONENT, + REPORT_SECURITY_COMPONENT_CLICK_EVENT, + GET_SECURITY_COMPONENT_ENHANCE_OBJECT, + VERIFY_TEMP_SAVE_PERMISSION, +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_IPC_INTERFACE_CODE_H + diff --git a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp index 0c8d229..1737fbb 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp @@ -58,7 +58,7 @@ int32_t SecCompProxy::RegisterSecurityComponent(SecCompType type, return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; } int32_t requestResult = remote->SendRequest( - static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option); + static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option); if (requestResult != SC_OK) { SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); return requestResult; @@ -101,7 +101,7 @@ int32_t SecCompProxy::UpdateSecurityComponent(int32_t scId, const std::string& c return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; } int32_t requestResult = remote->SendRequest( - static_cast(ISecCompService::InterfaceCode::UPDATE_SECURITY_COMPONENT), data, reply, option); + static_cast(SecurityComponentServiceInterfaceCode::UPDATE_SECURITY_COMPONENT), data, reply, option); if (requestResult != SC_OK) { SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); return requestResult; @@ -135,7 +135,8 @@ int32_t SecCompProxy::UnregisterSecurityComponent(int32_t scId) return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; } int32_t requestResult = remote->SendRequest( - static_cast(ISecCompService::InterfaceCode::UNREGISTER_SECURITY_COMPONENT), data, reply, option); + static_cast(SecurityComponentServiceInterfaceCode::UNREGISTER_SECURITY_COMPONENT), + data, reply, option); if (requestResult != SC_OK) { SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); return requestResult; @@ -186,7 +187,7 @@ int32_t SecCompProxy::ReportSecurityComponentClickEvent(int32_t scId, return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; } int32_t requestResult = remote->SendRequest( - static_cast(ISecCompService::InterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT), + static_cast(SecurityComponentServiceInterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT), data, reply, option); if (requestResult != SC_OK) { SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); @@ -221,7 +222,7 @@ bool SecCompProxy::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID to return false; } int32_t requestResult = remote->SendRequest( - static_cast(ISecCompService::InterfaceCode::VERIFY_TEMP_SAVE_PERMISSION), + static_cast(SecurityComponentServiceInterfaceCode::VERIFY_TEMP_SAVE_PERMISSION), data, reply, option); if (requestResult != SC_OK) { SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index 101377f..688118e 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -184,15 +184,16 @@ int32_t SecCompStub::ReduceAfterVerifySavePermissionInner(MessageParcel& data, M SecCompStub::SecCompStub() { - requestFuncMap_[static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT)] = + requestFuncMap_[static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT)] = &SecCompStub::RegisterSecurityComponentInner; - requestFuncMap_[static_cast(ISecCompService::InterfaceCode::UPDATE_SECURITY_COMPONENT)] = + requestFuncMap_[static_cast(SecurityComponentServiceInterfaceCode::UPDATE_SECURITY_COMPONENT)] = &SecCompStub::UpdateSecurityComponentInner; - requestFuncMap_[static_cast(ISecCompService::InterfaceCode::UNREGISTER_SECURITY_COMPONENT)] = + requestFuncMap_[static_cast(SecurityComponentServiceInterfaceCode::UNREGISTER_SECURITY_COMPONENT)] = &SecCompStub::UnregisterSecurityComponentInner; - requestFuncMap_[static_cast(ISecCompService::InterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT)] = + requestFuncMap_[static_cast( + SecurityComponentServiceInterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT)] = &SecCompStub::ReportSecurityComponentClickEventInner; - requestFuncMap_[static_cast(ISecCompService::InterfaceCode::VERIFY_TEMP_SAVE_PERMISSION)] = + requestFuncMap_[static_cast(SecurityComponentServiceInterfaceCode::VERIFY_TEMP_SAVE_PERMISSION)] = &SecCompStub::ReduceAfterVerifySavePermissionInner; } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp index 9f0ff9e..97522c7 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp @@ -62,11 +62,13 @@ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) MessageOption option; data.WriteInterfaceToken(u"wrong"); - ASSERT_EQ(stub_->OnRemoteRequest(static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT), + ASSERT_EQ(stub_->OnRemoteRequest( + static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option), SC_SERVICE_ERROR_IPC_REQUEST_FAIL); data.WriteInterfaceToken(u"ohos.security.ISecCompService"); - ASSERT_EQ(stub_->OnRemoteRequest(static_cast(ISecCompService::InterfaceCode::REGISTER_SECURITY_COMPONENT), + ASSERT_EQ(stub_->OnRemoteRequest( + static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); data.WriteInterfaceToken(u"ohos.security.ISecCompService"); -- Gitee From 9bbfd72cc312fb012c00d6fcdc699720000d739e Mon Sep 17 00:00:00 2001 From: xxx Date: Sat, 1 Jul 2023 16:18:25 +0800 Subject: [PATCH 29/59] =?UTF-8?q?Description:=20HMAC=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-508648b84058aaf416b8b35d9c3bc929e8b8be3f --- bundle.json | 2 + frameworks/BUILD.gn | 2 + .../include/sec_comp_enhance_adapter.h | 15 +- .../src/sec_comp_enhance_adapter.cpp | 36 +- hisysevent.yaml | 94 +++++ interfaces/inner_api/enhance_kits/BUILD.gn | 1 - .../include/sec_comp_enhance_kit.h | 7 +- .../enhance_kits/src/sec_comp_enhance_kit.cpp | 7 +- .../unittest/src/sec_comp_enhance_test.cpp | 60 +-- .../test/unittest/src/sec_comp_enhance_test.h | 1 - .../inner_api/security_component/BUILD.gn | 1 + .../include/sec_comp_client.h | 2 +- .../include/sec_comp_info.h | 6 + .../src/sec_comp_client.cpp | 16 +- .../security_component/src/sec_comp_kit.cpp | 20 + .../security_component/test/BUILD.gn | 1 + .../security_component_service/sa/BUILD.gn | 1 + .../sa/sa_main/sec_comp_entity.cpp | 9 +- .../sa/sa_main/sec_comp_manager.cpp | 33 +- .../sa/sa_main/sec_comp_service.cpp | 10 +- .../sa/test/BUILD.gn | 55 ++- .../mock/include/sec_comp_enhance_adapter.h | 25 +- .../mock/src/sec_comp_enhance_adapter.cpp | 42 +- .../unittest/src/sec_comp_entity_test.cpp | 7 +- .../src/sec_comp_service_mock_test.cpp | 393 ++++++++++++++++++ .../unittest/src/sec_comp_service_test.cpp | 257 +----------- 26 files changed, 699 insertions(+), 404 deletions(-) create mode 100644 hisysevent.yaml rename interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h => services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h (58%) rename interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp => services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp (48%) create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp diff --git a/bundle.json b/bundle.json index 54a8bd8..33452d4 100644 --- a/bundle.json +++ b/bundle.json @@ -14,6 +14,7 @@ "subsystem": "security", "syscap": [], "hisysevent_config": [ + "//base/security/security_component/hisysevent.yaml" ], "features": [ "security_component_feature_coverage = false" ], "adapted_system_type": [ @@ -28,6 +29,7 @@ "c_utils", "eventhandler", "hilog", + "hisysevent", "graphic_standard", "ipc", "safwk", diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 3b19b45..a8b9f42 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -58,6 +58,7 @@ config("libsecurity_component_enhance_adapter_config") { include_dirs = [ "common/include", "enhance_adapter/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", ] } @@ -69,6 +70,7 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { include_dirs = [ "common/include", "enhance_adapter/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", ] sources = [ "enhance_adapter/src/sec_comp_enhance_adapter.cpp" ] diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index c3defd6..00b5eac 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -16,6 +16,7 @@ #define SECURITY_COMPONENT_ENHANCE_ADAPTER_H #include #include "parcel.h" +#include "sec_comp_info.h" namespace OHOS { namespace Security { @@ -30,10 +31,8 @@ class SecCompEnhanceCfgBase { class SecCompEnhanceInputInterface { public: - virtual bool MarshallEnhanceCfg(SecCompEnhanceCfgBase* cfg, Parcel& out) = 0; - virtual SecCompEnhanceCfgBase* UnmarshallEnhanceCfg(Parcel& in) = 0; - virtual int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) = 0; - virtual int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, + virtual int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) = 0; + virtual int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen) = 0; }; @@ -41,15 +40,15 @@ class SecCompEnhanceServiceInterface { public: virtual int32_t EnableInputEnhance() = 0; virtual int32_t DisableInputEnhance() = 0; + virtual int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo) = 0; }; struct SecCompEnhanceAdapter { static void InitEnhanceHandler(EnhanceInterfaceType type); - static int32_t MarshallEnhanceCfg(SecCompEnhanceCfgBase* cfg, Parcel& out); - static int32_t UnmarshallEnhanceCfg(Parcel& in, SecCompEnhanceCfgBase*& result); - static int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); - static int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, + static int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen); + static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen); + static int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo); static int32_t EnableInputEnhance(); static int32_t DisableInputEnhance(); diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index 57fcdbf..84df5f9 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -64,52 +64,36 @@ void SecCompEnhanceAdapter::InitEnhanceHandler(EnhanceInterfaceType type) } } -int32_t SecCompEnhanceAdapter::MarshallEnhanceCfg(SecCompEnhanceCfgBase* cfg, Parcel& out) +int32_t SecCompEnhanceAdapter::SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) { if (!isEnhanceInputHandlerInit) { InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); } if (inputHandler != nullptr) { - if (!inputHandler->MarshallEnhanceCfg(cfg, out)) { - return SC_ENHANCE_ERROR_OPER_FAIL; - } else { - return SC_OK; - } + return inputHandler->SetEnhanceCfg(cfg, cfgLen); } return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; } -int32_t SecCompEnhanceAdapter::UnmarshallEnhanceCfg(Parcel& in, SecCompEnhanceCfgBase*& result) -{ - if (!isEnhanceInputHandlerInit) { - InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); - } - if (inputHandler != nullptr) { - result = inputHandler->UnmarshallEnhanceCfg(in); - return SC_OK; - } - return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; -} - -int32_t SecCompEnhanceAdapter::SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) +int32_t SecCompEnhanceAdapter::GetPointerEventEnhanceData(void* data, uint32_t dataLen, + uint8_t* enhanceData, uint32_t& enHancedataLen) { if (!isEnhanceInputHandlerInit) { InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); } if (inputHandler != nullptr) { - return inputHandler->SetEnhanceCfg(cfg); + return inputHandler->GetPointerEventEnhanceData(data, dataLen, enhanceData, enHancedataLen); } return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; } -int32_t SecCompEnhanceAdapter::GetPointerEventEnhanceData(void *data, uint32_t dataLen, - uint8_t* enhanceData, uint32_t& enHancedataLen) +int32_t SecCompEnhanceAdapter::CheckExtraInfo(const SecCompClickEvent& touchInfo) { - if (!isEnhanceInputHandlerInit) { - InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE); + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); } - if (inputHandler != nullptr) { - return inputHandler->GetPointerEventEnhanceData(data, dataLen, enhanceData, enHancedataLen); + if (srvHandler != nullptr) { + return srvHandler->CheckExtraInfo(touchInfo); } return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; } diff --git a/hisysevent.yaml b/hisysevent.yaml new file mode 100644 index 0000000..51ab79c --- /dev/null +++ b/hisysevent.yaml @@ -0,0 +1,94 @@ +# Copyright (c) 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. + +domain: SEC_COMPONENT + +CALLBACK_FAILED: + __BASE: {type: SECURITY, level: CRITICAL, desc: Failed to execute the callback} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + CALL_SCENE: {type: STRING, desc: call scene} + REASON: {type: STRING, desc: callback execution failure cause} + SC_ID: {type: INT32, desc: security component id} + SC_TYPE: {type: STRING, desc: security component type} + +CALLER_CHECK_FAILED: + __BASE: {type: SECURITY, level: CRITICAL, desc: Illegal invocation} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + CALL_SCENE: {type: STRING, desc: call scene} + +CHALLENGE_CHECK_FAILED: + __BASE: {type: SECURITY, level: CRITICAL, desc: Challenge value verification failed} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + CALL_SCENE: {type: STRING, desc: call scene} + SC_ID: {type: INT32, desc: security component id} + SC_TYPE: {type: STRING, desc: security component type} + +CLICK_INFO_CHECK_FAILED: + __BASE: {type: SECURITY, level: CRITICAL, desc: Failed to verify the click event} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + SC_ID: {type: INT32, desc: security component id} + SC_TYPE: {type: STRING, desc: security component type} + +COMPONENT_INFO_CHECK_FAILED: + __BASE: {type: SECURITY, level: CRITICAL, desc: Failed to verify the component information} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + CALL_SCENE: {type: STRING, desc: call scene} + SC_ID: {type: INT32, desc: security component id} + SC_TYPE: {type: STRING, desc: security component type} + +INIT_FAILED: + __BASE: {type: FAULT, level: CRITICAL, desc: Failed to initialize the security component environment} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + REASON: {type: STRING, desc: callback execution failure cause} + +REGISTER_SUCCESS: + __BASE: {type: BEHAVIOR, level: MINOR, desc: The security component is registered successfully} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + SC_ID: {type: INT32, desc: security component id} + SC_TYPE: {type: STRING, desc: security component type} + +REPEAT_CALLBACK_REGISTER: + __BASE: {type: SECURITY, level: CRITICAL, desc: Repeated registration callback} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + +SERVICE_INIT_SUCCESS: + __BASE: {type: BEHAVIOR, level: MINOR, desc: Security component service starts successfully} + PID: {type: INT32, desc: pid of the service process} + +TEMP_GRANT_FAILED: + __BASE: {type: FAULT, level: CRITICAL, desc: Security component authorization failed} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + SC_ID: {type: INT32, desc: security component id} + SC_TYPE: {type: STRING, desc: security component type} + +TEMP_GRANT_SUCCESS: + __BASE: {type: BEHAVIOR, level: MINOR, desc: Security component authorization successfully} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + SC_ID: {type: INT32, desc: security component id} + SC_TYPE: {type: STRING, desc: security component type} + +UNREGISTER_SUCCESS: + __BASE: {type: BEHAVIOR, level: MINOR, desc: Security component unregister} + CALLER_PID: {type: INT32, desc: caller pid} + CALLER_UID: {type: INT32, desc: caller uid} + SC_ID: {type: INT32, desc: security component id} diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index 7ed3906..bab187a 100644 --- a/interfaces/inner_api/enhance_kits/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -38,7 +38,6 @@ ohos_shared_library("libsecurity_component_enhance_sdk") { ] sources = [ - "src/sec_comp_enhance_cfg_parcel.cpp", "src/sec_comp_enhance_kit.cpp", ] diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h index 5c7ee04..587b5a8 100644 --- a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h +++ b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h @@ -14,14 +14,15 @@ */ #ifndef SECURITY_COMPONENT_ENHANCE_KITS_H #define SECURITY_COMPONENT_ENHANCE_KITS_H -#include "sec_comp_enhance_adapter.h" + +#include namespace OHOS { namespace Security { namespace SecurityComponent { struct SecCompEnhanceKit { - static int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); - static int32_t GetPointerEventEnhanceData(void *data, uint32_t dataLen, + static int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen); + static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen); }; } // namespace SecurityComponent diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp index 2a88edb..6369c99 100644 --- a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp +++ b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp @@ -13,16 +13,17 @@ * limitations under the License. */ #include "sec_comp_enhance_kit.h" +#include "sec_comp_enhance_adapter.h" namespace OHOS { namespace Security { namespace SecurityComponent { -int32_t SecCompEnhanceKit::SetEnhanceCfg(SecCompEnhanceCfgBase* cfg) +int32_t SecCompEnhanceKit::SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) { - return SecCompEnhanceAdapter::SetEnhanceCfg(cfg); + return SecCompEnhanceAdapter::SetEnhanceCfg(cfg, cfgLen); } -int32_t SecCompEnhanceKit::GetPointerEventEnhanceData(void *data, uint32_t dataLen, +int32_t SecCompEnhanceKit::GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen) { return SecCompEnhanceAdapter::GetPointerEventEnhanceData(data, dataLen, enhanceData, enHancedataLen); diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index 0961c8f..cb3c2ab 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -25,6 +25,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceTest"}; static bool g_inputEnhanceExist = false; static bool g_srvEnhanceExist = false; +static constexpr uint32_t SEC_COMP_ENHANCE_CFG_SIZE = 76; #ifdef _ARM64_ static const std::string LIB_PATH = "/system/lib64/"; #else @@ -71,8 +72,8 @@ void SecCompEnhanceTest::TearDown() */ HWTEST_F(SecCompEnhanceTest, SetEnhanceCfg001, TestSize.Level1) { - uint8_t cfgData[16] = { 0 }; - int32_t result = SecCompEnhanceKit::SetEnhanceCfg(reinterpret_cast(&cfgData)); + uint8_t cfgData[SEC_COMP_ENHANCE_CFG_SIZE] = { 0 }; + int32_t result = SecCompEnhanceKit::SetEnhanceCfg(cfgData, SEC_COMP_ENHANCE_CFG_SIZE); if (g_inputEnhanceExist) { EXPECT_EQ(result, SC_OK); } else { @@ -95,60 +96,7 @@ HWTEST_F(SecCompEnhanceTest, GetPoniterEventEnhanceData001, TestSize.Level1) int32_t result = SecCompEnhanceKit::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen); if (g_inputEnhanceExist) { - EXPECT_EQ(result, SC_OK); - } else { - EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); - } -} - -/** - * @tc.name: MarshallEnhanceCfg001 - * @tc.desc: test MarshallEnhanceCfg - * @tc.type: FUNC - * @tc.require: AR000HO9IN - */ -HWTEST_F(SecCompEnhanceTest, MarshallEnhanceCfg001, TestSize.Level1) -{ - OHOS::Parcel out; - int32_t result = SecCompEnhanceAdapter::MarshallEnhanceCfg(nullptr, out); - if (g_inputEnhanceExist) { - EXPECT_EQ(result, SC_ENHANCE_ERROR_OPER_FAIL); - } else { - EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); - } -} - -/** - * @tc.name: SecCompEnhanceCfgParcel001 - * @tc.desc: test Marshalling/Unmarshalling - * @tc.type: FUNC - * @tc.require: AR000HO9IN - */ -HWTEST_F(SecCompEnhanceTest, SecCompEnhanceCfgParcel001, TestSize.Level1) -{ - SecCompEnhanceCfgParcel secCompEnhanceCfgParcel; - OHOS::Parcel out; - bool result = secCompEnhanceCfgParcel.Marshalling(out); - EXPECT_TRUE(result); - - OHOS::Parcel in; - EXPECT_NE(nullptr, secCompEnhanceCfgParcel.Unmarshalling(in)); -} - -/** - * @tc.name: UnmarshallEnhanceCfg001 - * @tc.desc: test UnmarshallEnhanceCfg - * @tc.type: FUNC - * @tc.require: AR000HO9IN - */ -HWTEST_F(SecCompEnhanceTest, UnmarshallEnhanceCfg001, TestSize.Level1) -{ - OHOS::Parcel in; - SecCompEnhanceCfgBase* cfg = nullptr; - int32_t result = SecCompEnhanceAdapter::UnmarshallEnhanceCfg(in, cfg); - if (g_inputEnhanceExist) { - EXPECT_EQ(result, SC_OK); - EXPECT_EQ(cfg, nullptr); + EXPECT_EQ(result, SC_SERVICE_ERROR_SERVICE_NOT_EXIST); } else { EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); } diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h index 18cc9f5..deff0d4 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.h @@ -17,7 +17,6 @@ #include #include "sec_comp_enhance_adapter.h" -#include "sec_comp_enhance_cfg_parcel.h" #include "sec_comp_enhance_kit.h" namespace OHOS { diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 774ae8b..4ca2acf 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -55,6 +55,7 @@ ohos_shared_library("libsecurity_component_sdk") { "access_token:libaccesstoken_sdk", "c_utils:utils", "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", "samgr:samgr_proxy", ] diff --git a/interfaces/inner_api/security_component/include/sec_comp_client.h b/interfaces/inner_api/security_component/include/sec_comp_client.h index 0b17497..0d2773e 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_client.h +++ b/interfaces/inner_api/security_component/include/sec_comp_client.h @@ -50,7 +50,7 @@ private: void WaitForSecCompSa(); void GetSecCompSa(); void LoadSecCompSa(); - sptr GetProxy(); + sptr GetProxy(bool doLoadSa); void GetProxyFromRemoteObject(const sptr& remoteObject); std::mutex cvLock_; diff --git a/interfaces/inner_api/security_component/include/sec_comp_info.h b/interfaces/inner_api/security_component/include/sec_comp_info.h index 5d49e10..173c57d 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_info.h +++ b/interfaces/inner_api/security_component/include/sec_comp_info.h @@ -114,6 +114,12 @@ struct SecCompClickEvent { uint64_t timestamp; ExtraInfo extraInfo; }; + +struct SecCompPointEvent { + double touchX; + double touchY; + uint64_t timestamp; +}; } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp index 77b80b0..d7c157c 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -51,7 +51,7 @@ SecCompClient::~SecCompClient() int32_t SecCompClient::RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId) { - auto proxy = GetProxy(); + auto proxy = GetProxy(true); if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return SC_SERVICE_ERROR_VALUE_INVALID; @@ -62,7 +62,7 @@ int32_t SecCompClient::RegisterSecurityComponent(SecCompType type, int32_t SecCompClient::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) { - auto proxy = GetProxy(); + auto proxy = GetProxy(true); if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return SC_SERVICE_ERROR_VALUE_INVALID; @@ -73,7 +73,7 @@ int32_t SecCompClient::UpdateSecurityComponent(int32_t scId, const std::string& int32_t SecCompClient::UnregisterSecurityComponent(int32_t scId) { - auto proxy = GetProxy(); + auto proxy = GetProxy(true); if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return SC_SERVICE_ERROR_VALUE_INVALID; @@ -85,7 +85,7 @@ int32_t SecCompClient::UnregisterSecurityComponent(int32_t scId) int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) { - auto proxy = GetProxy(); + auto proxy = GetProxy(true); if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return SC_SERVICE_ERROR_VALUE_INVALID; @@ -96,7 +96,7 @@ int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, bool SecCompClient::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) { - auto proxy = GetProxy(); + auto proxy = GetProxy(false); if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return false; @@ -217,13 +217,15 @@ void SecCompClient::GetProxyFromRemoteObject(const sptr& remoteOb return; } -sptr SecCompClient::GetProxy() +sptr SecCompClient::GetProxy(bool doLoadSa) { std::unique_lock lock(proxyMutex_); if (proxy_ != nullptr) { return proxy_; } - LoadSecCompSa(); + if (doLoadSa) { + LoadSecCompSa(); + } return proxy_; } } // namespace SecurityComponent diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp index a34adbc..32ce141 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ #include "sec_comp_kit.h" +#include "hisysevent.h" +#include "ipc_skeleton.h" #include "sec_comp_caller_authorization.h" #include "sec_comp_client.h" #include "sec_comp_log.h" @@ -30,6 +32,9 @@ int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { SC_LOG_ERROR(LABEL, "register security component fail, caller invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "REGITSTER"); return SC_SERVICE_ERROR_CALLER_INVALID; } @@ -37,6 +42,9 @@ int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, if (res != SC_OK) { SC_LOG_ERROR(LABEL, "register security component fail, error: %{public}d", res); } + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "REGISTER_SUCCESS", + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", type); return res; } @@ -45,6 +53,9 @@ int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, const std::string& com if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { SC_LOG_ERROR(LABEL, "update security component fail, caller invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "CLICK"); return SC_SERVICE_ERROR_CALLER_INVALID; } @@ -60,6 +71,9 @@ int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { SC_LOG_ERROR(LABEL, "unregister security component fail, caller invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "UNREGISTER"); return SC_SERVICE_ERROR_CALLER_INVALID; } @@ -67,6 +81,9 @@ int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) if (res != SC_OK) { SC_LOG_ERROR(LABEL, "unregister security component fail, error: %{public}d", res); } + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "UNREGISTER_SUCCESS", + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId); return res; } @@ -76,6 +93,9 @@ int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { SC_LOG_ERROR(LABEL, "report click event fail, caller invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "CLICK"); return SC_SERVICE_ERROR_CALLER_INVALID; } diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index 28a2fc9..4f88a7a 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -44,6 +44,7 @@ ohos_unittest("sec_comp_sdk_test") { "access_token:libaccesstoken_sdk", "c_utils:utils", "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", ] } diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 613f6d6..0a23310 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -75,6 +75,7 @@ ohos_shared_library("security_component_service") { "eventhandler:libeventhandler", "graphic_standard:librender_service_client", "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index 44e22f2..03977d5 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -23,7 +23,8 @@ namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntity"}; -static constexpr uint64_t MAX_TOUCH_INTERVAL = 1000000000L; // 1000ms +static constexpr uint64_t MAX_TOUCH_INTERVAL = 1000000L; // 1000ms +static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; } int32_t SecCompEntity::RevokeTempPermission() @@ -49,9 +50,11 @@ bool SecCompEntity::CompareComponentBasicInfo(SecCompBase* other) const bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const { - auto current = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + auto current = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; if (touchInfo.timestamp < current - MAX_TOUCH_INTERVAL || touchInfo.timestamp > current) { - SC_LOG_ERROR(LABEL, "touch timestamp invalid"); + SC_LOG_ERROR(LABEL, "touch timestamp invalid touchInfo. timestamp: %{public}lu, current: %{public}lu", + touchInfo.timestamp, current); return false; } diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 52a0f84..4bb13c8 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -15,7 +15,9 @@ #include "sec_comp_manager.h" +#include "hisysevent.h" #include "i_sec_comp_service.h" +#include "ipc_skeleton.h" #include "iservice_registry.h" #include "sec_comp_enhance_adapter.h" #include "sec_comp_err.h" @@ -207,6 +209,9 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, std::shared_ptr component(componentPtr); if (component == nullptr) { SC_LOG_ERROR(LABEL, "Parse component info invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "COMPONENT_INFO_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "REGITSTER", "SC_TYPE", type); return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } @@ -236,6 +241,9 @@ int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::js std::shared_ptr reportComponentInfo(report); if (reportComponentInfo == nullptr) { SC_LOG_ERROR(LABEL, "Update component info invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "COMPONENT_INFO_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "CLICK", "SC_TYPE", sc->GetType()); return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } sc->SetComponentInfo(reportComponentInfo); @@ -269,16 +277,37 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, std::shared_ptr reportComponentInfo(report); if ((reportComponentInfo == nullptr) || !reportComponentInfo->GetValid()) { SC_LOG_ERROR(LABEL, "report component info invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "COMPONENT_INFO_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "CLICK", "SC_TYPE", sc->GetType()); return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } + if (SecCompEnhanceAdapter::CheckExtraInfo(touchInfo) != SC_OK) { + SC_LOG_ERROR(LABEL, "check extra info failed, HMAC is invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CHALLENGE_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType(), "CALL_SCENE", "CLICK"); + return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; + } sc->SetComponentInfo(reportComponentInfo); if (!sc->CheckTouchInfo(touchInfo)) { SC_LOG_ERROR(LABEL, "touchInfo is invalid"); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; } - - return sc->GrantTempPermission(); + int res = sc->GrantTempPermission(); + if (res != SC_OK) { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "TEMP_GRANT_FAILED", + HiviewDFX::HiSysEvent::EventType::FAULT, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); + } + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "TEMP_GRANT_SUCCESS", + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); + return res; } bool SecCompManager::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index d0815fc..c78d49c 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -14,6 +14,9 @@ */ #include "sec_comp_service.h" + +#include +#include "hisysevent.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "sec_comp_err.h" @@ -26,6 +29,7 @@ namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompService"}; +static const int32_t ROOT_UID = 0; } REGISTER_SYSTEM_ABILITY_BY_ID(SecCompService, SA_ID_SECURITY_COMPONENT_SERVICE, true); @@ -62,6 +66,8 @@ void SecCompService::OnStart() SC_LOG_ERROR(LABEL, "Failed to initialize"); return; } + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "SERVICE_INIT_SUCCESS", + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "PID", getpid()); SC_LOG_INFO(LABEL, "Congratulations, SecCompService start successfully!"); } @@ -127,11 +133,11 @@ bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); int32_t pid = IPCSkeleton::GetCallingPid(); int32_t uid = IPCSkeleton::GetCallingUid(); - if (AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) != AccessToken::TOKEN_HAP) { + if ((uid != ROOT_UID) && (AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) != AccessToken::TOKEN_HAP)) { SC_LOG_ERROR(LABEL, "Get caller tokenId invalid"); return false; } - if (!appStateObserver_->IsProcessForeground(uid)) { + if ((uid != ROOT_UID) && (!appStateObserver_->IsProcessForeground(uid))) { SC_LOG_ERROR(LABEL, "caller uid is not in foreground"); return false; } diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 93fdabe..2fbf1ec 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -65,6 +65,56 @@ ohos_unittest("sec_comp_service_test") { "eventhandler:libeventhandler", "graphic_standard:librender_service_client", "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + "window_manager:libdm", + ] +} + +ohos_unittest("sec_comp_service_mock_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/include", + "//third_party/json/include", + ] + + sources = [ + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_manager.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp", + "unittest/src/sec_comp_service_mock_test.cpp", + ] + + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "//third_party/googletest:gmock_main", + ] + + external_deps = [ + "access_token:libtoken_setproc", + "c_utils:utils", + "eventhandler:libeventhandler", + "graphic_standard:librender_service_client", + "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", "window_manager:libdm", ] @@ -72,5 +122,8 @@ ohos_unittest("sec_comp_service_test") { group("unittest") { testonly = true - deps = [ ":sec_comp_service_test" ] + deps = [ + ":sec_comp_service_test", + ":sec_comp_service_mock_test", + ] } diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h b/services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h similarity index 58% rename from interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h rename to services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h index 08f5eee..36bbaaa 100644 --- a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_cfg_parcel.h +++ b/services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h @@ -12,26 +12,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef SECURITY_COMPONENT_ENHANCE_CFG_PARCEL_H -#define SECURITY_COMPONENT_ENHANCE_CFG_PARCEL_H +#ifndef SECURITY_COMPONENT_ENHANCE_ADAPTER_H +#define SECURITY_COMPONENT_ENHANCE_ADAPTER_H +#include #include "parcel.h" -#include "sec_comp_enhance_adapter.h" +#include "sec_comp_info.h" namespace OHOS { namespace Security { namespace SecurityComponent { -struct SecCompEnhanceCfgParcel final : public Parcelable { - SecCompEnhanceCfgParcel(); +struct SecCompEnhanceAdapter { + static int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen); + static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, + uint8_t* enhanceData, uint32_t& enHancedataLen); + static int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo); - ~SecCompEnhanceCfgParcel() override; - - bool Marshalling(Parcel& out) const override; - - static SecCompEnhanceCfgParcel* Unmarshalling(Parcel& in); - - SecCompEnhanceCfgBase* cfg_; + static int32_t EnableInputEnhance(); + static int32_t DisableInputEnhance(); }; } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // SECURITY_COMPONENT_ENHANCE_CFG_PARCEL_H \ No newline at end of file +#endif // SECURITY_COMPONENT_ENHANCE_ADAPTER_H diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp similarity index 48% rename from interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp rename to services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp index 343e6bc..a441245 100644 --- a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_cfg_parcel.cpp +++ b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "sec_comp_enhance_cfg_parcel.h" +#include "sec_comp_enhance_adapter.h" #include "sec_comp_err.h" #include "sec_comp_log.h" @@ -21,40 +21,38 @@ namespace Security { namespace SecurityComponent { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceCfgParcel"}; + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "MockSecCompEnhanceAdapter"}; } -SecCompEnhanceCfgParcel::SecCompEnhanceCfgParcel() +int32_t SecCompEnhanceAdapter::SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) { - cfg_ = nullptr; + SC_LOG_DEBUG(LABEL, "SetEnhanceCfg success"); + return SC_OK; } -SecCompEnhanceCfgParcel::~SecCompEnhanceCfgParcel() +int32_t SecCompEnhanceAdapter::GetPointerEventEnhanceData(void* data, uint32_t dataLen, + uint8_t* enhanceData, uint32_t& enHancedataLen) { - if (cfg_ != nullptr) { - delete cfg_; - } + SC_LOG_DEBUG(LABEL, "GetPointerEventEnhanceData success"); + return SC_OK; } -bool SecCompEnhanceCfgParcel::Marshalling(Parcel& out) const +int32_t SecCompEnhanceAdapter::CheckExtraInfo(const SecCompClickEvent& touchInfo) { - return (SecCompEnhanceAdapter::MarshallEnhanceCfg(cfg_, out) != SC_OK); + SC_LOG_DEBUG(LABEL, "CheckExtraInfo success"); + return SC_OK; } -SecCompEnhanceCfgParcel* SecCompEnhanceCfgParcel::Unmarshalling(Parcel& in) +int32_t SecCompEnhanceAdapter::EnableInputEnhance() { - auto* SecCompCfgParcel = new (std::nothrow) SecCompEnhanceCfgParcel(); - if (SecCompCfgParcel == nullptr) { - SC_LOG_ERROR(LABEL, "New SecCompCfgParcel failed"); - return nullptr; - } + SC_LOG_DEBUG(LABEL, "EnableInputEnhance success"); + return SC_OK; +} - if (SecCompEnhanceAdapter::UnmarshallEnhanceCfg(in, SecCompCfgParcel->cfg_) != SC_OK) { - SC_LOG_ERROR(LABEL, "Unmarshall failed"); - delete SecCompCfgParcel; - return nullptr; - } - return SecCompCfgParcel; +int32_t SecCompEnhanceAdapter::DisableInputEnhance() +{ + SC_LOG_DEBUG(LABEL, "DisableInputEnhance success"); + return SC_OK; } } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index ec2935d..4ade7ed 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -30,6 +30,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntityTest"}; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIFF_COORDINATE = 200.0; +static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; } void SecCompEntityTest::SetUpTestCase() @@ -144,11 +145,13 @@ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) entity_->componentInfo_->rect_.y_ = TEST_DIFF_COORDINATE; entity_->componentInfo_->rect_.width_ = TEST_DIFF_COORDINATE; entity_->componentInfo_->rect_.height_ = TEST_DIFF_COORDINATE; - touch.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + touch.timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; ASSERT_FALSE(entity_->CheckTouchInfo(touch)); entity_->componentInfo_->rect_.x_ = TEST_COORDINATE; entity_->componentInfo_->rect_.y_ = TEST_COORDINATE; - touch.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + touch.timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; ASSERT_TRUE(entity_->CheckTouchInfo(touch)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp new file mode 100644 index 0000000..ba90387 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -0,0 +1,393 @@ +/* + * Copyright (c) 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. + */ +#include "sec_comp_service_test.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "location_button.h" +#include "mock_system_ability_proxy.h" +#include "mock_app_mgr_proxy.h" +#include "paste_button.h" +#include "save_button.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" +#include "sec_comp_enhance_adapter.h" +#include "system_ability.h" +#include "token_setproc.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompServiceTest"}; + +static constexpr uint32_t HAP_TOKEN_ID = 537715419; +static constexpr int32_t SA_ID = 3506; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr double TEST_SIZE = 100.0; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; +static AccessTokenID g_selfTokenId = 0; + +static std::string BuildLocationComponentInfo() +{ + LocationButton button; + button.fontSize_ = TEST_SIZE; + button.iconSize_ = TEST_SIZE; + button.padding_.top = TEST_SIZE; + button.padding_.right = TEST_SIZE; + button.padding_.bottom = TEST_SIZE; + button.padding_.left = TEST_SIZE; + button.textIconSpace_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_YELLOW; + button.iconColor_.value = TEST_COLOR_RED; + button.bgColor_.value = TEST_COLOR_BLUE; + button.borderWidth_ = TEST_SIZE; + button.type_ = LOCATION_COMPONENT; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.windowRect_.x_ = TEST_COORDINATE; + button.windowRect_.y_ = TEST_COORDINATE; + button.windowRect_.width_ = TEST_COORDINATE; + button.windowRect_.height_ = TEST_COORDINATE; + button.text_ = static_cast(LocationDesc::SELECT_LOCATION); + button.icon_ = static_cast(LocationIcon::LINE_ICON); + button.bg_ = SecCompBackground::CIRCLE; + + nlohmann::json jsonRes; + button.ToJson(jsonRes); + return jsonRes.dump(); +} + +static std::string BuildSaveComponentInfo() +{ + SaveButton button; + button.fontSize_ = TEST_SIZE; + button.iconSize_ = TEST_SIZE; + button.padding_.top = TEST_SIZE; + button.padding_.right = TEST_SIZE; + button.padding_.bottom = TEST_SIZE; + button.padding_.left = TEST_SIZE; + button.textIconSpace_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_YELLOW; + button.iconColor_.value = TEST_COLOR_RED; + button.bgColor_.value = TEST_COLOR_BLUE; + button.borderWidth_ = TEST_SIZE; + button.type_ = SAVE_COMPONENT; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.windowRect_.x_ = TEST_COORDINATE; + button.windowRect_.y_ = TEST_COORDINATE; + button.windowRect_.width_ = TEST_COORDINATE; + button.windowRect_.height_ = TEST_COORDINATE; + button.text_ = static_cast(SaveDesc::DOWNLOAD); + button.icon_ = static_cast(SaveIcon::LINE_ICON); + button.bg_ = SecCompBackground::CIRCLE; + + nlohmann::json jsonRes; + button.ToJson(jsonRes); + return jsonRes.dump(); +} +} + +void SecCompServiceTest::SetUpTestCase() +{} + +void SecCompServiceTest::TearDownTestCase() +{} + +void SecCompServiceTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); + if (secCompService_ != nullptr) { + return; + } + SecCompService* ptr = new (std::nothrow) SecCompService(SA_ID, true); + secCompService_ = sptr(ptr); + ASSERT_NE(nullptr, secCompService_); + secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); + ASSERT_TRUE(secCompService_->appStateObserver_ != nullptr); + g_selfTokenId = GetSelfTokenID(); +} + +void SecCompServiceTest::TearDown() +{ + if (secCompService_ != nullptr) { + secCompService_->appStateObserver_ = nullptr; + } + secCompService_ = nullptr; + EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId)); +} + +/** + * @tc.name: RegisterSecurityComponent001 + * @tc.desc: Test register security component + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) +{ + // get caller fail + int32_t scId; + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId), + SC_SERVICE_ERROR_VALUE_INVALID); + + // parse component json fail + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "{a=", scId), + SC_SERVICE_ERROR_VALUE_INVALID); // wrong json + + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), + SC_OK); + EXPECT_EQ(secCompService_->UpdateSecurityComponent(scId, BuildLocationComponentInfo()), + SC_OK); + struct SecCompClickEvent touch = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + SC_OK); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: RegisterSecurityComponent002 + * @tc.desc: Test register security component check touch info failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, RegisterSecurityComponent002, TestSize.Level1) +{ + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touch = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) + }; + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + SC_SERVICE_ERROR_CLICK_EVENT_INVALID); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: RegisterSecurityComponent003 + * @tc.desc: Test register security component permission grant failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, RegisterSecurityComponent003, TestSize.Level1) +{ + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + + ASSERT_EQ(SetSelfTokenID(0), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touch = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent001 + * @tc.desc: Test register security component + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) +{ + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touchInfo = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo)); + + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(6); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent002 + * @tc.desc: Test report security component click with save button + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Level1) +{ + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touchInfo = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + touchInfo.timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + touchInfo.timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + touchInfo.timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;; + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + sleep(3); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.desc: Test report security component click twice with save button + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +{ + int32_t scId; + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), + SC_OK); + struct SecCompClickEvent touchInfo = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + touchInfo.timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(6); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + touchInfo.timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + SC_OK); + sleep(3); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 6c15a7b..dbbddca 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -37,77 +37,8 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr int32_t SA_ID = 3506; -static constexpr double TEST_COORDINATE = 100.0; static constexpr int32_t TEST_SC_ID = 1; -static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; static AccessTokenID g_selfTokenId = 0; - -static std::string BuildLocationComponentInfo() -{ - LocationButton button; - button.fontSize_ = TEST_SIZE; - button.iconSize_ = TEST_SIZE; - button.padding_.top = TEST_SIZE; - button.padding_.right = TEST_SIZE; - button.padding_.bottom = TEST_SIZE; - button.padding_.left = TEST_SIZE; - button.textIconSpace_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_YELLOW; - button.iconColor_.value = TEST_COLOR_RED; - button.bgColor_.value = TEST_COLOR_BLUE; - button.borderWidth_ = TEST_SIZE; - button.type_ = LOCATION_COMPONENT; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.windowRect_.x_ = TEST_COORDINATE; - button.windowRect_.y_ = TEST_COORDINATE; - button.windowRect_.width_ = TEST_COORDINATE; - button.windowRect_.height_ = TEST_COORDINATE; - button.text_ = static_cast(LocationDesc::SELECT_LOCATION); - button.icon_ = static_cast(LocationIcon::LINE_ICON); - button.bg_ = SecCompBackground::CIRCLE; - - nlohmann::json jsonRes; - button.ToJson(jsonRes); - return jsonRes.dump(); -} - -static std::string BuildSaveComponentInfo() -{ - SaveButton button; - button.fontSize_ = TEST_SIZE; - button.iconSize_ = TEST_SIZE; - button.padding_.top = TEST_SIZE; - button.padding_.right = TEST_SIZE; - button.padding_.bottom = TEST_SIZE; - button.padding_.left = TEST_SIZE; - button.textIconSpace_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_YELLOW; - button.iconColor_.value = TEST_COLOR_RED; - button.bgColor_.value = TEST_COLOR_BLUE; - button.borderWidth_ = TEST_SIZE; - button.type_ = SAVE_COMPONENT; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.windowRect_.x_ = TEST_COORDINATE; - button.windowRect_.y_ = TEST_COORDINATE; - button.windowRect_.width_ = TEST_COORDINATE; - button.windowRect_.height_ = TEST_COORDINATE; - button.text_ = static_cast(SaveDesc::DOWNLOAD); - button.icon_ = static_cast(SaveIcon::LINE_ICON); - button.bg_ = SecCompBackground::CIRCLE; - - nlohmann::json jsonRes; - button.ToJson(jsonRes); - return jsonRes.dump(); -} } void SecCompServiceTest::SetUpTestCase() @@ -250,13 +181,14 @@ HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level1) */ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) { - // token id is shell + // not root uid + setuid(1); SecCompCallerInfo caller; EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); // set token id to hap token, but uid is not in foreground - EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); + setuid(0); ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); // add local uid to foreground. AppExecFwk::AppStateData stateData = { @@ -266,44 +198,6 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) EXPECT_TRUE(secCompService_->GetCallerInfo(caller)); } -/** - * @tc.name: RegisterSecurityComponent001 - * @tc.desc: Test register security component - * @tc.type: FUNC - * @tc.require: AR000HO9J7 - */ -HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) -{ - // get caller fail - int32_t scId; - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId), - SC_SERVICE_ERROR_VALUE_INVALID); - - // parse component json fail - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); - AppExecFwk::AppStateData stateData = { - .uid = getuid() - }; - secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "{a=", scId), - SC_SERVICE_ERROR_VALUE_INVALID); // wrong json - - // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), - SC_OK); - EXPECT_EQ(secCompService_->UpdateSecurityComponent(scId, BuildLocationComponentInfo()), - SC_OK); - struct SecCompClickEvent touch = { - .touchX = 100, - .touchY = 100, - .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) - }; - - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), - SC_OK); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); -} - /** * @tc.name: UnregisterSecurityComponent001 * @tc.desc: Test unregister security component @@ -313,7 +207,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) { // get caller fail - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(TEST_SC_ID), SC_SERVICE_ERROR_VALUE_INVALID); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(TEST_SC_ID), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); } /** @@ -334,146 +228,3 @@ HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, "{a"), SC_SERVICE_ERROR_VALUE_INVALID); } - -/** - * @tc.name: ReportSecurityComponentClickEvent001 - * @tc.desc: Test register security component - * @tc.type: FUNC - * @tc.require: AR000HO9J7 - */ -HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) -{ - int32_t scId; - secCompService_->state_ = ServiceRunningState::STATE_RUNNING; - secCompService_->Initialize(); - - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); - AppExecFwk::AppStateData stateData = { - .uid = getuid() - }; - secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); - struct SecCompClickEvent touchInfo = { - .touchX = 100, - .touchY = 100, - .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) - }; - - ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo)); - - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - sleep(6); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); -} - -/** - * @tc.name: ReportSecurityComponentClickEvent002 - * @tc.desc: Test report security component click with save button - * @tc.type: FUNC - * @tc.require: AR000HO9IN - */ -HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Level1) -{ - int32_t scId; - secCompService_->state_ = ServiceRunningState::STATE_RUNNING; - secCompService_->Initialize(); - - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); - AppExecFwk::AppStateData stateData = { - .uid = getuid() - }; - secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); - struct SecCompClickEvent touchInfo = { - .touchX = 100, - .touchY = 100, - .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) - }; - - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - sleep(3); - touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - sleep(3); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - - touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - sleep(3); - touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - sleep(3); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); -} - -/** - * @tc.name: ReportSecurityComponentClickEvent003 - * @tc.desc: Test report security component click twice with save button - * @tc.type: FUNC - * @tc.require: AR000HO9J7 - */ -HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent003, TestSize.Level1) -{ - int32_t scId; - secCompService_->state_ = ServiceRunningState::STATE_RUNNING; - secCompService_->Initialize(); - - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); - AppExecFwk::AppStateData stateData = { - .uid = getuid() - }; - secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); - struct SecCompClickEvent touchInfo = { - .touchX = 100, - .touchY = 100, - .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) - }; - - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - sleep(3); - touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - sleep(6); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - - touchInfo.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), - SC_OK); - sleep(3); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); -} -- Gitee From fdc4fcdb8735eb901d6264d3cdcee2b5e599b51a Mon Sep 17 00:00:00 2001 From: xxx Date: Sun, 2 Jul 2023 19:11:03 +0800 Subject: [PATCH 30/59] =?UTF-8?q?Description:=E9=92=88=E5=AF=B9cJSON?= =?UTF-8?q?=E9=83=A8=E4=BB=B6=E5=90=8D=E6=95=B4=E6=94=B9=EF=BC=8C=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E9=80=82=E9=85=8D=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-c5039975e75448d5f89331fa998844eb95d219d0 --- bundle.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundle.json b/bundle.json index 33452d4..738eb79 100644 --- a/bundle.json +++ b/bundle.json @@ -37,7 +37,7 @@ "window_manager" ], "third_party": [ - "cjson", + "cJSON", "googletest" ] }, -- Gitee From 836c0944f0ae5bdd75de0286794d926fdccbc6d4 Mon Sep 17 00:00:00 2001 From: xxx Date: Sun, 2 Jul 2023 17:44:08 +0800 Subject: [PATCH 31/59] Description: bugfix Match-id-21c5a03cdb69336c0d7ce831aecf14ce4a9831cf --- interfaces/inner_api/security_component/src/sec_comp_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp index d7c157c..976870b 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -96,7 +96,7 @@ int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, bool SecCompClient::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) { - auto proxy = GetProxy(false); + auto proxy = GetProxy(true); if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return false; -- Gitee From 3827098112149f1c0aec87b28246a7a341341297 Mon Sep 17 00:00:00 2001 From: xxx Date: Sat, 1 Jul 2023 22:58:56 +0800 Subject: [PATCH 32/59] Description: DFX Match-id-e90319cb41fecc4fa1f5edf2aab06bba33976595 --- bundle.json | 1 + .../security_component_service/sa/BUILD.gn | 1 + .../sa/sa_main/sec_comp_service.cpp | 21 +++++- .../sa/test/BUILD.gn | 2 + .../unittest/src/sec_comp_service_test.cpp | 75 +++++++++++++++++++ 5 files changed, 98 insertions(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index 738eb79..7c865b3 100644 --- a/bundle.json +++ b/bundle.json @@ -30,6 +30,7 @@ "eventhandler", "hilog", "hisysevent", + "hitrace", "graphic_standard", "ipc", "safwk", diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 0a23310..86031d6 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -76,6 +76,7 @@ ohos_shared_library("security_component_service") { "graphic_standard:librender_service_client", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_meter", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index c78d49c..ced7ca5 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -17,6 +17,7 @@ #include #include "hisysevent.h" +#include "hitrace_meter.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "sec_comp_err.h" @@ -47,28 +48,34 @@ SecCompService::~SecCompService() void SecCompService::OnStart() { + StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentOnStart"); if (state_ == ServiceRunningState::STATE_RUNNING) { SC_LOG_INFO(LABEL, "SecCompService has already started!"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return; } SC_LOG_INFO(LABEL, "SecCompService is starting"); if (!RegisterAppStateObserver()) { SC_LOG_ERROR(LABEL, "Failed to register app state observer!"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return; } state_ = ServiceRunningState::STATE_RUNNING; bool ret = Publish(this); if (!ret) { SC_LOG_ERROR(LABEL, "Failed to publish service!"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return; } if (!Initialize()) { SC_LOG_ERROR(LABEL, "Failed to initialize"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return; } HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "SERVICE_INIT_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "PID", getpid()); SC_LOG_INFO(LABEL, "Congratulations, SecCompService start successfully!"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); } void SecCompService::OnStop() @@ -150,19 +157,24 @@ bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) int32_t SecCompService::RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId) { + StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentRegister"); SecCompCallerInfo caller; if (!GetCallerInfo(caller)) { SC_LOG_ERROR(LABEL, "Check caller failed"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return SC_SERVICE_ERROR_VALUE_INVALID; } nlohmann::json jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); if (jsonRes.is_discarded()) { SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return SC_SERVICE_ERROR_VALUE_INVALID; } - return SecCompManager::GetInstance().RegisterSecurityComponent(type, jsonRes, caller, scId); + int32_t res = SecCompManager::GetInstance().RegisterSecurityComponent(type, jsonRes, caller, scId); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + return res; } int32_t SecCompService::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) @@ -195,18 +207,23 @@ int32_t SecCompService::UnregisterSecurityComponent(int32_t scId) int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) { + StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentClick"); SecCompCallerInfo caller; if (!GetCallerInfo(caller)) { SC_LOG_ERROR(LABEL, "Check caller failed"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return SC_SERVICE_ERROR_VALUE_INVALID; } nlohmann::json jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); if (jsonRes.is_discarded()) { SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return SC_SERVICE_ERROR_VALUE_INVALID; } - return SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, touchInfo); + int32_t res = SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, touchInfo); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + return res; } bool SecCompService::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 2fbf1ec..1ce1872 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -66,6 +66,7 @@ ohos_unittest("sec_comp_service_test") { "graphic_standard:librender_service_client", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_meter", "ipc:ipc_core", "window_manager:libdm", ] @@ -115,6 +116,7 @@ ohos_unittest("sec_comp_service_mock_test") { "graphic_standard:librender_service_client", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_meter", "ipc:ipc_core", "window_manager:libdm", ] diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index dbbddca..40cd10f 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -39,6 +39,44 @@ static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr int32_t SA_ID = 3506; static constexpr int32_t TEST_SC_ID = 1; static AccessTokenID g_selfTokenId = 0; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr double TEST_SIZE = 100.0; +static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; + +static std::string BuildLocationComponentInfo() +{ + LocationButton button; + button.fontSize_ = TEST_SIZE; + button.iconSize_ = TEST_SIZE; + button.padding_.top = TEST_SIZE; + button.padding_.right = TEST_SIZE; + button.padding_.bottom = TEST_SIZE; + button.padding_.left = TEST_SIZE; + button.textIconSpace_ = TEST_SIZE; + button.fontColor_.value = TEST_COLOR_YELLOW; + button.iconColor_.value = TEST_COLOR_RED; + button.bgColor_.value = TEST_COLOR_BLUE; + button.borderWidth_ = TEST_SIZE; + button.type_ = LOCATION_COMPONENT; + button.rect_.x_ = TEST_COORDINATE; + button.rect_.y_ = TEST_COORDINATE; + button.rect_.width_ = TEST_COORDINATE; + button.rect_.height_ = TEST_COORDINATE; + button.windowRect_.x_ = TEST_COORDINATE; + button.windowRect_.y_ = TEST_COORDINATE; + button.windowRect_.width_ = TEST_COORDINATE; + button.windowRect_.height_ = TEST_COORDINATE; + button.text_ = static_cast(LocationDesc::SELECT_LOCATION); + button.icon_ = static_cast(LocationIcon::LINE_ICON); + button.bg_ = SecCompBackground::CIRCLE; + + nlohmann::json jsonRes; + button.ToJson(jsonRes); + return jsonRes.dump(); +} } void SecCompServiceTest::SetUpTestCase() @@ -228,3 +266,40 @@ HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, "{a"), SC_SERVICE_ERROR_VALUE_INVALID); } + +/** + * @tc.name: ReportSecurityComponentClickEvent001 + * @tc.desc: Test report security component + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) +{ + // get caller fail + int32_t scId; + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId), + SC_SERVICE_ERROR_VALUE_INVALID); + + // parse component json fail + ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + AppExecFwk::AppStateData stateData = { + .uid = getuid() + }; + secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); + + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), + SC_OK); + uint8_t data[16] = { 0 }; + struct SecCompClickEvent touch = { + .touchX = 100, + .touchY = 100, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT, + .extraInfo.data = data, + .extraInfo.dataSize = 16, + }; + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + SC_SERVICE_ERROR_CLICK_EVENT_INVALID); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} -- Gitee From b1b925a002f0a1872dd9764b6be0616cbb700f41 Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 3 Jul 2023 16:38:34 +0800 Subject: [PATCH 33/59] Description:add enhance callback Match-id-3bcc76166be3a45dc8f044a8c3469ccb429ea19f --- bundle.json | 1 + frameworks/BUILD.gn | 3 + frameworks/common/include/sec_comp_err.h | 7 + .../include/sec_comp_enhance_adapter.h | 44 +++- .../src/sec_comp_enhance_adapter.cpp | 120 +++++++++- .../security_component/src/sec_comp_base.cpp | 8 + interfaces/inner_api/enhance_kits/BUILD.gn | 1 + .../include/sec_comp_enhance_kit.h | 3 +- .../include/sec_comp_enhance_kit_c.h | 21 ++ .../enhance_kits/src/sec_comp_enhance_kit.cpp | 10 + .../inner_api/security_component/BUILD.gn | 5 +- .../include/i_sec_comp_probe.h | 30 +++ .../include/i_sec_comp_service.h | 1 + .../include/sec_comp_base.h | 2 + .../include/sec_comp_client.h | 2 + .../security_component/include/sec_comp_kit.h | 8 +- .../include/sec_comp_proxy.h | 1 + .../include/sec_comp_ui_register.h | 4 +- ...ity_component_service_ipc_interface_code.h | 8 + .../src/sec_comp_client.cpp | 37 ++- .../security_component/src/sec_comp_kit.cpp | 40 +++- .../security_component/src/sec_comp_proxy.cpp | 32 ++- .../src/sec_comp_ui_register.cpp | 5 +- .../unittest/src/location_button_test.cpp | 3 +- .../test/unittest/src/paste_button_test.cpp | 3 +- .../test/unittest/src/save_button_test.cpp | 3 +- .../test/unittest/src/sec_comp_kit_test.cpp | 33 ++- .../security_component_service/sa/BUILD.gn | 2 + .../sa/sa_main/app_state_observer.cpp | 36 ++- .../sa/sa_main/app_state_observer.h | 5 +- .../sa/sa_main/delay_exit_task.cpp | 74 ++++++ .../sa/sa_main/delay_exit_task.h | 45 ++++ .../sa/sa_main/sec_comp_entity.h | 2 +- .../sa/sa_main/sec_comp_manager.cpp | 223 +++++++++++++++--- .../sa/sa_main/sec_comp_manager.h | 29 ++- .../sa/sa_main/sec_comp_perm_manager.cpp | 10 +- .../sa/sa_main/sec_comp_perm_manager.h | 3 +- .../sa/sa_main/sec_comp_service.cpp | 21 +- .../sa/sa_main/sec_comp_service.h | 2 + .../sa/sa_main/sec_comp_stub.cpp | 13 + .../sa/sa_main/sec_comp_stub.h | 2 + .../sa/test/BUILD.gn | 3 + .../mock/src/sec_comp_enhance_adapter.cpp | 50 ++++ .../unittest/src/app_state_observer_test.cpp | 55 +++-- .../src/sec_comp_info_helper_test.cpp | 3 +- .../unittest/src/sec_comp_manager_test.cpp | 84 +++---- .../test/unittest/src/sec_comp_stub_test.cpp | 2 +- .../sa/test/unittest/src/sec_comp_stub_test.h | 5 + 48 files changed, 888 insertions(+), 216 deletions(-) create mode 100644 interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h create mode 100644 interfaces/inner_api/security_component/include/i_sec_comp_probe.h create mode 100644 services/security_component_service/sa/sa_main/delay_exit_task.cpp create mode 100644 services/security_component_service/sa/sa_main/delay_exit_task.h diff --git a/bundle.json b/bundle.json index 7c865b3..dd40314 100644 --- a/bundle.json +++ b/bundle.json @@ -51,6 +51,7 @@ "name": "//base/security/security_component/interfaces/inner_api/security_component:libsecurity_component_sdk", "header": { "header_files": [ + "i_sec_comp_probe.h", "sec_comp_kit.h" ], "header_base": "//base/security/security_component/interfaces/inner_api/security_component/include" diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index a8b9f42..3c0b9ea 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -59,6 +59,7 @@ config("libsecurity_component_enhance_adapter_config") { "common/include", "enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + "//third_party/json/include" ] } @@ -71,6 +72,7 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { "common/include", "enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + "//third_party/json/include" ] sources = [ "enhance_adapter/src/sec_comp_enhance_adapter.cpp" ] @@ -81,6 +83,7 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { external_deps = [ "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/frameworks/common/include/sec_comp_err.h b/frameworks/common/include/sec_comp_err.h index 675e47d..ca1d4f6 100644 --- a/frameworks/common/include/sec_comp_err.h +++ b/frameworks/common/include/sec_comp_err.h @@ -38,6 +38,13 @@ enum SCErrCode : int32_t { SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE = -100, SC_ENHANCE_ERROR_VALUE_INVALID = -101, SC_ENHANCE_ERROR_OPER_FAIL = -102, + SC_ENHANCE_ERROR_CALLBACK_REDIRECT = -103, + SC_ENHANCE_ERROR_CALLBACK_REGIST_FAIL = -104, + SC_ENHANCE_ERROR_CALLBACK_HAS_EXIST = -105, + SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST = -106, + SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL = -107, + SC_ENHANCE_ERROR_CALLBACK_CHECK_FAIL = -108, + SC_ENHANCE_ERROR_IN_MALICIOUS_LIST = -109, }; } // namespace SecurityComponent } // namespace Security diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index 00b5eac..6ffee03 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -15,7 +15,10 @@ #ifndef SECURITY_COMPONENT_ENHANCE_ADAPTER_H #define SECURITY_COMPONENT_ENHANCE_ADAPTER_H #include +#include "iremote_object.h" +#include "nlohmann/json.hpp" #include "parcel.h" +#include "sec_comp_base.h" #include "sec_comp_info.h" namespace OHOS { @@ -24,23 +27,38 @@ namespace SecurityComponent { enum EnhanceInterfaceType { SEC_COMP_ENHANCE_INPUT_INTERFACE = 0, SEC_COMP_ENHANCE_SRV_INTERFACE = 1, + SEC_COMP_ENHANCE_CLIENT_INTERFACE = 2, }; class SecCompEnhanceCfgBase { }; -class SecCompEnhanceInputInterface { +class SecCompInputEnhanceInterface { public: virtual int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) = 0; virtual int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen) = 0; }; -class SecCompEnhanceServiceInterface { -public: +class SecCompSrvEnhanceInterface { + public: virtual int32_t EnableInputEnhance() = 0; virtual int32_t DisableInputEnhance() = 0; virtual int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo) = 0; + virtual int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, + const nlohmann::json& jsonComponent) = 0; + virtual sptr GetEnhanceRemoteObject() = 0; + virtual void StartEnhanceService() = 0; + virtual void ExitEnhanceService() = 0; + virtual void NotifyProcessDied(int32_t pid) = 0; +}; + +class SecCompClientEnhanceInterface { +public: + virtual bool EnhanceDataPreprocess(const uintptr_t caller, std::string& componentInfo) = 0; + virtual bool EnhanceDataPreprocess(const uintptr_t caller, int32_t scId, std::string& componentInfo) = 0; + virtual void RegisterScIdEnhance(const uintptr_t caller, int32_t scId) = 0; + virtual void UnregisterScIdEnhance(const uintptr_t caller, int32_t scId) = 0; }; struct SecCompEnhanceAdapter { @@ -49,16 +67,30 @@ struct SecCompEnhanceAdapter { static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen); static int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo); - static int32_t EnableInputEnhance(); static int32_t DisableInputEnhance(); - static SecCompEnhanceInputInterface* inputHandler; + static int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, + const nlohmann::json& jsonComponent); + static sptr GetEnhanceRemoteObject(); + static bool EnhanceDataPreprocess(std::string& componentInfo); + static bool EnhanceDataPreprocess(int32_t scId, std::string& componentInfo); + static void RegisterScIdEnhance(int32_t scId); + static void UnregisterScIdEnhance(int32_t scId); + + static void StartEnhanceService(); + static void ExistEnhanceService(); + static void NotifyProcessDied(int32_t pid); + + static SecCompInputEnhanceInterface* inputHandler; static bool isEnhanceInputHandlerInit; - static SecCompEnhanceServiceInterface* srvHandler; + static SecCompSrvEnhanceInterface* srvHandler; static bool isEnhanceSrvHandlerInit; + static SecCompClientEnhanceInterface* clientHandler; + static bool isEnhanceClientHandlerInit; + static std::mutex initMtx; }; } // namespace SecurityComponent diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index 84df5f9..b319653 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -31,14 +31,18 @@ static const std::string LIB_PATH = "/system/lib/"; #endif static const std::string ENHANCE_INPUT_INTERFACE_LIB = LIB_PATH + "libsec_comp_input_enhance.z.so"; static const std::string ENHANCE_SRV_INTERFACE_LIB = LIB_PATH + "libsec_comp_service_enhance.z.so"; +static const std::string ENHANCE_CLIENT_INTERFACE_LIB = LIB_PATH + "libsec_comp_client_enhance.z.so"; } -SecCompEnhanceInputInterface* SecCompEnhanceAdapter::inputHandler = nullptr; +SecCompInputEnhanceInterface* SecCompEnhanceAdapter::inputHandler = nullptr; bool SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; -SecCompEnhanceServiceInterface* SecCompEnhanceAdapter::srvHandler = nullptr; +SecCompSrvEnhanceInterface* SecCompEnhanceAdapter::srvHandler = nullptr; bool SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false; +SecCompClientEnhanceInterface* SecCompEnhanceAdapter::clientHandler = nullptr; +bool SecCompEnhanceAdapter::isEnhanceClientHandlerInit = false; + std::mutex SecCompEnhanceAdapter::initMtx; void SecCompEnhanceAdapter::InitEnhanceHandler(EnhanceInterfaceType type) @@ -54,13 +58,15 @@ void SecCompEnhanceAdapter::InitEnhanceHandler(EnhanceInterfaceType type) libPath = ENHANCE_SRV_INTERFACE_LIB; isEnhanceSrvHandlerInit = true; break; + case SEC_COMP_ENHANCE_CLIENT_INTERFACE: + libPath = ENHANCE_CLIENT_INTERFACE_LIB; + isEnhanceClientHandlerInit = true; + break; default: break; } if (dlopen(libPath.c_str(), RTLD_LAZY) == nullptr) { - SC_LOG_ERROR(LABEL, "init enhance lib %{public}s failed", libPath.c_str()); - } else { - SC_LOG_INFO(LABEL, "init enhance lib %{public}s ok", libPath.c_str()); + SC_LOG_ERROR(LABEL, "init enhance lib %{public}s failed, error %{public}s", libPath.c_str(), dlerror()); } } @@ -98,6 +104,56 @@ int32_t SecCompEnhanceAdapter::CheckExtraInfo(const SecCompClickEvent& touchInfo return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; } +bool SecCompEnhanceAdapter::EnhanceDataPreprocess(std::string& componentInfo) +{ + if (!isEnhanceClientHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); + } + + uintptr_t enhanceCallerAddr = reinterpret_cast(__builtin_return_address(0)); + if (clientHandler != nullptr) { + return clientHandler->EnhanceDataPreprocess(enhanceCallerAddr, componentInfo); + } + return true; +} + +bool SecCompEnhanceAdapter::EnhanceDataPreprocess(int32_t scId, std::string& componentInfo) +{ + if (!isEnhanceClientHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); + } + + uintptr_t enhanceCallerAddr = reinterpret_cast(__builtin_return_address(0)); + if (clientHandler != nullptr) { + return clientHandler->EnhanceDataPreprocess(enhanceCallerAddr, scId, componentInfo); + } + return true; +} + +void SecCompEnhanceAdapter::RegisterScIdEnhance(int32_t scId) +{ + if (!isEnhanceClientHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); + } + + uintptr_t enhanceCallerAddr = reinterpret_cast(__builtin_return_address(0)); + if (clientHandler != nullptr) { + clientHandler->RegisterScIdEnhance(enhanceCallerAddr, scId); + } +} + +void SecCompEnhanceAdapter::UnregisterScIdEnhance(int32_t scId) +{ + if (!isEnhanceClientHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); + } + + uintptr_t enhanceCallerAddr = reinterpret_cast(__builtin_return_address(0)); + if (clientHandler != nullptr) { + clientHandler->UnregisterScIdEnhance(enhanceCallerAddr, scId); + } +} + int32_t SecCompEnhanceAdapter::EnableInputEnhance() { if (!isEnhanceSrvHandlerInit) { @@ -119,6 +175,60 @@ int32_t SecCompEnhanceAdapter::DisableInputEnhance() } return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; } + +void SecCompEnhanceAdapter::StartEnhanceService() +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + srvHandler->StartEnhanceService(); + } +} + +void SecCompEnhanceAdapter::ExistEnhanceService() +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + srvHandler->ExitEnhanceService(); + } +} + +void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + srvHandler->NotifyProcessDied(pid); + } +} + +int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, + std::shared_ptr& compInfo, const nlohmann::json& jsonComponent) +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + return srvHandler->CheckComponentInfoEnhnace(pid, compInfo, jsonComponent); + } + return SC_OK; +} + +sptr SecCompEnhanceAdapter::GetEnhanceRemoteObject() +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + auto service = srvHandler->GetEnhanceRemoteObject(); + return service; + } + return nullptr; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index d66cee0..e25a068 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -24,6 +24,7 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompBase"}; static const std::string JSON_RECT = "rect"; static const std::string JSON_SC_TYPE = "type"; +static const std::string JSON_NODE_ID = "nodeId"; static const std::string JSON_RECT_X = "x"; static const std::string JSON_RECT_Y = "y"; static const std::string JSON_RECT_WIDTH = "width"; @@ -221,6 +222,12 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) } type_ = static_cast(value); + if ((jsonSrc.find(JSON_NODE_ID) == jsonSrc.end()) || + !jsonSrc.at(JSON_NODE_ID).is_number()) { + return false; + } + nodeId_ = jsonSrc.at(JSON_NODE_ID).get(); + if (!ParseRect(jsonSrc, JSON_RECT, rect_)) { return false; } @@ -252,6 +259,7 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) void SecCompBase::ToJson(nlohmann::json& jsonRes) const { jsonRes[JSON_SC_TYPE] = type_; + jsonRes[JSON_NODE_ID] = nodeId_; jsonRes[JSON_RECT] = nlohmann::json { {JSON_RECT_X, rect_.x_}, {JSON_RECT_Y, rect_.y_}, diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index bab187a..507104a 100644 --- a/interfaces/inner_api/enhance_kits/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("libsecurity_component_enhance_sdk") { external_deps = [ "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core" ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h index 587b5a8..11f46ee 100644 --- a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h +++ b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit.h @@ -21,6 +21,7 @@ namespace OHOS { namespace Security { namespace SecurityComponent { struct SecCompEnhanceKit { + static void InitClientEnhance(); static int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen); static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen); @@ -28,4 +29,4 @@ struct SecCompEnhanceKit { } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // SECURITY_COMPONENT_ENHANCE_KITS_H \ No newline at end of file +#endif // SECURITY_COMPONENT_ENHANCE_KITS_H diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h new file mode 100644 index 0000000..0eebcc8 --- /dev/null +++ b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_ENHANCE_KIT_C_H +#define SECURITY_COMPONENT_ENHANCE_KIT_C_H + +void InitSecCompClientEnhance(); + +#endif // SECURITY_COMPONENT_ENHANCE_KIT_C_H + diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp index 6369c99..35af5bd 100644 --- a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp +++ b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp @@ -15,9 +15,19 @@ #include "sec_comp_enhance_kit.h" #include "sec_comp_enhance_adapter.h" +extern "C" void InitSecCompClientEnhance() +{ + OHOS::Security::SecurityComponent::SecCompEnhanceKit::InitClientEnhance(); +} + namespace OHOS { namespace Security { namespace SecurityComponent { +void SecCompEnhanceKit::InitClientEnhance() +{ + SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); +} + int32_t SecCompEnhanceKit::SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) { return SecCompEnhanceAdapter::SetEnhanceCfg(cfg, cfgLen); diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 4ca2acf..a286b4c 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -47,7 +47,10 @@ ohos_shared_library("libsecurity_component_sdk") { "src/sec_comp_ui_register.cpp" ] - deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_framework" ] + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework" + ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] diff --git a/interfaces/inner_api/security_component/include/i_sec_comp_probe.h b/interfaces/inner_api/security_component/include/i_sec_comp_probe.h new file mode 100644 index 0000000..c83cd99 --- /dev/null +++ b/interfaces/inner_api/security_component/include/i_sec_comp_probe.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 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 I_SECURITY_COMPONENT_PROPE_H +#define I_SECURITY_COMPONENT_PROPE_H + +#include + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class ISecCompProbe { +public: + virtual int32_t GetComponentInfo(int32_t nodeId, std::string& componentInfo) = 0; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // I_SECURITY_COMPONENT_PROPE_H diff --git a/interfaces/inner_api/security_component/include/i_sec_comp_service.h b/interfaces/inner_api/security_component/include/i_sec_comp_service.h index 377a475..b0e69d0 100644 --- a/interfaces/inner_api/security_component/include/i_sec_comp_service.h +++ b/interfaces/inner_api/security_component/include/i_sec_comp_service.h @@ -37,6 +37,7 @@ public: virtual int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) = 0; virtual bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) = 0; + virtual sptr GetEnhanceRemoteObject() = 0; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index 92c743f..87ae211 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_base.h +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -73,6 +73,8 @@ public: int32_t text_ = UNKNOWN_TYPE; int32_t icon_ = UNKNOWN_TYPE; SecCompBackground bg_ = SecCompBackground::UNKNOWN_BG; + + int32_t nodeId_ = 0; protected: virtual bool IsParamValid() = 0; private: diff --git a/interfaces/inner_api/security_component/include/sec_comp_client.h b/interfaces/inner_api/security_component/include/sec_comp_client.h index 0d2773e..a9f5839 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_client.h +++ b/interfaces/inner_api/security_component/include/sec_comp_client.h @@ -36,6 +36,7 @@ public: int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo); bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); + sptr GetEnhanceRemoteObject(bool doLoadSa); void FinishStartSASuccess(const sptr& remoteObject); void FinishStartSAFail(); @@ -46,6 +47,7 @@ private: virtual ~SecCompClient(); DISALLOW_COPY_AND_MOVE(SecCompClient); + bool TryToGetSecCompSa(); bool StartLoadSecCompSa(); void WaitForSecCompSa(); void GetSecCompSa(); diff --git a/interfaces/inner_api/security_component/include/sec_comp_kit.h b/interfaces/inner_api/security_component/include/sec_comp_kit.h index d0ab068..d134307 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_kit.h +++ b/interfaces/inner_api/security_component/include/sec_comp_kit.h @@ -17,6 +17,7 @@ #include #include "accesstoken_kit.h" +#include "iremote_object.h" #include "sec_comp_info.h" #include "sec_comp_ui_register.h" @@ -26,12 +27,13 @@ namespace SecurityComponent { class SecCompKit { public: - static int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId); - static int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); + static int32_t RegisterSecurityComponent(SecCompType type, std::string& componentInfo, int32_t& scId); + static int32_t UpdateSecurityComponent(int32_t scId, std::string& componentInfo); static int32_t UnregisterSecurityComponent(int32_t scId); static int32_t ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo); + std::string& componentInfo, const SecCompClickEvent& touchInfo); static bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); + static sptr GetEnhanceRemoteObject(bool isLoad); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_proxy.h b/interfaces/inner_api/security_component/include/sec_comp_proxy.h index fbea5b3..a2dc220 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_proxy.h +++ b/interfaces/inner_api/security_component/include/sec_comp_proxy.h @@ -32,6 +32,7 @@ public: int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) override; + sptr GetEnhanceRemoteObject() override; private: static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_api/security_component/include/sec_comp_ui_register.h b/interfaces/inner_api/security_component/include/sec_comp_ui_register.h index ab8efc9..c009ed7 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_ui_register.h +++ b/interfaces/inner_api/security_component/include/sec_comp_ui_register.h @@ -17,14 +17,16 @@ #include #include +#include "i_sec_comp_probe.h" namespace OHOS { namespace Security { namespace SecurityComponent { class SecCompUiRegister { public: - SecCompUiRegister(std::vector& callerList); + SecCompUiRegister(std::vector& callerList, ISecCompProbe* probe); virtual ~SecCompUiRegister() = default; + static ISecCompProbe* callbackProbe; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h index ae8d92c..26abf52 100644 --- a/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h +++ b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h @@ -28,6 +28,14 @@ enum SecurityComponentServiceInterfaceCode { GET_SECURITY_COMPONENT_ENHANCE_OBJECT, VERIFY_TEMP_SAVE_PERMISSION, }; + +enum EnhanceInterfaceCode { + REGISTER_CHECK_CALLBACK = 0, +}; + +enum EnhanceCallbackCheckCode { + GET_CALLER_SECURITY_COMPONENT_INFO = 0, +}; } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp index 976870b..351ac3d 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -96,7 +96,7 @@ int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, bool SecCompClient::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) { - auto proxy = GetProxy(true); + auto proxy = GetProxy(false); if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return false; @@ -105,6 +105,17 @@ bool SecCompClient::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID t return proxy->ReduceAfterVerifySavePermission(tokenId); } +sptr SecCompClient::GetEnhanceRemoteObject(bool doLoadSa) +{ + auto proxy = GetProxy(doLoadSa); + if (proxy == nullptr) { + SC_LOG_INFO(LABEL, "Proxy is null"); + return nullptr; + } + + return proxy->GetEnhanceRemoteObject(); +} + bool SecCompClient::StartLoadSecCompSa() { { @@ -116,7 +127,6 @@ bool SecCompClient::StartLoadSecCompSa() SC_LOG_ERROR(LABEL, "GetSystemAbilityManager return null"); return false; } - sptr ptrSecCompLoadCallback = new (std::nothrow) SecCompLoadCallback(); if (ptrSecCompLoadCallback == nullptr) { @@ -134,6 +144,23 @@ bool SecCompClient::StartLoadSecCompSa() return true; } +bool SecCompClient::TryToGetSecCompSa() +{ + auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + SC_LOG_ERROR(LABEL, "GetSystemAbilityManager return null"); + return false; + } + + auto secCompSa = sam->CheckSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE); + if (secCompSa == nullptr) { + SC_LOG_INFO(LABEL, "Get secComp sa return null"); + return false; + } + GetProxyFromRemoteObject(secCompSa); + return true; +} + void SecCompClient::WaitForSecCompSa() { // wait_for release lock and block until time out(1s) or match the condition with notice @@ -223,9 +250,11 @@ sptr SecCompClient::GetProxy(bool doLoadSa) if (proxy_ != nullptr) { return proxy_; } - if (doLoadSa) { - LoadSecCompSa(); + if (TryToGetSecCompSa() || !doLoadSa) { + return proxy_; } + + LoadSecCompSa(); return proxy_; } } // namespace SecurityComponent diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp index 32ce141..03002d2 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -17,6 +17,7 @@ #include "ipc_skeleton.h" #include "sec_comp_caller_authorization.h" #include "sec_comp_client.h" +#include "sec_comp_enhance_adapter.h" #include "sec_comp_log.h" namespace OHOS { @@ -27,7 +28,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ } // namespace int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, - const std::string& componentInfo, int32_t& scId) + std::string& componentInfo, int32_t& scId) { if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { @@ -38,17 +39,25 @@ int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, return SC_SERVICE_ERROR_CALLER_INVALID; } + if (!SecCompEnhanceAdapter::EnhanceDataPreprocess(componentInfo)) { + SC_LOG_ERROR(LABEL, "Preprocess security component fail"); + return SC_ENHANCE_ERROR_VALUE_INVALID; + } + int32_t res = SecCompClient::GetInstance().RegisterSecurityComponent(type, componentInfo, scId); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "register security component fail, error: %{public}d", res); + } else { + SecCompEnhanceAdapter::RegisterScIdEnhance(scId); } + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "REGISTER_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", type); return res; } -int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) +int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, std::string& componentInfo) { if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { @@ -59,6 +68,11 @@ int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, const std::string& com return SC_SERVICE_ERROR_CALLER_INVALID; } + if (!SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo)) { + SC_LOG_ERROR(LABEL, "Preprocess security component fail"); + return SC_ENHANCE_ERROR_VALUE_INVALID; + } + int32_t res = SecCompClient::GetInstance().UpdateSecurityComponent(scId, componentInfo); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "update security component fail, error: %{public}d", res); @@ -68,15 +82,6 @@ int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, const std::string& com int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) { - if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( - reinterpret_cast(__builtin_return_address(0)))) { - SC_LOG_ERROR(LABEL, "unregister security component fail, caller invalid"); - HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED", - HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "UNREGISTER"); - return SC_SERVICE_ERROR_CALLER_INVALID; - } - int32_t res = SecCompClient::GetInstance().UnregisterSecurityComponent(scId); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "unregister security component fail, error: %{public}d", res); @@ -84,11 +89,12 @@ int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "UNREGISTER_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId); + SecCompEnhanceAdapter::UnregisterScIdEnhance(scId); return res; } int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo) + std::string& componentInfo, const SecCompClickEvent& touchInfo) { if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { @@ -99,6 +105,11 @@ int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, return SC_SERVICE_ERROR_CALLER_INVALID; } + if (!SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo)) { + SC_LOG_ERROR(LABEL, "Preprocess security component fail"); + return SC_ENHANCE_ERROR_VALUE_INVALID; + } + int32_t res = SecCompClient::GetInstance().ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo); if (res != SC_OK) { @@ -116,6 +127,11 @@ bool SecCompKit::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID toke } return res; } + +sptr SecCompKit::GetEnhanceRemoteObject(bool isLoad) +{ + return SecCompClient::GetInstance().GetEnhanceRemoteObject(isLoad); +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp index 1737fbb..2c16172 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp @@ -58,7 +58,8 @@ int32_t SecCompProxy::RegisterSecurityComponent(SecCompType type, return SC_SERVICE_ERROR_IPC_REQUEST_FAIL; } int32_t requestResult = remote->SendRequest( - static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option); + static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), + data, reply, option); if (requestResult != SC_OK) { SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); return requestResult; @@ -235,6 +236,35 @@ bool SecCompProxy::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID to } return res; } + +sptr SecCompProxy::GetEnhanceRemoteObject() +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { + SC_LOG_ERROR(LABEL, "Write descriptor fail"); + return nullptr; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + SC_LOG_ERROR(LABEL, "Remote service is null"); + return nullptr; + } + int32_t requestResult = remote->SendRequest( + static_cast(SecurityComponentServiceInterfaceCode::GET_SECURITY_COMPONENT_ENHANCE_OBJECT), + data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "Request fail, result: %{public}d", requestResult); + return nullptr; + } + sptr callback = reply.ReadRemoteObject(); + if (callback == nullptr) { + SC_LOG_ERROR(LABEL, "Read remote object fail"); + } + return callback; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp b/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp index e2a9bf4..966df1a 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp @@ -24,10 +24,13 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompUiRegister"}; } // namespace -SecCompUiRegister::SecCompUiRegister(std::vector& callerList) +ISecCompProbe* SecCompUiRegister::callbackProbe = nullptr; + +SecCompUiRegister::SecCompUiRegister(std::vector& callerList, ISecCompProbe* probe) { SC_LOG_INFO(LABEL, "Init"); SecCompCallerAuthorization::GetInstance().RegisterSecCompKitCaller(callerList); + callbackProbe = probe; } } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 52d7b26..8ab1716 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -29,7 +29,7 @@ static const std::string JSON_STYLE_TAG = "style"; static const std::string JSON_TEXT_TAG = "text"; static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; - +static const std::string JSON_NODE_ID = "nodeId"; static const std::string JSON_RECT = "rect"; static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; @@ -81,6 +81,7 @@ static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) {JSON_RECT_WIDTH, TEST_COORDINATE }, {JSON_RECT_HEIGHT, TEST_COORDINATE } }; + jsonComponent[JSON_NODE_ID] = 0; jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE }, {JSON_RECT_Y, TEST_COORDINATE }, diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index fc293a5..3f9653d 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -29,7 +29,7 @@ static const std::string JSON_STYLE_TAG = "style"; static const std::string JSON_TEXT_TAG = "text"; static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; - +static const std::string JSON_NODE_ID = "nodeId"; static const std::string JSON_RECT = "rect"; static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; @@ -74,6 +74,7 @@ static constexpr int32_t UNKNOWN_ICON = -2; static void BuildPasteComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JSON_SC_TYPE] = PASTE_COMPONENT; + jsonComponent[JSON_NODE_ID] = 0; jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE }, {JSON_RECT_Y, TEST_COORDINATE }, diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index 403e3f6..db23523 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -29,7 +29,7 @@ static const std::string JSON_STYLE_TAG = "style"; static const std::string JSON_TEXT_TAG = "text"; static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; - +static const std::string JSON_NODE_ID = "nodeId"; static const std::string JSON_RECT = "rect"; static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; @@ -74,6 +74,7 @@ static constexpr int32_t UNKNOWN_ICON = -2; static void BuildSaveComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JSON_SC_TYPE] = SAVE_COMPONENT; + jsonComponent[JSON_NODE_ID] = 0; jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, TEST_COORDINATE }, {JSON_RECT_Y, TEST_COORDINATE }, diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 699a523..7e810ba 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -38,30 +38,28 @@ static void TestInCallerNotCheckList() { int32_t scId = -1; struct SecCompClickEvent touch; - int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId); - int updateRes = SecCompKit::UpdateSecurityComponent(scId, ""); - int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch); - int unregistRes = SecCompKit::UnregisterSecurityComponent(scId); + std::string emptyStr = ""; + int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId); + int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr); + int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch); EXPECT_EQ(registerRes, SC_SERVICE_ERROR_CALLER_INVALID); EXPECT_EQ(updateRes, SC_SERVICE_ERROR_CALLER_INVALID); EXPECT_EQ(reportRes, SC_SERVICE_ERROR_CALLER_INVALID); - EXPECT_EQ(unregistRes, SC_SERVICE_ERROR_CALLER_INVALID); } static void TestInCallerCheckList() { int32_t scId = -1; struct SecCompClickEvent touch; - int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, "", scId); - int updateRes = SecCompKit::UpdateSecurityComponent(scId, ""); - int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, "", touch); - int unregistRes = SecCompKit::UnregisterSecurityComponent(scId); + std::string emptyStr = ""; + int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId); + int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr); + int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch); EXPECT_NE(registerRes, SC_SERVICE_ERROR_CALLER_INVALID); EXPECT_NE(updateRes, SC_SERVICE_ERROR_CALLER_INVALID); EXPECT_NE(reportRes, SC_SERVICE_ERROR_CALLER_INVALID); - EXPECT_NE(unregistRes, SC_SERVICE_ERROR_CALLER_INVALID); } } // namespace @@ -115,16 +113,17 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) nlohmann::json jsonRes; comp.ToJson(jsonRes); int32_t scId = -1; - ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonRes.dump(), scId)); + std::string jsonStr = jsonRes.dump(); + ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonStr, scId)); ASSERT_EQ(-1, scId); - ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonRes.dump())); + ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonStr)); struct SecCompClickEvent touch = { .touchX = TEST_COORDINATE, .touchY = TEST_COORDINATE, .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; - EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonRes.dump(), touch)); + EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonStr, touch)); EXPECT_NE(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } @@ -139,7 +138,7 @@ HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1) std::vector callerList = { reinterpret_cast(TestInCallerCheckList), }; - SecCompUiRegister registerCallback(callerList); + SecCompUiRegister registerCallback(callerList, nullptr); TestInCallerCheckList(); TestInCallerNotCheckList(); @@ -147,7 +146,7 @@ HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1) std::vector callerList1 = { reinterpret_cast(TestInCallerNotCheckList), }; - SecCompUiRegister registerCallback1(callerList1); + SecCompUiRegister registerCallback1(callerList1, nullptr); TestInCallerNotCheckList(); SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); SecCompCallerAuthorization::GetInstance().isInit_ = false; @@ -162,7 +161,7 @@ HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1) HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) { std::vector callerList; - SecCompUiRegister registerCallback(callerList); + SecCompUiRegister registerCallback(callerList, nullptr); TestInCallerNotCheckList(); SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); SecCompCallerAuthorization::GetInstance().isInit_ = false; @@ -170,7 +169,7 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) for (size_t i = 0; i < MAX_CALLER_SIZE + 1; i++) { callerList.emplace_back(reinterpret_cast(TestInCallerNotCheckList)); } - SecCompUiRegister registerCallback2(callerList); + SecCompUiRegister registerCallback2(callerList, nullptr); TestInCallerNotCheckList(); SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); SecCompCallerAuthorization::GetInstance().isInit_ = false; diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 86031d6..34381a8 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -36,6 +36,7 @@ ohos_prebuilt_etc("security_component_service.rc") { ohos_shared_library("security_component_service") { subsystem_name = "security" part_name = "security_component" + #output_name = "libsec_comp_service" include_dirs = [ "sa_main", @@ -47,6 +48,7 @@ ohos_shared_library("security_component_service") { sources = [ "sa_main/app_state_observer.cpp", + "sa_main/delay_exit_task.cpp", "sa_main/sec_comp_entity.cpp", "sa_main/sec_comp_info_helper.cpp", "sa_main/sec_comp_manager.cpp", diff --git a/services/security_component_service/sa/sa_main/app_state_observer.cpp b/services/security_component_service/sa/sa_main/app_state_observer.cpp index ede34eb..67cf51f 100644 --- a/services/security_component_service/sa/sa_main/app_state_observer.cpp +++ b/services/security_component_service/sa/sa_main/app_state_observer.cpp @@ -32,23 +32,33 @@ AppStateObserver::~AppStateObserver() { } -bool AppStateObserver::IsProcessForeground(int32_t uid) +bool AppStateObserver::IsProcessForeground(int32_t pid, int32_t uid) { Utils::UniqueReadGuard infoGuard(this->fgProcLock_); for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { - if (uid == iter->uid) { + if (pid == iter->pid) { + return true; + } + + if ((iter->pid == -1) && (uid == iter->uid)) { + iter->pid = pid; return true; } } return false; } -void AppStateObserver::AddProcessToForegroundSet(int32_t uid, const SecCompProcessData& data) +void AppStateObserver::AddProcessToForegroundSet(int32_t pid, const SecCompProcessData& data) { Utils::UniqueWriteGuard infoGuard(this->fgProcLock_); for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { - if (uid == iter->uid) { - SC_LOG_INFO(LABEL, "uid %{public}d is already in foreground", uid); + if (pid == -1) { + if (iter->uid == data.uid) { + SC_LOG_INFO(LABEL, "uid %{public}d is already in foreground", data.uid); + return; + } + } else if (pid == iter->pid) { + SC_LOG_INFO(LABEL, "pid %{public}d is already in foreground", pid); return; } } @@ -62,7 +72,7 @@ void AppStateObserver::AddProcessToForegroundSet(const AppExecFwk::AppStateData& .pid = stateData.pid, .uid = stateData.uid }; - AddProcessToForegroundSet(stateData.uid, proc); + AddProcessToForegroundSet(stateData.pid, proc); } void AppStateObserver::AddProcessToForegroundSet(const AppExecFwk::ProcessData &processData) @@ -72,14 +82,14 @@ void AppStateObserver::AddProcessToForegroundSet(const AppExecFwk::ProcessData & .pid = processData.pid, .uid = processData.uid }; - AddProcessToForegroundSet(processData.uid, proc); + AddProcessToForegroundSet(processData.pid, proc); } void AppStateObserver::RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData &processData) { Utils::UniqueWriteGuard infoGuard(this->fgProcLock_); for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { - if (processData.uid == iter->uid) { + if (processData.pid == iter->pid) { foregrandProcList_.erase(iter); return; } @@ -90,19 +100,19 @@ void AppStateObserver::OnProcessStateChanged(const AppExecFwk::ProcessData &proc { if (processData.state == AppExecFwk::AppProcessState::APP_STATE_FOREGROUND) { AddProcessToForegroundSet(processData); - SecCompManager::GetInstance().NotifyProcessForeground(processData.uid); + SecCompManager::GetInstance().NotifyProcessForeground(processData.pid); } else if (processData.state == AppExecFwk::AppProcessState::APP_STATE_BACKGROUND) { RemoveProcessFromForegroundSet(processData); - SecCompManager::GetInstance().NotifyProcessBackground(processData.uid); + SecCompManager::GetInstance().NotifyProcessBackground(processData.pid); } } void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData& processData) { - SC_LOG_INFO(LABEL, "OnProcessDied die %{public}s uid %{public}d", - processData.bundleName.c_str(), processData.uid); + SC_LOG_INFO(LABEL, "OnProcessDied die %{public}s pid %{public}d", + processData.bundleName.c_str(), processData.pid); RemoveProcessFromForegroundSet(processData); - SecCompManager::GetInstance().NotifyProcessDied(processData.uid); + SecCompManager::GetInstance().NotifyProcessDied(processData.pid); } void AppStateObserver::DumpProcess(std::string& dumpStr) diff --git a/services/security_component_service/sa/sa_main/app_state_observer.h b/services/security_component_service/sa/sa_main/app_state_observer.h index 2ab134d..d3bb959 100644 --- a/services/security_component_service/sa/sa_main/app_state_observer.h +++ b/services/security_component_service/sa/sa_main/app_state_observer.h @@ -16,6 +16,7 @@ #ifndef SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H #define SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H +#include #include #include "app_mgr_interface.h" #include "application_state_observer_stub.h" @@ -38,8 +39,8 @@ public: void OnProcessStateChanged(const AppExecFwk::ProcessData &processData) override; void OnProcessDied(const AppExecFwk::ProcessData& processData) override; - bool IsProcessForeground(int32_t uid); - void AddProcessToForegroundSet(int32_t uid, const SecCompProcessData& data); + bool IsProcessForeground(int32_t pid, int32_t uid); + void AddProcessToForegroundSet(int32_t pid, const SecCompProcessData& data); void AddProcessToForegroundSet(const AppExecFwk::ProcessData &processData); void AddProcessToForegroundSet(const AppExecFwk::AppStateData& stateData); void DumpProcess(std::string& dumpStr); diff --git a/services/security_component_service/sa/sa_main/delay_exit_task.cpp b/services/security_component_service/sa/sa_main/delay_exit_task.cpp new file mode 100644 index 0000000..cdb1633 --- /dev/null +++ b/services/security_component_service/sa/sa_main/delay_exit_task.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 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. + */ + +#include "delay_exit_task.h" + +#include "sec_comp_log.h" +#include "sec_comp_manager.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "DelayExitTask"}; +static const std::string DELAY_EXIT_TASK = "DelayExitTask"; +static const int32_t DELAY_EXIT_MILLISECONDS = 30 * 1000; // 30s +} + +DelayExitTask::DelayExitTask() +{ + SC_LOG_INFO(LABEL, "DelayExitTask()"); +} + +DelayExitTask& DelayExitTask::GetInstance() +{ + static DelayExitTask instance; + return instance; +} + +void DelayExitTask::Init(std::shared_ptr& secHandler) +{ + secHandler_ = secHandler; +} + +void DelayExitTask::Start() +{ + if (secHandler_ == nullptr) { + SC_LOG_ERROR(LABEL, "fail to get EventHandler"); + return; + } + + std::function delayed = ([]() { + SecCompManager::GetInstance().ExitSaProcess(); + }); + + SC_LOG_INFO(LABEL, "Delay exit service after %{public}d ms", DELAY_EXIT_MILLISECONDS); + secHandler_->ProxyPostTask(delayed, DELAY_EXIT_TASK, DELAY_EXIT_MILLISECONDS); +} + +void DelayExitTask::Stop() +{ + if (secHandler_ == nullptr) { + SC_LOG_ERROR(LABEL, "fail to get EventHandler"); + return; + } + + SC_LOG_INFO(LABEL, "Delay exit service stop"); + secHandler_->ProxyRemoveTask(DELAY_EXIT_TASK); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + diff --git a/services/security_component_service/sa/sa_main/delay_exit_task.h b/services/security_component_service/sa/sa_main/delay_exit_task.h new file mode 100644 index 0000000..27a59ca --- /dev/null +++ b/services/security_component_service/sa/sa_main/delay_exit_task.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 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 SECURITY_COMPONENT_DELAY_EXIT_TASK_H +#define SECURITY_COMPONENT_DELAY_EXIT_TASK_H +#include +#include + +#include "nocopyable.h" +#include "sec_event_handler.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class DelayExitTask { +public: + static DelayExitTask& GetInstance(); + virtual ~DelayExitTask() = default; + + void Init(std::shared_ptr& secHandler); + void Start(); + void Stop(); +private: + DelayExitTask(); + std::shared_ptr secHandler_; + + DISALLOW_COPY_AND_MOVE(DelayExitTask); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_DELAY_EXIT_TASK_H + diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.h b/services/security_component_service/sa/sa_main/sec_comp_entity.h index 912ea8a..ef9163c 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.h +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.h @@ -29,7 +29,7 @@ class SecCompEntity { public: SecCompEntity(std::shared_ptr component, AccessToken::AccessTokenID token, int32_t scId) : componentInfo_(component), - tokenId_(token), scId_(scId) {}; + tokenId_(token), scId_(scId){}; ~SecCompEntity() = default; int32_t RevokeTempPermission(); int32_t GrantTempPermission(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 4bb13c8..fae60db 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -15,6 +15,7 @@ #include "sec_comp_manager.h" +#include "delay_exit_task.h" #include "hisysevent.h" #include "i_sec_comp_service.h" #include "ipc_skeleton.h" @@ -68,24 +69,24 @@ int32_t SecCompManager::AddProcessComponent(std::vector& componen return SC_OK; } -int32_t SecCompManager::AddSecurityComponentToList(int32_t uid, const SecCompEntity& newEntity) +int32_t SecCompManager::AddSecurityComponentToList(int32_t pid, const SecCompEntity& newEntity) { OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); - auto iter = componentMap_.find(uid); + auto iter = componentMap_.find(pid); if (iter != componentMap_.end()) { return AddProcessComponent(iter->second, newEntity); } std::vector newComponentList; AddProcessComponent(newComponentList, newEntity); - componentMap_[uid] = newComponentList; + componentMap_[pid] = newComponentList; return SC_OK; } -int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t uid, int32_t scId) +int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t pid, int32_t scId) { OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); - auto iter = componentMap_.find(uid); + auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { SC_LOG_ERROR(LABEL, "Can not find registered process"); return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; @@ -94,6 +95,7 @@ int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t uid, int32_t scI for (auto it = list.begin(); it != list.end(); ++it) { if (it->GetScId() == scId) { it->RevokeTempPermission(); + SC_LOG_INFO(LABEL, "scValidCount %{public}d before delete", scValidCount_); if (it->GetEffective()) { scValidCount_--; } @@ -106,9 +108,62 @@ int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t uid, int32_t scI return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; } -SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t uid, int32_t scId) +bool SecCompManager::IsInMaliciousAppList(int32_t pid) { - auto iter = componentMap_.find(uid); + std::lock_guard lock(maliciousMtx_); + return (maliciousAppList_.find(pid) != maliciousAppList_.end()); +} + +void SecCompManager::AddAppToMaliciousAppList(int32_t pid) +{ + std::lock_guard lock(maliciousMtx_); + maliciousAppList_.insert(pid); +} + +void SecCompManager::RemoveAppFromMaliciousAppList(int32_t pid) +{ + std::lock_guard lock(maliciousMtx_); + maliciousAppList_.erase(pid); +} + +bool SecCompManager::IsMaliciousAppListEmpty() +{ + std::lock_guard lock(maliciousMtx_); + return (maliciousAppList_.size() == 0); +} + +static std::string TransformCallBackResult(enum SCErrCode error) +{ + std::string errMsg = ""; + switch (error) { + case SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE: + errMsg = "enhance do not exist"; + break; + case SC_ENHANCE_ERROR_VALUE_INVALID: + errMsg = "value is invalid"; + break; + case SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST: + errMsg = "callback do not exist"; + break; + case SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL: + errMsg = "callback operate fail"; + break; + case SC_SERVICE_ERROR_COMPONENT_INFO_INVALID: + errMsg = "component info invalid"; + break; + case SC_ENHANCE_ERROR_CALLBACK_CHECK_FAIL: + errMsg = "callback check fail"; + break; + default: + errMsg = "unknown error"; + break; + } + return errMsg; +} + +SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t pid, int32_t scId) +{ + auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { return nullptr; } @@ -121,15 +176,15 @@ SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t uid, int32_t return nullptr; } -void SecCompManager::NotifyProcessForeground(int32_t uid) +void SecCompManager::NotifyProcessForeground(int32_t pid) { OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); - auto iter = componentMap_.find(uid); + auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { return; } - SC_LOG_INFO(LABEL, "App uid %{public}d to foreground", uid); + SC_LOG_INFO(LABEL, "App pid %{public}d to foreground", pid); std::vector& list = iter->second; for (auto it = list.begin(); it != list.end(); ++it) { it->SetEffective(true); @@ -140,15 +195,15 @@ void SecCompManager::NotifyProcessForeground(int32_t uid) } } -void SecCompManager::NotifyProcessBackground(int32_t uid) +void SecCompManager::NotifyProcessBackground(int32_t pid) { OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); - auto iter = componentMap_.find(uid); + auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { return; } - SC_LOG_INFO(LABEL, "App uid %{public}d to background", uid); + SC_LOG_INFO(LABEL, "App pid %{public}d to background", pid); std::vector& list = iter->second; for (auto it = list.begin(); it != list.end(); ++it) { it->RevokeTempPermission(); @@ -160,14 +215,17 @@ void SecCompManager::NotifyProcessBackground(int32_t uid) } } -void SecCompManager::NotifyProcessDied(int32_t uid) +void SecCompManager::NotifyProcessDied(int32_t pid) { + // notify enhance process died. + SecCompEnhanceAdapter::NotifyProcessDied(pid); + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); - auto iter = componentMap_.find(uid); + auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { return; } - SC_LOG_INFO(LABEL, "App uid %{public}d died", uid); + SC_LOG_INFO(LABEL, "App pid %{public}d died", pid); std::vector& list = iter->second; for (auto it = list.begin(); it != list.end(); ++it) { it->RevokeTempPermission(); @@ -176,18 +234,23 @@ void SecCompManager::NotifyProcessDied(int32_t uid) } } list.clear(); + if (scValidCount_ <= 0) { SecCompEnhanceAdapter::DisableInputEnhance(); } - componentMap_.erase(uid); - ExitSaAfterAllProcessDie(); + componentMap_.erase(pid); + RemoveAppFromMaliciousAppList(pid); + DelayExitTask::GetInstance().Start(); } -void SecCompManager::ExitSaAfterAllProcessDie() +void SecCompManager::ExitSaProcess() { - if (!componentMap_.empty()) { + if (!componentMap_.empty() || !IsMaliciousAppListEmpty()) { + SC_LOG_INFO(LABEL, "Apps using security component still exist, no exit sa"); return; } + SecCompEnhanceAdapter::ExistEnhanceService(); + SC_LOG_INFO(LABEL, "All processes using security component died, start sa exit"); auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -205,6 +268,13 @@ void SecCompManager::ExitSaAfterAllProcessDie() int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) { + DelayExitTask::GetInstance().Stop(); + + if (IsInMaliciousAppList(caller.pid)) { + SC_LOG_ERROR(LABEL, "app is in MaliciousAppList, never allow it"); + return SC_ENHANCE_ERROR_IN_MALICIOUS_LIST; + } + SecCompBase* componentPtr = SecCompInfoHelper::ParseComponent(type, jsonComponent); std::shared_ptr component(componentPtr); if (component == nullptr) { @@ -215,10 +285,22 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } + int32_t enhanceRes = + SecCompEnhanceAdapter::CheckComponentInfoEnhnace(caller.pid, component, jsonComponent); + if (enhanceRes != SC_OK) { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLBACK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_TYPE", type, + "CALL_SCENE", "REGITSTER", "REASON", TransformCallBackResult(static_cast(enhanceRes))); + SC_LOG_ERROR(LABEL, "enhance check failed"); + AddAppToMaliciousAppList(caller.pid); + return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + } + int32_t registerId = CreateScId(); SecCompEntity entity(component, caller.tokenId, registerId); entity.SetEffective(true); - int32_t ret = AddSecurityComponentToList(caller.uid, entity); + int32_t ret = AddSecurityComponentToList(caller.pid, entity); if (ret == SC_OK) { scId = registerId; } else { @@ -231,8 +313,13 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller) { + if (IsInMaliciousAppList(caller.pid)) { + SC_LOG_ERROR(LABEL, "app is in MaliciousAppList, never allow it"); + return SC_ENHANCE_ERROR_IN_MALICIOUS_LIST; + } + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); - SecCompEntity* sc = GetSecurityComponentFromList(caller.uid, scId); + SecCompEntity* sc = GetSecurityComponentFromList(caller.pid, scId); if (sc == nullptr) { SC_LOG_ERROR(LABEL, "Can not find target component"); return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; @@ -243,9 +330,24 @@ int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::js SC_LOG_ERROR(LABEL, "Update component info invalid"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "COMPONENT_INFO_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "CLICK", "SC_TYPE", sc->GetType()); + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "UPDATE", + "SC_TYPE", sc->GetType()); return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } + + int32_t enhanceRes = + SecCompEnhanceAdapter::CheckComponentInfoEnhnace(caller.pid, reportComponentInfo, jsonComponent); + if (enhanceRes != SC_OK) { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLBACK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_TYPE", sc->GetType(), + "CALL_SCENE", "UPDATE", "REASON", TransformCallBackResult(static_cast(enhanceRes))); + + SC_LOG_ERROR(LABEL, "enhance check failed"); + AddAppToMaliciousAppList(caller.pid); + return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + } + sc->SetComponentInfo(reportComponentInfo); return SC_OK; } @@ -256,41 +358,73 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC SC_LOG_ERROR(LABEL, "ScId is invalid"); return SC_SERVICE_ERROR_VALUE_INVALID; } - int32_t res = DeleteSecurityComponentFromList(caller.uid, scId); + int32_t res = DeleteSecurityComponentFromList(caller.pid, scId); if (scValidCount_ <= 0) { SecCompEnhanceAdapter::DisableInputEnhance(); } return res; } -int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo) +int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, + const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller) { - OHOS::Utils::UniqueReadGuard lk(this->componentInfoLock_); - SecCompEntity* sc = GetSecurityComponentFromList(caller.uid, scId); - if (sc == nullptr) { - SC_LOG_ERROR(LABEL, "Can not find target component"); - return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; - } - SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); std::shared_ptr reportComponentInfo(report); if ((reportComponentInfo == nullptr) || !reportComponentInfo->GetValid()) { SC_LOG_ERROR(LABEL, "report component info invalid"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "COMPONENT_INFO_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "CLICK", "SC_TYPE", sc->GetType()); + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "CLICK", "SC_TYPE", + sc->GetType()); return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } + int32_t enhanceRes = + SecCompEnhanceAdapter::CheckComponentInfoEnhnace(caller.pid, reportComponentInfo, jsonComponent); + if (enhanceRes != SC_OK) { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLBACK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_TYPE", sc->GetType(), + "CALL_SCENE", "CLICK", "REASON", TransformCallBackResult(static_cast(enhanceRes))); + + SC_LOG_ERROR(LABEL, "enhance check failed"); + AddAppToMaliciousAppList(caller.pid); + return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + } + + sc->SetComponentInfo(reportComponentInfo); + return SC_OK; +} + +int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, + const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo) +{ + if (IsInMaliciousAppList(caller.pid)) { + SC_LOG_ERROR(LABEL, "app is in MaliciousAppList, never allow it"); + return SC_ENHANCE_ERROR_IN_MALICIOUS_LIST; + } + + OHOS::Utils::UniqueReadGuard lk(this->componentInfoLock_); + SecCompEntity* sc = GetSecurityComponentFromList(caller.pid, scId); + if (sc == nullptr) { + SC_LOG_ERROR(LABEL, "Can not find target component"); + return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; + } + int32_t res = CheckClickSecurityComponentInfo(sc, scId, jsonComponent, caller); + if (res != SC_OK) { + return res; + } + if (SecCompEnhanceAdapter::CheckExtraInfo(touchInfo) != SC_OK) { SC_LOG_ERROR(LABEL, "check extra info failed, HMAC is invalid"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CHALLENGE_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType(), "CALL_SCENE", "CLICK"); + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType(), "CALL_SCENE", + "CLICK"); + AddAppToMaliciousAppList(caller.pid); return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; } - sc->SetComponentInfo(reportComponentInfo); + if (!sc->CheckTouchInfo(touchInfo)) { SC_LOG_ERROR(LABEL, "touchInfo is invalid"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", @@ -298,7 +432,7 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; } - int res = sc->GrantTempPermission(); + res = sc->GrantTempPermission(); if (res != SC_OK) { HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "TEMP_GRANT_FAILED", HiviewDFX::HiSysEvent::EventType::FAULT, "CALLER_UID", IPCSkeleton::GetCallingUid(), @@ -321,7 +455,7 @@ bool SecCompManager::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID void SecCompManager::DumpSecComp(std::string& dumpStr) const { for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { - dumpStr.append("uid:" + std::to_string(iter->first) + "\n"); + dumpStr.append("pid:" + std::to_string(iter->first) + "\n"); for (auto compIter = iter->second.begin(); compIter != iter->second.end(); compIter ++) { nlohmann::json json; compIter->GetComponentInfo()->ToJson(json); @@ -334,7 +468,18 @@ void SecCompManager::DumpSecComp(std::string& dumpStr) const bool SecCompManager::Initialize() { SC_LOG_DEBUG(LABEL, "Initialize!!"); - return SecCompPermManager::GetInstance().InitEventHandler(); + SecCompEnhanceAdapter::StartEnhanceService(); + + secRunner_ = AppExecFwk::EventRunner::Create(true); + if (!secRunner_) { + SC_LOG_ERROR(LABEL, "failed to create a recvRunner."); + return false; + } + + secHandler_ = std::make_shared(secRunner_); + DelayExitTask::GetInstance().Init(secHandler_); + + return SecCompPermManager::GetInstance().InitEventHandler(secHandler_); } } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index 501094a..c9f4857 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -28,6 +29,7 @@ #include "sec_comp_base.h" #include "sec_comp_entity.h" #include "sec_comp_info.h" +#include "sec_event_handler.h" namespace OHOS { namespace Security { @@ -51,21 +53,26 @@ public: int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo); bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); - void NotifyProcessForeground(int32_t uid); - void NotifyProcessBackground(int32_t uid); - void NotifyProcessDied(int32_t uid); + void NotifyProcessForeground(int32_t pid); + void NotifyProcessBackground(int32_t pid); + void NotifyProcessDied(int32_t pid); void DumpSecComp(std::string& dumpStr) const; bool Initialize(); + void ExitSaProcess(); private: SecCompManager(); int32_t AddProcessComponent(std::vector& componentList, const SecCompEntity& newEntity); - int32_t AddSecurityComponentToList(int32_t uid, const SecCompEntity& newEntity); - int32_t DeleteSecurityComponentFromList(int32_t uid, int32_t scId); - SecCompEntity* GetSecurityComponentFromList(int32_t uid, int32_t scId); - void ExitSaAfterAllProcessDie(); - + int32_t AddSecurityComponentToList(int32_t pid, const SecCompEntity& newEntity); + int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId); + SecCompEntity* GetSecurityComponentFromList(int32_t pid, int32_t scId); + int32_t CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, + const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller); + bool IsInMaliciousAppList(int32_t pid); + void AddAppToMaliciousAppList(int32_t pid); + void RemoveAppFromMaliciousAppList(int32_t pid); + bool IsMaliciousAppListEmpty(); int32_t CreateScId(); OHOS::Utils::RWLock componentInfoLock_; @@ -73,6 +80,12 @@ private: std::unordered_map> componentMap_; int32_t scValidCount_; int32_t scIdStart_; + + std::shared_ptr secRunner_; + std::shared_ptr secHandler_; + std::set maliciousAppList_; // pid set + std::mutex maliciousMtx_; + DISALLOW_COPY_AND_MOVE(SecCompManager); }; } // namespace SecurityComponent diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index e42f730..384d62e 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -120,15 +120,9 @@ int32_t SecCompPermManager::RevokeTempSavePermission(AccessToken::AccessTokenID return SC_OK; } -bool SecCompPermManager::InitEventHandler() +bool SecCompPermManager::InitEventHandler(std::shared_ptr& secHandler) { - secRunner_ = AppExecFwk::EventRunner::Create(true); - if (!secRunner_) { - SC_LOG_ERROR(LABEL, "failed to create a recvRunner."); - return false; - } - - secHandler_ = std::make_shared(secRunner_); + secHandler_ = secHandler; return true; } diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h index 314b0d1..440d419 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h @@ -41,7 +41,7 @@ public: int32_t GrantTempSavePermission(AccessToken::AccessTokenID tokenId); int32_t RevokeTempSavePermission(AccessToken::AccessTokenID tokenId); - bool InitEventHandler(); + bool InitEventHandler(std::shared_ptr& secHandler); std::shared_ptr GetSecEventHandler() const; private: @@ -51,7 +51,6 @@ private: std::unordered_map applySaveCountMap_; std::deque taskQue_; std::mutex mutex_; - std::shared_ptr secRunner_; std::shared_ptr secHandler_; }; } // namespace SecurityComponent diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index ced7ca5..100cee8 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -20,6 +20,7 @@ #include "hitrace_meter.h" #include "ipc_skeleton.h" #include "iservice_registry.h" +#include "sec_comp_enhance_adapter.h" #include "sec_comp_err.h" #include "sec_comp_manager.h" #include "sec_comp_log.h" @@ -60,6 +61,12 @@ void SecCompService::OnStart() FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return; } + if (!Initialize()) { + SC_LOG_ERROR(LABEL, "Failed to initialize"); + FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + return; + } + state_ = ServiceRunningState::STATE_RUNNING; bool ret = Publish(this); if (!ret) { @@ -67,11 +74,6 @@ void SecCompService::OnStart() FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return; } - if (!Initialize()) { - SC_LOG_ERROR(LABEL, "Failed to initialize"); - FinishTrace(HITRACE_TAG_ACCESS_CONTROL); - return; - } HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "SERVICE_INIT_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "PID", getpid()); SC_LOG_INFO(LABEL, "Congratulations, SecCompService start successfully!"); @@ -144,8 +146,8 @@ bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) SC_LOG_ERROR(LABEL, "Get caller tokenId invalid"); return false; } - if ((uid != ROOT_UID) && (!appStateObserver_->IsProcessForeground(uid))) { - SC_LOG_ERROR(LABEL, "caller uid is not in foreground"); + if ((uid != ROOT_UID) && (!appStateObserver_->IsProcessForeground(pid, uid))) { + SC_LOG_ERROR(LABEL, "caller pid is not in foreground"); return false; } caller.tokenId = tokenId; @@ -231,6 +233,11 @@ bool SecCompService::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID return SecCompManager::GetInstance().ReduceAfterVerifySavePermission(tokenId); } +sptr SecCompService::GetEnhanceRemoteObject() +{ + return SecCompEnhanceAdapter::GetEnhanceRemoteObject(); +} + int SecCompService::Dump(int fd, const std::vector& args) { if (fd < 0) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.h b/services/security_component_service/sa/sa_main/sec_comp_service.h index 8b347e0..c9ac178 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.h +++ b/services/security_component_service/sa/sa_main/sec_comp_service.h @@ -46,6 +46,8 @@ public: int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) override; + sptr GetEnhanceRemoteObject() override; + int Dump(int fd, const std::vector& args) override; private: diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index 688118e..47ec4d1 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -182,6 +182,16 @@ int32_t SecCompStub::ReduceAfterVerifySavePermissionInner(MessageParcel& data, M return SC_OK; } +int32_t SecCompStub::GetEnhanceRemoteObjectInner(MessageParcel& data, MessageParcel& reply) +{ + auto res = this->GetEnhanceRemoteObject(); + if (!reply.WriteRemoteObject(res)) { + SC_LOG_ERROR(LABEL, "Register security component enhance remote object fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return SC_OK; +} + SecCompStub::SecCompStub() { requestFuncMap_[static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT)] = @@ -195,6 +205,9 @@ SecCompStub::SecCompStub() &SecCompStub::ReportSecurityComponentClickEventInner; requestFuncMap_[static_cast(SecurityComponentServiceInterfaceCode::VERIFY_TEMP_SAVE_PERMISSION)] = &SecCompStub::ReduceAfterVerifySavePermissionInner; + requestFuncMap_[static_cast( + SecurityComponentServiceInterfaceCode::GET_SECURITY_COMPONENT_ENHANCE_OBJECT)] = + &SecCompStub::GetEnhanceRemoteObjectInner; } SecCompStub::~SecCompStub() diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.h b/services/security_component_service/sa/sa_main/sec_comp_stub.h index 9f7205a..c435876 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.h +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.h @@ -36,6 +36,8 @@ private: int32_t UnregisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply); int32_t ReportSecurityComponentClickEventInner(MessageParcel& data, MessageParcel& reply); int32_t ReduceAfterVerifySavePermissionInner(MessageParcel& data, MessageParcel& reply); + int32_t GetEnhanceRemoteObjectInner(MessageParcel& data, MessageParcel& reply); + using RequestFuncType = int32_t (SecCompStub::*)(MessageParcel& data, MessageParcel& reply); std::map requestFuncMap_; }; diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 1ce1872..db60cd4 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -33,6 +33,7 @@ ohos_unittest("sec_comp_service_test") { sources = [ "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", @@ -80,6 +81,7 @@ ohos_unittest("sec_comp_service_mock_test") { include_dirs = [ "${sec_comp_root_dir}/frameworks/common/include", "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/include", @@ -88,6 +90,7 @@ ohos_unittest("sec_comp_service_mock_test") { sources = [ "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", diff --git a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp index a441245..7c5538e 100644 --- a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp +++ b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp @@ -54,6 +54,56 @@ int32_t SecCompEnhanceAdapter::DisableInputEnhance() SC_LOG_DEBUG(LABEL, "DisableInputEnhance success"); return SC_OK; } + +bool SecCompEnhanceAdapter::EnhanceDataPreprocess(std::string& componentInfo) +{ + SC_LOG_DEBUG(LABEL, "EnhanceDataPreprocess success"); + return true; +} + +bool SecCompEnhanceAdapter::EnhanceDataPreprocess(int32_t scId, std::string& componentInfo) +{ + SC_LOG_DEBUG(LABEL, "EnhanceDataPreprocess success"); + return true; +} + +void SecCompEnhanceAdapter::RegisterScIdEnhance(int32_t scId) +{ + SC_LOG_DEBUG(LABEL, "RegisterScIdEnhance success"); +} + +void SecCompEnhanceAdapter::UnregisterScIdEnhance(int32_t scId) +{ + SC_LOG_DEBUG(LABEL, "UnregisterScIdEnhance success"); +} + +void SecCompEnhanceAdapter::StartEnhanceService() +{ + SC_LOG_DEBUG(LABEL, "StartEnhanceService success"); +} + +void SecCompEnhanceAdapter::ExistEnhanceService() +{ + SC_LOG_DEBUG(LABEL, "ExistEnhanceService success"); +} + +void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) +{ + SC_LOG_DEBUG(LABEL, "NotifyProcessDied success"); +} + +int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, + std::shared_ptr& compInfo, const nlohmann::json& jsonComponent) +{ + SC_LOG_DEBUG(LABEL, "CheckComponentInfoEnhnace success"); + return SC_OK; +} + +sptr SecCompEnhanceAdapter::GetEnhanceRemoteObject() +{ + SC_LOG_DEBUG(LABEL, "GetEnhanceRemoteObject success"); + return nullptr; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp index 420015b..4923cdb 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -26,6 +26,8 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static constexpr int32_t TEST_UID = 1; static constexpr int32_t TEST_UID_2 = 2; +static constexpr int32_t TEST_PID = 1; +static constexpr int32_t TEST_PID_2 = 2; } void AppStateObserverTest::SetUpTestCase() @@ -58,12 +60,13 @@ void AppStateObserverTest::TearDown() */ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) { - ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); AppExecFwk::AppStateData stateData = { - .uid = TEST_UID + .pid = TEST_PID, + .uid = TEST_UID, }; observer_->AddProcessToForegroundSet(stateData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); } /** @@ -75,18 +78,20 @@ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) { AppExecFwk::AppStateData stateData = { - .uid = TEST_UID + .pid = TEST_PID, + .uid = TEST_UID, }; observer_->AddProcessToForegroundSet(stateData); observer_->AddProcessToForegroundSet(stateData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); AppExecFwk::ProcessData procData = { - .uid = TEST_UID_2 + .pid = TEST_PID_2, + .uid = TEST_UID_2, }; observer_->AddProcessToForegroundSet(procData); observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID_2)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_2, TEST_UID_2)); } /** @@ -98,13 +103,14 @@ HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level1) { AppExecFwk::ProcessData procData = { - .uid = TEST_UID + .pid = TEST_PID, + .uid = TEST_UID, }; observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); observer_->RemoveProcessFromForegroundSet(procData); observer_->RemoveProcessFromForegroundSet(procData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); } /** @@ -116,13 +122,14 @@ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet002, TestSize.Level1) { AppExecFwk::ProcessData procData = { - .uid = TEST_UID + .pid = TEST_PID, + .uid = TEST_UID, }; observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); - procData.uid = TEST_UID_2; + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + procData.pid = TEST_PID_2; observer_->RemoveProcessFromForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); } /** @@ -139,14 +146,14 @@ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) ASSERT_EQ(observer_->foregrandProcList_.size(), static_cast(0)); processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; - processData.uid = TEST_UID; + processData.pid = TEST_PID; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); processData.state = AppExecFwk::AppProcessState::APP_STATE_BACKGROUND; - processData.uid = TEST_UID; + processData.pid = TEST_PID; observer_->OnProcessStateChanged(processData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); } /** @@ -159,17 +166,17 @@ HWTEST_F(AppStateObserverTest, OnProcessDied001, TestSize.Level1) { AppExecFwk::ProcessData processData; processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; - processData.uid = TEST_UID; + processData.pid = TEST_PID; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); // if last process died, the sec_comp service will exit. - processData.uid = TEST_UID_2; + processData.pid = TEST_PID_2; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_UID_2)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_2, TEST_UID_2)); // notify process 1 died - processData.uid = TEST_UID; + processData.pid = TEST_PID; observer_->OnProcessDied(processData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 12d1bcd..05c7933 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -34,7 +34,7 @@ static const std::string JSON_STYLE_TAG = "style"; static const std::string JSON_TEXT_TAG = "text"; static const std::string JSON_ICON_TAG = "icon"; static const std::string JSON_BG_TAG = "bg"; - +static const std::string JSON_NODE_ID = "nodeId"; static const std::string JSON_RECT = "rect"; static const std::string JSON_WINDOW_RECT = "windowRect"; static const std::string JSON_SC_TYPE = "type"; @@ -102,6 +102,7 @@ static bool GetScreenSize() static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_NODE_ID] = 0; jsonComponent[JSON_RECT] = nlohmann::json { {JSON_RECT_X, g_testWidth }, {JSON_RECT_Y, g_testHeight }, diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 974a900..0964136 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -36,9 +36,9 @@ static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; static constexpr uint32_t TEST_COLOR_RED = 0xff0000; static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; -static constexpr int32_t TEST_UID_1 = 1; -static constexpr int32_t TEST_UID_2 = 2; -static constexpr int32_t TEST_UID_3 = 3; +static constexpr int32_t TEST_PID_1 = 1; +static constexpr int32_t TEST_PID_2 = 2; +static constexpr int32_t TEST_PID_3 = 3; static constexpr int32_t UNKNOWN_TEXT = -2; static constexpr int32_t UNKNOWN_ICON = -2; @@ -197,7 +197,7 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) */ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1) { - ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), + ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); std::shared_ptr compPtr = std::make_shared(); @@ -207,12 +207,12 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); - ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_2), + ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); - ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), SC_OK); } /** @@ -223,7 +223,7 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 */ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) { - ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); @@ -232,9 +232,9 @@ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); - ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_2), nullptr); - ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2), nullptr); + ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); } /** @@ -245,7 +245,7 @@ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) */ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) { - SecCompManager::GetInstance().NotifyProcessBackground(TEST_UID_1); + SecCompManager::GetInstance().NotifyProcessBackground(TEST_PID_1); std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(compPtr, nullptr); @@ -254,13 +254,13 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); - auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); + auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1); ASSERT_NE(component, nullptr); - SecCompManager::GetInstance().NotifyProcessForeground(TEST_UID_1); + SecCompManager::GetInstance().NotifyProcessForeground(TEST_PID_1); ASSERT_TRUE(component->isEffective_); - SecCompManager::GetInstance().NotifyProcessBackground(TEST_UID_1); + SecCompManager::GetInstance().NotifyProcessBackground(TEST_PID_1); ASSERT_NE(component, nullptr); ASSERT_FALSE(component->isEffective_); } @@ -280,7 +280,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_1, entity), SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); std::shared_ptr compPtr2 = std::make_shared(); ASSERT_NE(compPtr2, nullptr); @@ -289,13 +289,13 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) compPtr2->rect_.width_ = TEST_COORDINATE; compPtr2->rect_.height_ = TEST_COORDINATE; SecCompEntity entity2(compPtr2, TEST_TOKEN_ID, TEST_SC_ID_2); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_UID_2, entity2), SC_OK); + ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_2, entity2), SC_OK); - SecCompManager::GetInstance().NotifyProcessDied(TEST_UID_3); - ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + SecCompManager::GetInstance().NotifyProcessDied(TEST_PID_3); + ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); - SecCompManager::GetInstance().NotifyProcessDied(TEST_UID_1); - ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_UID_1, TEST_SC_ID_1), nullptr); + SecCompManager::GetInstance().NotifyProcessDied(TEST_PID_1); + ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); } /** @@ -312,7 +312,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) int32_t scId; SecCompCallerInfo caller = { .tokenId = TEST_TOKEN_ID, - .uid = TEST_UID_1 + .pid = TEST_PID_1 }; ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId), SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); @@ -321,8 +321,10 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); + // callback check failed ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent( - LOCATION_COMPONENT, jsonValid, caller, scId), SC_OK); + LOCATION_COMPONENT, jsonValid, caller, scId), SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); + SecCompManager::GetInstance().maliciousAppList_.clear(); } /** @@ -338,19 +340,10 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) buttonValid.ToJson(jsonValid); SecCompCallerInfo caller = { .tokenId = TEST_TOKEN_ID, - .uid = TEST_UID_1 + .pid = TEST_PID_1 }; ASSERT_EQ(SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); - - int32_t scId; - ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId), - SC_OK); - nlohmann::json jsonInvalid; - LocationButton buttonInvalid = BuildInvalidLocationComponent(); - buttonInvalid.ToJson(jsonInvalid); - ASSERT_EQ(SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonInvalid, caller), - SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); } /** @@ -363,7 +356,7 @@ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) { SecCompCallerInfo caller = { .tokenId = TEST_TOKEN_ID, - .uid = TEST_UID_1 + .pid = TEST_PID_1 }; ASSERT_EQ(SecCompManager::GetInstance().UnregisterSecurityComponent(TEST_INVALID_SC_ID, caller), @@ -380,7 +373,7 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve { SecCompCallerInfo caller = { .tokenId = TEST_TOKEN_ID, - .uid = TEST_UID_1 + .pid = TEST_PID_1 }; SecCompClickEvent touchInfo; nlohmann::json jsonVaild; @@ -388,23 +381,4 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve buttonValid.ToJson(jsonVaild); ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); - - int32_t scId; - ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonVaild, caller, scId), - SC_OK); - - nlohmann::json jsonInvalid; - LocationButton buttonInvalid = BuildInvalidLocationComponent(); - buttonInvalid.ToJson(jsonInvalid); - - ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonInvalid, caller, touchInfo), - SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); - - ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonInvalid, caller, touchInfo), - SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); - - touchInfo.touchX = 500.0; // not hit component - touchInfo.touchY = 500.0; // not hit component - ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonVaild, caller, touchInfo), - SC_SERVICE_ERROR_CLICK_EVENT_INVALID); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp index 97522c7..d29720d 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp @@ -72,7 +72,7 @@ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) data, reply, option), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); data.WriteInterfaceToken(u"ohos.security.ISecCompService"); - ASSERT_EQ(stub_->OnRemoteRequest(0, data, reply, option), 305); + ASSERT_EQ(stub_->OnRemoteRequest(1000, data, reply, option), 305); } /** diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h index 23e7000..2516cbf 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h @@ -54,6 +54,11 @@ public: { return true; } + + sptr GetEnhanceRemoteObject() override + { + return nullptr; + } }; class SecCompStubTest : public testing::Test { -- Gitee From 84137f0b046374ab584cff10dc1643f630c9587b Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 3 Jul 2023 21:11:41 +0800 Subject: [PATCH 34/59] Description:modify ut fail Match-id-cce59188a3d2813bd404c60ed0c53635d8fc1c8e --- .../sa/test/unittest/src/sec_comp_service_test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 40cd10f..8f5984a 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -289,7 +289,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve // register security component ok EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), - SC_OK); + SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); uint8_t data[16] = { 0 }; struct SecCompClickEvent touch = { .touchX = 100, @@ -300,6 +300,6 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve .extraInfo.dataSize = 16, }; EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), - SC_SERVICE_ERROR_CLICK_EVENT_INVALID); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); + SC_ENHANCE_ERROR_IN_MALICIOUS_LIST); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); } -- Gitee From e7cb97efc535d17d72dbce36528ea82d6eb6247a Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 7 Jul 2023 21:16:02 +0800 Subject: [PATCH 35/59] Description:add ut Match-id-8d9f77b32845bee5bb19ceb871a492f34d95d3ec --- frameworks/common/include/sec_comp_err.h | 1 + .../include/sec_comp_enhance_kit_c.h | 2 +- .../enhance_kits/src/sec_comp_enhance_kit.cpp | 2 +- .../security_component/test/BUILD.gn | 138 +++++++++++++++- .../src/location_button_sample_build.cpp | 110 +++++++++++++ .../src/location_button_sample_build.h | 31 ++++ .../src/sec_comp_register_callback_test.cpp | 155 ++++++++++++++++++ .../src/sec_comp_register_challenge_test.cpp | 111 +++++++++++++ ...ec_comp_register_without_callback_test.cpp | 113 +++++++++++++ ...ec_comp_report_click_without_hmac_test.cpp | 117 +++++++++++++ .../sa/sa_main/sec_comp_manager.cpp | 45 ++--- .../sa/sa_main/sec_comp_manager.h | 2 + .../unittest/src/sec_comp_manager_test.cpp | 10 +- .../unittest/src/sec_comp_service_test.cpp | 3 +- 14 files changed, 810 insertions(+), 30 deletions(-) create mode 100644 interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp diff --git a/frameworks/common/include/sec_comp_err.h b/frameworks/common/include/sec_comp_err.h index ca1d4f6..0b93da4 100644 --- a/frameworks/common/include/sec_comp_err.h +++ b/frameworks/common/include/sec_comp_err.h @@ -45,6 +45,7 @@ enum SCErrCode : int32_t { SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL = -107, SC_ENHANCE_ERROR_CALLBACK_CHECK_FAIL = -108, SC_ENHANCE_ERROR_IN_MALICIOUS_LIST = -109, + SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL = -110 }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h index 0eebcc8..c7e0c6f 100644 --- a/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h +++ b/interfaces/inner_api/enhance_kits/include/sec_comp_enhance_kit_c.h @@ -15,7 +15,7 @@ #ifndef SECURITY_COMPONENT_ENHANCE_KIT_C_H #define SECURITY_COMPONENT_ENHANCE_KIT_C_H -void InitSecCompClientEnhance(); +void InitSecCompClientEnhance(void); #endif // SECURITY_COMPONENT_ENHANCE_KIT_C_H diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp index 35af5bd..dabdcf7 100644 --- a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp +++ b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp @@ -23,7 +23,7 @@ extern "C" void InitSecCompClientEnhance() namespace OHOS { namespace Security { namespace SecurityComponent { -void SecCompEnhanceKit::InitClientEnhance() +void SecCompEnhanceKit::InitClientEnhance(void) { SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); } diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index 4f88a7a..0aabcdc 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -49,7 +49,143 @@ ohos_unittest("sec_comp_sdk_test") { ] } +ohos_unittest("sec_comp_register_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] + + sources = [ + "unittest/src/location_button_sample_build.cpp", + "unittest/src/sec_comp_register_callback_test.cpp", + ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + ] +} + +ohos_unittest("sec_comp_report_click_without_hmac_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] + + sources = [ + "unittest/src/location_button_sample_build.cpp", + "unittest/src/sec_comp_report_click_without_hmac_test.cpp", + ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + ] +} + +ohos_unittest("sec_comp_register_without_callback_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] + + sources = [ + "unittest/src/location_button_sample_build.cpp", + "unittest/src/sec_comp_register_without_callback_test.cpp", + ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + ] +} + +ohos_unittest("sec_comp_register_challenge_test") { + subsystem_name = "security" + part_name = "security_component" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", + "${sec_comp_root_dir}/frameworks/security_component/include", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + ] + + sources = [ + "unittest/src/location_button_sample_build.cpp", + "unittest/src/sec_comp_register_challenge_test.cpp", + ] + configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] + cflags_cc = [ "-DHILOG_ENABLE" ] + + deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", + "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + ] +} + group("unittest") { testonly = true - deps = [ ":sec_comp_sdk_test" ] + deps = [ + ":sec_comp_sdk_test", + ":sec_comp_register_challenge_test", + ":sec_comp_register_test", + ":sec_comp_register_without_callback_test", + ":sec_comp_report_click_without_hmac_test" + ] } diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp new file mode 100644 index 0000000..c0fdde0 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 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. + */ +#include "location_button_sample_build.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +static const std::string JSON_STYLE_TAG = "style"; +static const std::string JSON_TEXT_TAG = "text"; +static const std::string JSON_ICON_TAG = "icon"; +static const std::string JSON_BG_TAG = "bg"; +static const std::string JSON_NODE_ID = "nodeId"; +static const std::string JSON_RECT = "rect"; +static const std::string JSON_WINDOW_RECT = "windowRect"; +static const std::string JSON_SC_TYPE = "type"; +static const std::string JSON_RECT_X = "x"; +static const std::string JSON_RECT_Y = "y"; +static const std::string JSON_RECT_WIDTH = "width"; +static const std::string JSON_RECT_HEIGHT = "height"; +static const std::string JSON_SIZE_TAG = "size"; +static const std::string JSON_FONT_SIZE_TAG = "fontSize"; +static const std::string JSON_ICON_SIZE_TAG = "iconSize"; +static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; +static const std::string JSON_PADDING_LEFT_TAG = "left"; +static const std::string JSON_PADDING_TOP_TAG = "top"; +static const std::string JSON_PADDING_RIGHT_TAG = "right"; +static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; +static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; +static const std::string JSON_RECT_WIDTH_TAG = "width"; +static const std::string JSON_RECT_HEIGHT_TAG = "height"; +static const std::string JSON_INVALID_TAG = "test"; +static const std::string JSON_COLORS_TAG = "colors"; +static const std::string JSON_FONT_COLOR_TAG = "fontColor"; +static const std::string JSON_ICON_COLOR_TAG = "iconColor"; +static const std::string JSON_BG_COLOR_TAG = "bgColor"; +static const std::string JSON_BORDER_TAG = "border"; +static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; +static const std::string JSON_PARENT_TAG = "parent"; +static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; +static const std::string WRONG_TYPE = "wrongType"; +static constexpr float TEST_SIZE = 100.0; +static constexpr double TEST_COORDINATE = 100.0; +static constexpr double TEST_DIMENSION = 100.0; +static constexpr uint32_t TEST_COLOR_YELLOW = 0xffffff00; +static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; + +void LocationButtonSampleBuild::BuildLocationComponentInfo(nlohmann::json& jsonComponent) +{ + jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JSON_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JSON_NODE_ID] = 0; + jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { + {JSON_RECT_X, TEST_COORDINATE }, + {JSON_RECT_Y, TEST_COORDINATE }, + {JSON_RECT_WIDTH, TEST_COORDINATE }, + {JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JSON_SIZE_TAG] = nlohmann::json { + { JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JSON_COLORS_TAG] = nlohmann::json { + { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + + jsonComponent[JSON_BORDER_TAG] = nlohmann::json { + { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JSON_PARENT_TAG] = nlohmann::json { + { JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JSON_STYLE_TAG] = nlohmann::json { + { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h b/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h new file mode 100644 index 0000000..9bde892 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 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 LOCATION_BUTTON_SAMPLE_BUILD_H +#define LOCATION_BUTTON_SAMPLE_BUILD_H +#include "location_button.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { + +class LocationButtonSampleBuild { +public: + static void BuildLocationComponentInfo(nlohmann::json& jsonComponent); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // LOCATION_BUTTON_SAMPLE_BUILD_H diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp new file mode 100644 index 0000000..ad8f922 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -0,0 +1,155 @@ +/* + * Copyright (c) 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. + */ +#include + +#include "i_sec_comp_probe.h" +#include "location_button.h" +#include "location_button_sample_build.h" +#define private public +#include "sec_comp_caller_authorization.h" +#undef private +#include "sec_comp_err.h" +#include "sec_comp_info.h" +#include "sec_comp_kit.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" +#include "sec_comp_ui_register.h" + +using namespace testing::ext; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterCallbackTest"}; + +class MockUiSecCompProbe : public ISecCompProbe { +public: + int32_t GetComponentInfo(int32_t nodeId, std::string& componentInfo) override + { + componentInfo = mockComponentInfo; + return mockRes; + } + std::string mockComponentInfo; + int32_t mockRes; +}; + +static MockUiSecCompProbe g_probe; +static void InitUiRegister() +{ + std::vector callerList; + SecCompUiRegister registerCallback(callerList, &g_probe); + SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); + SecCompCallerAuthorization::GetInstance().isInit_ = false; +} +} // namespace + +class SecCompRegisterCallbackTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; + +void SecCompRegisterCallbackTest::SetUpTestCase() +{ + InitUiRegister(); + SC_LOG_INFO(LABEL, "SecCompRegisterCallbackTest."); +} + +void SecCompRegisterCallbackTest::TearDownTestCase() +{ + SC_LOG_INFO(LABEL, "SecCompRegisterCallbackTest."); +} + +void SecCompRegisterCallbackTest::SetUp() +{ + SC_LOG_INFO(LABEL, "SetUp ok."); +} + +void SecCompRegisterCallbackTest::TearDown() +{ + SC_LOG_INFO(LABEL, "TearDown."); +} + +/** + * @tc.name: Register001 + * @tc.desc: test register security component success. + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterCallbackTest, Register001, TestSize.Level1) +{ + SC_LOG_INFO(LABEL, "Register001."); + nlohmann::json jsonRes; + LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + g_probe.mockComponentInfo = locationInfo; + g_probe.mockRes = 0; + + int32_t scId; + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_NE(-1, scId); + + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); +} + +/** + * @tc.name: Register002 + * @tc.desc: test register callback failed. + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) +{ + SC_LOG_INFO(LABEL, "Register002."); + + nlohmann::json jsonRes; + LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + g_probe.mockComponentInfo = locationInfo; + g_probe.mockRes = -1; + + int32_t scId; + ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL, + SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(-1, scId); +} + +/** + * @tc.name: Register003 + * @tc.desc: test register in MaliciousAppList. + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) +{ + SC_LOG_INFO(LABEL, "Register003."); + + nlohmann::json jsonRes; + LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + g_probe.mockComponentInfo = locationInfo; + g_probe.mockRes = 0; + + int32_t scId; + ASSERT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, + SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(-1, scId); +} + diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp new file mode 100644 index 0000000..c7e0273 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 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. + */ +#include + +#include "i_sec_comp_probe.h" +#include "location_button.h" +#include "location_button_sample_build.h" +#define private public +#include "sec_comp_caller_authorization.h" +#undef private +#include "sec_comp_client.h" +#include "sec_comp_enhance_adapter.h" +#include "sec_comp_err.h" +#include "sec_comp_info.h" +#include "sec_comp_kit.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" +#include "sec_comp_ui_register.h" + +using namespace testing::ext; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterChallengeTest"}; + +class MockUiSecCompProbe : public ISecCompProbe { +public: + int32_t GetComponentInfo(int32_t nodeId, std::string& componentInfo) override + { + componentInfo = mockComponentInfo; + return mockRes; + } + std::string mockComponentInfo; + int32_t mockRes; +}; + +static MockUiSecCompProbe g_probe; + +static void InitUiRegister() +{ + std::vector callerList; + SecCompUiRegister registerCallback(callerList, &g_probe); + SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); + SecCompCallerAuthorization::GetInstance().isInit_ = false; +} +} // namespace + +class SecCompRegisterChallengeTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; + +void SecCompRegisterChallengeTest::SetUpTestCase() +{ + InitUiRegister(); + SC_LOG_INFO(LABEL, "SecCompRegisterChallengeTest."); +} + +void SecCompRegisterChallengeTest::TearDownTestCase() +{ + SC_LOG_INFO(LABEL, "SecCompRegisterChallengeTest."); +} + +void SecCompRegisterChallengeTest::SetUp() +{ + SC_LOG_INFO(LABEL, "SetUp ok."); +} + +void SecCompRegisterChallengeTest::TearDown() +{ + SC_LOG_INFO(LABEL, "TearDown."); +} + +/** + * @tc.name: RegisterWithoutPreprocess001 + * @tc.desc: test register without preprocess + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterChallengeTest, RegisterWithoutPreprocess001, TestSize.Level1) +{ + nlohmann::json jsonRes; + LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + + SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); + int32_t scId; + ASSERT_EQ(SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL, + SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(-1, scId); +} + diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp new file mode 100644 index 0000000..ddb319b --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 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. + */ +#include + +#include "i_sec_comp_probe.h" +#include "location_button.h" +#include "location_button_sample_build.h" +#define private public +#include "sec_comp_caller_authorization.h" +#undef private +#include "sec_comp_err.h" +#include "sec_comp_info.h" +#include "sec_comp_kit.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" +#include "sec_comp_ui_register.h" + +using namespace testing::ext; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterWithoutCallbackTest"}; + +static void InitUiRegister() +{ + std::vector callerList; + SecCompUiRegister registerCallback(callerList, nullptr); + SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); + SecCompCallerAuthorization::GetInstance().isInit_ = false; +} +} // namespace + +class SecCompRegisterWithoutCallbackTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; + +void SecCompRegisterWithoutCallbackTest::SetUpTestCase() +{ + InitUiRegister(); + SC_LOG_INFO(LABEL, "SecCompRegisterWithoutCallbackTest."); +} + +void SecCompRegisterWithoutCallbackTest::TearDownTestCase() +{ + SC_LOG_INFO(LABEL, "SecCompRegisterWithoutCallbackTest."); +} + +void SecCompRegisterWithoutCallbackTest::SetUp() +{ + SC_LOG_INFO(LABEL, "SetUp ok."); +} + +void SecCompRegisterWithoutCallbackTest::TearDown() +{ + SC_LOG_INFO(LABEL, "TearDown."); +} + +/** + * @tc.name: RegisterWithoutCallback001 + * @tc.desc: test register without callback + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback001, TestSize.Level1) +{ + nlohmann::json jsonRes; + LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + + int32_t scId; + ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, + SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(-1, scId); +} + +/** + * @tc.name: RegisterWithoutCallback002 + * @tc.desc: test register in MaliciousAppList. + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback002, TestSize.Level1) +{ + nlohmann::json jsonRes; + LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + + int32_t scId; + ASSERT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, + SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(-1, scId); +} + diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp new file mode 100644 index 0000000..aed9ef8 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 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. + */ +#include + +#include "i_sec_comp_probe.h" +#include "location_button.h" +#include "location_button_sample_build.h" +#define private public +#include "sec_comp_caller_authorization.h" +#undef private +#include "sec_comp_err.h" +#include "sec_comp_info.h" +#include "sec_comp_kit.h" +#include "sec_comp_log.h" +#include "sec_comp_tool.h" +#include "sec_comp_ui_register.h" + +using namespace testing::ext; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompReportClickWithoutHmacTest"}; +static constexpr double TEST_COORDINATE = 100.0; + +class MockUiSecCompProbe : public ISecCompProbe { +public: + int32_t GetComponentInfo(int32_t nodeId, std::string& componentInfo) override + { + componentInfo = mockComponentInfo; + return mockRes; + } + std::string mockComponentInfo; + int32_t mockRes; +}; + +static MockUiSecCompProbe g_probe; +static void InitUiRegister() +{ + std::vector callerList; + SecCompUiRegister registerCallback(callerList, &g_probe); + SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); + SecCompCallerAuthorization::GetInstance().isInit_ = false; +} +} // namespace + +class SecCompReportClickWithoutHmacTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; + +void SecCompReportClickWithoutHmacTest::SetUpTestCase() +{ + InitUiRegister(); + SC_LOG_INFO(LABEL, "SecCompReportClickWithoutHmacTest."); +} + +void SecCompReportClickWithoutHmacTest::TearDownTestCase() +{ + SC_LOG_INFO(LABEL, "SecCompReportClickWithoutHmacTest."); +} + +void SecCompReportClickWithoutHmacTest::SetUp() +{ + SC_LOG_INFO(LABEL, "SetUp ok."); +} + +void SecCompReportClickWithoutHmacTest::TearDown() +{ + SC_LOG_INFO(LABEL, "TearDown."); +} + +/** + * @tc.name: ReportClickWithoutHmac001 + * @tc.desc: test report click event without hmac data + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompReportClickWithoutHmacTest, ReportClickWithoutHmac001, TestSize.Level1) +{ + nlohmann::json jsonRes; + LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + g_probe.mockComponentInfo = locationInfo; + g_probe.mockRes = 0; + + int32_t scId; + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_NE(-1, scId); + + struct SecCompClickEvent touch = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) + }; + EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch)); +} + diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index fae60db..ec4e634 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -265,6 +265,22 @@ void SecCompManager::ExitSaProcess() SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); } +void SecCompManager::SendCheckInfoEnhanceSysEvent(int32_t scId, + SecCompType type, const std::string& scene, int32_t res) +{ + if (res == SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL) { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CHALLENGE_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", type, "CALL_SCENE", + scene); + } else { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLBACK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_TYPE", type, + "CALL_SCENE", scene, "REASON", TransformCallBackResult(static_cast(res))); + } +} + int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) { @@ -288,13 +304,10 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, int32_t enhanceRes = SecCompEnhanceAdapter::CheckComponentInfoEnhnace(caller.pid, component, jsonComponent); if (enhanceRes != SC_OK) { - HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLBACK_FAILED", - HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_TYPE", type, - "CALL_SCENE", "REGITSTER", "REASON", TransformCallBackResult(static_cast(enhanceRes))); + SendCheckInfoEnhanceSysEvent(INVALID_SC_ID, type, "REGISTER", enhanceRes); SC_LOG_ERROR(LABEL, "enhance check failed"); AddAppToMaliciousAppList(caller.pid); - return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + return enhanceRes; } int32_t registerId = CreateScId(); @@ -338,14 +351,10 @@ int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::js int32_t enhanceRes = SecCompEnhanceAdapter::CheckComponentInfoEnhnace(caller.pid, reportComponentInfo, jsonComponent); if (enhanceRes != SC_OK) { - HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLBACK_FAILED", - HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_TYPE", sc->GetType(), - "CALL_SCENE", "UPDATE", "REASON", TransformCallBackResult(static_cast(enhanceRes))); - + SendCheckInfoEnhanceSysEvent(scId, sc->GetType(), "UPDATE", enhanceRes); SC_LOG_ERROR(LABEL, "enhance check failed"); AddAppToMaliciousAppList(caller.pid); - return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + return enhanceRes; } sc->SetComponentInfo(reportComponentInfo); @@ -382,14 +391,10 @@ int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32 int32_t enhanceRes = SecCompEnhanceAdapter::CheckComponentInfoEnhnace(caller.pid, reportComponentInfo, jsonComponent); if (enhanceRes != SC_OK) { - HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLBACK_FAILED", - HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_TYPE", sc->GetType(), - "CALL_SCENE", "CLICK", "REASON", TransformCallBackResult(static_cast(enhanceRes))); - + SendCheckInfoEnhanceSysEvent(scId, sc->GetType(), "CLICK", enhanceRes); SC_LOG_ERROR(LABEL, "enhance check failed"); AddAppToMaliciousAppList(caller.pid); - return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + return enhanceRes; } sc->SetComponentInfo(reportComponentInfo); @@ -417,10 +422,9 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, if (SecCompEnhanceAdapter::CheckExtraInfo(touchInfo) != SC_OK) { SC_LOG_ERROR(LABEL, "check extra info failed, HMAC is invalid"); - HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CHALLENGE_CHECK_FAILED", + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType(), "CALL_SCENE", - "CLICK"); + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); AddAppToMaliciousAppList(caller.pid); return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; } @@ -437,6 +441,7 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "TEMP_GRANT_FAILED", HiviewDFX::HiSysEvent::EventType::FAULT, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); + return res; } HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "TEMP_GRANT_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index c9f4857..fefc92d 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -73,6 +73,8 @@ private: void AddAppToMaliciousAppList(int32_t pid); void RemoveAppFromMaliciousAppList(int32_t pid); bool IsMaliciousAppListEmpty(); + void SendCheckInfoEnhanceSysEvent(int32_t scId, + SecCompType type, const std::string& scene, int32_t res); int32_t CreateScId(); OHOS::Utils::RWLock componentInfoLock_; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 0964136..a4f46e2 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -323,7 +323,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) // callback check failed ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent( - LOCATION_COMPONENT, jsonValid, caller, scId), SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); + LOCATION_COMPONENT, jsonValid, caller, scId), SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST); SecCompManager::GetInstance().maliciousAppList_.clear(); } @@ -342,8 +342,8 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) .tokenId = TEST_TOKEN_ID, .pid = TEST_PID_1 }; - ASSERT_EQ(SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller), - SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + ASSERT_NE(SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller), + SC_OK); } /** @@ -379,6 +379,6 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve nlohmann::json jsonVaild; LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); - ASSERT_EQ(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo), - SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + ASSERT_NE(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo), + SC_OK); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 8f5984a..6b00d5f 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -287,9 +287,8 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - // register security component ok EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), - SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); + SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST); uint8_t data[16] = { 0 }; struct SecCompClickEvent touch = { .touchX = 100, -- Gitee From fbfe52cfef07af1aa64b8244902b619aeef8d0ca Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 12 Jul 2023 16:07:06 +0800 Subject: [PATCH 36/59] =?UTF-8?q?Description:=20=E6=B7=BB=E5=8A=A0log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-8cf7c93aa5a6aa13f83ca66707aaac325f23f799 --- .../sa/sa_main/sec_comp_entity.cpp | 13 +++++++++++++ .../sa/sa_main/sec_comp_manager.cpp | 15 ++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index 03977d5..58d6291 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -14,7 +14,10 @@ */ #include "sec_comp_entity.h" #include +#include "hisysevent.h" +#include "ipc_skeleton.h" #include "sec_comp_err.h" +#include "sec_comp_enhance_adapter.h" #include "sec_comp_info_helper.h" #include "sec_comp_log.h" @@ -62,6 +65,16 @@ bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const SC_LOG_ERROR(LABEL, "touch point is not in component rect"); return false; } + + if (SecCompEnhanceAdapter::CheckExtraInfo(touchInfo) != SC_OK) { + SC_LOG_ERROR(LABEL, "HMAC checkout failed" + "touchX:%{public}f, touchY:%{public}f, timestamp:%{public}lu, dataSize:%{public}d", + touchInfo.touchX, touchInfo.touchY, touchInfo.timestamp, touchInfo.extraInfo.dataSize); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId_, "SC_TYPE", componentInfo_->type_); + return false; + } return true; } } // namespace SecurityComponent diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index ec4e634..312ed23 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -285,7 +285,7 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) { DelayExitTask::GetInstance().Stop(); - + SC_LOG_DEBUG(LABEL, "PID: %{public}d, register security component", caller.pid); if (IsInMaliciousAppList(caller.pid)) { SC_LOG_ERROR(LABEL, "app is in MaliciousAppList, never allow it"); return SC_ENHANCE_ERROR_IN_MALICIOUS_LIST; @@ -326,6 +326,7 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller) { + SC_LOG_DEBUG(LABEL, "PID: %{public}d, update security component", caller.pid); if (IsInMaliciousAppList(caller.pid)) { SC_LOG_ERROR(LABEL, "app is in MaliciousAppList, never allow it"); return SC_ENHANCE_ERROR_IN_MALICIOUS_LIST; @@ -363,6 +364,7 @@ int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::js int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller) { + SC_LOG_DEBUG(LABEL, "PID: %{public}d, unregister security component", caller.pid); if (scId < 0) { SC_LOG_ERROR(LABEL, "ScId is invalid"); return SC_SERVICE_ERROR_VALUE_INVALID; @@ -420,20 +422,11 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, return res; } - if (SecCompEnhanceAdapter::CheckExtraInfo(touchInfo) != SC_OK) { - SC_LOG_ERROR(LABEL, "check extra info failed, HMAC is invalid"); - HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", - HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); - AddAppToMaliciousAppList(caller.pid); - return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; - } - if (!sc->CheckTouchInfo(touchInfo)) { - SC_LOG_ERROR(LABEL, "touchInfo is invalid"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); + AddAppToMaliciousAppList(caller.pid); return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; } res = sc->GrantTempPermission(); -- Gitee From 63a40ec331e00f92532e121a85324ece40a84fb8 Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 13 Jul 2023 10:40:32 +0800 Subject: [PATCH 37/59] =?UTF-8?q?Description:=E3=80=90private01=E3=80=91?= =?UTF-8?q?=E3=80=90base/security/security=5Fcomponent=E3=80=91graphic=5Fs?= =?UTF-8?q?tandard=E6=94=B9=E4=B8=BAgraphic=5F2d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-21dbda5e9818ca17ff41c66a15b0e13701f9399b --- bundle.json | 2 +- services/security_component_service/sa/BUILD.gn | 2 +- services/security_component_service/sa/test/BUILD.gn | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bundle.json b/bundle.json index dd40314..2a0e191 100644 --- a/bundle.json +++ b/bundle.json @@ -31,7 +31,7 @@ "hilog", "hisysevent", "hitrace", - "graphic_standard", + "graphic_2d", "ipc", "safwk", "samgr", diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 34381a8..42bf08d 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -75,7 +75,7 @@ ohos_shared_library("security_component_service") { "access_token:libaccesstoken_sdk", "c_utils:utils", "eventhandler:libeventhandler", - "graphic_standard:librender_service_client", + "graphic_2d:librender_service_client", "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index db60cd4..ee8dc5c 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -64,7 +64,7 @@ ohos_unittest("sec_comp_service_test") { "access_token:libtoken_setproc", "c_utils:utils", "eventhandler:libeventhandler", - "graphic_standard:librender_service_client", + "graphic_2d:librender_service_client", "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", @@ -116,7 +116,7 @@ ohos_unittest("sec_comp_service_mock_test") { "access_token:libtoken_setproc", "c_utils:utils", "eventhandler:libeventhandler", - "graphic_standard:librender_service_client", + "graphic_2d:librender_service_client", "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", -- Gitee From a6841c7f487bacbbdd16c3ba711418d5842a193c Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 17 Jul 2023 20:31:17 +0800 Subject: [PATCH 38/59] Description: version warnning Match-id-3397a80f15608a1716c445be212a67bc05364cfc --- .../security_component_service/sa/sa_main/delay_exit_task.cpp | 2 +- .../security_component_service/sa/sa_main/delay_exit_task.h | 2 +- .../sa/sa_main/sec_comp_perm_manager.cpp | 2 +- .../sa/sa_main/sec_comp_perm_manager.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/security_component_service/sa/sa_main/delay_exit_task.cpp b/services/security_component_service/sa/sa_main/delay_exit_task.cpp index cdb1633..bca167f 100644 --- a/services/security_component_service/sa/sa_main/delay_exit_task.cpp +++ b/services/security_component_service/sa/sa_main/delay_exit_task.cpp @@ -38,7 +38,7 @@ DelayExitTask& DelayExitTask::GetInstance() return instance; } -void DelayExitTask::Init(std::shared_ptr& secHandler) +void DelayExitTask::Init(const std::shared_ptr& secHandler) { secHandler_ = secHandler; } diff --git a/services/security_component_service/sa/sa_main/delay_exit_task.h b/services/security_component_service/sa/sa_main/delay_exit_task.h index 27a59ca..3d4da21 100644 --- a/services/security_component_service/sa/sa_main/delay_exit_task.h +++ b/services/security_component_service/sa/sa_main/delay_exit_task.h @@ -29,7 +29,7 @@ public: static DelayExitTask& GetInstance(); virtual ~DelayExitTask() = default; - void Init(std::shared_ptr& secHandler); + void Init(const std::shared_ptr& secHandler); void Start(); void Stop(); private: diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index 384d62e..46bcf9e 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -120,7 +120,7 @@ int32_t SecCompPermManager::RevokeTempSavePermission(AccessToken::AccessTokenID return SC_OK; } -bool SecCompPermManager::InitEventHandler(std::shared_ptr& secHandler) +bool SecCompPermManager::InitEventHandler(const std::shared_ptr& secHandler) { secHandler_ = secHandler; return true; diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h index 440d419..91bab7d 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h @@ -41,7 +41,7 @@ public: int32_t GrantTempSavePermission(AccessToken::AccessTokenID tokenId); int32_t RevokeTempSavePermission(AccessToken::AccessTokenID tokenId); - bool InitEventHandler(std::shared_ptr& secHandler); + bool InitEventHandler(const std::shared_ptr& secHandler); std::shared_ptr GetSecEventHandler() const; private: -- Gitee From 8233d07fec3ede9226c46e7d81967b24ebdf89ed Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 18 Jul 2023 12:01:53 +0800 Subject: [PATCH 39/59] =?UTF-8?q?Description:=20=E4=BF=AE=E5=A4=8Dtdd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-b4a94d7ab2a95b004790c9e84107188bf989f563 --- README_zh.md | 53 +++++++++++++++++++ .../src/sec_comp_register_callback_test.cpp | 4 +- ...ec_comp_register_without_callback_test.cpp | 2 +- .../security_component_service/sa/BUILD.gn | 1 - .../sa/sa_main/sec_comp_manager.cpp | 4 ++ .../unittest/src/sec_comp_entity_test.cpp | 2 +- .../unittest/src/sec_comp_service_test.cpp | 3 ++ 7 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 README_zh.md diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000..45921cd --- /dev/null +++ b/README_zh.md @@ -0,0 +1,53 @@ +# 安全控件管理服务 + + - [简介](#简介) + - [目录](#目录) + - [使用](#使用) + - [接口说明](#接口说明) + - [相关仓](#相关仓) + +## 简介 + +安全控件是OpenHarmony提供的一组系统实现的ArkUI基础组件。当用户点击了这类组件后,应用将被授予临时授权,应用开发者仅需要像使用Button等组件那样集成到应用自己的页面即可。 +安全控件管理服务主要提供如下功能: + +- 提供应用安全控件的注册、临时授权、取消注册等Native接口实现。 +- 负责安全控件的合法性检查,只有合法的安全控件才能授权成功。 +- 提供其他SA查询临时授权的Native接口实现。提供安全增强相关接口及适配层,供厂商定制安全增强功能使用。 + +## 目录 + +``` +/base/security/security_component +├── frameworks # 框架层,基础功能代码存放目录 +│ ├── common # 框架公共代码存放目录 +│ ├── enhance_adapter # 能力增强适配代码存放目录 +│ └── security_component # 安全控件服务框架代码存放目录 +├── interfaces # 接口层 +│ └── inner_api # 内部接口层 +│ ├── enhance_kits # 安全控件配置接口代码存放目录 +│ └── security_component # 安全控件服务接口层代码存放目录 +└── services # 服务层 + └── security_component_service/sa + └── sa_main # 安全控件服务侧代码存放目录 + +``` + +## 使用 +### 接口说明 + +| **接口申明** | **接口描述** | +| --- | --- | +| int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId); | 注册安全控件 | +| int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); | 更新安全控件信息 | +| int32_t UnregisterSecurityComponent(int32_t scId); | 取消注册安全控件 | +| int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo); | 上报点击事件,申请临时授权 | +| int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); | 设置安全控件增强的配置,供多模服务使用 | +| int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen); | 获取点击事件的安全增强数据,供多模服务使用 | +| bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); | 校验后取消保存控件权限 | + +## 相关仓 + +**[arkui\_ace\_engine](https://gitee.com/openharmony/arkui_ace_engine/blob/master/README_zh.md)** + +**[multimodalinput\_input](https://gitee.com/openharmony/multimodalinput_input/blob/master/README_zh.md)** diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index ad8f922..e907048 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -148,8 +148,6 @@ HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) g_probe.mockRes = 0; int32_t scId; - ASSERT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, - SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); - ASSERT_EQ(-1, scId); + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp index ddb319b..7c78cc8 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp @@ -106,7 +106,7 @@ HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback002, TestSiz std::string locationInfo = jsonRes.dump(); int32_t scId; - ASSERT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, + ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_EQ(-1, scId); } diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 42bf08d..ced309c 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -36,7 +36,6 @@ ohos_prebuilt_etc("security_component_service.rc") { ohos_shared_library("security_component_service") { subsystem_name = "security" part_name = "security_component" - #output_name = "libsec_comp_service" include_dirs = [ "sa_main", diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 312ed23..968ff06 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -32,6 +32,7 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompManager"}; static constexpr int32_t SC_ID_START = 1000; static constexpr int32_t MAX_INT_NUM = 0x7fffffff; +static constexpr int32_t ROOT_UID = 0; } SecCompManager::SecCompManager() @@ -111,6 +112,9 @@ int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t pid, int32_t scI bool SecCompManager::IsInMaliciousAppList(int32_t pid) { std::lock_guard lock(maliciousMtx_); + if (IPCSkeleton::GetCallingUid() == ROOT_UID) { + return false; + } return (maliciousAppList_.find(pid) != maliciousAppList_.end()); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 4ade7ed..66d2831 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -153,5 +153,5 @@ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) entity_->componentInfo_->rect_.y_ = TEST_COORDINATE; touch.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_TRUE(entity_->CheckTouchInfo(touch)); + ASSERT_FALSE(entity_->CheckTouchInfo(touch)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 6b00d5f..e9eb39f 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -275,6 +275,7 @@ HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) */ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { + auto uid = getuid(); // get caller fail int32_t scId; EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId), @@ -282,6 +283,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve // parse component json fail ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + setuid(100); AppExecFwk::AppStateData stateData = { .uid = getuid() }; @@ -301,4 +303,5 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), SC_ENHANCE_ERROR_IN_MALICIOUS_LIST); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + setuid(uid); } -- Gitee From 10fc48c4347beccffecfd122932d4e213506b6e3 Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 21 Jul 2023 13:19:22 +0800 Subject: [PATCH 40/59] =?UTF-8?q?Description:=20=E6=B7=BB=E5=8A=A0license?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-506cfc30a2f52b7180ff187e3552951e62413741 --- LICENSE | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4a45986 --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file -- Gitee From 3d478c2ec480ef6141be0084d85a1aff283706e8 Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 21 Jul 2023 16:48:44 +0800 Subject: [PATCH 41/59] =?UTF-8?q?Description:=20=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=8E=A7=E4=BB=B6=E5=B1=9E=E6=80=A7=E5=90=8D?= =?UTF-8?q?=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match-id-7d7e526be99e90c9983c756dc098ee958dc29b73 --- .../inner_api/security_component/include/save_button.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h index ee1e46e..fc7951e 100644 --- a/interfaces/inner_api/security_component/include/save_button.h +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -27,10 +27,10 @@ enum class SaveDesc : int32_t { UNKNOWN_TEXT = -2, NO_TEXT = -1, DOWNLOAD = 0, - DOWNLOAD_FILES, + DOWNLOAD_FILE, SAVE, - SAVE_IMAGES, - SAVE_FILES, + SAVE_IMAGE, + SAVE_FILE, DOWNLOAD_AND_SHARE, RECEIVE, CONTINUE_TO_RECEIVE, -- Gitee From b202e9be39f34ebe98afb8baa2e222f3aaa64770 Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 19 Jul 2023 21:20:32 +0800 Subject: [PATCH 42/59] Description:add first use dialog Match-id-0efa0fe1fe90bf316c11c48040f84834a2d2ce22 --- bundle.json | 1 + frameworks/common/src/sec_comp_tool.cpp | 2 +- .../include/i_sec_comp_service.h | 2 +- .../include/sec_comp_client.h | 2 +- .../security_component/include/sec_comp_kit.h | 2 +- .../include/sec_comp_proxy.h | 2 +- .../src/sec_comp_client.cpp | 4 +- .../security_component/src/sec_comp_kit.cpp | 4 +- .../security_component/src/sec_comp_proxy.cpp | 7 +- .../test/unittest/src/sec_comp_kit_test.cpp | 6 +- ...ec_comp_report_click_without_hmac_test.cpp | 2 +- .../security_component_service/sa/BUILD.gn | 5 + .../sa/sa_main/first_use_dialog.cpp | 299 ++++++++++++++++++ .../sa/sa_main/first_use_dialog.h | 65 ++++ .../sa/sa_main/sec_comp_entity.h | 11 - .../sa/sa_main/sec_comp_info_helper.cpp | 12 +- .../sa/sa_main/sec_comp_manager.cpp | 103 +++--- .../sa/sa_main/sec_comp_manager.h | 18 +- .../sa/sa_main/sec_comp_service.cpp | 6 +- .../sa/sa_main/sec_comp_service.h | 2 +- .../sa/sa_main/sec_comp_stub.cpp | 4 +- .../sa/security_component_service.cfg | 7 + .../sa/test/BUILD.gn | 10 + .../sa/test/mock/include/access_token.h | 9 +- .../sa/test/mock/include/accesstoken_kit.h | 8 + .../unittest/src/sec_comp_manager_test.cpp | 28 +- .../src/sec_comp_service_mock_test.cpp | 31 +- .../unittest/src/sec_comp_service_test.cpp | 2 +- .../sa/test/unittest/src/sec_comp_stub_test.h | 2 +- 29 files changed, 521 insertions(+), 135 deletions(-) create mode 100644 services/security_component_service/sa/sa_main/first_use_dialog.cpp create mode 100644 services/security_component_service/sa/sa_main/first_use_dialog.h diff --git a/bundle.json b/bundle.json index 2a0e191..0a68dd1 100644 --- a/bundle.json +++ b/bundle.json @@ -24,6 +24,7 @@ "ram": "5102KB", "deps": { "components": [ + "ability_base", "ability_runtime", "access_token", "c_utils", diff --git a/frameworks/common/src/sec_comp_tool.cpp b/frameworks/common/src/sec_comp_tool.cpp index 79a0f1d..d92771d 100644 --- a/frameworks/common/src/sec_comp_tool.cpp +++ b/frameworks/common/src/sec_comp_tool.cpp @@ -134,7 +134,7 @@ bool IsColorSimilar(const SecCompColor& color1, const SecCompColor& color2) bool IsColorTransparent(const SecCompColor& color) { - return ((color.argb.alpha != 0) && (color.argb.alpha < MAX_TRANSPARENT)); + return color.argb.alpha < MAX_TRANSPARENT; } } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/i_sec_comp_service.h b/interfaces/inner_api/security_component/include/i_sec_comp_service.h index b0e69d0..0b6d2e7 100644 --- a/interfaces/inner_api/security_component/include/i_sec_comp_service.h +++ b/interfaces/inner_api/security_component/include/i_sec_comp_service.h @@ -35,7 +35,7 @@ public: virtual int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) = 0; virtual int32_t UnregisterSecurityComponent(int32_t scId) = 0; virtual int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, - const SecCompClickEvent& touchInfo) = 0; + const SecCompClickEvent& touchInfo, sptr callerToken) = 0; virtual bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) = 0; virtual sptr GetEnhanceRemoteObject() = 0; }; diff --git a/interfaces/inner_api/security_component/include/sec_comp_client.h b/interfaces/inner_api/security_component/include/sec_comp_client.h index a9f5839..954fe11 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_client.h +++ b/interfaces/inner_api/security_component/include/sec_comp_client.h @@ -34,7 +34,7 @@ public: int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); int32_t UnregisterSecurityComponent(int32_t scId); int32_t ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo); + const std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken); bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); sptr GetEnhanceRemoteObject(bool doLoadSa); diff --git a/interfaces/inner_api/security_component/include/sec_comp_kit.h b/interfaces/inner_api/security_component/include/sec_comp_kit.h index d134307..1129c9f 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_kit.h +++ b/interfaces/inner_api/security_component/include/sec_comp_kit.h @@ -31,7 +31,7 @@ public: static int32_t UpdateSecurityComponent(int32_t scId, std::string& componentInfo); static int32_t UnregisterSecurityComponent(int32_t scId); static int32_t ReportSecurityComponentClickEvent(int32_t scId, - std::string& componentInfo, const SecCompClickEvent& touchInfo); + std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken = nullptr); static bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); static sptr GetEnhanceRemoteObject(bool isLoad); }; diff --git a/interfaces/inner_api/security_component/include/sec_comp_proxy.h b/interfaces/inner_api/security_component/include/sec_comp_proxy.h index a2dc220..729877b 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_proxy.h +++ b/interfaces/inner_api/security_component/include/sec_comp_proxy.h @@ -30,7 +30,7 @@ public: int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) override; int32_t UnregisterSecurityComponent(int32_t scId) override; int32_t ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; + const std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken) override; bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) override; sptr GetEnhanceRemoteObject() override; diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp index 351ac3d..6e9be79 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -83,7 +83,7 @@ int32_t SecCompClient::UnregisterSecurityComponent(int32_t scId) } int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo) + const std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken) { auto proxy = GetProxy(true); if (proxy == nullptr) { @@ -91,7 +91,7 @@ int32_t SecCompClient::ReportSecurityComponentClickEvent(int32_t scId, return SC_SERVICE_ERROR_VALUE_INVALID; } - return proxy->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo); + return proxy->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo, callerToken); } bool SecCompClient::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp index 03002d2..eda6bd7 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -94,7 +94,7 @@ int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) } int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, - std::string& componentInfo, const SecCompClickEvent& touchInfo) + std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken) { if (!SecCompCallerAuthorization::GetInstance().IsKitCaller( reinterpret_cast(__builtin_return_address(0)))) { @@ -111,7 +111,7 @@ int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId, } int32_t res = - SecCompClient::GetInstance().ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo); + SecCompClient::GetInstance().ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo, callerToken); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "report click event fail, error: %{public}d", res); } diff --git a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp index 2c16172..47b7824 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp @@ -151,7 +151,7 @@ int32_t SecCompProxy::UnregisterSecurityComponent(int32_t scId) } int32_t SecCompProxy::ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo) + const std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken) { MessageParcel data; if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { @@ -180,6 +180,11 @@ int32_t SecCompProxy::ReportSecurityComponentClickEvent(int32_t scId, return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; } + if ((callerToken != nullptr) && !data.WriteRemoteObject(callerToken)) { + SC_LOG_ERROR(LABEL, "Write caller token fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); sptr remote = Remote(); diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 7e810ba..c43a610 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -41,7 +41,7 @@ static void TestInCallerNotCheckList() std::string emptyStr = ""; int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId); int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr); - int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch); + int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch, nullptr); EXPECT_EQ(registerRes, SC_SERVICE_ERROR_CALLER_INVALID); EXPECT_EQ(updateRes, SC_SERVICE_ERROR_CALLER_INVALID); @@ -55,7 +55,7 @@ static void TestInCallerCheckList() std::string emptyStr = ""; int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId); int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr); - int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch); + int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch, nullptr); EXPECT_NE(registerRes, SC_SERVICE_ERROR_CALLER_INVALID); EXPECT_NE(updateRes, SC_SERVICE_ERROR_CALLER_INVALID); @@ -123,7 +123,7 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) .touchY = TEST_COORDINATE, .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; - EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonStr, touch)); + EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonStr, touch, nullptr)); EXPECT_NE(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp index aed9ef8..6a388f9 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp @@ -112,6 +112,6 @@ HWTEST_F(SecCompReportClickWithoutHmacTest, ReportClickWithoutHmac001, TestSize. .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, - SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch)); + SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); } diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index ced309c..3703091 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -48,6 +48,7 @@ ohos_shared_library("security_component_service") { sources = [ "sa_main/app_state_observer.cpp", "sa_main/delay_exit_task.cpp", + "sa_main/first_use_dialog.cpp", "sa_main/sec_comp_entity.cpp", "sa_main/sec_comp_info_helper.cpp", "sa_main/sec_comp_manager.cpp", @@ -70,7 +71,11 @@ ohos_shared_library("security_component_service") { public_configs = [ ":security_component_service_config" ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_runtime:ability_manager", "ability_runtime:app_manager", + "ability_runtime:runtime", "access_token:libaccesstoken_sdk", "c_utils:utils", "eventhandler:libeventhandler", diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp new file mode 100644 index 0000000..076d739 --- /dev/null +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -0,0 +1,299 @@ +/* + * Copyright (c) 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. + */ + +#include "first_use_dialog.h" + +#include +#include +#include +#include +#include + +#include "ability_manager_client.h" +#include "accesstoken_kit.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include "want_params_wrapper.h" +#include "want.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "FirstUseDialog"}; +static const std::string SEC_COMP_SRV_CFG_PATH = "/data/service/el1/public/security_component_service"; +static const std::string FIRST_USE_RECORD_JSON = SEC_COMP_SRV_CFG_PATH + "/first_use_record.json"; +static const std::string FIRST_USE_RECORD_TAG = "FirstUseRecord"; +static const std::string TOKEN_ID_TAG = "TokenId"; +static const std::string COMP_TYPE_TAG = "CompType"; + +const std::string GRANT_ABILITY_BUNDLE_NAME = "com.ohos.permissionmanager"; +const std::string GRANT_ABILITY_ABILITY_NAME = "com.ohos.permissionmanager.SecurityExtAbility"; +const std::string TYPE_KEY = "ohos.user.security.type"; +const std::string TOKEN_KEY = "ohos.ability.params.token"; + +constexpr uint32_t MAX_CFG_FILE_SIZE = 100 * 1024; // 100k +} + +bool FirstUseDialog::IsCfgDirExist(void) +{ + struct stat fstat = {}; + if (stat(SEC_COMP_SRV_CFG_PATH.c_str(), &fstat) != 0) { + SC_LOG_INFO(LABEL, "path %{public}s errno %{public}d.", SEC_COMP_SRV_CFG_PATH.c_str(), errno); + return false; + } + + if (!S_ISDIR(fstat.st_mode)) { + SC_LOG_ERROR(LABEL, "path %{public}s is not directory.", SEC_COMP_SRV_CFG_PATH.c_str()); + return false; + } + return true; +} + +bool FirstUseDialog::IsCfgFileExist(void) +{ + struct stat fstat = {}; + if (stat(FIRST_USE_RECORD_JSON.c_str(), &fstat) != 0) { + SC_LOG_INFO(LABEL, "path %{public}s errno %{public}d.", FIRST_USE_RECORD_JSON.c_str(), errno); + return false; + } + return true; +} + +bool FirstUseDialog::IsCfgFileValid(void) +{ + struct stat fstat = {}; + if (stat(FIRST_USE_RECORD_JSON.c_str(), &fstat) != 0) { + SC_LOG_INFO(LABEL, "path %{public}s errno %{public}d.", FIRST_USE_RECORD_JSON.c_str(), errno); + return false; + } + if (fstat.st_size > MAX_CFG_FILE_SIZE) { + SC_LOG_INFO(LABEL, "path %{public}s errno %{public}d.", FIRST_USE_RECORD_JSON.c_str(), errno); + return false; + } + return true; +} + +bool FirstUseDialog::ReadCfgContent(std::string& content) +{ + std::stringstream buffer; + std::ifstream i(FIRST_USE_RECORD_JSON); + if (!i.is_open()) { + SC_LOG_ERROR(LABEL, "cannot open file %{public}s, errno %{public}d.", FIRST_USE_RECORD_JSON.c_str(), errno); + return false; + } + buffer << i.rdbuf(); + content = buffer.str(); + i.close(); + return true; +} + +void FirstUseDialog::WriteCfgContent(std::string content) +{ + std::ofstream out(FIRST_USE_RECORD_JSON); + if (!out.is_open()) { + SC_LOG_ERROR(LABEL, "cannot open file %{public}s, errno %{public}d.", FIRST_USE_RECORD_JSON.c_str(), errno); + return; + } + out << content; + out.close(); +} + +bool FirstUseDialog::ParseRecord(nlohmann::json& jsonRes, + AccessToken::AccessTokenID& id, uint64_t& type) +{ + if (jsonRes.find(TOKEN_ID_TAG) == jsonRes.end() || + !jsonRes.at(TOKEN_ID_TAG).is_number()) { + SC_LOG_ERROR(LABEL, "parse TokenId failed."); + return false; + } + id = jsonRes.at(TOKEN_ID_TAG).get(); + if (id == AccessToken::INVALID_TOKENID) { + SC_LOG_ERROR(LABEL, "TokenId is not invalid."); + return false; + } + + if (jsonRes.find(COMP_TYPE_TAG) == jsonRes.end() || + !jsonRes.at(COMP_TYPE_TAG).is_number()) { + SC_LOG_ERROR(LABEL, "parse CompType failed."); + return false; + } + type = jsonRes.at(COMP_TYPE_TAG).get(); + return true; +} + +void FirstUseDialog::ParseRecords(nlohmann::json& jsonRes) +{ + std::unique_lock lock(useMapMutex_); + if (jsonRes.find(FIRST_USE_RECORD_TAG) == jsonRes.end() || + !jsonRes.at(FIRST_USE_RECORD_TAG).is_array()) { + SC_LOG_ERROR(LABEL, "parse tag failed."); + return; + } + + nlohmann::json recordListJson = jsonRes.at(FIRST_USE_RECORD_TAG); + for (auto& recordJson : recordListJson) { + AccessToken::AccessTokenID id; + uint64_t type; + if (!ParseRecord(recordJson, id, type)) { + SC_LOG_ERROR(LABEL, "parse record failed."); + return; + } + firstUseMap_[id] = type; + } +} + +void FirstUseDialog::LoadFirstUseRecord(void) +{ + if (!IsCfgFileValid()) { + SC_LOG_INFO(LABEL, "first use record is not valid."); + return; + } + + std::string content; + if (!ReadCfgContent(content)) { + return; + } + + nlohmann::json jsonRes = nlohmann::json::parse(content, nullptr, false); + if (jsonRes.is_discarded()) { + SC_LOG_ERROR(LABEL, "cfg info format is invalid"); + return; + } + + ParseRecords(jsonRes); +} + +void FirstUseDialog::SaveFirstUseRecord(void) +{ + SC_LOG_INFO(LABEL, "start save first_use_record json"); + if (!IsCfgDirExist()) { + SC_LOG_ERROR(LABEL, "dir %{public}s is not exist, errno %{public}d", + SEC_COMP_SRV_CFG_PATH.c_str(), errno); + return; + } + + if (!IsCfgFileExist()) { + if (creat(FIRST_USE_RECORD_JSON.c_str(), S_IRUSR | S_IWUSR) == -1) { + SC_LOG_ERROR(LABEL, "create %{public}s failed, errno %{public}d", + FIRST_USE_RECORD_JSON.c_str(), errno); + return; + } + } + + nlohmann::json jsonRes; + { + std::unique_lock lock(useMapMutex_); + nlohmann::json recordsJson; + for (auto iter = firstUseMap_.begin(); iter != firstUseMap_.end(); ++iter) { + AccessToken::AccessTokenID id = iter->first; + AccessToken::HapTokenInfo info; + if (AccessToken::AccessTokenKit::GetHapTokenInfo(id, info) != AccessToken::RET_SUCCESS) { + SC_LOG_INFO(LABEL, "token id %{public}d is not exist, do not update it.", id); + continue; + } + nlohmann::json recordJson; + recordJson[TOKEN_ID_TAG] = id; + recordJson[COMP_TYPE_TAG] = iter->second; + recordsJson.emplace_back(recordJson); + } + + jsonRes[FIRST_USE_RECORD_TAG] = recordsJson; + } + WriteCfgContent(jsonRes.dump()); +} + +void FirstUseDialog::StartDialogAbility(SecCompType type, sptr callerToken) +{ + int32_t typeNum; + if (type == LOCATION_COMPONENT) { + typeNum = 0; + } else if (type == SAVE_COMPONENT) { + typeNum = 1; + } else { + SC_LOG_ERROR(LABEL, "unknown type."); + return; + } + + AAFwk::Want want; + want.SetElementName(GRANT_ABILITY_BUNDLE_NAME, GRANT_ABILITY_ABILITY_NAME); + want.SetParam(TYPE_KEY, typeNum); + want.SetParam(TOKEN_KEY, callerToken); + int startRes = AAFwk::AbilityManagerClient::GetInstance()->StartExtensionAbility(want, callerToken); + SC_LOG_INFO(LABEL, "start ability res %{public}d", startRes); +} + +void FirstUseDialog::SendSaveEventHandler(void) +{ + std::function delayed = ([this]() { + this->SaveFirstUseRecord(); + }); + + SC_LOG_INFO(LABEL, "Delay first_use_record json"); + secHandler_->ProxyPostTask(delayed); +} + +void FirstUseDialog::NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, SecCompType type, + sptr callerToken) +{ + if (secHandler_ == nullptr) { + SC_LOG_ERROR(LABEL, "event handler invalid."); + return; + } + if (callerToken == nullptr) { + SC_LOG_INFO(LABEL, "callerToken is null, no need to notify dialog"); + return; + } + + uint64_t typeMask; + if (type == LOCATION_COMPONENT) { + typeMask = LOCATION_BUTTON_FIRST_USE; + } else if (type == SAVE_COMPONENT) { + typeMask = SAVE_BUTTON_FIRST_USE; + } else { + SC_LOG_INFO(LABEL, "this type need not notify dialog to user"); + return; + } + + std::unique_lock lock(useMapMutex_); + auto iter = firstUseMap_.find(tokenId); + if (iter == firstUseMap_.end()) { + SC_LOG_INFO(LABEL, "has not use record, start dialog"); + StartDialogAbility(type, callerToken); + firstUseMap_[tokenId] = typeMask; + SendSaveEventHandler(); + return; + } + + uint64_t compTypes = firstUseMap_[tokenId]; + if ((compTypes & typeMask) == typeMask) { + SC_LOG_INFO(LABEL, "no need notify again."); + return; + } + StartDialogAbility(type, callerToken); + firstUseMap_[tokenId] |= typeMask; + SendSaveEventHandler(); +} + +void FirstUseDialog::Init(std::shared_ptr secHandler) +{ + SC_LOG_DEBUG(LABEL, "Init!!"); + secHandler_ = secHandler; + LoadFirstUseRecord(); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h new file mode 100644 index 0000000..5b4aec6 --- /dev/null +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 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 FIRST_USE_DIALOG_H +#define FIRST_USE_DIALOG_H + +#include +#include +#include +#include +#include "access_token.h" +#include "iremote_object.h" +#include "nlohmann/json.hpp" +#include "sec_comp_err.h" +#include "sec_comp_info.h" +#include "sec_event_handler.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +constexpr uint64_t LOCATION_BUTTON_FIRST_USE = 1 << 0; +constexpr uint64_t SAVE_BUTTON_FIRST_USE = 1 << 1; + +class FirstUseDialog final { +public: + FirstUseDialog() = default; + ~FirstUseDialog() = default; + void NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, SecCompType type, sptr callerToken); + void Init(std::shared_ptr secHandler_); + +private: + bool IsCfgDirExist(void); + bool IsCfgFileExist(void); + bool IsCfgFileValid(void); + bool ReadCfgContent(std::string& content); + void WriteCfgContent(std::string content); + bool ParseRecord(nlohmann::json& jsonRes, + AccessToken::AccessTokenID& id, uint64_t& type); + void ParseRecords(nlohmann::json& jsonRes); + void LoadFirstUseRecord(void); + void SaveFirstUseRecord(void); + void StartDialogAbility(SecCompType type, sptr callerToken); + void SendSaveEventHandler(void); + + std::mutex useMapMutex_; + std::unordered_map firstUseMap_; + std::shared_ptr secHandler_; +}; +} // namespace SecurityComponentEnhance +} // namespace Security +} // namespace OHOS +#endif // FIRST_USE_DIALOG_H + diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.h b/services/security_component_service/sa/sa_main/sec_comp_entity.h index ef9163c..0407d10 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.h +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.h @@ -66,16 +66,6 @@ public: return componentInfo_; }; - bool GetEffective() const - { - return isEffective_; - }; - - void SetEffective(bool effective) - { - isEffective_ = effective; - }; - bool CompareComponentBasicInfo(SecCompBase* other) const; bool CheckTouchInfo(const SecCompClickEvent& touchInfo) const; @@ -84,7 +74,6 @@ private: bool isGrant_ = false; AccessToken::AccessTokenID tokenId_; int32_t scId_; - bool isEffective_ = false; }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index d234fa2..1cbe4db 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -33,6 +33,8 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelper"}; static constexpr double MAX_RECT_PERCENT = 0.1F; // 10% static constexpr double ZERO_OFFSET = 0.0F; +static constexpr int32_t NO_TEXT = -1; +static constexpr int32_t NO_ICON = -1; static std::mutex g_renderLock; } @@ -140,8 +142,9 @@ static bool CheckSecCompBaseButton(const SecCompBase* comp) return false; } - if (comp->bg_ != SecCompBackground::NO_BG_TYPE && - (IsColorSimilar(comp->fontColor_, comp->bgColor_) || IsColorSimilar(comp->iconColor_, comp->bgColor_))) { + if ((comp->bg_ != SecCompBackground::NO_BG_TYPE) && + (((comp->text_ != NO_TEXT) && (IsColorSimilar(comp->fontColor_, comp->bgColor_))) || + ((comp->icon_ != NO_ICON) && (IsColorSimilar(comp->iconColor_, comp->bgColor_))))) { SC_LOG_INFO(LABEL, "fontColor or iconColor is similar whith backgroundColor."); return false; } @@ -175,8 +178,9 @@ static bool CheckSecCompBase(const SecCompBase* comp) return false; } - if (IsColorTransparent(comp->bgColor_) || IsColorTransparent(comp->fontColor_) || - IsColorTransparent(comp->iconColor_)) { + if (((comp->bg_ != SecCompBackground::NO_BG_TYPE) && (IsColorTransparent(comp->bgColor_))) || + ((comp->text_ != NO_TEXT) && (IsColorTransparent(comp->fontColor_))) || + ((comp->icon_ != NO_ICON) && (IsColorTransparent(comp->iconColor_)))) { SC_LOG_ERROR(LABEL, "bgColor or fontColor or iconColor is too transparent."); return false; } diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 968ff06..fc43275 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -38,7 +38,6 @@ static constexpr int32_t ROOT_UID = 0; SecCompManager::SecCompManager() { scIdStart_ = SC_ID_START; - scValidCount_ = 0; SC_LOG_INFO(LABEL, "SecCompManager()"); } @@ -59,28 +58,27 @@ int32_t SecCompManager::CreateScId() return scIdStart_; } -int32_t SecCompManager::AddProcessComponent(std::vector& componentList, - const SecCompEntity& newEntity) -{ - componentList.emplace_back(newEntity); - if (scValidCount_ == 0) { - SecCompEnhanceAdapter::EnableInputEnhance(); - } - scValidCount_++; - return SC_OK; -} - int32_t SecCompManager::AddSecurityComponentToList(int32_t pid, const SecCompEntity& newEntity) { OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + if (isSaExit_) { + SC_LOG_ERROR(LABEL, "SA is exiting, retry..."); + return SC_SERVICE_ERROR_SERVICE_NOT_EXIST; + } + auto iter = componentMap_.find(pid); if (iter != componentMap_.end()) { - return AddProcessComponent(iter->second, newEntity); + iter->second.isForeground = true; + iter->second.compList.emplace_back(newEntity); + SecCompEnhanceAdapter::EnableInputEnhance(); + return SC_OK; } - std::vector newComponentList; - AddProcessComponent(newComponentList, newEntity); - componentMap_[pid] = newComponentList; + ProcessCompInfos newProcess; + newProcess.isForeground = true; + newProcess.compList.emplace_back(newEntity); + componentMap_[pid] = newProcess; + SecCompEnhanceAdapter::EnableInputEnhance(); return SC_OK; } @@ -92,16 +90,14 @@ int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t pid, int32_t scI SC_LOG_ERROR(LABEL, "Can not find registered process"); return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; } - std::vector& list = iter->second; + std::vector& list = iter->second.compList; for (auto it = list.begin(); it != list.end(); ++it) { if (it->GetScId() == scId) { it->RevokeTempPermission(); - SC_LOG_INFO(LABEL, "scValidCount %{public}d before delete", scValidCount_); - if (it->GetEffective()) { - scValidCount_--; - } - list.erase(it); + if (!IsForegroundCompExist()) { + SecCompEnhanceAdapter::DisableInputEnhance(); + } return SC_OK; } } @@ -171,7 +167,7 @@ SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t pid, int32_t if (iter == componentMap_.end()) { return nullptr; } - std::vector& list = iter->second; + std::vector& list = iter->second.compList; for (auto it = list.begin(); it != list.end(); ++it) { if (it->GetScId() == scId) { return std::addressof(*it); @@ -180,6 +176,13 @@ SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t pid, int32_t return nullptr; } +bool SecCompManager::IsForegroundCompExist() +{ + return std::any_of(componentMap_.begin(), componentMap_.end(), [](const auto & iter) { + return (iter.second.isForeground) && (iter.second.compList.size() > 0); + }); +} + void SecCompManager::NotifyProcessForeground(int32_t pid) { OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); @@ -187,16 +190,12 @@ void SecCompManager::NotifyProcessForeground(int32_t pid) if (iter == componentMap_.end()) { return; } - - SC_LOG_INFO(LABEL, "App pid %{public}d to foreground", pid); - std::vector& list = iter->second; - for (auto it = list.begin(); it != list.end(); ++it) { - it->SetEffective(true); - scValidCount_++; - } - if (!list.empty()) { + iter->second.isForeground = true; + if (IsForegroundCompExist()) { SecCompEnhanceAdapter::EnableInputEnhance(); } + + SC_LOG_INFO(LABEL, "App pid %{public}d to foreground", pid); } void SecCompManager::NotifyProcessBackground(int32_t pid) @@ -207,16 +206,11 @@ void SecCompManager::NotifyProcessBackground(int32_t pid) return; } - SC_LOG_INFO(LABEL, "App pid %{public}d to background", pid); - std::vector& list = iter->second; - for (auto it = list.begin(); it != list.end(); ++it) { - it->RevokeTempPermission(); - it->SetEffective(false); - scValidCount_--; - } - if (scValidCount_ <= 0) { + iter->second.isForeground = false; + if (!IsForegroundCompExist()) { SecCompEnhanceAdapter::DisableInputEnhance(); } + SC_LOG_INFO(LABEL, "App pid %{public}d to background", pid); } void SecCompManager::NotifyProcessDied(int32_t pid) @@ -230,29 +224,30 @@ void SecCompManager::NotifyProcessDied(int32_t pid) return; } SC_LOG_INFO(LABEL, "App pid %{public}d died", pid); - std::vector& list = iter->second; + std::vector& list = iter->second.compList; for (auto it = list.begin(); it != list.end(); ++it) { it->RevokeTempPermission(); - if (it->GetEffective()) { - scValidCount_--; - } } list.clear(); + componentMap_.erase(pid); - if (scValidCount_ <= 0) { + if (!IsForegroundCompExist()) { SecCompEnhanceAdapter::DisableInputEnhance(); } - componentMap_.erase(pid); + RemoveAppFromMaliciousAppList(pid); DelayExitTask::GetInstance().Start(); } void SecCompManager::ExitSaProcess() { + OHOS::Utils::UniqueReadGuard lk(this->componentInfoLock_); if (!componentMap_.empty() || !IsMaliciousAppListEmpty()) { SC_LOG_INFO(LABEL, "Apps using security component still exist, no exit sa"); return; } + isSaExit_ = true; + SecCompEnhanceAdapter::DisableInputEnhance(); SecCompEnhanceAdapter::ExistEnhanceService(); SC_LOG_INFO(LABEL, "All processes using security component died, start sa exit"); @@ -316,7 +311,6 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, int32_t registerId = CreateScId(); SecCompEntity entity(component, caller.tokenId, registerId); - entity.SetEffective(true); int32_t ret = AddSecurityComponentToList(caller.pid, entity); if (ret == SC_OK) { scId = registerId; @@ -373,11 +367,7 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC SC_LOG_ERROR(LABEL, "ScId is invalid"); return SC_SERVICE_ERROR_VALUE_INVALID; } - int32_t res = DeleteSecurityComponentFromList(caller.pid, scId); - if (scValidCount_ <= 0) { - SecCompEnhanceAdapter::DisableInputEnhance(); - } - return res; + return DeleteSecurityComponentFromList(caller.pid, scId); } int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, @@ -408,7 +398,8 @@ int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32 } int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo) + const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, + const SecCompClickEvent& touchInfo, sptr callerToken) { if (IsInMaliciousAppList(caller.pid)) { SC_LOG_ERROR(LABEL, "app is in MaliciousAppList, never allow it"); @@ -421,6 +412,7 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, SC_LOG_ERROR(LABEL, "Can not find target component"); return SC_SERVICE_ERROR_COMPONENT_NOT_EXIST; } + int32_t res = CheckClickSecurityComponentInfo(sc, scId, jsonComponent, caller); if (res != SC_OK) { return res; @@ -443,6 +435,7 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "TEMP_GRANT_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", sc->GetType()); + firstUseDialog_.NotifyFirstUseDialog(caller.tokenId, sc->GetType(), callerToken); return res; } @@ -454,11 +447,12 @@ bool SecCompManager::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID return false; } -void SecCompManager::DumpSecComp(std::string& dumpStr) const +void SecCompManager::DumpSecComp(std::string& dumpStr) { + OHOS::Utils::UniqueReadGuard lk(this->componentInfoLock_); for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { dumpStr.append("pid:" + std::to_string(iter->first) + "\n"); - for (auto compIter = iter->second.begin(); compIter != iter->second.end(); compIter ++) { + for (auto compIter = iter->second.compList.begin(); compIter != iter->second.compList.end(); compIter ++) { nlohmann::json json; compIter->GetComponentInfo()->ToJson(json); dumpStr.append(" scId:" + std::to_string(compIter->GetScId()) + @@ -480,6 +474,7 @@ bool SecCompManager::Initialize() secHandler_ = std::make_shared(secRunner_); DelayExitTask::GetInstance().Init(secHandler_); + firstUseDialog_.Init(secHandler_); return SecCompPermManager::GetInstance().InitEventHandler(secHandler_); } diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index fefc92d..dcf58ee 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -24,6 +24,7 @@ #include "accesstoken_kit.h" #include "app_state_observer.h" +#include "first_use_dialog.h" #include "nocopyable.h" #include "rwlock.h" #include "sec_comp_base.h" @@ -40,6 +41,11 @@ struct SecCompCallerInfo { int32_t pid; }; +struct ProcessCompInfos { + std::vector compList; + bool isForeground = false; +}; + class SecCompManager { public: static SecCompManager& GetInstance(); @@ -51,19 +57,18 @@ public: const SecCompCallerInfo& caller); int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent, - const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo); + const SecCompCallerInfo& caller, const SecCompClickEvent& touchInfo, sptr callerToken); bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); void NotifyProcessForeground(int32_t pid); void NotifyProcessBackground(int32_t pid); void NotifyProcessDied(int32_t pid); - void DumpSecComp(std::string& dumpStr) const; + void DumpSecComp(std::string& dumpStr); bool Initialize(); void ExitSaProcess(); private: SecCompManager(); - int32_t AddProcessComponent(std::vector& componentList, - const SecCompEntity& newEntity); + bool IsForegroundCompExist(); int32_t AddSecurityComponentToList(int32_t pid, const SecCompEntity& newEntity); int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId); SecCompEntity* GetSecurityComponentFromList(int32_t pid, int32_t scId); @@ -79,14 +84,15 @@ private: OHOS::Utils::RWLock componentInfoLock_; std::mutex scIdMtx_; - std::unordered_map> componentMap_; - int32_t scValidCount_; + std::unordered_map componentMap_; int32_t scIdStart_; + bool isSaExit_ = false; std::shared_ptr secRunner_; std::shared_ptr secHandler_; std::set maliciousAppList_; // pid set std::mutex maliciousMtx_; + FirstUseDialog firstUseDialog_; DISALLOW_COPY_AND_MOVE(SecCompManager); }; diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index 100cee8..2389aca 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -16,6 +16,7 @@ #include "sec_comp_service.h" #include + #include "hisysevent.h" #include "hitrace_meter.h" #include "ipc_skeleton.h" @@ -207,7 +208,7 @@ int32_t SecCompService::UnregisterSecurityComponent(int32_t scId) } int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo) + const std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken) { StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentClick"); SecCompCallerInfo caller; @@ -223,7 +224,8 @@ int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return SC_SERVICE_ERROR_VALUE_INVALID; } - int32_t res = SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, touchInfo); + int32_t res = + SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, touchInfo, callerToken); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return res; } diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.h b/services/security_component_service/sa/sa_main/sec_comp_service.h index c9ac178..7a3e92c 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.h +++ b/services/security_component_service/sa/sa_main/sec_comp_service.h @@ -44,7 +44,7 @@ public: int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) override; int32_t UnregisterSecurityComponent(int32_t scId) override; int32_t ReportSecurityComponentClickEvent(int32_t scId, - const std::string& componentInfo, const SecCompClickEvent& touchInfo) override; + const std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr callerToken) override; bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId) override; sptr GetEnhanceRemoteObject() override; diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index 47ec4d1..c492afd 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -153,7 +153,9 @@ int32_t SecCompStub::ReportSecurityComponentClickEventInner(MessageParcel& data, return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; } - int32_t res = this->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfoParcel->touchInfoParams_); + sptr callerToken = data.ReadRemoteObject(); + int32_t res = + this->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfoParcel->touchInfoParams_, callerToken); if (!reply.WriteInt32(res)) { SC_LOG_ERROR(LABEL, "Register security component result fail"); return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; diff --git a/services/security_component_service/sa/security_component_service.cfg b/services/security_component_service/sa/security_component_service.cfg index 818b6d5..009ac03 100644 --- a/services/security_component_service/sa/security_component_service.cfg +++ b/services/security_component_service/sa/security_component_service.cfg @@ -1,4 +1,11 @@ { + "jobs" : [{ + "name" : "post-fs-data", + "cmds" : [ + "mkdir /data/service/el1/public/security_component_service 0750 security_component security_component" + ] + } + ], "services" : [{ "name" : "security_component_service", "path" : ["/system/bin/sa_main", "/system/profile/security_component_service.json"], diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index ee8dc5c..c35aa14 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -34,6 +34,7 @@ ohos_unittest("sec_comp_service_test") { sources = [ "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", @@ -61,6 +62,10 @@ ohos_unittest("sec_comp_service_test") { ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_runtime:ability_manager", + "ability_runtime:runtime", "access_token:libtoken_setproc", "c_utils:utils", "eventhandler:libeventhandler", @@ -91,6 +96,7 @@ ohos_unittest("sec_comp_service_mock_test") { sources = [ "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", @@ -113,6 +119,10 @@ ohos_unittest("sec_comp_service_mock_test") { ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_runtime:ability_manager", + "ability_runtime:runtime", "access_token:libtoken_setproc", "c_utils:utils", "eventhandler:libeventhandler", diff --git a/services/security_component_service/sa/test/mock/include/access_token.h b/services/security_component_service/sa/test/mock/include/access_token.h index a2314f9..654bb5b 100644 --- a/services/security_component_service/sa/test/mock/include/access_token.h +++ b/services/security_component_service/sa/test/mock/include/access_token.h @@ -19,6 +19,13 @@ namespace OHOS { namespace Security { namespace AccessToken { typedef unsigned int AccessTokenID; +static const AccessTokenID INVALID_TOKENID = 0; + +enum AccessTokenKitRet { + RET_FAILED = -1, + RET_SUCCESS = 0, +}; + typedef enum TypeATokenTypeEnum { TOKEN_INVALID = -1, TOKEN_HAP = 0, @@ -47,4 +54,4 @@ typedef struct { } // namespace AccessToken } // namespace Security } // namespace OHOS -#endif // SECURITY_COMPONENT_MOCK_ACCESS_TOKEN_DEF_H \ No newline at end of file +#endif // SECURITY_COMPONENT_MOCK_ACCESS_TOKEN_DEF_H diff --git a/services/security_component_service/sa/test/mock/include/accesstoken_kit.h b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h index bb44771..f4a2af7 100644 --- a/services/security_component_service/sa/test/mock/include/accesstoken_kit.h +++ b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h @@ -21,6 +21,9 @@ namespace OHOS { namespace Security { namespace AccessToken { +struct HapTokenInfo { +}; + class AccessTokenKit { public: static int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag) @@ -33,6 +36,11 @@ public: return 0; }; + static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes) + { + return 0; + }; + static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID) { AccessTokenIDInner *idInner = reinterpret_cast(&tokenID); diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index a4f46e2..02c2760 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -142,25 +142,6 @@ HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) ASSERT_EQ(SecCompManager::GetInstance().CreateScId(), SC_ID_START + 1); } -/** - * @tc.name: AddProcessComponent001 - * @tc.desc: Test add process component - * @tc.type: FUNC - * @tc.require: AR000HO9J7 - */ -HWTEST_F(SecCompManagerTest, AddProcessComponent001, TestSize.Level1) -{ - std::shared_ptr compPtr = std::make_shared(); - ASSERT_NE(compPtr, nullptr); - compPtr->rect_.x_ = TEST_COORDINATE; - compPtr->rect_.y_ = TEST_COORDINATE; - compPtr->rect_.width_ = TEST_COORDINATE; - compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - std::vector componentList; - ASSERT_EQ(SecCompManager::GetInstance().AddProcessComponent(componentList, entity), SC_OK); -} - /** * @tc.name: AddSecurityComponentToList001 * @tc.desc: Test add security component to list @@ -258,11 +239,10 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1); ASSERT_NE(component, nullptr); SecCompManager::GetInstance().NotifyProcessForeground(TEST_PID_1); - ASSERT_TRUE(component->isEffective_); + ASSERT_TRUE(SecCompManager::GetInstance().IsForegroundCompExist()); SecCompManager::GetInstance().NotifyProcessBackground(TEST_PID_1); - ASSERT_NE(component, nullptr); - ASSERT_FALSE(component->isEffective_); + ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); } /** @@ -379,6 +359,6 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve nlohmann::json jsonVaild; LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); - ASSERT_NE(SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo), - SC_OK); + ASSERT_NE(SecCompManager::GetInstance(). + ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo, nullptr), SC_OK); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp index ba90387..087f1e8 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -174,7 +174,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), SC_OK); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); } @@ -205,7 +205,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent002, TestSize.Level1) .timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), SC_SERVICE_ERROR_CLICK_EVENT_INVALID); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); } @@ -236,7 +236,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent003, TestSize.Level1) .timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); } @@ -268,13 +268,14 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo)); + ASSERT_EQ(SC_OK, + secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr)); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); sleep(6); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); @@ -308,21 +309,21 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Leve std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); sleep(3); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); sleep(3); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); @@ -330,12 +331,12 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Leve touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); sleep(3); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); sleep(3); @@ -370,22 +371,22 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent003, TestSize.Leve std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); sleep(3); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); sleep(6); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo), + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), SC_OK); sleep(3); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index e9eb39f..6c339e3 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -300,7 +300,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve .extraInfo.data = data, .extraInfo.dataSize = 16, }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch), + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), SC_ENHANCE_ERROR_IN_MALICIOUS_LIST); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); setuid(uid); diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h index 2516cbf..e56866c 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h @@ -45,7 +45,7 @@ public: }; int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, - const SecCompClickEvent& touchInfo) override + const SecCompClickEvent& touchInfo, sptr callerToken) override { return 0; }; -- Gitee From 81f1d95df99c6111c56905f6596c5821ced80be0 Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 26 Jul 2023 18:46:17 +0800 Subject: [PATCH 43/59] Feature or Bugfix: Bugfix Description: md Match-id-5168bdd5d7dbb02dfec70708cc8f9bdd98251d19 --- README_zh.md | 8 +------- .../sa/test/mock/src/mock_iservice_registry.cpp | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/README_zh.md b/README_zh.md index 45921cd..bd54370 100644 --- a/README_zh.md +++ b/README_zh.md @@ -1,10 +1,4 @@ -# 安全控件管理服务 - - - [简介](#简介) - - [目录](#目录) - - [使用](#使用) - - [接口说明](#接口说明) - - [相关仓](#相关仓) +# 安全控件管理服务 ## 简介 diff --git a/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp b/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp index 410bca5..7abc1fa 100644 --- a/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp +++ b/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 Huawei Device Co., Ltd. + * Copyright (c) 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 -- Gitee From a3065c91817f0afc534b24f03466b6d33a7639d6 Mon Sep 17 00:00:00 2001 From: xxx Date: Wed, 26 Jul 2023 16:57:26 +0800 Subject: [PATCH 44/59] Description: version warnning Match-id-e3ef479e3e668d7a58783cd0703b4c753ec9c72b --- .../security_component_service/sa/sa_main/first_use_dialog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index 5b4aec6..fd0110f 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -38,7 +38,7 @@ public: FirstUseDialog() = default; ~FirstUseDialog() = default; void NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, SecCompType type, sptr callerToken); - void Init(std::shared_ptr secHandler_); + void Init(std::shared_ptr secHandler); private: bool IsCfgDirExist(void); -- Gitee From b47b5564740339b0fdff4ac23d313527545aae51 Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 27 Jul 2023 13:35:15 +0800 Subject: [PATCH 45/59] Feature or Bugfix: Bugfix Description: tddfix Match-id-7b313abd1a6cd4defdec9ed020bc7959e3ef0f5f --- .../sa/test/unittest/src/sec_comp_info_helper_test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 05c7933..a0bb772 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -68,12 +68,12 @@ static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_DIMENSION = 100.0; static constexpr double TEST_INVALID_DIMENSION = -100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; -static constexpr uint32_t TEST_COLOR_BLACK = 0x000000; -static constexpr uint32_t TEST_COLOR_WHITE = 0xffffff; +static constexpr uint32_t TEST_COLOR_BLACK = 0x00000000; +static constexpr uint32_t TEST_COLOR_WHITE = 0xffffffff; static constexpr uint32_t QUARTER = 4; static constexpr double ZERO_OFFSET = 0.0F; static double g_curScreenWidth = 0.0F; -- Gitee From 3733ff50d416451c75442f5914bc3c34ccc879e8 Mon Sep 17 00:00:00 2001 From: xxx Date: Fri, 28 Jul 2023 11:39:44 +0800 Subject: [PATCH 46/59] Description: version warnning Match-id-7fb4e78b814f954e2487af18c25890aaade06725 --- .../security_component_service/sa/sa_main/first_use_dialog.cpp | 2 +- .../security_component_service/sa/sa_main/first_use_dialog.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp index 076d739..e75fb62 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -100,7 +100,7 @@ bool FirstUseDialog::ReadCfgContent(std::string& content) return true; } -void FirstUseDialog::WriteCfgContent(std::string content) +void FirstUseDialog::WriteCfgContent(const std::string content) { std::ofstream out(FIRST_USE_RECORD_JSON); if (!out.is_open()) { diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index fd0110f..d2703c2 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -45,7 +45,7 @@ private: bool IsCfgFileExist(void); bool IsCfgFileValid(void); bool ReadCfgContent(std::string& content); - void WriteCfgContent(std::string content); + void WriteCfgContent(const std::string content); bool ParseRecord(nlohmann::json& jsonRes, AccessToken::AccessTokenID& id, uint64_t& type); void ParseRecords(nlohmann::json& jsonRes); -- Gitee From 890ee59b645385e44a812d5605d2cd404b753b85 Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 31 Jul 2023 16:47:18 +0800 Subject: [PATCH 47/59] Description: version warnning Match-id-aa0acc5ada806ce681dc82a391edbe5b96126c77 --- .../security_component_service/sa/sa_main/first_use_dialog.cpp | 2 +- .../security_component_service/sa/sa_main/first_use_dialog.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp index e75fb62..0907f77 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -100,7 +100,7 @@ bool FirstUseDialog::ReadCfgContent(std::string& content) return true; } -void FirstUseDialog::WriteCfgContent(const std::string content) +void FirstUseDialog::WriteCfgContent(const std::string& content) { std::ofstream out(FIRST_USE_RECORD_JSON); if (!out.is_open()) { diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index d2703c2..21485f0 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -45,7 +45,7 @@ private: bool IsCfgFileExist(void); bool IsCfgFileValid(void); bool ReadCfgContent(std::string& content); - void WriteCfgContent(const std::string content); + void WriteCfgContent(const std::string& content); bool ParseRecord(nlohmann::json& jsonRes, AccessToken::AccessTokenID& id, uint64_t& type); void ParseRecords(nlohmann::json& jsonRes); -- Gitee From 29a61612f7e3880c1674475ee848f5a0bda033eb Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 1 Aug 2023 17:30:22 +0800 Subject: [PATCH 48/59] Feature or Bugfix: Bugfix Description: tddfix Match-id-f81c0711f6b697aebaf34176c66b324399c8ba14 --- .../sa/test/unittest/src/sec_comp_manager_test.cpp | 8 ++++---- .../sa/test/unittest/src/sec_comp_service_mock_test.cpp | 6 +++--- .../sa/test/unittest/src/sec_comp_service_test.cpp | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 02c2760..380f6cb 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -31,10 +31,10 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static constexpr double TEST_SIZE = 100.0; static constexpr uint32_t TEST_INVALID_SIZE = 0; static constexpr double TEST_COORDINATE = 100.0; -static constexpr uint32_t TEST_COLOR = 0xffffff; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint32_t TEST_COLOR = 0xffffffff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; static constexpr int32_t TEST_PID_1 = 1; static constexpr int32_t TEST_PID_2 = 2; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp index 087f1e8..7efdc84 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -40,9 +40,9 @@ static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr int32_t SA_ID = 3506; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; static AccessTokenID g_selfTokenId = 0; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 6c339e3..5b6fe8e 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -41,9 +41,9 @@ static constexpr int32_t TEST_SC_ID = 1; static AccessTokenID g_selfTokenId = 0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; +static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; +static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; +static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; static std::string BuildLocationComponentInfo() -- Gitee From 1f9e35ab953f052521122c914a6b7517fba5add5 Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 7 Aug 2023 22:07:12 +0800 Subject: [PATCH 49/59] Description:security_component FUZZ Match-id-c4eb14c99cc7a20ee30fbf5f9edbe2d55a624aa8 --- BUILD.gn | 10 ++++ bundle.json | 3 +- security_component.gni | 1 + test/fuzztest/security_component/BUILD.gn | 21 ++++++++ .../BUILD.gn | 49 +++++++++++++++++ .../corpus/init | 14 +++++ .../getenhanceremoteobjectInner_fuzzer.cpp | 49 +++++++++++++++++ .../getenhanceremoteobjectInner_fuzzer.h | 24 +++++++++ .../project.xml | 25 +++++++++ .../getenhanceremoteobject_fuzzer/BUILD.gn | 48 +++++++++++++++++ .../getenhanceremoteobject_fuzzer/corpus/init | 14 +++++ .../getenhanceremoteobject_fuzzer.cpp | 41 +++++++++++++++ .../getenhanceremoteobject_fuzzer.h | 23 ++++++++ .../getenhanceremoteobject_fuzzer/project.xml | 25 +++++++++ .../BUILD.gn | 48 +++++++++++++++++ .../corpus/init | 14 +++++ .../getpointereventenhancedata_fuzzer.cpp | 43 +++++++++++++++ .../getpointereventenhancedata_fuzzer.h | 23 ++++++++ .../project.xml | 25 +++++++++ .../BUILD.gn | 49 +++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ ...eafterverifysavepermissionInner_fuzzer.cpp | 48 +++++++++++++++++ ...uceafterverifysavepermissionInner_fuzzer.h | 24 +++++++++ .../BUILD.gn | 48 +++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ ...reduceafterverifysavepermission_fuzzer.cpp | 40 ++++++++++++++ .../reduceafterverifysavepermission_fuzzer.h | 23 ++++++++ .../BUILD.gn | 49 +++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ .../registersecuritycomponentInner_fuzzer.cpp | 51 ++++++++++++++++++ .../registersecuritycomponentInner_fuzzer.h | 21 ++++++++ .../registersecuritycomponent_fuzzer/BUILD.gn | 50 ++++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ .../registersecuritycomponent_fuzzer.cpp | 43 +++++++++++++++ .../registersecuritycomponent_fuzzer.h | 21 ++++++++ .../BUILD.gn | 49 +++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ ...ecuritycomponentclickeventInner_fuzzer.cpp | 49 +++++++++++++++++ ...tsecuritycomponentclickeventInner_fuzzer.h | 21 ++++++++ .../BUILD.gn | 50 ++++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ ...portsecuritycomponentclickevent_fuzzer.cpp | 51 ++++++++++++++++++ ...reportsecuritycomponentclickevent_fuzzer.h | 23 ++++++++ .../setenhancecfg_fuzzer/BUILD.gn | 49 +++++++++++++++++ .../setenhancecfg_fuzzer/corpus/init | 14 +++++ .../setenhancecfg_fuzzer/project.xml | 25 +++++++++ .../setenhancecfg_fuzzer.cpp | 41 +++++++++++++++ .../setenhancecfg_fuzzer.h | 21 ++++++++ .../BUILD.gn | 49 +++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ ...nregistersecuritycomponentInner_fuzzer.cpp | 49 +++++++++++++++++ .../unregistersecuritycomponentInner_fuzzer.h | 21 ++++++++ .../BUILD.gn | 48 +++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ .../unregistersecuritycomponent_fuzzer.cpp | 40 ++++++++++++++ .../unregistersecuritycomponent_fuzzer.h | 21 ++++++++ .../BUILD.gn | 49 +++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ .../updatesecuritycomponentInner_fuzzer.cpp | 49 +++++++++++++++++ .../updatesecuritycomponentInner_fuzzer.h | 21 ++++++++ .../updatesecuritycomponent_fuzzer/BUILD.gn | 52 +++++++++++++++++++ .../corpus/init | 14 +++++ .../project.xml | 25 +++++++++ .../updatesecuritycomponent_fuzzer.cpp | 41 +++++++++++++++ .../updatesecuritycomponent_fuzzer.h | 21 ++++++++ 74 files changed, 2210 insertions(+), 1 deletion(-) create mode 100644 test/fuzztest/security_component/BUILD.gn create mode 100644 test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.cpp create mode 100644 test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.h create mode 100644 test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/getenhanceremoteobject_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/getenhanceremoteobject_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.cpp create mode 100644 test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.h create mode 100644 test/fuzztest/security_component/getenhanceremoteobject_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/getpointereventenhancedata_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/getpointereventenhancedata_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.cpp create mode 100644 test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.h create mode 100644 test/fuzztest/security_component/getpointereventenhancedata_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.h create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.cpp create mode 100644 test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.h create mode 100644 test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.cpp create mode 100644 test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.h create mode 100644 test/fuzztest/security_component/registersecuritycomponent_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/registersecuritycomponent_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/registersecuritycomponent_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp create mode 100644 test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.h create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.cpp create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.h create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.cpp create mode 100644 test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.h create mode 100644 test/fuzztest/security_component/setenhancecfg_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/setenhancecfg_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/setenhancecfg_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.cpp create mode 100644 test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.h create mode 100644 test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.cpp create mode 100644 test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.h create mode 100644 test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.cpp create mode 100644 test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.h create mode 100644 test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.cpp create mode 100644 test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.h create mode 100644 test/fuzztest/security_component/updatesecuritycomponent_fuzzer/BUILD.gn create mode 100644 test/fuzztest/security_component/updatesecuritycomponent_fuzzer/corpus/init create mode 100644 test/fuzztest/security_component/updatesecuritycomponent_fuzzer/project.xml create mode 100644 test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.cpp create mode 100644 test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.h diff --git a/BUILD.gn b/BUILD.gn index 40b1ede..58080d8 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -35,3 +35,13 @@ group("security_component_build_module_test") { ] } } + +group("security_component_build_fuzz_test") { + testonly = true + deps = [] + if (is_standard_system) { + deps += [ + "test/fuzztest/security_component:fuzztest", + ] + } +} \ No newline at end of file diff --git a/bundle.json b/bundle.json index 0a68dd1..f9134f4 100644 --- a/bundle.json +++ b/bundle.json @@ -98,7 +98,8 @@ } ], "test": [ - "//base/security/security_component:security_component_build_module_test" + "//base/security/security_component:security_component_build_module_test", + "//base/security/security_component:security_component_build_fuzz_test" ] } } diff --git a/security_component.gni b/security_component.gni index 4d0f814..af3aa08 100644 --- a/security_component.gni +++ b/security_component.gni @@ -10,3 +10,4 @@ # 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. +sec_comp_dir = "//base/security/security_component" diff --git a/test/fuzztest/security_component/BUILD.gn b/test/fuzztest/security_component/BUILD.gn new file mode 100644 index 0000000..4ca6507 --- /dev/null +++ b/test/fuzztest/security_component/BUILD.gn @@ -0,0 +1,21 @@ +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + "getenhanceremoteobjectInner_fuzzer:GetEnhanceRemoteObjectInnerFuzzTest", + "getenhanceremoteobject_fuzzer:GetEnhanceRemoteObjectFuzzTest", + "getpointereventenhancedata_fuzzer:GetPointerEventEnhanceDataFuzzTest", + "reduceafterverifysavepermissionInner_fuzzer:ReduceAfterVerifySavePermissionInnerFuzzTest", + "reduceafterverifysavepermission_fuzzer:ReduceAfterVerifySavePermissionFuzzTest", + "registersecuritycomponentInner_fuzzer:RegisterSecurityComponentInnerFuzzTest", + "registersecuritycomponent_fuzzer:RegisterSecurityComponentFuzzTest", + "reportsecuritycomponentclickeventInner_fuzzer:ReportSecurityComponentClickEventInnerFuzzTest", + "reportsecuritycomponentclickevent_fuzzer:ReportSecurityComponentClickEventFuzzTest", + "setenhancecfg_fuzzer:SetEnhanceCfgFuzzTest", + "unregistersecuritycomponentInner_fuzzer:UnregisterSecurityComponentInnerFuzzTest", + "unregistersecuritycomponent_fuzzer:UnregisterSecurityComponentFuzzTest", + "updatesecuritycomponentInner_fuzzer:UpdateSecurityComponentInnerFuzzTest", + "updatesecuritycomponent_fuzzer:UpdateSecurityComponentFuzzTest", + ] +} diff --git a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn new file mode 100644 index 0000000..68d43fe --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("GetEnhanceRemoteObjectInnerFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/services/security_component_service/sa/sa_main", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "getenhanceremoteobjectInner_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/services/security_component_service/sa:security_component_service" ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/corpus/init b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.cpp b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.cpp new file mode 100644 index 0000000..48d375e --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.cpp @@ -0,0 +1,49 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "getenhanceremoteobjectInner_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void GetEnhanceRemoteObjectInnerFuzzTest(const uint8_t *data, size_t size) +{ + uint32_t code = SecurityComponentServiceInterfaceCode::GET_SECURITY_COMPONENT_ENHANCE_OBJECT; + MessageParcel datas; + datas.WriteInterfaceToken(u"ohos.security.ISecCompService"); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + auto service = std::make_shared(SA_ID_SECURITY_COMPONENT_SERVICE, true); + service->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::GetEnhanceRemoteObjectInnerFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.h b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.h new file mode 100644 index 0000000..a3ec5aa --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/getenhanceremoteobjectInner_fuzzer.h @@ -0,0 +1,24 @@ +/* + * 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 GET_ENHANCE_REMOTE_OBJECT_INNER_FUZZER_H +#define GET_ENHANCE_REMOTE_OBJECT_INNER_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" + +#include "sec_comp_stub.h" +#include "sec_comp_service.h" + +#endif diff --git a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/project.xml b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/BUILD.gn b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/BUILD.gn new file mode 100644 index 0000000..d0a5c65 --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/BUILD.gn @@ -0,0 +1,48 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("GetEnhanceRemoteObjectFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/getenhanceremoteobject_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/interfaces/inner_api/security_component/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "getenhanceremoteobject_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + ] +} diff --git a/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/corpus/init b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.cpp b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.cpp new file mode 100644 index 0000000..47d9293 --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.cpp @@ -0,0 +1,41 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "getenhanceremoteobject_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void GetEnhanceRemoteObjectFuzzTest(const uint8_t *data, size_t size) +{ + bool isLoad = true; + SecCompKit::GetEnhanceRemoteObject(isLoad); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::GetEnhanceRemoteObjectFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.h b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.h new file mode 100644 index 0000000..a907797 --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/getenhanceremoteobject_fuzzer.h @@ -0,0 +1,23 @@ +/* + * 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 GET_ENHANCE_REMOTE_OBJECT_FUZZER_H +#define GET_ENHANCE_REMOTE_OBJECT_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" + +#include "sec_comp_kit.h" + +#endif diff --git a/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/project.xml b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/getenhanceremoteobject_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/BUILD.gn b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/BUILD.gn new file mode 100644 index 0000000..bca0c52 --- /dev/null +++ b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/BUILD.gn @@ -0,0 +1,48 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("GetPointerEventEnhanceDataFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/getpointereventenhancedata_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/interfaces/inner_api/enhance_kits/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "getpointereventenhancedata_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/interfaces/inner_api/enhance_kits:libsecurity_component_enhance_sdk" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + ] +} diff --git a/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/corpus/init b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.cpp b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.cpp new file mode 100644 index 0000000..c894798 --- /dev/null +++ b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.cpp @@ -0,0 +1,43 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "getpointereventenhancedata_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void GetPointerEventEnhanceDataFuzzTest(const uint8_t *data, size_t size) +{ + uint8_t *data1 = const_cast(data); + uint8_t enhanceData[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + uint32_t enhancedataLen = 10; + SecCompEnhanceKit::GetPointerEventEnhanceData(data1, size, enhanceData, enhancedataLen); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::GetPointerEventEnhanceDataFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.h b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.h new file mode 100644 index 0000000..5d8ac75 --- /dev/null +++ b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/getpointereventenhancedata_fuzzer.h @@ -0,0 +1,23 @@ +/* + * 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 GET_POINTER_EVENT_ENHANCE_DATA_FUZZER_H +#define GET_POINTER_EVENT_ENHANCE_DATA_FUZZER_H + +#define FUZZ_PROJECT_NAME "getpointereventenhancedata_fuzzer" + +#include "sec_comp_enhance_kit.h" + +#endif diff --git a/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/project.xml b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/getpointereventenhancedata_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/BUILD.gn new file mode 100644 index 0000000..ff5172e --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("ReduceAfterVerifySavePermissionInnerFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/services/security_component_service/sa/sa_main", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "reduceafterverifysavepermissionInner_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/services/security_component_service/sa:security_component_service" ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/corpus/init b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/project.xml b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp new file mode 100644 index 0000000..5afd4e1 --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp @@ -0,0 +1,48 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "reduceafterverifysavepermissionInner_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void ReduceAfterVerifySavePermissionInnerFuzzTest(const uint8_t *data, size_t size) +{ + uint32_t code = SecurityComponentServiceInterfaceCode::VERIFY_TEMP_SAVE_PERMISSION; + MessageParcel datas; + datas.WriteInterfaceToken(u"ohos.security.ISecCompService"); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + auto service = std::make_shared(SA_ID_SECURITY_COMPONENT_SERVICE, true); + service->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::ReduceAfterVerifySavePermissionInnerFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.h b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.h new file mode 100644 index 0000000..d17798d --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.h @@ -0,0 +1,24 @@ +/* + * 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 REDUCE_AFTER_VERIFY_SAVEPERMISSION_INNER_FUZZER_H +#define REDUCE_AFTER_VERIFY_SAVEPERMISSION_INNER_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" + +#include "sec_comp_stub.h" +#include "sec_comp_service.h" + +#endif diff --git a/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/BUILD.gn b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/BUILD.gn new file mode 100644 index 0000000..aec22dc --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/BUILD.gn @@ -0,0 +1,48 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("ReduceAfterVerifySavePermissionFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/interfaces/inner_api/security_component/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "reduceafterverifysavepermission_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + ] +} diff --git a/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/corpus/init b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/project.xml b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.cpp b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.cpp new file mode 100644 index 0000000..62a7c88 --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.cpp @@ -0,0 +1,40 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "reduceafterverifysavepermission_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void ReduceAfterVerifySavePermissionFuzzTest(const uint8_t *data, size_t size) +{ + SecCompKit::ReduceAfterVerifySavePermission(size); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::ReduceAfterVerifySavePermissionFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.h b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.h new file mode 100644 index 0000000..6d024ef --- /dev/null +++ b/test/fuzztest/security_component/reduceafterverifysavepermission_fuzzer/reduceafterverifysavepermission_fuzzer.h @@ -0,0 +1,23 @@ +/* + * 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 REDUCE_AFTER_VERIFY_SAVEPERMISSION_FUZZER_H +#define REDUCE_AFTER_VERIFY_SAVEPERMISSION_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" + +#include "sec_comp_kit.h" + +#endif // TEST_FUZZTEST_SECURITY_COMPONENT_FUZZER_H diff --git a/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn new file mode 100644 index 0000000..3e81ae7 --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("RegisterSecurityComponentInnerFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/services/security_component_service/sa/sa_main", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "registersecuritycomponentInner_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/services/security_component_service/sa:security_component_service" ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/corpus/init b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/project.xml b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.cpp b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.cpp new file mode 100644 index 0000000..24385f9 --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.cpp @@ -0,0 +1,51 @@ +/* + * 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 +#include +#include +#include +#include "registersecuritycomponentInner_fuzzer.h" +#include "accesstoken_kit.h" +#include "sec_comp_service.h" +#include "securec.h" +#include "token_setproc.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void RegisterSecurityComponentInnerFuzzTest(const uint8_t *data, size_t size) +{ + uint32_t code = + SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT; + MessageParcel datas; + datas.WriteInterfaceToken(u"ohos.security.ISecCompService"); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + auto service = + std::make_shared(SA_ID_SECURITY_COMPONENT_SERVICE, true); + service->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::RegisterSecurityComponentInnerFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.h b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.h new file mode 100644 index 0000000..a8ada33 --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/registersecuritycomponentInner_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 REGISTER_SECURITY_COMPONENT_INNER_FUZZER_H +#define REGISTER_SECURITY_COMPONENT_INNER_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" +#include "sec_comp_stub.h" +#endif diff --git a/test/fuzztest/security_component/registersecuritycomponent_fuzzer/BUILD.gn b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/BUILD.gn new file mode 100644 index 0000000..68f586d --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/BUILD.gn @@ -0,0 +1,50 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("RegisterSecurityComponentFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/registersecuritycomponent_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/interfaces/inner_api/security_component/include", + "//third_party/json/include", + "//third_party/json/test/thirdparty/fifo_map", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "registersecuritycomponent_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + ] +} diff --git a/test/fuzztest/security_component/registersecuritycomponent_fuzzer/corpus/init b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/registersecuritycomponent_fuzzer/project.xml b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp new file mode 100644 index 0000000..ea9d275 --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp @@ -0,0 +1,43 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "registersecuritycomponent_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void RegisterSecurityComponentFuzzTest(const uint8_t *data, size_t size) +{ + std::string componentInfo(reinterpret_cast(data), size); + int32_t scId = size; + enum SecCompType type = UNKNOWN_SC_TYPE; + SecCompKit::RegisterSecurityComponent(type, componentInfo, scId); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::RegisterSecurityComponentFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.h b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.h new file mode 100644 index 0000000..667b592 --- /dev/null +++ b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 REGISTER_SECURITY_COMPONENT_FUZZER_H +#define REGISTER_SECURITY_COMPONENT_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" +#include "sec_comp_kit.h" +#endif diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn new file mode 100644 index 0000000..fa374fa --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("ReportSecurityComponentClickEventInnerFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/services/security_component_service/sa/sa_main", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "reportsecuritycomponentclickeventInner_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/services/security_component_service/sa:security_component_service" ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/corpus/init b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/project.xml b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.cpp b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.cpp new file mode 100644 index 0000000..5cea508 --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.cpp @@ -0,0 +1,49 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "sec_comp_service.h" +#include "reportsecuritycomponentclickeventInner_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void ReportSecurityComponentClickEventInnerFuzzTest(const uint8_t *data, size_t size) +{ + uint32_t code = SecurityComponentServiceInterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT; + MessageParcel datas; + datas.WriteInterfaceToken(u"ohos.security.ISecCompService"); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + auto service = std::make_shared(SA_ID_SECURITY_COMPONENT_SERVICE, true); + service->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::ReportSecurityComponentClickEventInnerFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.h b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.h new file mode 100644 index 0000000..8af2a89 --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/reportsecuritycomponentclickeventInner_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 REPORT_SECURITY_COMPONENT_CLICK_EVENT_INNER_FUZZER_H +#define REPORT_SECURITY_COMPONENT_CLICK_EVENT_INNER_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" +#include "sec_comp_stub.h" +#endif diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/BUILD.gn b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/BUILD.gn new file mode 100644 index 0000000..1ac949d --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/BUILD.gn @@ -0,0 +1,50 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("ReportSecurityComponentClickEventFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/interfaces/inner_api/security_component/include", + "//third_party/json/include", + "//third_party/json/test/thirdparty/fifo_map", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "reportsecuritycomponentclickevent_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + ] +} diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/corpus/init b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/project.xml b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.cpp b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.cpp new file mode 100644 index 0000000..89477c3 --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.cpp @@ -0,0 +1,51 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "reportsecuritycomponentclickevent_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void ReportSecurityComponentClickEventFuzzTest(const uint8_t *data, size_t size) +{ + std::string componentInfo(reinterpret_cast(data), size); + uint8_t *data1 = const_cast(data); + const double touchX = size; + const double touchY = size; + const uint64_t timesStamp = size; + struct SecCompClickEvent touchInfo; + touchInfo.touchX = touchX; + touchInfo.touchY = touchY; + touchInfo.timestamp = timesStamp; + touchInfo.extraInfo.data = data1; + touchInfo.extraInfo.dataSize = size; + SecCompKit::ReportSecurityComponentClickEvent(size, componentInfo, touchInfo); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::ReportSecurityComponentClickEventFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.h b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.h new file mode 100644 index 0000000..989ec28 --- /dev/null +++ b/test/fuzztest/security_component/reportsecuritycomponentclickevent_fuzzer/reportsecuritycomponentclickevent_fuzzer.h @@ -0,0 +1,23 @@ +/* + * 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 REPORT_SECURITY_COMPONENT_CLICK_EVENT_FUZZER_H +#define REPORT_SECURITY_COMPONENT_CLICK_EVENT_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" + +#include "sec_comp_kit.h" + +#endif diff --git a/test/fuzztest/security_component/setenhancecfg_fuzzer/BUILD.gn b/test/fuzztest/security_component/setenhancecfg_fuzzer/BUILD.gn new file mode 100644 index 0000000..c7a950f --- /dev/null +++ b/test/fuzztest/security_component/setenhancecfg_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("SetEnhanceCfgFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "${sec_comp_dir}/test/fuzztest/security_component/setenhancecfg_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/interfaces/inner_api/enhance_kits/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "setenhancecfg_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/interfaces/inner_api/enhance_kits:libsecurity_component_enhance_sdk" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + ] +} diff --git a/test/fuzztest/security_component/setenhancecfg_fuzzer/corpus/init b/test/fuzztest/security_component/setenhancecfg_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/setenhancecfg_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/setenhancecfg_fuzzer/project.xml b/test/fuzztest/security_component/setenhancecfg_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/setenhancecfg_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.cpp b/test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.cpp new file mode 100644 index 0000000..be86e48 --- /dev/null +++ b/test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.cpp @@ -0,0 +1,41 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "setenhancecfg_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void SetEnhanceCfgFuzzTest(const uint8_t *data, size_t size) +{ + uint8_t *cfg = const_cast(data); + SecCompEnhanceKit::SetEnhanceCfg(cfg, size); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::SetEnhanceCfgFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.h b/test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.h new file mode 100644 index 0000000..47d46f1 --- /dev/null +++ b/test/fuzztest/security_component/setenhancecfg_fuzzer/setenhancecfg_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 SETENHANCE_CFG_FUZZER_H +#define SETENHANCE_CFG_FUZZER_H + +#define FUZZ_PROJECT_NAME "setenhancecfg_fuzzer" +#include "sec_comp_enhance_kit.h" +#endif diff --git a/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn new file mode 100644 index 0000000..02f554e --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("UnregisterSecurityComponentInnerFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/services/security_component_service/sa/sa_main", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "unregistersecuritycomponentInner_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/services/security_component_service/sa:security_component_service" ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/corpus/init b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/project.xml b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.cpp b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.cpp new file mode 100644 index 0000000..8eddb37 --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.cpp @@ -0,0 +1,49 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "sec_comp_service.h" +#include "unregistersecuritycomponentInner_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void UnregisterSecurityComponentInnerFuzzTest(const uint8_t *data, size_t size) +{ + uint32_t code = SecurityComponentServiceInterfaceCode::UNREGISTER_SECURITY_COMPONENT; + MessageParcel datas; + datas.WriteInterfaceToken(u"ohos.security.ISecCompService"); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + auto service = std::make_shared(SA_ID_SECURITY_COMPONENT_SERVICE, true); + service->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::UnregisterSecurityComponentInnerFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.h b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.h new file mode 100644 index 0000000..02a5307 --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/unregistersecuritycomponentInner_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 UNREGISTER_SECURITY_COMPONENT_INNER_FUZZER_H +#define UNREGISTER_SECURITY_COMPONENT_INNER_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" +#include "sec_comp_stub.h" +#endif diff --git a/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/BUILD.gn b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/BUILD.gn new file mode 100644 index 0000000..2afbc27 --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/BUILD.gn @@ -0,0 +1,48 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("UnregisterSecurityComponentFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/interfaces/inner_api/security_component/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "unregistersecuritycomponent_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + ] +} diff --git a/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/corpus/init b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/project.xml b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.cpp b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.cpp new file mode 100644 index 0000000..3219fb3 --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.cpp @@ -0,0 +1,40 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "unregistersecuritycomponent_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void UnregisterSecurityComponentFuzzTest(const uint8_t *data, size_t size) +{ + SecCompKit::UnregisterSecurityComponent(size); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::UnregisterSecurityComponentFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.h b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.h new file mode 100644 index 0000000..794af54 --- /dev/null +++ b/test/fuzztest/security_component/unregistersecuritycomponent_fuzzer/unregistersecuritycomponent_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 UNREGISTER_SECURITY_COMPONENT_FUZZER_H +#define UNREGISTER_SECURITY_COMPONENT_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" +#include "sec_comp_kit.h" +#endif diff --git a/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn new file mode 100644 index 0000000..061dcc7 --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("UpdateSecurityComponentInnerFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/services/security_component_service/sa/sa_main", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "updatesecuritycomponentInner_fuzzer.cpp" ] + + deps = [ "${sec_comp_dir}/services/security_component_service/sa:security_component_service" ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/corpus/init b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/project.xml b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.cpp b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.cpp new file mode 100644 index 0000000..7f1ab9f --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.cpp @@ -0,0 +1,49 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "sec_comp_service.h" +#include "updatesecuritycomponentInner_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void UpdateSecurityComponentInnerFuzzTest(const uint8_t *data, size_t size) +{ + uint32_t code = SecurityComponentServiceInterfaceCode::UPDATE_SECURITY_COMPONENT; + MessageParcel datas; + datas.WriteInterfaceToken(u"ohos.security.ISecCompService"); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + auto service = std::make_shared(SA_ID_SECURITY_COMPONENT_SERVICE, true); + service->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::UpdateSecurityComponentInnerFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.h b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.h new file mode 100644 index 0000000..2d25590 --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/updatesecuritycomponentInner_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 UPDATE_SECURITY_COMPONENT_INNER_FUZZER_H +#define UPDATE_SECURITY_COMPONENT_INNER_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" +#include "sec_comp_stub.h" +#endif diff --git a/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/BUILD.gn b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/BUILD.gn new file mode 100644 index 0000000..0e675b2 --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../../security_component.gni") + +module_output_path = "security_component/security_component" + +ohos_fuzztest("UpdateSecurityComponentFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "${sec_comp_dir}/test/fuzztest/security_component/updatesecuritycomponent_fuzzer" + + include_dirs = [ + "${sec_comp_dir}/frameworks/common/include", + "${sec_comp_dir}/services/security_component_service/sa/sa_main", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "${sec_comp_dir}/config:coverage_flags" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + sources = [ "updatesecuritycomponent_fuzzer.cpp" ] + + deps = [ + "${sec_comp_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + "${sec_comp_dir}/services/security_component_service/sa:security_component_service", + ] + + external_deps = [ + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/corpus/init b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/corpus/init new file mode 100644 index 0000000..bc977bd --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/project.xml b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/project.xml new file mode 100644 index 0000000..6e8ad2c --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.cpp b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.cpp new file mode 100644 index 0000000..0a15493 --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.cpp @@ -0,0 +1,41 @@ +/* + * 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 +#include +#include +#include +#include "accesstoken_kit.h" +#include "securec.h" +#include "token_setproc.h" +#include "updatesecuritycomponent_fuzzer.h" + +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +static void UpdateSecurityComponentFuzzTest(const uint8_t *data, size_t size) +{ + std::string str(reinterpret_cast(data), size); + SecCompKit::UpdateSecurityComponent(size, str); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::UpdateSecurityComponentFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.h b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.h new file mode 100644 index 0000000..85f7f16 --- /dev/null +++ b/test/fuzztest/security_component/updatesecuritycomponent_fuzzer/updatesecuritycomponent_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 UPDATE_SECURITY_COMPONENT_FUZZER_H +#define UPDATE_SECURITY_COMPONENT_FUZZER_H + +#define FUZZ_PROJECT_NAME "securitycomponent_fuzzer" +#include "sec_comp_kit.h" +#endif -- Gitee From 0b1f6f92cb74c91e38782fc853bec4305930bdd8 Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 8 Aug 2023 21:59:49 +0800 Subject: [PATCH 50/59] Description:add app mgr death recipient Match-id-c353d56d57180bacae3c0da8a4065ec9054a8d03 --- .../security_component_service/sa/BUILD.gn | 1 + .../sa/sa_main/app_mgr_death_recipient.cpp | 28 ++++++++++++++++ .../sa/sa_main/app_mgr_death_recipient.h | 33 +++++++++++++++++++ .../sa/sa_main/sec_comp_manager.cpp | 32 +++++++++++++++++- .../sa/sa_main/sec_comp_manager.h | 1 + .../sa/sa_main/sec_comp_service.cpp | 15 ++++++++- .../sa/test/BUILD.gn | 2 ++ .../mock/include/if_system_ability_manager.h | 7 ++++ 8 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp create mode 100644 services/security_component_service/sa/sa_main/app_mgr_death_recipient.h diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 3703091..bb0d7aa 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -46,6 +46,7 @@ ohos_shared_library("security_component_service") { ] sources = [ + "sa_main/app_mgr_death_recipient.cpp", "sa_main/app_state_observer.cpp", "sa_main/delay_exit_task.cpp", "sa_main/first_use_dialog.cpp", diff --git a/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp new file mode 100644 index 0000000..7e37725 --- /dev/null +++ b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 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. + */ +#include "app_mgr_death_recipient.h" +#include "sec_comp_manager.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +void AppMgrDeathRecipient::OnRemoteDied(const wptr& object) +{ + SecCompManager::GetInstance().ExitWhenAppMgrDied(); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + diff --git a/services/security_component_service/sa/sa_main/app_mgr_death_recipient.h b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.h new file mode 100644 index 0000000..d4dc41c --- /dev/null +++ b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 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 APP_MGR_DEATH_RECIPIENT_H +#define APP_MGR_DEATH_RECIPIENT_H + +#include "iremote_object.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient { +public: + AppMgrDeathRecipient() {} + virtual ~AppMgrDeathRecipient() = default; + void OnRemoteDied(const wptr& object) override; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // APP_MGR_DEATH_RECIPIENT_H + diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index fc43275..1737413 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -218,6 +218,7 @@ void SecCompManager::NotifyProcessDied(int32_t pid) // notify enhance process died. SecCompEnhanceAdapter::NotifyProcessDied(pid); + RemoveAppFromMaliciousAppList(pid); OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { @@ -235,7 +236,6 @@ void SecCompManager::NotifyProcessDied(int32_t pid) SecCompEnhanceAdapter::DisableInputEnhance(); } - RemoveAppFromMaliciousAppList(pid); DelayExitTask::GetInstance().Start(); } @@ -264,6 +264,36 @@ void SecCompManager::ExitSaProcess() SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); } +void SecCompManager::ExitWhenAppMgrDied() +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { + std::vector& list = iter->second.compList; + for (auto it = list.begin(); it != list.end(); ++it) { + it->RevokeTempPermission(); + } + list.clear(); + } + componentMap_.clear(); + + // no need exit enhance service, only disable input enhance. + SecCompEnhanceAdapter::DisableInputEnhance(); + isSaExit_ = true; + + SC_LOG_INFO(LABEL, "app mgr died, start sa exit"); + auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + SC_LOG_ERROR(LABEL, "failed to get SystemAbilityManager."); + return; + } + int32_t ret = systemAbilityMgr->UnloadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE); + if (ret != SC_OK) { + SC_LOG_ERROR(LABEL, "failed to UnloadSystemAbility service! errcode=%{public}d", ret); + return; + } + SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); +} + void SecCompManager::SendCheckInfoEnhanceSysEvent(int32_t scId, SecCompType type, const std::string& scene, int32_t res) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index dcf58ee..fd974ce 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -65,6 +65,7 @@ public: void DumpSecComp(std::string& dumpStr); bool Initialize(); void ExitSaProcess(); + void ExitWhenAppMgrDied(); private: SecCompManager(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index 2389aca..3cbfb5d 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -17,6 +17,7 @@ #include +#include "app_mgr_death_recipient.h" #include "hisysevent.h" #include "hitrace_meter.h" #include "ipc_skeleton.h" @@ -107,7 +108,8 @@ bool SecCompService::RegisterAppStateObserver() appStateObserver_ = nullptr; return false; } - iAppMgr_ = iface_cast(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID)); + auto remoteObject = samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID); + iAppMgr_ = iface_cast(remoteObject); if (iAppMgr_ == nullptr) { SC_LOG_ERROR(LABEL, "Failed to get ability manager service"); appStateObserver_ = nullptr; @@ -121,6 +123,17 @@ bool SecCompService::RegisterAppStateObserver() return false; } + sptr appMgrDeathRecipient = new (std::nothrow) AppMgrDeathRecipient(); + if (appMgrDeathRecipient == nullptr) { + SC_LOG_ERROR(LABEL, "Alloc appMgr death observer fail"); + return false; + } + + if (!remoteObject->AddDeathRecipient(appMgrDeathRecipient)) { + SC_LOG_ERROR(LABEL, "Add service death observer fail"); + return false; + } + std::vector list; if (iAppMgr_->GetForegroundApplications(list) == ERR_OK) { for (auto it = list.begin(); it != list.end(); ++it) { diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index c35aa14..80d1d9f 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -32,6 +32,7 @@ ohos_unittest("sec_comp_service_test") { ] sources = [ + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", @@ -94,6 +95,7 @@ ohos_unittest("sec_comp_service_mock_test") { ] sources = [ + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", diff --git a/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h b/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h index e67a1a1..eee66c0 100644 --- a/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h +++ b/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h @@ -21,6 +21,13 @@ namespace OHOS { class MockIRemoteObject : public virtual RefBase { +public: + bool AddDeathRecipient(const sptr& recipient) + { + (void)recipient; + return addResult; + } + bool addResult = true; }; template inline sptr iface_cast(const sptr &object) -- Gitee From 016a823acf2ccd99705ee4064a412669bc4de934 Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 10 Aug 2023 17:52:35 +0800 Subject: [PATCH 51/59] Description: CheckRect Match-id-68dbee403307313bff0601ff02333df5367039b4 --- README_zh.md | 2 +- frameworks/BUILD.gn | 2 +- frameworks/common/include/sec_comp_err.h | 1 + frameworks/common/include/sec_comp_tool.h | 1 - frameworks/common/src/sec_comp_tool.cpp | 31 +- .../include/sec_comp_enhance_adapter.h | 43 +- .../src/sec_comp_enhance_adapter.cpp | 1 + .../include/sec_comp_click_event_parcel.h | 7 +- .../src/location_button.cpp | 18 +- .../security_component/src/paste_button.cpp | 18 +- .../security_component/src/save_button.cpp | 18 +- .../security_component/src/sec_comp_base.cpp | 255 +++---- .../src/sec_comp_click_event_parcel.cpp | 1 + interfaces/inner_api/enhance_kits/BUILD.gn | 2 +- .../enhance_kits/src/sec_comp_enhance_kit.cpp | 2 + .../inner_api/enhance_kits/test/BUILD.gn | 1 + .../include/location_button.h | 16 +- .../security_component/include/paste_button.h | 6 +- .../security_component/include/save_button.h | 20 +- .../include/sec_comp_base.h | 49 +- .../include/sec_comp_info.h | 38 +- .../security_component/include/sec_comp_kit.h | 1 - .../include/sec_comp_ui_register.h | 2 - ...ity_component_service_ipc_interface_code.h | 10 +- .../src/sec_comp_caller_authorization.cpp | 1 - .../src/sec_comp_client.cpp | 10 +- .../src/sec_comp_death_recipient.cpp | 1 + .../security_component/src/sec_comp_kit.cpp | 11 +- .../src/sec_comp_load_callback.cpp | 2 +- .../security_component/src/sec_comp_proxy.cpp | 3 +- .../src/sec_comp_ui_register.cpp | 1 + .../src/location_button_sample_build.cpp | 98 +-- .../unittest/src/location_button_test.cpp | 406 +++++------ .../test/unittest/src/paste_button_test.cpp | 187 ++--- .../test/unittest/src/save_button_test.cpp | 188 ++--- ...ec_comp_register_without_callback_test.cpp | 1 - ...ec_comp_report_click_without_hmac_test.cpp | 1 - security_component.gni | 1 + .../security_component_service/sa/BUILD.gn | 1 - .../sa/sa_main/app_state_observer.cpp | 2 +- .../sa/sa_main/app_state_observer.h | 7 +- .../sa/sa_main/delay_exit_task.cpp | 4 +- .../sa/sa_main/delay_exit_task.h | 3 +- .../sa/sa_main/first_use_dialog.cpp | 4 +- .../sa/sa_main/first_use_dialog.h | 1 - .../sa/sa_main/sec_comp_entity.cpp | 8 +- .../sa/sa_main/sec_comp_entity.h | 3 +- .../sa/sa_main/sec_comp_info_helper.cpp | 62 +- .../sa/sa_main/sec_comp_info_helper.h | 1 + .../sa/sa_main/sec_comp_manager.cpp | 16 +- .../sa/sa_main/sec_comp_manager.h | 2 - .../sa/sa_main/sec_comp_perm_manager.cpp | 1 - .../sa/sa_main/sec_comp_perm_manager.h | 2 - .../sa/sa_main/sec_comp_service.cpp | 3 - .../sa/sa_main/sec_comp_service.h | 1 - .../sa/sa_main/sec_event_handler.cpp | 1 - .../sa/sa_main/sec_event_handler.h | 2 - .../sa/test/BUILD.gn | 1 - .../src/sec_comp_info_helper_test.cpp | 650 +++++++----------- .../unittest/src/sec_comp_manager_test.cpp | 3 - .../src/sec_comp_service_mock_test.cpp | 49 +- 61 files changed, 987 insertions(+), 1296 deletions(-) diff --git a/README_zh.md b/README_zh.md index bd54370..141edb1 100644 --- a/README_zh.md +++ b/README_zh.md @@ -36,7 +36,7 @@ | int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); | 更新安全控件信息 | | int32_t UnregisterSecurityComponent(int32_t scId); | 取消注册安全控件 | | int32_t ReportSecurityComponentClickEvent(int32_t scId, const std::string& componentInfo, const SecCompClickEvent& touchInfo); | 上报点击事件,申请临时授权 | -| int32_t SetEnhanceCfg(SecCompEnhanceCfgBase* cfg); | 设置安全控件增强的配置,供多模服务使用 | +| int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen); | 设置安全控件增强的配置,供多模服务使用 | | int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen); | 获取点击事件的安全增强数据,供多模服务使用 | | bool ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId); | 校验后取消保存控件权限 | diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 3c0b9ea..866b79a 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -83,7 +83,7 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { external_deps = [ "c_utils:utils", "hilog:libhilog", - "ipc:ipc_core", + "ipc:ipc_single", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/frameworks/common/include/sec_comp_err.h b/frameworks/common/include/sec_comp_err.h index 0b93da4..8cacb68 100644 --- a/frameworks/common/include/sec_comp_err.h +++ b/frameworks/common/include/sec_comp_err.h @@ -16,6 +16,7 @@ #define FRAMEWORKS_COMMON_SECURITY_COMPONENT_ERR_H #include + namespace OHOS { namespace Security { namespace SecurityComponent { diff --git a/frameworks/common/include/sec_comp_tool.h b/frameworks/common/include/sec_comp_tool.h index 98aa805..197f67d 100644 --- a/frameworks/common/include/sec_comp_tool.h +++ b/frameworks/common/include/sec_comp_tool.h @@ -15,7 +15,6 @@ #ifndef SECURITY_COMPONENT_TOOL_H #define SECURITY_COMPONENT_TOOL_H -#include #include "sec_comp_info.h" namespace OHOS { diff --git a/frameworks/common/src/sec_comp_tool.cpp b/frameworks/common/src/sec_comp_tool.cpp index d92771d..36d45c4 100644 --- a/frameworks/common/src/sec_comp_tool.cpp +++ b/frameworks/common/src/sec_comp_tool.cpp @@ -22,18 +22,18 @@ namespace OHOS { namespace Security { namespace SecurityComponent { namespace { -static constexpr double PI_CIRCLE = 3.1415926F; -static constexpr double MIN_HSV_DISTANCE = 25.0F; -static constexpr double MAX_RGB_VALUE = 255.0F; +static constexpr double PI_CIRCLE = 3.1415926; +static constexpr double MIN_HSV_DISTANCE = 25.0; +static constexpr double MAX_RGB_VALUE = 255.0; static constexpr uint8_t MAX_TRANSPARENT = 0x99; // 60% -static constexpr double ZERO_DOUBLE = 0.0F; -static constexpr double THIRTY_ANGLE = 30.0F; -static constexpr double SIXTY_ANGLE = 60.0F; -static constexpr double ONE_HUNDRED_TWEENTY_ANGLE = 120.0F; -static constexpr double ONE_HUNDRED_EIGHTY_ANGLE = 180.0F; -static constexpr double TWO_HUNDREDS_FORTY_ANGLE = 240.0F; -static constexpr double THREE_HUNDREDS_SIXTY_ANGLE = 360.0F; -static constexpr double DEFAULT_R = 100.0F; +static constexpr double ZERO_DOUBLE = 0.0; +static constexpr double THIRTY_ANGLE = 30.0; +static constexpr double SIXTY_ANGLE = 60.0; +static constexpr double ONE_HUNDRED_TWEENTY_ANGLE = 120.0; +static constexpr double ONE_HUNDRED_EIGHTY_ANGLE = 180.0; +static constexpr double TWO_HUNDREDS_FORTY_ANGLE = 240.0; +static constexpr double THREE_HUNDREDS_SIXTY_ANGLE = 360.0; +static constexpr double DEFAULT_R = 100.0; constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompTool"}; } @@ -60,9 +60,9 @@ static HsvColor RgbToHsv(uint8_t r, uint8_t g, uint8_t b) double red = static_cast(r) / MAX_RGB_VALUE; double green = static_cast(g) / MAX_RGB_VALUE; double blue = static_cast(b) / MAX_RGB_VALUE; - float max = MaxValue(red, green, blue); - float min = MinValue(red, green, blue); - float delta = max - min; + double max = MaxValue(red, green, blue); + double min = MinValue(red, green, blue); + double delta = max - min; if (max == min) { hsv.h = ZERO_DOUBLE; } else if (max == red) { @@ -127,13 +127,14 @@ bool IsColorSimilar(const SecCompColor& color1, const SecCompColor& color2) HsvColor hsv2 = RgbToHsv(color2.argb.red, color2.argb.green, color2.argb.blue); double distance = HsvDistance(hsv1, hsv2); - SC_LOG_INFO(LABEL, "Compare color %{public}x %{public}x distance %{public}f", + SC_LOG_DEBUG(LABEL, "Compare color %{public}x %{public}x distance %{public}f", color1.value, color2.value, distance); return (distance < MIN_HSV_DISTANCE); } bool IsColorTransparent(const SecCompColor& color) { + SC_LOG_DEBUG(LABEL, "Color %{public}x alpha %{public}x", color.value, color.argb.alpha); return color.argb.alpha < MAX_TRANSPARENT; } } // namespace SecurityComponent diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index 6ffee03..faada7e 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -14,10 +14,10 @@ */ #ifndef SECURITY_COMPONENT_ENHANCE_ADAPTER_H #define SECURITY_COMPONENT_ENHANCE_ADAPTER_H + #include #include "iremote_object.h" #include "nlohmann/json.hpp" -#include "parcel.h" #include "sec_comp_base.h" #include "sec_comp_info.h" @@ -30,58 +30,81 @@ enum EnhanceInterfaceType { SEC_COMP_ENHANCE_CLIENT_INTERFACE = 2, }; -class SecCompEnhanceCfgBase { -}; - +// for multimodalinput to add enhance data to PointerEvent class SecCompInputEnhanceInterface { public: + // for multimodalinput to set enhance cfg which is from security component enhance service virtual int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) = 0; + + // for multimodalinput to get enhance data virtual int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen) = 0; }; +// for security component service to send command to enhance service class SecCompSrvEnhanceInterface { public: + // enable input enhance, then enhance service send config to multimodalinput virtual int32_t EnableInputEnhance() = 0; + + // disable input enhance virtual int32_t DisableInputEnhance() = 0; + + // send click event to enhance service for checking extra data validity virtual int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo) = 0; + + // send component info to enhance service for checking its validity virtual int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, const nlohmann::json& jsonComponent) = 0; + + // get RemoteObject of enhance service to connect it virtual sptr GetEnhanceRemoteObject() = 0; + + // start enhance service virtual void StartEnhanceService() = 0; + + // exit enhance service virtual void ExitEnhanceService() = 0; + + // notify process died virtual void NotifyProcessDied(int32_t pid) = 0; }; +// for client class SecCompClientEnhanceInterface { public: + // preprocess component info which is send to security component service, e.g. RegisterSecurityComponent virtual bool EnhanceDataPreprocess(const uintptr_t caller, std::string& componentInfo) = 0; virtual bool EnhanceDataPreprocess(const uintptr_t caller, int32_t scId, std::string& componentInfo) = 0; + + // regiter scid to enhance client virtual void RegisterScIdEnhance(const uintptr_t caller, int32_t scId) = 0; + // unregiter scid to enhance client virtual void UnregisterScIdEnhance(const uintptr_t caller, int32_t scId) = 0; }; -struct SecCompEnhanceAdapter { +class SecCompEnhanceAdapter final { +public: static void InitEnhanceHandler(EnhanceInterfaceType type); static int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen); static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, uint8_t* enhanceData, uint32_t& enHancedataLen); + static int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo); static int32_t EnableInputEnhance(); static int32_t DisableInputEnhance(); - static int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, const nlohmann::json& jsonComponent); static sptr GetEnhanceRemoteObject(); + static void StartEnhanceService(); + static void ExistEnhanceService(); + static void NotifyProcessDied(int32_t pid); + static bool EnhanceDataPreprocess(std::string& componentInfo); static bool EnhanceDataPreprocess(int32_t scId, std::string& componentInfo); static void RegisterScIdEnhance(int32_t scId); static void UnregisterScIdEnhance(int32_t scId); - static void StartEnhanceService(); - static void ExistEnhanceService(); - static void NotifyProcessDied(int32_t pid); - static SecCompInputEnhanceInterface* inputHandler; static bool isEnhanceInputHandlerInit; diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index b319653..28d1aa9 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_enhance_adapter.h" + #include #include "sec_comp_err.h" #include "sec_comp_log.h" diff --git a/frameworks/security_component/include/sec_comp_click_event_parcel.h b/frameworks/security_component/include/sec_comp_click_event_parcel.h index 3e1d1a0..ea39a20 100644 --- a/frameworks/security_component/include/sec_comp_click_event_parcel.h +++ b/frameworks/security_component/include/sec_comp_click_event_parcel.h @@ -12,8 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef SECURITY_COMPONENT_TOUCH_INFO_PARCEL_H -#define SECURITY_COMPONENT_TOUCH_INFO_PARCEL_H +#ifndef SECURITY_COMPONENT_CLICK_INFO_PARCEL_H +#define SECURITY_COMPONENT_CLICK_INFO_PARCEL_H + #include "parcel.h" #include "sec_comp_info.h" @@ -34,4 +35,4 @@ struct SecCompClickEventParcel final : public Parcelable { } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // SECURITY_COMPONENT_TOUCH_INFO_PARCEL_H +#endif // SECURITY_COMPONENT_CLICK_INFO_PARCEL_H diff --git a/frameworks/security_component/src/location_button.cpp b/frameworks/security_component/src/location_button.cpp index 14f9a56..b62b9e9 100644 --- a/frameworks/security_component/src/location_button.cpp +++ b/frameworks/security_component/src/location_button.cpp @@ -12,39 +12,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "location_button.h" + #include -#include "sec_comp_err.h" #include "sec_comp_log.h" -#include "sec_comp_tool.h" namespace OHOS { namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButton"}; -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; } bool LocationButton::IsParamValid() { - if ((static_cast(text_) <= LocationDesc::UNKNOWN_TEXT) || - (static_cast(text_) >= LocationDesc::MAX_LABEL_TYPE) || - (static_cast(icon_) <= LocationIcon::UNKNOWN_ICON) || - (static_cast(icon_) >= LocationIcon::MAX_ICON_TYPE)) { + if ((text_ <= UNKNOWN_TEXT) || (static_cast(text_) >= LocationDesc::MAX_LABEL_TYPE) || + (icon_ <= UNKNOWN_ICON) || (static_cast(icon_) >= LocationIcon::MAX_ICON_TYPE)) { return false; } return true; } -bool LocationButton::CompareComponentBasicInfo(SecCompBase *other) const +bool LocationButton::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const { - if (!SecCompBase::CompareComponentBasicInfo(other)) { + if (!SecCompBase::CompareComponentBasicInfo(other, isRectCheck)) { SC_LOG_ERROR(LABEL, "SecComp base not equal."); return false; } diff --git a/frameworks/security_component/src/paste_button.cpp b/frameworks/security_component/src/paste_button.cpp index ea20e3f..41941dc 100644 --- a/frameworks/security_component/src/paste_button.cpp +++ b/frameworks/security_component/src/paste_button.cpp @@ -12,39 +12,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "paste_button.h" + #include -#include "sec_comp_err.h" #include "sec_comp_log.h" -#include "sec_comp_tool.h" namespace OHOS { namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "PasteButton"}; -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; } bool PasteButton::IsParamValid() { - if ((static_cast(text_) <= PasteDesc::UNKNOWN_TEXT) || - (static_cast(text_) >= PasteDesc::MAX_LABEL_TYPE) || - (static_cast(icon_) <= PasteIcon::UNKNOWN_ICON) || - (static_cast(icon_) >= PasteIcon::MAX_ICON_TYPE)) { + if ((text_ <= UNKNOWN_TEXT) || (static_cast(text_) >= PasteDesc::MAX_LABEL_TYPE) || + (icon_ <= UNKNOWN_ICON) || (static_cast(icon_) >= PasteIcon::MAX_ICON_TYPE)) { return false; } return true; } -bool PasteButton::CompareComponentBasicInfo(SecCompBase *other) const +bool PasteButton::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const { - if (!SecCompBase::CompareComponentBasicInfo(other)) { + if (!SecCompBase::CompareComponentBasicInfo(other, isRectCheck)) { SC_LOG_ERROR(LABEL, "SecComp base not equal."); return false; } diff --git a/frameworks/security_component/src/save_button.cpp b/frameworks/security_component/src/save_button.cpp index 7c7a5ca..22fc45c 100644 --- a/frameworks/security_component/src/save_button.cpp +++ b/frameworks/security_component/src/save_button.cpp @@ -12,39 +12,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "save_button.h" + #include -#include "sec_comp_err.h" #include "sec_comp_log.h" -#include "sec_comp_tool.h" namespace OHOS { namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SaveButton"}; -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; } bool SaveButton::IsParamValid() { - if ((static_cast(text_) <= SaveDesc::UNKNOWN_TEXT) || - (static_cast(text_) >= SaveDesc::MAX_LABEL_TYPE) || - (static_cast(icon_) <= SaveIcon::UNKNOWN_ICON) || - (static_cast(icon_) >= SaveIcon::MAX_ICON_TYPE)) { + if ((text_ <= UNKNOWN_TEXT) || (static_cast(text_) >= SaveDesc::MAX_LABEL_TYPE) || + (icon_ <= UNKNOWN_ICON) || (static_cast(icon_) >= SaveIcon::MAX_ICON_TYPE)) { return false; } return true; } -bool SaveButton::CompareComponentBasicInfo(SecCompBase *other) const +bool SaveButton::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const { - if (!SecCompBase::CompareComponentBasicInfo(other)) { + if (!SecCompBase::CompareComponentBasicInfo(other, isRectCheck)) { SC_LOG_ERROR(LABEL, "SecComp base not equal."); return false; } diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index e25a068..4c12e77 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_comp_base.h" + #include "sec_comp_err.h" #include "sec_comp_log.h" @@ -22,47 +22,44 @@ namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompBase"}; -static const std::string JSON_RECT = "rect"; -static const std::string JSON_SC_TYPE = "type"; -static const std::string JSON_NODE_ID = "nodeId"; -static const std::string JSON_RECT_X = "x"; -static const std::string JSON_RECT_Y = "y"; -static const std::string JSON_RECT_WIDTH = "width"; -static const std::string JSON_RECT_HEIGHT = "height"; -static const std::string JSON_WINDOW_RECT = "windowRect"; - -static const std::string JSON_SIZE_TAG = "size"; -static const std::string JSON_FONT_SIZE_TAG = "fontSize"; -static const std::string JSON_ICON_SIZE_TAG = "iconSize"; -static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; -static const std::string JSON_PADDING_LEFT_TAG = "left"; -static const std::string JSON_PADDING_TOP_TAG = "top"; -static const std::string JSON_PADDING_RIGHT_TAG = "right"; -static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; -static const std::string JSON_RECT_WIDTH_TAG = "width"; -static const std::string JSON_RECT_HEIGHT_TAG = "height"; - -static const std::string JSON_COLORS_TAG = "colors"; -static const std::string JSON_FONT_COLOR_TAG = "fontColor"; -static const std::string JSON_ICON_COLOR_TAG = "iconColor"; -static const std::string JSON_BG_COLOR_TAG = "bgColor"; - -static const std::string JSON_BORDER_TAG = "border"; -static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; -static const std::string JSON_PARENT_TAG = "parent"; -static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; - -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; } +const std::string JsonTagConstants::JSON_RECT = "rect"; +const std::string JsonTagConstants::JSON_SC_TYPE = "type"; +const std::string JsonTagConstants::JSON_NODE_ID = "nodeId"; +const std::string JsonTagConstants::JSON_RECT_X = "x"; +const std::string JsonTagConstants::JSON_RECT_Y = "y"; +const std::string JsonTagConstants::JSON_RECT_WIDTH = "width"; +const std::string JsonTagConstants::JSON_RECT_HEIGHT = "height"; +const std::string JsonTagConstants::JSON_WINDOW_RECT = "windowRect"; +const std::string JsonTagConstants::JSON_SIZE_TAG = "size"; +const std::string JsonTagConstants::JSON_FONT_SIZE_TAG = "fontSize"; +const std::string JsonTagConstants::JSON_ICON_SIZE_TAG = "iconSize"; +const std::string JsonTagConstants::JSON_PADDING_SIZE_TAG = "paddingSize"; +const std::string JsonTagConstants::JSON_PADDING_LEFT_TAG = "left"; +const std::string JsonTagConstants::JSON_PADDING_TOP_TAG = "top"; +const std::string JsonTagConstants::JSON_PADDING_RIGHT_TAG = "right"; +const std::string JsonTagConstants::JSON_PADDING_BOTTOM_TAG = "bottom"; +const std::string JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; +const std::string JsonTagConstants::JSON_RECT_WIDTH_TAG = "width"; +const std::string JsonTagConstants::JSON_RECT_HEIGHT_TAG = "height"; +const std::string JsonTagConstants::JSON_COLORS_TAG = "colors"; +const std::string JsonTagConstants::JSON_FONT_COLOR_TAG = "fontColor"; +const std::string JsonTagConstants::JSON_ICON_COLOR_TAG = "iconColor"; +const std::string JsonTagConstants::JSON_BG_COLOR_TAG = "bgColor"; +const std::string JsonTagConstants::JSON_BORDER_TAG = "border"; +const std::string JsonTagConstants::JSON_BORDER_WIDTH_TAG = "borderWidth"; +const std::string JsonTagConstants::JSON_PARENT_TAG = "parent"; +const std::string JsonTagConstants::JSON_PARENT_EFFECT_TAG = "parentEffect"; +const std::string JsonTagConstants::JSON_STYLE_TAG = "style"; +const std::string JsonTagConstants::JSON_TEXT_TAG = "text"; +const std::string JsonTagConstants::JSON_ICON_TAG = "icon"; +const std::string JsonTagConstants::JSON_BG_TAG = "bg"; + bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res) { if ((json.find(tag) == json.end()) || !json.at(tag).is_number_float()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } @@ -73,7 +70,7 @@ bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res) { if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } @@ -84,7 +81,7 @@ bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool& res) { if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } @@ -95,21 +92,21 @@ bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } auto jsonPadding = json.at(tag); - if (!ParseDimension(jsonPadding, JSON_PADDING_TOP_TAG, res.top)) { + if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, res.top)) { return false; } - if (!ParseDimension(jsonPadding, JSON_PADDING_RIGHT_TAG, res.right)) { + if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, res.right)) { return false; } - if (!ParseDimension(jsonPadding, JSON_PADDING_BOTTOM_TAG, res.bottom)) { + if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, res.bottom)) { return false; } - if (!ParseDimension(jsonPadding, JSON_PADDING_LEFT_TAG, res.left)) { + if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, res.left)) { return false; } return true; @@ -118,17 +115,17 @@ bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& ta bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } auto jsonColors = json.at(tag); - if (!ParseColor(jsonColors, JSON_FONT_COLOR_TAG, fontColor_)) { + if (!ParseColor(jsonColors, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_)) { return false; } - if (!ParseColor(jsonColors, JSON_ICON_COLOR_TAG, iconColor_)) { + if (!ParseColor(jsonColors, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_)) { return false; } - if (!ParseColor(jsonColors, JSON_BG_COLOR_TAG, bgColor_)) { + if (!ParseColor(jsonColors, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_)) { return false; } return true; @@ -137,34 +134,34 @@ bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag bool SecCompBase::ParseBorders(const nlohmann::json& json, const std::string& tag) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } auto jsonBorder = json.at(tag); - return ParseDimension(jsonBorder, JSON_BORDER_WIDTH_TAG, borderWidth_); + return ParseDimension(jsonBorder, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_); } bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } auto jsonSize = json.at(tag); - if (!ParseDimension(jsonSize, JSON_FONT_SIZE_TAG, fontSize_)) { + if (!ParseDimension(jsonSize, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_)) { return false; } - if (!ParseDimension(jsonSize, JSON_ICON_SIZE_TAG, iconSize_)) { + if (!ParseDimension(jsonSize, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_)) { return false; } - if (!ParseDimension(jsonSize, JSON_TEXT_ICON_PADDING_TAG, textIconSpace_)) { + if (!ParseDimension(jsonSize, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_)) { return false; } - if (!ParsePadding(jsonSize, JSON_PADDING_SIZE_TAG, padding_)) { + if (!ParsePadding(jsonSize, JsonTagConstants::JSON_PADDING_SIZE_TAG, padding_)) { return false; } @@ -174,34 +171,34 @@ bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } auto jsonParent = json.at(tag); - return ParseBool(jsonParent, JSON_PARENT_EFFECT_TAG, parentEffect_); + return ParseBool(jsonParent, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_); } bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has not %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } auto jsonSize = json.at(tag); - if (!ParseDimension(jsonSize, JSON_RECT_X, rect.x_)) { + if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_X, rect.x_)) { return false; } - if (!ParseDimension(jsonSize, JSON_RECT_Y, rect.y_)) { + if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_Y, rect.y_)) { return false; } - if (!ParseDimension(jsonSize, JSON_RECT_WIDTH, rect.width_)) { + if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_WIDTH, rect.width_)) { return false; } - if (!ParseDimension(jsonSize, JSON_RECT_HEIGHT, rect.height_)) { + if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_HEIGHT, rect.height_)) { return false; } @@ -211,45 +208,45 @@ bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) { SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonSrc.dump().c_str()); - if ((jsonSrc.find(JSON_SC_TYPE) == jsonSrc.end()) || - !jsonSrc.at(JSON_SC_TYPE).is_number()) { + if ((jsonSrc.find(JsonTagConstants::JSON_SC_TYPE) == jsonSrc.end()) || + !jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).is_number()) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_SC_TYPE.c_str()); return false; } - int32_t value = jsonSrc.at(JSON_SC_TYPE).get(); + int32_t value = jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).get(); if ((value <= static_cast(SecCompType::UNKNOWN_SC_TYPE)) || (value >= static_cast(SecCompType::MAX_SC_TYPE))) { + SC_LOG_ERROR(LABEL, "scType is invalid."); return false; } type_ = static_cast(value); - if ((jsonSrc.find(JSON_NODE_ID) == jsonSrc.end()) || - !jsonSrc.at(JSON_NODE_ID).is_number()) { + if ((jsonSrc.find(JsonTagConstants::JSON_NODE_ID) == jsonSrc.end()) || + !jsonSrc.at(JsonTagConstants::JSON_NODE_ID).is_number()) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_NODE_ID.c_str()); return false; } - nodeId_ = jsonSrc.at(JSON_NODE_ID).get(); + nodeId_ = jsonSrc.at(JsonTagConstants::JSON_NODE_ID).get(); - if (!ParseRect(jsonSrc, JSON_RECT, rect_)) { + if (!ParseRect(jsonSrc, JsonTagConstants::JSON_RECT, rect_)) { return false; } - if (!ParseRect(jsonSrc, JSON_WINDOW_RECT, windowRect_)) { + if (!ParseRect(jsonSrc, JsonTagConstants::JSON_WINDOW_RECT, windowRect_)) { return false; } - if (!ParseSize(jsonSrc, JSON_SIZE_TAG)) { + if (!ParseSize(jsonSrc, JsonTagConstants::JSON_SIZE_TAG)) { return false; } - - if (!ParseColors(jsonSrc, JSON_COLORS_TAG)) { + if (!ParseColors(jsonSrc, JsonTagConstants::JSON_COLORS_TAG)) { return false; } - - if (!ParseBorders(jsonSrc, JSON_BORDER_TAG)) { + if (!ParseBorders(jsonSrc, JsonTagConstants::JSON_BORDER_TAG)) { return false; } - if (!ParseParent(jsonSrc, JSON_PARENT_TAG)) { + if (!ParseParent(jsonSrc, JsonTagConstants::JSON_PARENT_TAG)) { return false; } - - if (!ParseStyle(jsonSrc, JSON_STYLE_TAG)) { + if (!ParseStyle(jsonSrc, JsonTagConstants::JSON_STYLE_TAG)) { return false; } @@ -258,51 +255,51 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) void SecCompBase::ToJson(nlohmann::json& jsonRes) const { - jsonRes[JSON_SC_TYPE] = type_; - jsonRes[JSON_NODE_ID] = nodeId_; - jsonRes[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, rect_.x_}, - {JSON_RECT_Y, rect_.y_}, - {JSON_RECT_WIDTH, rect_.width_}, - {JSON_RECT_HEIGHT, rect_.height_} + jsonRes[JsonTagConstants::JSON_SC_TYPE] = type_; + jsonRes[JsonTagConstants::JSON_NODE_ID] = nodeId_; + jsonRes[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, rect_.x_}, + {JsonTagConstants::JSON_RECT_Y, rect_.y_}, + {JsonTagConstants::JSON_RECT_WIDTH, rect_.width_}, + {JsonTagConstants::JSON_RECT_HEIGHT, rect_.height_} }; - jsonRes[JSON_WINDOW_RECT] = nlohmann::json { - {JSON_RECT_X, windowRect_.x_}, - {JSON_RECT_Y, windowRect_.y_}, - {JSON_RECT_WIDTH, windowRect_.width_}, - {JSON_RECT_HEIGHT, windowRect_.height_} + jsonRes[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, windowRect_.x_}, + {JsonTagConstants::JSON_RECT_Y, windowRect_.y_}, + {JsonTagConstants::JSON_RECT_WIDTH, windowRect_.width_}, + {JsonTagConstants::JSON_RECT_HEIGHT, windowRect_.height_} }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, padding_.top }, - { JSON_PADDING_RIGHT_TAG, padding_.right }, - { JSON_PADDING_BOTTOM_TAG, padding_.bottom }, - { JSON_PADDING_LEFT_TAG, padding_.left }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left }, }; - jsonRes[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, fontSize_ }, - { JSON_ICON_SIZE_TAG, iconSize_ }, - { JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonRes[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_ }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_ }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; - jsonRes[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, fontColor_.value }, - { JSON_ICON_COLOR_TAG, iconColor_.value }, - { JSON_BG_COLOR_TAG, bgColor_.value } + jsonRes[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value }, + { JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value } }; - jsonRes[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, borderWidth_ }, + jsonRes[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_ }, }; - jsonRes[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, parentEffect_ }, + jsonRes[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_ }, }; - jsonRes[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, text_ }, - { JSON_ICON_TAG, icon_ }, - { JSON_BG_TAG, bg_ }, + jsonRes[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, text_ }, + { JsonTagConstants::JSON_ICON_TAG, icon_ }, + { JsonTagConstants::JSON_BG_TAG, bg_ }, }; } @@ -313,18 +310,27 @@ std::string SecCompBase::ToJsonStr() const return json.dump(); } -bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other) const +bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const { if (other == nullptr) { - SC_LOG_ERROR(LABEL, "other is invalid."); + SC_LOG_ERROR(LABEL, "other is nullptr."); return false; } + SecCompRect rect = other->rect_; + SecCompRect windowRect = other->windowRect_; + if (isRectCheck) { + rect = rect_; + windowRect = windowRect_; + } + auto leftValue = std::tie(type_, fontSize_, iconSize_, textIconSpace_, padding_.top, padding_.right, - padding_.bottom, padding_.left, fontColor_.value, bgColor_.value, iconColor_.value, borderWidth_); + padding_.bottom, padding_.left, fontColor_.value, bgColor_.value, iconColor_.value, borderWidth_, + rect, windowRect); auto rightValue = std::tie(other->type_, other->fontSize_, other->iconSize_, other->textIconSpace_, other->padding_.top, other->padding_.right, other->padding_.bottom, other->padding_.left, - other->fontColor_.value, other->bgColor_.value, other->iconColor_.value, other->borderWidth_); + other->fontColor_.value, other->bgColor_.value, other->iconColor_.value, other->borderWidth_, + other->rect_, other->windowRect_); return (leftValue == rightValue); } @@ -332,23 +338,26 @@ bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other) const bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag) { if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { - SC_LOG_ERROR(LABEL, "has no %{public}s.", tag.c_str()); + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } auto jsonStyle = json.at(tag); - if (!(jsonStyle.at(JSON_TEXT_TAG).is_number() && jsonStyle.at(JSON_ICON_TAG).is_number() && - jsonStyle.at(JSON_BG_TAG).is_number())) { - SC_LOG_ERROR(LABEL, "%{public}s is not number.", tag.c_str()); + if (!(jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).is_number() && + jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).is_number() && + jsonStyle.at(JsonTagConstants::JSON_BG_TAG).is_number())) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - text_ = jsonStyle.at(JSON_TEXT_TAG).get(); - icon_ = jsonStyle.at(JSON_ICON_TAG).get(); - if (!(IsParamValid())) { + text_ = jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).get(); + icon_ = jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).get(); + if (!IsParamValid()) { + SC_LOG_ERROR(LABEL, "text or icon is invalid."); return false; } - bg_ = static_cast(jsonStyle.at(JSON_BG_TAG).get()); + bg_ = static_cast(jsonStyle.at(JsonTagConstants::JSON_BG_TAG).get()); if ((bg_ <= SecCompBackground::UNKNOWN_BG) || (bg_ >= SecCompBackground::MAX_BG_TYPE)) { + SC_LOG_ERROR(LABEL, "bg is invalid."); return false; } diff --git a/frameworks/security_component/src/sec_comp_click_event_parcel.cpp b/frameworks/security_component/src/sec_comp_click_event_parcel.cpp index 5fc9a42..55390c9 100644 --- a/frameworks/security_component/src/sec_comp_click_event_parcel.cpp +++ b/frameworks/security_component/src/sec_comp_click_event_parcel.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_click_event_parcel.h" + #include "sec_comp_log.h" #include "securec.h" diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index 507104a..03d5acf 100644 --- a/interfaces/inner_api/enhance_kits/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -50,7 +50,7 @@ ohos_shared_library("libsecurity_component_enhance_sdk") { external_deps = [ "c_utils:utils", "hilog:libhilog", - "ipc:ipc_core" + "ipc:ipc_single" ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp index dabdcf7..4b16511 100644 --- a/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp +++ b/interfaces/inner_api/enhance_kits/src/sec_comp_enhance_kit.cpp @@ -12,7 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "sec_comp_enhance_kit.h" + #include "sec_comp_enhance_adapter.h" extern "C" void InitSecCompClientEnhance() diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn index 6bf644b..bf1409c 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -35,6 +35,7 @@ ohos_unittest("sec_comp_enhance_sdk_test") { external_deps = [ "c_utils:utils", "hilog:libhilog", + "ipc:ipc_single", ] if (current_cpu == "arm64") { defines = [ "_ARM64_" ] diff --git a/interfaces/inner_api/security_component/include/location_button.h b/interfaces/inner_api/security_component/include/location_button.h index f55e984..927189c 100644 --- a/interfaces/inner_api/security_component/include/location_button.h +++ b/interfaces/inner_api/security_component/include/location_button.h @@ -24,13 +24,11 @@ namespace OHOS { namespace Security { namespace SecurityComponent { enum class LocationDesc : int32_t { - UNKNOWN_TEXT = -2, - NO_TEXT = -1, - CURRENT_LOCATION = 0, - ADD_LOCATION = 4, - SELECT_LOCATION = 1, - SHARE_LOCATION = 2, - SEND_LOCATION = 3, + CURRENT_LOCATION = 0, + ADD_LOCATION = 1, + SELECT_LOCATION = 2, + SHARE_LOCATION = 3, + SEND_LOCATION = 4, LOCATING = 5, LOCATION = 6, SEND_CURRENT_LOCATION = 7, @@ -41,8 +39,6 @@ enum class LocationDesc : int32_t { }; enum class LocationIcon : int32_t { - UNKNOWN_ICON = -2, - NO_ICON = -1, FILLED_ICON = 0, LINE_ICON = 1, MAX_ICON_TYPE @@ -51,7 +47,7 @@ enum class LocationIcon : int32_t { class LocationButton : public SecCompBase { public: virtual bool IsParamValid() override; - virtual bool CompareComponentBasicInfo(SecCompBase *other) const override; + virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; diff --git a/interfaces/inner_api/security_component/include/paste_button.h b/interfaces/inner_api/security_component/include/paste_button.h index d096d40..241a316 100644 --- a/interfaces/inner_api/security_component/include/paste_button.h +++ b/interfaces/inner_api/security_component/include/paste_button.h @@ -23,15 +23,11 @@ namespace OHOS { namespace Security { namespace SecurityComponent { enum class PasteDesc : int32_t { - UNKNOWN_TEXT = -2, - NO_TEXT = -1, PASTE = 0, MAX_LABEL_TYPE }; enum class PasteIcon : int32_t { - UNKNOWN_ICON = -2, - NO_ICON = -1, FILLED_ICON = 0, LINE_ICON = 1, MAX_ICON_TYPE @@ -40,7 +36,7 @@ enum class PasteIcon : int32_t { class PasteButton : public SecCompBase { public: virtual bool IsParamValid() override; - virtual bool CompareComponentBasicInfo(SecCompBase *other) const override; + virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h index fc7951e..ea7b5f5 100644 --- a/interfaces/inner_api/security_component/include/save_button.h +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -24,22 +24,18 @@ namespace OHOS { namespace Security { namespace SecurityComponent { enum class SaveDesc : int32_t { - UNKNOWN_TEXT = -2, - NO_TEXT = -1, DOWNLOAD = 0, - DOWNLOAD_FILE, - SAVE, - SAVE_IMAGE, - SAVE_FILE, - DOWNLOAD_AND_SHARE, - RECEIVE, - CONTINUE_TO_RECEIVE, + DOWNLOAD_FILE = 1, + SAVE = 2, + SAVE_IMAGE = 3, + SAVE_FILE = 4, + DOWNLOAD_AND_SHARE = 5, + RECEIVE = 6, + CONTINUE_TO_RECEIVE = 7, MAX_LABEL_TYPE }; enum class SaveIcon : int32_t { - UNKNOWN_ICON = -2, - NO_ICON = -1, FILLED_ICON = 0, LINE_ICON = 1, MAX_ICON_TYPE @@ -48,7 +44,7 @@ enum class SaveIcon : int32_t { class SaveButton : public SecCompBase { public: virtual bool IsParamValid() override; - virtual bool CompareComponentBasicInfo(SecCompBase *other) const override; + virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: bool ParseStyle(const nlohmann::json& json, const std::string& tag); }; diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index 87ae211..7449a00 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_base.h +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -21,6 +21,11 @@ namespace OHOS { namespace Security { namespace SecurityComponent { +constexpr int32_t UNKNOWN_TEXT = -2; +constexpr int32_t NO_TEXT = -1; +constexpr int32_t UNKNOWN_ICON = -2; +constexpr int32_t NO_ICON = -1; + enum class SecCompBackground { UNKNOWN_BG = -2, NO_BG_TYPE = -1, @@ -30,6 +35,44 @@ enum class SecCompBackground { MAX_BG_TYPE }; +class JsonTagConstants final { +public: + static const std::string JSON_RECT; + static const std::string JSON_SC_TYPE; + static const std::string JSON_NODE_ID; + static const std::string JSON_RECT_X; + static const std::string JSON_RECT_Y; + static const std::string JSON_RECT_WIDTH; + static const std::string JSON_RECT_HEIGHT; + static const std::string JSON_WINDOW_RECT; + + static const std::string JSON_SIZE_TAG; + static const std::string JSON_FONT_SIZE_TAG; + static const std::string JSON_ICON_SIZE_TAG; + static const std::string JSON_PADDING_SIZE_TAG; + static const std::string JSON_PADDING_LEFT_TAG; + static const std::string JSON_PADDING_TOP_TAG; + static const std::string JSON_PADDING_RIGHT_TAG; + static const std::string JSON_PADDING_BOTTOM_TAG; + static const std::string JSON_TEXT_ICON_PADDING_TAG; + static const std::string JSON_RECT_WIDTH_TAG; + static const std::string JSON_RECT_HEIGHT_TAG; + static const std::string JSON_COLORS_TAG; + static const std::string JSON_FONT_COLOR_TAG; + static const std::string JSON_ICON_COLOR_TAG; + static const std::string JSON_BG_COLOR_TAG; + + static const std::string JSON_BORDER_TAG; + static const std::string JSON_BORDER_WIDTH_TAG; + static const std::string JSON_PARENT_TAG; + static const std::string JSON_PARENT_EFFECT_TAG; + + static const std::string JSON_STYLE_TAG; + static const std::string JSON_TEXT_TAG; + static const std::string JSON_ICON_TAG; + static const std::string JSON_BG_TAG; +}; + class SecCompBase { public: SecCompBase() = default; @@ -37,7 +80,7 @@ public: bool FromJson(const nlohmann::json& jsonSrc); void ToJson(nlohmann::json& jsonRes) const; std::string ToJsonStr(void) const; - virtual bool CompareComponentBasicInfo(SecCompBase *other) const; + virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const; void SetValid(bool valid) { isValid_ = valid; @@ -70,8 +113,8 @@ public: SecCompRect windowRect_; bool isValid_ = false; - int32_t text_ = UNKNOWN_TYPE; - int32_t icon_ = UNKNOWN_TYPE; + int32_t text_ = UNKNOWN_TEXT; + int32_t icon_ = UNKNOWN_ICON; SecCompBackground bg_ = SecCompBackground::UNKNOWN_BG; int32_t nodeId_ = 0; diff --git a/interfaces/inner_api/security_component/include/sec_comp_info.h b/interfaces/inner_api/security_component/include/sec_comp_info.h index 173c57d..d02ebb2 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_info.h +++ b/interfaces/inner_api/security_component/include/sec_comp_info.h @@ -16,18 +16,19 @@ #ifndef SECURITY_COMPONENT_INFO_H #define SECURITY_COMPONENT_INFO_H +#include + namespace OHOS { namespace Security { namespace SecurityComponent { static constexpr int32_t INVALID_SC_ID = -1; using DimensionT = double; // unit is vp -static constexpr DimensionT DEFAULT_DIMENSION = 0.0f; -static constexpr DimensionT MIN_FONT_SIZE = 12.0f; -static constexpr DimensionT MIN_ICON_SIZE = 12.0f; -static constexpr DimensionT MIN_PADDING_SIZE = 0.0f; -static constexpr DimensionT MIN_PADDING_WITHOUT_BG = 4.0f; +static constexpr DimensionT DEFAULT_DIMENSION = 0.0; +static constexpr DimensionT MIN_FONT_SIZE = 12.0; +static constexpr DimensionT MIN_ICON_SIZE = 12.0; +static constexpr DimensionT MIN_PADDING_SIZE = 0.0; +static constexpr DimensionT MIN_PADDING_WITHOUT_BG = 4.0; static constexpr uint32_t MAX_EXTRA_SIZE = 0x1000; -static constexpr int32_t UNKNOWN_TYPE = -2; struct PaddingSize { DimensionT top = DEFAULT_DIMENSION; @@ -56,27 +57,24 @@ union SecCompColor { inline bool IsComponentTypeValid(int32_t type) { - if (!(type > UNKNOWN_SC_TYPE) || !(type < MAX_SC_TYPE)) { - return false; - } - return true; + return (type > UNKNOWN_SC_TYPE && type < MAX_SC_TYPE); } inline bool GreatOrEqual(double left, double right) { - constexpr double epsilon = -0.001f; + constexpr double epsilon = -0.001; return (left - right) > epsilon; } inline bool GreatNotEqual(double left, double right) { - constexpr double epsilon = 0.001f; + constexpr double epsilon = 0.001; return (left - right) > epsilon; } inline bool IsEqual(double left, double right) { - constexpr double epsilon = 0.001f; + constexpr double epsilon = 0.001; if (left > right) { return (left - right) < epsilon; } else if (right > left) { @@ -97,10 +95,16 @@ public: GreatOrEqual(y, y_) && GreatOrEqual((y_ + height_), y)); }; - DimensionT x_ = 0.0F; - DimensionT y_ = 0.0F; - DimensionT width_ = 0.0F; - DimensionT height_ = 0.0F; + bool operator==(const SecCompRect& other) const + { + return (IsEqual(x_, other.x_)) && (IsEqual(y_, other.y_)) && + (IsEqual(width_, other.width_)) && (IsEqual(height_, other.height_)); + } + + DimensionT x_ = 0.0; + DimensionT y_ = 0.0; + DimensionT width_ = 0.0; + DimensionT height_ = 0.0; }; struct ExtraInfo { diff --git a/interfaces/inner_api/security_component/include/sec_comp_kit.h b/interfaces/inner_api/security_component/include/sec_comp_kit.h index 1129c9f..80c20c2 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_kit.h +++ b/interfaces/inner_api/security_component/include/sec_comp_kit.h @@ -25,7 +25,6 @@ namespace OHOS { namespace Security { namespace SecurityComponent { class SecCompKit { - public: static int32_t RegisterSecurityComponent(SecCompType type, std::string& componentInfo, int32_t& scId); static int32_t UpdateSecurityComponent(int32_t scId, std::string& componentInfo); diff --git a/interfaces/inner_api/security_component/include/sec_comp_ui_register.h b/interfaces/inner_api/security_component/include/sec_comp_ui_register.h index c009ed7..b415bb1 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_ui_register.h +++ b/interfaces/inner_api/security_component/include/sec_comp_ui_register.h @@ -15,8 +15,6 @@ #ifndef INTERFACES_INNER_API_SECURITY_COMPONENT_UI_REGISTER_H #define INTERFACES_INNER_API_SECURITY_COMPONENT_UI_REGISTER_H -#include -#include #include "i_sec_comp_probe.h" namespace OHOS { diff --git a/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h index 26abf52..a873e90 100644 --- a/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h +++ b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h @@ -22,11 +22,11 @@ namespace Security { namespace SecurityComponent { enum SecurityComponentServiceInterfaceCode { REGISTER_SECURITY_COMPONENT = 0, - UPDATE_SECURITY_COMPONENT, - UNREGISTER_SECURITY_COMPONENT, - REPORT_SECURITY_COMPONENT_CLICK_EVENT, - GET_SECURITY_COMPONENT_ENHANCE_OBJECT, - VERIFY_TEMP_SAVE_PERMISSION, + UPDATE_SECURITY_COMPONENT = 1, + UNREGISTER_SECURITY_COMPONENT = 2, + REPORT_SECURITY_COMPONENT_CLICK_EVENT = 3, + GET_SECURITY_COMPONENT_ENHANCE_OBJECT = 4, + VERIFY_TEMP_SAVE_PERMISSION = 5, }; enum EnhanceInterfaceCode { diff --git a/interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp b/interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp index def9e78..b98be28 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_caller_authorization.cpp @@ -19,7 +19,6 @@ namespace OHOS { namespace Security { namespace SecurityComponent { - namespace { static constexpr int32_t MAX_FUNC_ASM_SIZE = 0x250; static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp index 6e9be79..0e20cee 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_client.h" + #include "iservice_registry.h" #include "sec_comp_load_callback.h" #include "sec_comp_log.h" @@ -109,7 +110,6 @@ sptr SecCompClient::GetEnhanceRemoteObject(bool doLoadSa) { auto proxy = GetProxy(doLoadSa); if (proxy == nullptr) { - SC_LOG_INFO(LABEL, "Proxy is null"); return nullptr; } @@ -140,7 +140,7 @@ bool SecCompClient::StartLoadSecCompSa() SC_LOG_ERROR(LABEL, "LoadSystemAbility %{public}d failed", SA_ID_SECURITY_COMPONENT_SERVICE); return false; } - SC_LOG_INFO(LABEL, "Notify samgr load sa %{public}d success", SA_ID_SECURITY_COMPONENT_SERVICE); + SC_LOG_INFO(LABEL, "Notify samgr load sa %{public}d, waiting for service start", SA_ID_SECURITY_COMPONENT_SERVICE); return true; } @@ -154,7 +154,7 @@ bool SecCompClient::TryToGetSecCompSa() auto secCompSa = sam->CheckSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE); if (secCompSa == nullptr) { - SC_LOG_INFO(LABEL, "Get secComp sa return null"); + SC_LOG_INFO(LABEL, "Service is not start."); return false; } GetProxyFromRemoteObject(secCompSa); @@ -176,10 +176,7 @@ void SecCompClient::WaitForSecCompSa() void SecCompClient::FinishStartSASuccess(const sptr& remoteObject) { - SC_LOG_INFO(LABEL, "Get security component sa success."); - GetProxyFromRemoteObject(remoteObject); - // get lock which wait_for release and send a notice so that wait_for can out of block std::unique_lock lock(cvLock_); readyFlag_ = true; @@ -189,7 +186,6 @@ void SecCompClient::FinishStartSASuccess(const sptr& remoteObject void SecCompClient::FinishStartSAFail() { SC_LOG_ERROR(LABEL, "get security component sa failed."); - // get lock which wait_for release and send a notice std::unique_lock lock(cvLock_); readyFlag_ = true; diff --git a/interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp b/interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp index d1c21ef..bbdf7fc 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_death_recipient.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_death_recipient.h" + #include "sec_comp_client.h" namespace OHOS { diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp index eda6bd7..05b7bd2 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_kit.h" + #include "hisysevent.h" #include "ipc_skeleton.h" #include "sec_comp_caller_authorization.h" @@ -47,10 +48,9 @@ int32_t SecCompKit::RegisterSecurityComponent(SecCompType type, int32_t res = SecCompClient::GetInstance().RegisterSecurityComponent(type, componentInfo, scId); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "register security component fail, error: %{public}d", res); - } else { - SecCompEnhanceAdapter::RegisterScIdEnhance(scId); + return res; } - + SecCompEnhanceAdapter::RegisterScIdEnhance(scId); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "REGISTER_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", type); @@ -64,7 +64,7 @@ int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, std::string& component SC_LOG_ERROR(LABEL, "update security component fail, caller invalid"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), - "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "CLICK"); + "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "UPDATE"); return SC_SERVICE_ERROR_CALLER_INVALID; } @@ -83,13 +83,14 @@ int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, std::string& component int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId) { int32_t res = SecCompClient::GetInstance().UnregisterSecurityComponent(scId); + SecCompEnhanceAdapter::UnregisterScIdEnhance(scId); if (res != SC_OK) { SC_LOG_ERROR(LABEL, "unregister security component fail, error: %{public}d", res); + return res; } HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "UNREGISTER_SUCCESS", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId); - SecCompEnhanceAdapter::UnregisterScIdEnhance(scId); return res; } diff --git a/interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp b/interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp index a302e92..8fd9f26 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_load_callback.cpp @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_comp_load_callback.h" + #include "i_sec_comp_service.h" #include "sec_comp_client.h" #include "sec_comp_log.h" diff --git a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp index 47b7824..3d8e205 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp @@ -12,9 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_comp_proxy.h" -#include "parcel.h" + #include "sec_comp_click_event_parcel.h" #include "sec_comp_err.h" #include "sec_comp_log.h" diff --git a/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp b/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp index 966df1a..a94215f 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_ui_register.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_ui_register.h" + #include "sec_comp_caller_authorization.h" #include "sec_comp_log.h" diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp index c0fdde0..6cdb68d 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp @@ -17,38 +17,6 @@ namespace OHOS { namespace Security { namespace SecurityComponent { -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; -static const std::string JSON_NODE_ID = "nodeId"; -static const std::string JSON_RECT = "rect"; -static const std::string JSON_WINDOW_RECT = "windowRect"; -static const std::string JSON_SC_TYPE = "type"; -static const std::string JSON_RECT_X = "x"; -static const std::string JSON_RECT_Y = "y"; -static const std::string JSON_RECT_WIDTH = "width"; -static const std::string JSON_RECT_HEIGHT = "height"; -static const std::string JSON_SIZE_TAG = "size"; -static const std::string JSON_FONT_SIZE_TAG = "fontSize"; -static const std::string JSON_ICON_SIZE_TAG = "iconSize"; -static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; -static const std::string JSON_PADDING_LEFT_TAG = "left"; -static const std::string JSON_PADDING_TOP_TAG = "top"; -static const std::string JSON_PADDING_RIGHT_TAG = "right"; -static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; -static const std::string JSON_RECT_WIDTH_TAG = "width"; -static const std::string JSON_RECT_HEIGHT_TAG = "height"; -static const std::string JSON_INVALID_TAG = "test"; -static const std::string JSON_COLORS_TAG = "colors"; -static const std::string JSON_FONT_COLOR_TAG = "fontColor"; -static const std::string JSON_ICON_COLOR_TAG = "iconColor"; -static const std::string JSON_BG_COLOR_TAG = "bgColor"; -static const std::string JSON_BORDER_TAG = "border"; -static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; -static const std::string JSON_PARENT_TAG = "parent"; -static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; static const std::string WRONG_TYPE = "wrongType"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; @@ -59,50 +27,50 @@ static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; void LocationButtonSampleBuild::BuildLocationComponentInfo(nlohmann::json& jsonComponent) { - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } }; - jsonComponent[JSON_NODE_ID] = 0; - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; - jsonComponent[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, }; - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, false }, + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, }; - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } } // namespace SecurityComponent diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 8ab1716..ef7503a 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -25,42 +25,6 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButtonTest"}; -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; -static const std::string JSON_NODE_ID = "nodeId"; -static const std::string JSON_RECT = "rect"; -static const std::string JSON_WINDOW_RECT = "windowRect"; -static const std::string JSON_SC_TYPE = "type"; -static const std::string JSON_RECT_X = "x"; -static const std::string JSON_RECT_Y = "y"; -static const std::string JSON_RECT_WIDTH = "width"; -static const std::string JSON_RECT_HEIGHT = "height"; - -static const std::string JSON_SIZE_TAG = "size"; -static const std::string JSON_FONT_SIZE_TAG = "fontSize"; -static const std::string JSON_ICON_SIZE_TAG = "iconSize"; -static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; -static const std::string JSON_PADDING_LEFT_TAG = "left"; -static const std::string JSON_PADDING_TOP_TAG = "top"; -static const std::string JSON_PADDING_RIGHT_TAG = "right"; -static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; -static const std::string JSON_RECT_WIDTH_TAG = "width"; -static const std::string JSON_RECT_HEIGHT_TAG = "height"; -static const std::string JSON_INVALID_TAG = "test"; - -static const std::string JSON_COLORS_TAG = "colors"; -static const std::string JSON_FONT_COLOR_TAG = "fontColor"; -static const std::string JSON_ICON_COLOR_TAG = "iconColor"; -static const std::string JSON_BG_COLOR_TAG = "bgColor"; - -static const std::string JSON_BORDER_TAG = "border"; -static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; -static const std::string JSON_PARENT_TAG = "parent"; -static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; - static const std::string WRONG_TYPE = "wrongType"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; @@ -69,55 +33,53 @@ static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; static constexpr uint32_t TEST_COLOR_RED = 0xff0000; static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; static constexpr uint32_t TEST_DIFF_COLOR = 0; -static constexpr int32_t UNKNOWN_TEXT = -2; -static constexpr int32_t UNKNOWN_ICON = -2; static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } - }; - jsonComponent[JSON_NODE_ID] = 0; - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; - jsonComponent[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, }; - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, false }, + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, }; - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } } @@ -176,10 +138,10 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_SC_TYPE] = WRONG_TYPE; + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = WRONG_TYPE; ASSERT_FALSE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_SC_TYPE] = 0; + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = 0; ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -193,43 +155,43 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); - jsonComponent[JSON_RECT] = wrongJson; + jsonComponent[JsonTagConstants::JSON_RECT] = wrongJson; ASSERT_FALSE(comp.FromJson(jsonComponent)); BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, WRONG_TYPE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, WRONG_TYPE}, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, WRONG_TYPE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, WRONG_TYPE}, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, WRONG_TYPE}, - {JSON_RECT_HEIGHT, TEST_COORDINATE} + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE}, - {JSON_RECT_Y, TEST_COORDINATE}, - {JSON_RECT_WIDTH, TEST_COORDINATE}, - {JSON_RECT_HEIGHT, WRONG_TYPE} + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -248,33 +210,33 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, WRONG_TYPE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, WRONG_TYPE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -293,30 +255,30 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, WRONG_TYPE }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, WRONG_TYPE }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -335,30 +297,30 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, WRONG_TYPE }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, WRONG_TYPE }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -376,14 +338,14 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - jsonComponent[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, WRONG_TYPE }, + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); BuildLocationComponentInfo(jsonComponent); - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -401,24 +363,24 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, WRONG_TYPE }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, WRONG_TYPE }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, WRONG_TYPE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, WRONG_TYPE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -436,24 +398,24 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, WRONG_TYPE }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, WRONG_TYPE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, WRONG_TYPE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, WRONG_TYPE } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, WRONG_TYPE } }; ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -472,45 +434,45 @@ nlohmann::json jsonComponent; ASSERT_TRUE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::UNKNOWN_TEXT }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::UNKNOWN_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::MAX_LABEL_TYPE }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::MAX_LABEL_TYPE }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::MAX_ICON_TYPE }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::MAX_ICON_TYPE }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -543,7 +505,7 @@ HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) BuildLocationComponentInfo(jsonComponent); LocationButton button; - ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr)); + ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr, true)); } /** @@ -562,21 +524,21 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_TRUE(button2.FromJson(jsonComponent)); - ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); button1.text_ = UNKNOWN_TEXT; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.text_ = static_cast(LocationDesc::SELECT_LOCATION); button1.icon_ = UNKNOWN_ICON; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.icon_ = static_cast(LocationIcon::LINE_ICON); button1.bg_ = SecCompBackground::UNKNOWN_BG; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.bg_ = SecCompBackground::CIRCLE; - ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); } /** @@ -594,52 +556,52 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) LocationButton comp2 = comp1; comp1.type_ = SAVE_COMPONENT; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.type_ = LOCATION_COMPONENT; comp1.fontSize_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.fontSize_ = TEST_SIZE; comp1.iconSize_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.iconSize_ = TEST_SIZE; comp1.padding_.top = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.top = TEST_DIMENSION; comp1.padding_.right = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.right = TEST_DIMENSION; comp1.padding_.bottom = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.bottom = TEST_DIMENSION; comp1.padding_.left = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.left = TEST_DIMENSION; comp1.textIconSpace_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.textIconSpace_ = TEST_SIZE; comp1.borderWidth_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.borderWidth_ = TEST_SIZE; comp1.fontColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.fontColor_.value = TEST_COLOR_RED; comp1.bgColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.bgColor_.value = TEST_COLOR_YELLOW; comp1.iconColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.iconColor_.value = TEST_COLOR_BLUE; - ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index 3f9653d..625c4ac 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -25,41 +25,6 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "PasteButtonTest"}; -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; -static const std::string JSON_NODE_ID = "nodeId"; -static const std::string JSON_RECT = "rect"; -static const std::string JSON_WINDOW_RECT = "windowRect"; -static const std::string JSON_SC_TYPE = "type"; -static const std::string JSON_RECT_X = "x"; -static const std::string JSON_RECT_Y = "y"; -static const std::string JSON_RECT_WIDTH = "width"; -static const std::string JSON_RECT_HEIGHT = "height"; - -static const std::string JSON_SIZE_TAG = "size"; -static const std::string JSON_FONT_SIZE_TAG = "fontSize"; -static const std::string JSON_ICON_SIZE_TAG = "iconSize"; -static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; -static const std::string JSON_PADDING_LEFT_TAG = "left"; -static const std::string JSON_PADDING_TOP_TAG = "top"; -static const std::string JSON_PADDING_RIGHT_TAG = "right"; -static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; -static const std::string JSON_RECT_WIDTH_TAG = "width"; -static const std::string JSON_RECT_HEIGHT_TAG = "height"; - -static const std::string JSON_COLORS_TAG = "colors"; -static const std::string JSON_FONT_COLOR_TAG = "fontColor"; -static const std::string JSON_ICON_COLOR_TAG = "iconColor"; -static const std::string JSON_BG_COLOR_TAG = "bgColor"; - -static const std::string JSON_BORDER_TAG = "border"; -static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; -static const std::string JSON_PARENT_TAG = "parent"; -static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; - static const std::string WRONG_TYPE = "wrongType"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; @@ -68,55 +33,53 @@ static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; static constexpr uint32_t TEST_COLOR_RED = 0xff0000; static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; static constexpr uint32_t TEST_DIFF_COLOR = 0; -static constexpr int32_t UNKNOWN_TEXT = -2; -static constexpr int32_t UNKNOWN_ICON = -2; static void BuildPasteComponentInfo(nlohmann::json& jsonComponent) { - jsonComponent[JSON_SC_TYPE] = PASTE_COMPONENT; - jsonComponent[JSON_NODE_ID] = 0; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } }; - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; - jsonComponent[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, }; - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, false }, + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, }; - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, PasteDesc::PASTE }, - { JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } } @@ -149,45 +112,45 @@ nlohmann::json jsonComponent; ASSERT_TRUE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, PasteDesc::UNKNOWN_TEXT }, - { JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, PasteDesc::PASTE}, - { JSON_ICON_TAG, PasteIcon::UNKNOWN_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE}, + { JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, PasteDesc::PASTE }, - { JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, PasteDesc::MAX_LABEL_TYPE }, - { JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::MAX_LABEL_TYPE }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, PasteDesc::PASTE }, - { JSON_ICON_TAG, PasteIcon::MAX_ICON_TYPE }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::MAX_ICON_TYPE }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, PasteDesc::PASTE }, - { JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -208,21 +171,21 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_TRUE(button2.FromJson(jsonComponent)); - ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); button1.text_ = UNKNOWN_TEXT; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.text_ = static_cast(PasteDesc::PASTE); button1.icon_ = UNKNOWN_ICON; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.icon_ = static_cast(PasteIcon::LINE_ICON); button1.bg_ = SecCompBackground::UNKNOWN_BG; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.bg_ = SecCompBackground::CIRCLE; - ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); } /** @@ -240,52 +203,52 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) PasteButton comp2 = comp1; comp1.type_ = LOCATION_COMPONENT; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.type_ = PASTE_COMPONENT; comp1.fontSize_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.fontSize_ = TEST_SIZE; comp1.iconSize_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.iconSize_ = TEST_SIZE; comp1.padding_.top = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.top = TEST_DIMENSION; comp1.padding_.right = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.right = TEST_DIMENSION; comp1.padding_.bottom = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.bottom = TEST_DIMENSION; comp1.padding_.left = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.left = TEST_DIMENSION; comp1.textIconSpace_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.textIconSpace_ = TEST_SIZE; comp1.borderWidth_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.borderWidth_ = TEST_SIZE; comp1.fontColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.fontColor_.value = TEST_COLOR_RED; comp1.bgColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.bgColor_.value = TEST_COLOR_YELLOW; comp1.iconColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.iconColor_.value = TEST_COLOR_BLUE; - ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } \ No newline at end of file diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index db23523..8771c78 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -25,42 +25,6 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SaveButtonTest"}; -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; -static const std::string JSON_NODE_ID = "nodeId"; -static const std::string JSON_RECT = "rect"; -static const std::string JSON_WINDOW_RECT = "windowRect"; -static const std::string JSON_SC_TYPE = "type"; -static const std::string JSON_RECT_X = "x"; -static const std::string JSON_RECT_Y = "y"; -static const std::string JSON_RECT_WIDTH = "width"; -static const std::string JSON_RECT_HEIGHT = "height"; - -static const std::string JSON_SIZE_TAG = "size"; -static const std::string JSON_FONT_SIZE_TAG = "fontSize"; -static const std::string JSON_ICON_SIZE_TAG = "iconSize"; -static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; -static const std::string JSON_PADDING_LEFT_TAG = "left"; -static const std::string JSON_PADDING_TOP_TAG = "top"; -static const std::string JSON_PADDING_RIGHT_TAG = "right"; -static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; -static const std::string JSON_RECT_WIDTH_TAG = "width"; -static const std::string JSON_RECT_HEIGHT_TAG = "height"; - -static const std::string JSON_COLORS_TAG = "colors"; -static const std::string JSON_FONT_COLOR_TAG = "fontColor"; -static const std::string JSON_ICON_COLOR_TAG = "iconColor"; -static const std::string JSON_BG_COLOR_TAG = "bgColor"; - -static const std::string JSON_BORDER_TAG = "border"; -static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; -static const std::string JSON_PARENT_TAG = "parent"; -static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; - -static const std::string WRONG_TYPE = "wrongType"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; @@ -68,55 +32,53 @@ static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; static constexpr uint32_t TEST_COLOR_RED = 0xff0000; static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; static constexpr uint32_t TEST_DIFF_COLOR = 0; -static constexpr int32_t UNKNOWN_TEXT = -2; -static constexpr int32_t UNKNOWN_ICON = -2; static void BuildSaveComponentInfo(nlohmann::json& jsonComponent) { - jsonComponent[JSON_SC_TYPE] = SAVE_COMPONENT; - jsonComponent[JSON_NODE_ID] = 0; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } }; - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_COORDINATE }, - {JSON_RECT_Y, TEST_COORDINATE }, - {JSON_RECT_WIDTH, TEST_COORDINATE }, - {JSON_RECT_HEIGHT, TEST_COORDINATE } + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; - jsonComponent[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, }; - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, false }, + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, }; - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } } @@ -149,45 +111,45 @@ nlohmann::json jsonComponent; ASSERT_TRUE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, SaveDesc::UNKNOWN_TEXT }, - { JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, SaveDesc::DOWNLOAD}, - { JSON_ICON_TAG, SaveIcon::UNKNOWN_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD}, + { JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::UNKNOWN_BG }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, SaveDesc::MAX_LABEL_TYPE }, - { JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::MAX_LABEL_TYPE }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JSON_ICON_TAG, SaveIcon::MAX_ICON_TYPE }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::MAX_ICON_TYPE }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::MAX_BG_TYPE }, }; ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -208,21 +170,21 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_TRUE(button2.FromJson(jsonComponent)); - ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); button1.text_ = UNKNOWN_TEXT; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.text_ = static_cast(SaveDesc::DOWNLOAD); button1.icon_ = UNKNOWN_ICON; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.icon_ = static_cast(SaveIcon::LINE_ICON); button1.bg_ = SecCompBackground::UNKNOWN_BG; - ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); button1.bg_ = SecCompBackground::CIRCLE; - ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2)); + ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); } /** @@ -240,52 +202,52 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) SaveButton comp2 = comp1; comp1.type_ = PASTE_COMPONENT; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.type_ = SAVE_COMPONENT; comp1.fontSize_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.fontSize_ = TEST_SIZE; comp1.iconSize_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.iconSize_ = TEST_SIZE; comp1.padding_.top = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.top = TEST_DIMENSION; comp1.padding_.right = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.right = TEST_DIMENSION; comp1.padding_.bottom = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.bottom = TEST_DIMENSION; comp1.padding_.left = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.padding_.left = TEST_DIMENSION; comp1.textIconSpace_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.textIconSpace_ = TEST_SIZE; comp1.borderWidth_ = DEFAULT_DIMENSION; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.borderWidth_ = TEST_SIZE; comp1.fontColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.fontColor_.value = TEST_COLOR_RED; comp1.bgColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.bgColor_.value = TEST_COLOR_YELLOW; comp1.iconColor_.value = TEST_DIFF_COLOR; - ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); comp1.iconColor_.value = TEST_COLOR_BLUE; - ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2)); + ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } \ No newline at end of file diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp index 7c78cc8..8c835ca 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp @@ -110,4 +110,3 @@ HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback002, TestSiz SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_EQ(-1, scId); } - diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp index 6a388f9..7c59fb2 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp @@ -114,4 +114,3 @@ HWTEST_F(SecCompReportClickWithoutHmacTest, ReportClickWithoutHmac001, TestSize. EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); } - diff --git a/security_component.gni b/security_component.gni index af3aa08..f92bdb1 100644 --- a/security_component.gni +++ b/security_component.gni @@ -10,4 +10,5 @@ # 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. + sec_comp_dir = "//base/security/security_component" diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index bb0d7aa..4cc4ace 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -12,7 +12,6 @@ # limitations under the License. import("//build/ohos.gni") -import("../../../security_component.gni") sec_comp_root_dir = "../../.." diff --git a/services/security_component_service/sa/sa_main/app_state_observer.cpp b/services/security_component_service/sa/sa_main/app_state_observer.cpp index 67cf51f..be4fa4e 100644 --- a/services/security_component_service/sa/sa_main/app_state_observer.cpp +++ b/services/security_component_service/sa/sa_main/app_state_observer.cpp @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "app_state_observer.h" + #include "sec_comp_log.h" #include "sec_comp_manager.h" diff --git a/services/security_component_service/sa/sa_main/app_state_observer.h b/services/security_component_service/sa/sa_main/app_state_observer.h index d3bb959..56998a0 100644 --- a/services/security_component_service/sa/sa_main/app_state_observer.h +++ b/services/security_component_service/sa/sa_main/app_state_observer.h @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H #define SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H @@ -37,16 +36,16 @@ public: explicit AppStateObserver(); virtual ~AppStateObserver(); - void OnProcessStateChanged(const AppExecFwk::ProcessData &processData) override; + void OnProcessStateChanged(const AppExecFwk::ProcessData& processData) override; void OnProcessDied(const AppExecFwk::ProcessData& processData) override; bool IsProcessForeground(int32_t pid, int32_t uid); void AddProcessToForegroundSet(int32_t pid, const SecCompProcessData& data); - void AddProcessToForegroundSet(const AppExecFwk::ProcessData &processData); + void AddProcessToForegroundSet(const AppExecFwk::ProcessData& processData); void AddProcessToForegroundSet(const AppExecFwk::AppStateData& stateData); void DumpProcess(std::string& dumpStr); private: - void RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData &processData); + void RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData& processData); std::vector foregrandProcList_; OHOS::Utils::RWLock fgProcLock_; }; diff --git a/services/security_component_service/sa/sa_main/delay_exit_task.cpp b/services/security_component_service/sa/sa_main/delay_exit_task.cpp index bca167f..5d78873 100644 --- a/services/security_component_service/sa/sa_main/delay_exit_task.cpp +++ b/services/security_component_service/sa/sa_main/delay_exit_task.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "delay_exit_task.h" #include "sec_comp_log.h" @@ -29,7 +28,6 @@ static const int32_t DELAY_EXIT_MILLISECONDS = 30 * 1000; // 30s DelayExitTask::DelayExitTask() { - SC_LOG_INFO(LABEL, "DelayExitTask()"); } DelayExitTask& DelayExitTask::GetInstance() @@ -65,7 +63,7 @@ void DelayExitTask::Stop() return; } - SC_LOG_INFO(LABEL, "Delay exit service stop"); + SC_LOG_INFO(LABEL, "service delay exit handler stop"); secHandler_->ProxyRemoveTask(DELAY_EXIT_TASK); } } // namespace SecurityComponent diff --git a/services/security_component_service/sa/sa_main/delay_exit_task.h b/services/security_component_service/sa/sa_main/delay_exit_task.h index 3d4da21..d4315df 100644 --- a/services/security_component_service/sa/sa_main/delay_exit_task.h +++ b/services/security_component_service/sa/sa_main/delay_exit_task.h @@ -12,12 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef SECURITY_COMPONENT_DELAY_EXIT_TASK_H #define SECURITY_COMPONENT_DELAY_EXIT_TASK_H + #include #include - #include "nocopyable.h" #include "sec_event_handler.h" diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp index 0907f77..6439824 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "first_use_dialog.h" #include @@ -20,7 +19,6 @@ #include #include #include - #include "ability_manager_client.h" #include "accesstoken_kit.h" #include "sec_comp_err.h" @@ -158,7 +156,7 @@ void FirstUseDialog::ParseRecords(nlohmann::json& jsonRes) void FirstUseDialog::LoadFirstUseRecord(void) { if (!IsCfgFileValid()) { - SC_LOG_INFO(LABEL, "first use record is not valid."); + SC_LOG_INFO(LABEL, "first use record is invalid."); return; } diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index 21485f0..fd04777 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef FIRST_USE_DIALOG_H #define FIRST_USE_DIALOG_H diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index 58d6291..aec385a 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_entity.h" + #include #include "hisysevent.h" #include "ipc_skeleton.h" @@ -46,9 +47,9 @@ int32_t SecCompEntity::GrantTempPermission() return SecCompInfoHelper::GrantTempPermission(tokenId_, componentInfo_); } -bool SecCompEntity::CompareComponentBasicInfo(SecCompBase* other) const +bool SecCompEntity::CompareComponentBasicInfo(SecCompBase* other, bool isRectCheck) const { - return componentInfo_->CompareComponentBasicInfo(other); + return componentInfo_->CompareComponentBasicInfo(other, isRectCheck); } bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const @@ -66,7 +67,8 @@ bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const return false; } - if (SecCompEnhanceAdapter::CheckExtraInfo(touchInfo) != SC_OK) { + int32_t res = SecCompEnhanceAdapter::CheckExtraInfo(touchInfo); + if ((res != SC_OK) && (res != SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE)) { SC_LOG_ERROR(LABEL, "HMAC checkout failed" "touchX:%{public}f, touchY:%{public}f, timestamp:%{public}lu, dataSize:%{public}d", touchInfo.touchX, touchInfo.touchY, touchInfo.timestamp, touchInfo.extraInfo.dataSize); diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.h b/services/security_component_service/sa/sa_main/sec_comp_entity.h index 0407d10..d95f6f6 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.h +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.h @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef SECURITY_COMPONENT_ENTITY_H #define SECURITY_COMPONENT_ENTITY_H @@ -66,7 +65,7 @@ public: return componentInfo_; }; - bool CompareComponentBasicInfo(SecCompBase* other) const; + bool CompareComponentBasicInfo(SecCompBase* other, bool isRectCheck) const; bool CheckTouchInfo(const SecCompClickEvent& touchInfo) const; private: diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 1cbe4db..7058200 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_comp_info_helper.h" #include "display.h" @@ -33,8 +32,6 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelper"}; static constexpr double MAX_RECT_PERCENT = 0.1F; // 10% static constexpr double ZERO_OFFSET = 0.0F; -static constexpr int32_t NO_TEXT = -1; -static constexpr int32_t NO_ICON = -1; static std::mutex g_renderLock; } @@ -86,7 +83,7 @@ static bool GetScreenSize(double& width, double& height) return true; } -static bool CheckRectValid(const SecCompRect& rect, const SecCompRect& windowRect) +bool SecCompInfoHelper::CheckRectValid(const SecCompRect& rect, const SecCompRect& windowRect) { double curScreenWidth = 0.0F; double curScreenHeight = 0.0F; @@ -123,7 +120,7 @@ static bool CheckRectValid(const SecCompRect& rect, const SecCompRect& windowRec SC_LOG_ERROR(LABEL, "rect area is too large"); return false; } - + SC_LOG_DEBUG(LABEL, "check component rect success."); return true; } @@ -161,11 +158,6 @@ static bool CheckSecCompBaseButton(const SecCompBase* comp) static bool CheckSecCompBase(const SecCompBase* comp) { - if (!CheckRectValid(comp->rect_, comp->windowRect_)) { - SC_LOG_INFO(LABEL, "check component rect failed."); - return false; - } - if (comp->parentEffect_) { SC_LOG_ERROR(LABEL, "parentEffect is active, security component invalid."); return false; @@ -205,10 +197,10 @@ bool SecCompInfoHelper::CheckComponentValid(const SecCompBase* comp) return true; } -int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID tokenId, +int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo) { - if (componentInfo == nullptr) { + if ((tokenId <= 0) || (componentInfo == nullptr)) { SC_LOG_ERROR(LABEL, "revoke component is null"); return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } @@ -217,29 +209,38 @@ int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID token switch (type) { case LOCATION_COMPONENT: { - int32_t locationRes = SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, - "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); - int32_t approxLocationRes = SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, + int32_t res = SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.APPROXIMATELY_LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); - if ((locationRes != SC_OK) || (approxLocationRes != SC_OK)) { + if (res != SC_OK) { + return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; + } + res = SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + if (res != SC_OK) { + SecCompPermManager::GetInstance().RevokeLocationPermission( + tokenId, "ohos.permission.APPROXIMATELY_LOCATION", + AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } return SC_OK; } case PASTE_COMPONENT: - SC_LOG_DEBUG(LABEL, "revoke paste permission"); + SC_LOG_DEBUG(LABEL, "grant paste permission"); return SC_OK; + case SAVE_COMPONENT: + SC_LOG_DEBUG(LABEL, "grant save permission"); + return SecCompPermManager::GetInstance().GrantTempSavePermission(tokenId); default: - SC_LOG_ERROR(LABEL, "revoke component type unknown"); + SC_LOG_ERROR(LABEL, "Parse component type unknown"); break; } return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } -int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenId, +int32_t SecCompInfoHelper::RevokeTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo) { - if ((tokenId <= 0) || (componentInfo == nullptr)) { + if (componentInfo == nullptr) { SC_LOG_ERROR(LABEL, "revoke component is null"); return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } @@ -248,29 +249,20 @@ int32_t SecCompInfoHelper::GrantTempPermission(AccessToken::AccessTokenID tokenI switch (type) { case LOCATION_COMPONENT: { - int32_t res = SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, + int32_t locationRes = SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, + "ohos.permission.LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + int32_t approxLocationRes = SecCompPermManager::GetInstance().RevokeLocationPermission(tokenId, "ohos.permission.APPROXIMATELY_LOCATION", AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); - if (res != SC_OK) { - return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; - } - res = SecCompPermManager::GetInstance().GrantLocationPermission(tokenId, "ohos.permission.LOCATION", - AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); - if (res != SC_OK) { - SecCompPermManager::GetInstance().RevokeLocationPermission( - tokenId, "ohos.permission.APPROXIMATELY_LOCATION", - AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); + if ((locationRes != SC_OK) || (approxLocationRes != SC_OK)) { return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; } return SC_OK; } case PASTE_COMPONENT: - SC_LOG_DEBUG(LABEL, "grant paste permission"); + SC_LOG_DEBUG(LABEL, "revoke paste permission"); return SC_OK; - case SAVE_COMPONENT: - SC_LOG_DEBUG(LABEL, "grant save permission"); - return SecCompPermManager::GetInstance().GrantTempSavePermission(tokenId); default: - SC_LOG_ERROR(LABEL, "Parse component type unknown"); + SC_LOG_ERROR(LABEL, "revoke component type unknown"); break; } return SC_SERVICE_ERROR_PERMISSION_OPER_FAIL; diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h index 64d9cd3..463c9fc 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h @@ -43,6 +43,7 @@ public: static int32_t GrantTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo); static bool CheckComponentValid(const SecCompBase* comp); + static bool CheckRectValid(const SecCompRect& rect, const SecCompRect& windowRect); }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 1737413..899e2b4 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_comp_manager.h" #include "delay_exit_task.h" @@ -38,7 +37,6 @@ static constexpr int32_t ROOT_UID = 0; SecCompManager::SecCompManager() { scIdStart_ = SC_ID_START; - SC_LOG_INFO(LABEL, "SecCompManager()"); } SecCompManager& SecCompManager::GetInstance() @@ -401,11 +399,12 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC } int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller) + const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller) { + SC_LOG_DEBUG(LABEL, "PID: %{public}d, Check security component", caller.pid); SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); std::shared_ptr reportComponentInfo(report); - if ((reportComponentInfo == nullptr) || !reportComponentInfo->GetValid()) { + if ((reportComponentInfo == nullptr) || (!reportComponentInfo->GetValid())) { SC_LOG_ERROR(LABEL, "report component info invalid"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "COMPONENT_INFO_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), @@ -413,7 +412,14 @@ int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32 sc->GetType()); return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; } - + if ((!SecCompInfoHelper::CheckRectValid(reportComponentInfo->rect_, reportComponentInfo->windowRect_))) { + SC_LOG_ERROR(LABEL, "compare component info failed."); + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "COMPONENT_INFO_CHECK_FAILED", + HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), + "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "CALL_SCENE", "CLICK", "SC_TYPE", + sc->GetType()); + return SC_SERVICE_ERROR_COMPONENT_INFO_INVALID; + } int32_t enhanceRes = SecCompEnhanceAdapter::CheckComponentInfoEnhnace(caller.pid, reportComponentInfo, jsonComponent); if (enhanceRes != SC_OK) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index fd974ce..3c0848c 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef SECURITY_COMPONENT_MANAGER_H #define SECURITY_COMPONENT_MANAGER_H @@ -21,7 +20,6 @@ #include #include #include - #include "accesstoken_kit.h" #include "app_state_observer.h" #include "first_use_dialog.h" diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index 46bcf9e..23f9d1a 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_comp_perm_manager.h" #include "location_button.h" diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h index 91bab7d..0cd7f76 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.h @@ -12,13 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef SECURITY_COMPONENT_PERMISSION_MANAGER_H #define SECURITY_COMPONENT_PERMISSION_MANAGER_H #include #include - #include "accesstoken_kit.h" #include "rwlock.h" #include "sec_comp_base.h" diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index 3cbfb5d..40aad2d 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_comp_service.h" #include @@ -41,12 +40,10 @@ REGISTER_SYSTEM_ABILITY_BY_ID(SecCompService, SA_ID_SECURITY_COMPONENT_SERVICE, SecCompService::SecCompService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START) { - SC_LOG_INFO(LABEL, "SecCompService()"); } SecCompService::~SecCompService() { - SC_LOG_INFO(LABEL, "~SecCompService()"); } void SecCompService::OnStart() diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.h b/services/security_component_service/sa/sa_main/sec_comp_service.h index 7a3e92c..99d21f2 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.h +++ b/services/security_component_service/sa/sa_main/sec_comp_service.h @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef SECURITY_COMPONENT_SERVICE_H #define SECURITY_COMPONENT_SERVICE_H diff --git a/services/security_component_service/sa/sa_main/sec_event_handler.cpp b/services/security_component_service/sa/sa_main/sec_event_handler.cpp index c60b00a..32e3d8b 100644 --- a/services/security_component_service/sa/sa_main/sec_event_handler.cpp +++ b/services/security_component_service/sa/sa_main/sec_event_handler.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sec_event_handler.h" #include "sec_comp_log.h" diff --git a/services/security_component_service/sa/sa_main/sec_event_handler.h b/services/security_component_service/sa/sa_main/sec_event_handler.h index e3c0fff..86b0734 100644 --- a/services/security_component_service/sa/sa_main/sec_event_handler.h +++ b/services/security_component_service/sa/sa_main/sec_event_handler.h @@ -12,12 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef SEC_EVENT_HANDLER_H #define SEC_EVENT_HANDLER_H #include - #include "event_handler.h" #include "event_runner.h" diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 80d1d9f..04537f3 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -12,7 +12,6 @@ # limitations under the License. import("//build/test.gni") -import("../../../../security_component.gni") sec_comp_root_dir = "../../../.." diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index a0bb772..ac86c7f 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -30,40 +30,6 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelperTest"}; -static const std::string JSON_STYLE_TAG = "style"; -static const std::string JSON_TEXT_TAG = "text"; -static const std::string JSON_ICON_TAG = "icon"; -static const std::string JSON_BG_TAG = "bg"; -static const std::string JSON_NODE_ID = "nodeId"; -static const std::string JSON_RECT = "rect"; -static const std::string JSON_WINDOW_RECT = "windowRect"; -static const std::string JSON_SC_TYPE = "type"; -static const std::string JSON_RECT_X = "x"; -static const std::string JSON_RECT_Y = "y"; -static const std::string JSON_RECT_WIDTH = "width"; -static const std::string JSON_RECT_HEIGHT = "height"; - -static const std::string JSON_SIZE_TAG = "size"; -static const std::string JSON_FONT_SIZE_TAG = "fontSize"; -static const std::string JSON_ICON_SIZE_TAG = "iconSize"; -static const std::string JSON_PADDING_SIZE_TAG = "paddingSize"; -static const std::string JSON_PADDING_LEFT_TAG = "left"; -static const std::string JSON_PADDING_TOP_TAG = "top"; -static const std::string JSON_PADDING_RIGHT_TAG = "right"; -static const std::string JSON_PADDING_BOTTOM_TAG = "bottom"; -static const std::string JSON_TEXT_ICON_PADDING_TAG = "textIconSpace"; -static const std::string JSON_RECT_WIDTH_TAG = "width"; -static const std::string JSON_RECT_HEIGHT_TAG = "height"; - -static const std::string JSON_COLORS_TAG = "colors"; -static const std::string JSON_FONT_COLOR_TAG = "fontColor"; -static const std::string JSON_ICON_COLOR_TAG = "iconColor"; -static const std::string JSON_BG_COLOR_TAG = "bgColor"; - -static const std::string JSON_BORDER_TAG = "border"; -static const std::string JSON_BORDER_WIDTH_TAG = "borderWidth"; -static const std::string JSON_PARENT_TAG = "parent"; -static const std::string JSON_PARENT_EFFECT_TAG = "parentEffect"; static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_DIMENSION = 100.0; @@ -101,50 +67,50 @@ static bool GetScreenSize() static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) { - jsonComponent[JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JSON_NODE_ID] = 0; - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - { JSON_RECT_X, g_testWidth }, - { JSON_RECT_Y, g_testHeight }, - { JSON_RECT_WIDTH, g_testWidth }, - { JSON_RECT_HEIGHT, g_testHeight } + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, g_testWidth }, + {JsonTagConstants::JSON_RECT_Y, g_testHeight }, + {JsonTagConstants::JSON_RECT_WIDTH, g_testWidth }, + {JsonTagConstants::JSON_RECT_HEIGHT, g_testHeight } + }; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + { JsonTagConstants::JSON_RECT_X, g_testWidth }, + { JsonTagConstants::JSON_RECT_Y, g_testHeight }, + { JsonTagConstants::JSON_RECT_WIDTH, g_testWidth }, + { JsonTagConstants::JSON_RECT_HEIGHT, g_testHeight } }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; - jsonComponent[JSON_BORDER_TAG] = nlohmann::json { - { JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, }; - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, false }, + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, }; - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } } @@ -212,7 +178,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_EQ(comp, nullptr); } @@ -225,105 +191,72 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) { - nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); - SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_TRUE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, TEST_INVALID_DIMENSION }, - {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, TEST_INVALID_DIMENSION }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_curScreenWidth + 1 }, - {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, g_curScreenHeight + 1 }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); + SecCompRect rect = { + .x_ = g_testWidth, + .y_ = g_testHeight, + .width_ = g_testWidth, + .height_ = g_testHeight + }; + SecCompRect windowRect = { + .x_ = g_testWidth, + .y_ = g_testHeight, + .width_ = g_testWidth, + .height_ = g_testHeight + }; + ASSERT_TRUE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + + rect.x_ = TEST_INVALID_DIMENSION; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = TEST_INVALID_DIMENSION; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_curScreenWidth + 1; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_curScreenHeight + 1; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.width_ = g_curScreenWidth; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.height_ = g_curScreenHeight; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_curScreenWidth - g_testWidth; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_curScreenHeight - g_testHeight; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); } /** * @tc.name: ParseComponent005 - * @tc.desc: Test parse component info with invalid rect + * @tc.desc: Test parse component info with windowRect invalid * @tc.type: FUNC * @tc.require: AR000HO9J7 */ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) { - nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); - SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_TRUE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_curScreenWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_curScreenHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_curScreenWidth - g_testWidth }, - {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, g_curScreenHeight - g_testHeight }, - {JSON_RECT_WIDTH, g_testWidth }, - {JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); - - jsonComponent[JSON_RECT] = nlohmann::json { - {JSON_RECT_X, g_testWidth }, - {JSON_RECT_Y, g_testHeight }, - {JSON_RECT_WIDTH, g_curScreenWidth }, - {JSON_RECT_HEIGHT, g_curScreenHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_FALSE(comp->GetValid()); + SecCompRect rect = { + .x_ = g_testWidth, + .y_ = g_testHeight, + .width_ = g_testWidth, + .height_ = g_testHeight + }; + SecCompRect windowRect = { + .x_ = g_testWidth, + .y_ = g_testHeight, + .width_ = g_testWidth, + .height_ = g_testHeight + }; + ASSERT_TRUE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + + windowRect.x_ = g_testWidth + 1; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.y_ = g_testHeight + 1; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.width_ = g_testWidth - 1; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.height_ = g_testHeight - 1; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.width_ = TEST_INVALID_DIMENSION; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.height_ = TEST_INVALID_DIMENSION; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); } /** @@ -340,8 +273,8 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_PARENT_TAG] = nlohmann::json { - { JSON_PARENT_EFFECT_TAG, true }, + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, true }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -362,49 +295,49 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_INVALID_DIMENSION }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_INVALID_DIMENSION }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_INVALID_DIMENSION }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -424,33 +357,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_INVALID_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_INVALID_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -470,33 +403,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_INVALID_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_INVALID_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -515,26 +448,26 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_INVALID }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_INVALID }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_INVALID }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_INVALID }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_INVALID } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_INVALID } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -553,10 +486,10 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::NO_TEXT }, - { JSON_ICON_TAG, LocationIcon::NO_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, NO_TEXT }, + { JsonTagConstants::JSON_ICON_TAG, NO_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -575,41 +508,41 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_YELLOW }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_YELLOW }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::CIRCLE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, - { JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -628,39 +561,39 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -679,132 +612,45 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_STYLE_TAG] = nlohmann::json { - { JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; nlohmann::json jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, - { JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, }; - jsonComponent[JSON_SIZE_TAG] = nlohmann::json { - { JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JSON_PADDING_SIZE_TAG, jsonPadding }, + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } -/** - * @tc.name: ParseComponent015 - * @tc.desc: Test parse component info with invalid rect - * @tc.type: FUNC - * @tc.require: AR000HO9J7 - */ -HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) -{ - nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); - jsonComponent[JSON_RECT] = nlohmann::json { - { JSON_RECT_X, g_testWidth }, - { JSON_RECT_Y, g_testHeight }, - { JSON_RECT_WIDTH, TEST_INVALID_DIMENSION }, - { JSON_RECT_HEIGHT, g_testHeight } - }; - SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_NE(comp, nullptr); - EXPECT_FALSE(comp->GetValid()); - delete comp; - - jsonComponent[JSON_RECT] = nlohmann::json { - { JSON_RECT_X, g_testWidth }, - { JSON_RECT_Y, g_testHeight }, - { JSON_RECT_WIDTH, g_testWidth }, - { JSON_RECT_HEIGHT, TEST_INVALID_DIMENSION } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_NE(comp, nullptr); - EXPECT_FALSE(comp->GetValid()); - delete comp; -} - -/** - * @tc.name: ParseComponent016 - * @tc.desc: Test parse component info with windowRect invalid - * @tc.type: FUNC - * @tc.require: AR000HO9J7 - */ -HWTEST_F(SecCompInfoHelperTest, ParseComponent016, TestSize.Level1) -{ - nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - { JSON_RECT_X, g_testWidth }, - { JSON_RECT_Y, g_testHeight + 1 }, - { JSON_RECT_WIDTH, g_testWidth }, - { JSON_RECT_HEIGHT, g_testHeight } - }; - SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_NE(comp, nullptr); - EXPECT_FALSE(comp->GetValid()); - delete comp; - - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - { JSON_RECT_X, g_testWidth + 1 }, - { JSON_RECT_Y, g_testHeight }, - { JSON_RECT_WIDTH, g_testWidth }, - { JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_NE(comp, nullptr); - EXPECT_FALSE(comp->GetValid()); - delete comp; - - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - { JSON_RECT_X, g_testWidth }, - { JSON_RECT_Y, g_testHeight }, - { JSON_RECT_WIDTH, g_testWidth - 1 }, - { JSON_RECT_HEIGHT, g_testHeight } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_NE(comp, nullptr); - EXPECT_FALSE(comp->GetValid()); - delete comp; - - jsonComponent[JSON_WINDOW_RECT] = nlohmann::json { - { JSON_RECT_X, g_testWidth }, - { JSON_RECT_Y, g_testHeight }, - { JSON_RECT_WIDTH, g_testWidth }, - { JSON_RECT_HEIGHT, g_testHeight - 1 } - }; - comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_NE(comp, nullptr); - EXPECT_FALSE(comp->GetValid()); - delete comp; -} /** * @tc.name: CheckComponentValid001 @@ -818,10 +664,10 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) BuildLocationComponentInfo(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JSON_COLORS_TAG] = nlohmann::json { - { JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JSON_ICON_COLOR_TAG, TEST_COLOR_BLACK }, - { JSON_BG_COLOR_TAG, TEST_COLOR_WHITE } + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLACK }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_WHITE } }; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); } @@ -837,14 +683,14 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) nlohmann::json jsonComponent; BuildLocationComponentInfo(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - comp->text_ = static_cast(LocationDesc::UNKNOWN_TEXT); + comp->text_ = UNKNOWN_TEXT; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); comp->text_ = static_cast(LocationDesc::SELECT_LOCATION); - comp->icon_ = static_cast(LocationIcon::UNKNOWN_ICON); + comp->icon_ = UNKNOWN_ICON; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); - comp->text_ = static_cast(LocationDesc::UNKNOWN_TEXT); + comp->text_ = UNKNOWN_TEXT; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 380f6cb..737c938 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -40,9 +40,6 @@ static constexpr int32_t TEST_PID_1 = 1; static constexpr int32_t TEST_PID_2 = 2; static constexpr int32_t TEST_PID_3 = 3; -static constexpr int32_t UNKNOWN_TEXT = -2; -static constexpr int32_t UNKNOWN_ICON = -2; - static constexpr AccessTokenID TEST_TOKEN_ID = 1; static constexpr int32_t TEST_SC_ID_1 = 1; static constexpr int32_t TEST_SC_ID_2 = 2; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp index 7efdc84..c430ebe 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -46,38 +46,6 @@ static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; static AccessTokenID g_selfTokenId = 0; -static std::string BuildLocationComponentInfo() -{ - LocationButton button; - button.fontSize_ = TEST_SIZE; - button.iconSize_ = TEST_SIZE; - button.padding_.top = TEST_SIZE; - button.padding_.right = TEST_SIZE; - button.padding_.bottom = TEST_SIZE; - button.padding_.left = TEST_SIZE; - button.textIconSpace_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_YELLOW; - button.iconColor_.value = TEST_COLOR_RED; - button.bgColor_.value = TEST_COLOR_BLUE; - button.borderWidth_ = TEST_SIZE; - button.type_ = LOCATION_COMPONENT; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.windowRect_.x_ = TEST_COORDINATE; - button.windowRect_.y_ = TEST_COORDINATE; - button.windowRect_.width_ = TEST_COORDINATE; - button.windowRect_.height_ = TEST_COORDINATE; - button.text_ = static_cast(LocationDesc::SELECT_LOCATION); - button.icon_ = static_cast(LocationIcon::LINE_ICON); - button.bg_ = SecCompBackground::CIRCLE; - - nlohmann::json jsonRes; - button.ToJson(jsonRes); - return jsonRes.dump(); -} - static std::string BuildSaveComponentInfo() { SaveButton button; @@ -150,7 +118,9 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) { // get caller fail int32_t scId; - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId), + secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + secCompService_->Initialize(); + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId), SC_SERVICE_ERROR_VALUE_INVALID); // parse component json fail @@ -159,13 +129,13 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "{a=", scId), + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "{a=", scId), SC_SERVICE_ERROR_VALUE_INVALID); // wrong json // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), SC_OK); - EXPECT_EQ(secCompService_->UpdateSecurityComponent(scId, BuildLocationComponentInfo()), + EXPECT_EQ(secCompService_->UpdateSecurityComponent(scId, BuildSaveComponentInfo()), SC_OK); struct SecCompClickEvent touch = { .touchX = 100, @@ -174,8 +144,9 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touch, nullptr), SC_OK); + sleep(5); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); } @@ -205,7 +176,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent002, TestSize.Level1) .timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touch, nullptr), SC_SERVICE_ERROR_CLICK_EVENT_INVALID); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); } @@ -236,7 +207,7 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent003, TestSize.Level1) .timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), + EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touch, nullptr), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); } -- Gitee From 5b737e6d7629056b8bb02d4acc60c14820249947 Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 15 Aug 2023 19:10:40 +0800 Subject: [PATCH 52/59] Description: CheckRect Match-id-f74377ca8c1371fdea3f40f68269577b6ff283b8 --- test/fuzztest/security_component/BUILD.gn | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/fuzztest/security_component/BUILD.gn b/test/fuzztest/security_component/BUILD.gn index 4ca6507..8d2c433 100644 --- a/test/fuzztest/security_component/BUILD.gn +++ b/test/fuzztest/security_component/BUILD.gn @@ -1,3 +1,16 @@ +# Copyright (c) 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. + group("fuzztest") { testonly = true deps = [] -- Gitee From 65074b8e4eecd56ed928291c378fa8eb8f3110ca Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 17 Aug 2023 21:59:05 +0800 Subject: [PATCH 53/59] Description:revoke location permission when app go to background Match-id-624fb4bff8ceba50495ce70af949e04eb454f0cd --- .../sa/sa_main/sec_comp_manager.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 899e2b4..49d0785 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -204,6 +204,11 @@ void SecCompManager::NotifyProcessBackground(int32_t pid) return; } + std::vector& list = iter->second.compList; + for (auto it = list.begin(); it != list.end(); ++it) { + it->RevokeTempPermission(); + } + iter->second.isForeground = false; if (!IsForegroundCompExist()) { SecCompEnhanceAdapter::DisableInputEnhance(); -- Gitee From fd9816fda5a4783ac4d373674ea40e720d31059b Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 21 Aug 2023 14:25:11 +0800 Subject: [PATCH 54/59] Description: CheckRect Match-id-1e43ad19f600fe3ff5ffda2b53824bc5bfd4cbee --- BUILD.gn | 6 +- frameworks/BUILD.gn | 4 +- interfaces/inner_api/enhance_kits/BUILD.gn | 6 +- .../inner_api/security_component/BUILD.gn | 4 +- .../security_component/test/BUILD.gn | 108 +---------- .../unittest/src/location_button_test.cpp | 94 ++------- .../test/unittest/src/paste_button_test.cpp | 70 +------ .../test/unittest/src/save_button_test.cpp | 69 +------ .../test/unittest/src/sec_comp_kit_test.cpp | 24 ++- .../src/sec_comp_register_callback_test.cpp | 174 ++++++++++++++++- .../src/sec_comp_register_challenge_test.cpp | 111 ----------- ...ec_comp_register_without_callback_test.cpp | 112 ----------- ...ec_comp_report_click_without_hmac_test.cpp | 116 ----------- ...utton_sample_build.cpp => test_common.cpp} | 110 ++++++++++- .../test/unittest/src/test_common.h | 37 ++-- .../sa/sa_main/sec_comp_entity.cpp | 2 +- .../sa/sa_main/sec_comp_perm_manager.cpp | 6 +- .../sa/test/BUILD.gn | 8 +- .../mock/src/sec_comp_enhance_adapter.cpp | 1 + .../unittest/src/app_state_observer_test.cpp | 53 +++-- .../unittest/src/sec_comp_entity_test.cpp | 21 +- .../src/sec_comp_info_helper_test.cpp | 109 +++-------- .../unittest/src/sec_comp_manager_test.cpp | 97 ++++------ .../src/sec_comp_service_mock_test.cpp | 182 +++++++----------- .../unittest/src/sec_comp_service_mock_test.h | 25 ++- .../unittest/src/sec_comp_service_test.cpp | 88 +++------ .../test/unittest/src/sec_comp_stub_test.cpp | 48 ++--- .../test/unittest/src/service_test_common.cpp | 168 ++++++++++++++++ .../test/unittest/src/service_test_common.h | 66 +++++++ 29 files changed, 835 insertions(+), 1084 deletions(-) delete mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp delete mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp delete mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp rename interfaces/inner_api/security_component/test/unittest/src/{location_button_sample_build.cpp => test_common.cpp} (38%) rename services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h => interfaces/inner_api/security_component/test/unittest/src/test_common.h (44%) rename interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h => services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.h (65%) create mode 100644 services/security_component_service/sa/test/unittest/src/service_test_common.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/service_test_common.h diff --git a/BUILD.gn b/BUILD.gn index 58080d8..1f7cd3c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -40,8 +40,6 @@ group("security_component_build_fuzz_test") { testonly = true deps = [] if (is_standard_system) { - deps += [ - "test/fuzztest/security_component:fuzztest", - ] + deps += [ "test/fuzztest/security_component:fuzztest" ] } -} \ No newline at end of file +} diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 866b79a..c0647d2 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -59,7 +59,7 @@ config("libsecurity_component_enhance_adapter_config") { "common/include", "enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - "//third_party/json/include" + "//third_party/json/include", ] } @@ -72,7 +72,7 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { "common/include", "enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - "//third_party/json/include" + "//third_party/json/include", ] sources = [ "enhance_adapter/src/sec_comp_enhance_adapter.cpp" ] diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index 03d5acf..4acbcce 100644 --- a/interfaces/inner_api/enhance_kits/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -37,9 +37,7 @@ ohos_shared_library("libsecurity_component_enhance_sdk") { "${sec_comp_root_dir}/frameworks/enhance_adapter/include", ] - sources = [ - "src/sec_comp_enhance_kit.cpp", - ] + sources = [ "src/sec_comp_enhance_kit.cpp" ] deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", @@ -50,7 +48,7 @@ ohos_shared_library("libsecurity_component_enhance_sdk") { external_deps = [ "c_utils:utils", "hilog:libhilog", - "ipc:ipc_single" + "ipc:ipc_single", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index a286b4c..117f24a 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -44,12 +44,12 @@ ohos_shared_library("libsecurity_component_sdk") { "src/sec_comp_kit.cpp", "src/sec_comp_load_callback.cpp", "src/sec_comp_proxy.cpp", - "src/sec_comp_ui_register.cpp" + "src/sec_comp_ui_register.cpp", ] deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", - "${sec_comp_root_dir}/frameworks:libsecurity_component_framework" + "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index 0aabcdc..fdba91b 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -31,6 +31,7 @@ ohos_unittest("sec_comp_sdk_test") { "unittest/src/paste_button_test.cpp", "unittest/src/save_button_test.cpp", "unittest/src/sec_comp_kit_test.cpp", + "unittest/src/test_common.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -49,7 +50,7 @@ ohos_unittest("sec_comp_sdk_test") { ] } -ohos_unittest("sec_comp_register_test") { +ohos_unittest("sec_comp_register_callback_test") { subsystem_name = "security" part_name = "security_component" module_out_path = part_name + "/" + part_name @@ -61,105 +62,8 @@ ohos_unittest("sec_comp_register_test") { ] sources = [ - "unittest/src/location_button_sample_build.cpp", "unittest/src/sec_comp_register_callback_test.cpp", - ] - configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] - cflags_cc = [ "-DHILOG_ENABLE" ] - - deps = [ - "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", - "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", - ] - - external_deps = [ - "access_token:libaccesstoken_sdk", - "c_utils:utils", - "hilog:libhilog", - "hisysevent:libhisysevent", - "ipc:ipc_core", - ] -} - -ohos_unittest("sec_comp_report_click_without_hmac_test") { - subsystem_name = "security" - part_name = "security_component" - module_out_path = part_name + "/" + part_name - - include_dirs = [ - "${sec_comp_root_dir}/frameworks/common/include", - "${sec_comp_root_dir}/frameworks/security_component/include", - "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - ] - - sources = [ - "unittest/src/location_button_sample_build.cpp", - "unittest/src/sec_comp_report_click_without_hmac_test.cpp", - ] - configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] - cflags_cc = [ "-DHILOG_ENABLE" ] - - deps = [ - "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", - "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", - ] - - external_deps = [ - "access_token:libaccesstoken_sdk", - "c_utils:utils", - "hilog:libhilog", - "hisysevent:libhisysevent", - "ipc:ipc_core", - ] -} - -ohos_unittest("sec_comp_register_without_callback_test") { - subsystem_name = "security" - part_name = "security_component" - module_out_path = part_name + "/" + part_name - - include_dirs = [ - "${sec_comp_root_dir}/frameworks/common/include", - "${sec_comp_root_dir}/frameworks/security_component/include", - "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - ] - - sources = [ - "unittest/src/location_button_sample_build.cpp", - "unittest/src/sec_comp_register_without_callback_test.cpp", - ] - configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] - cflags_cc = [ "-DHILOG_ENABLE" ] - - deps = [ - "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", - "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", - ] - - external_deps = [ - "access_token:libaccesstoken_sdk", - "c_utils:utils", - "hilog:libhilog", - "hisysevent:libhisysevent", - "ipc:ipc_core", - ] -} - -ohos_unittest("sec_comp_register_challenge_test") { - subsystem_name = "security" - part_name = "security_component" - module_out_path = part_name + "/" + part_name - - include_dirs = [ - "${sec_comp_root_dir}/frameworks/common/include", - "${sec_comp_root_dir}/frameworks/enhance_adapter/include", - "${sec_comp_root_dir}/frameworks/security_component/include", - "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - ] - - sources = [ - "unittest/src/location_button_sample_build.cpp", - "unittest/src/sec_comp_register_challenge_test.cpp", + "unittest/src/test_common.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -172,6 +76,7 @@ ohos_unittest("sec_comp_register_challenge_test") { external_deps = [ "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", "c_utils:utils", "hilog:libhilog", "hisysevent:libhisysevent", @@ -182,10 +87,7 @@ ohos_unittest("sec_comp_register_challenge_test") { group("unittest") { testonly = true deps = [ + ":sec_comp_register_callback_test", ":sec_comp_sdk_test", - ":sec_comp_register_challenge_test", - ":sec_comp_register_test", - ":sec_comp_register_without_callback_test", - ":sec_comp_report_click_without_hmac_test" ] } diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index ef7503a..972d4c2 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -13,9 +13,11 @@ * limitations under the License. */ #include "location_button_test.h" + #include #include "sec_comp_log.h" #include "sec_comp_err.h" +#include "test_common.h" using namespace testing::ext; using namespace OHOS; @@ -24,64 +26,6 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButtonTest"}; - -static const std::string WRONG_TYPE = "wrongType"; -static constexpr float TEST_SIZE = 100.0; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_DIMENSION = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; -static constexpr uint32_t TEST_DIFF_COLOR = 0; - -static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) -{ - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, - }; - - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } - }; - - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; -} } void LocationButtonTest::SetUpTestCase() @@ -107,7 +51,7 @@ void LocationButtonTest::TearDown() HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); } @@ -134,7 +78,7 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); @@ -160,7 +104,7 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) jsonComponent[JsonTagConstants::JSON_RECT] = wrongJson; ASSERT_FALSE(comp.FromJson(jsonComponent)); - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { @@ -206,7 +150,7 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { @@ -251,7 +195,7 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { @@ -293,7 +237,7 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { @@ -335,7 +279,7 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { @@ -343,7 +287,7 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) }; ASSERT_FALSE(comp.FromJson(jsonComponent)); - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { { JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, }; @@ -359,7 +303,7 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); @@ -394,7 +338,7 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); @@ -428,8 +372,8 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { -nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + nlohmann::json jsonComponent; + TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); @@ -486,7 +430,7 @@ nlohmann::json jsonComponent; HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); @@ -502,7 +446,7 @@ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr, true)); @@ -510,7 +454,7 @@ HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) /** * @tc.name: CompareLocationButton001 - * @tc.desc: Test compare location button + * @tc.desc: Test LocationButton compare * @tc.type: FUNC * @tc.require: AR000HO9J7 */ @@ -520,7 +464,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) LocationButton button2; nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_TRUE(button2.FromJson(jsonComponent)); @@ -551,7 +495,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp1; - BuildLocationComponentInfo(jsonComponent); + TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); LocationButton comp2 = comp1; diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index 625c4ac..4faa121 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -13,9 +13,11 @@ * limitations under the License. */ #include "paste_button_test.h" + #include #include "sec_comp_err.h" #include "sec_comp_log.h" +#include "test_common.h" using namespace testing::ext; using namespace OHOS; @@ -24,64 +26,6 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "PasteButtonTest"}; - -static const std::string WRONG_TYPE = "wrongType"; -static constexpr float TEST_SIZE = 100.0; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_DIMENSION = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; -static constexpr uint32_t TEST_DIFF_COLOR = 0; - -static void BuildPasteComponentInfo(nlohmann::json& jsonComponent) -{ - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, - }; - - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } - }; - - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, - { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; -} } void PasteButtonTest::SetUpTestCase() @@ -106,8 +50,8 @@ void PasteButtonTest::TearDown() */ HWTEST_F(PasteButtonTest, FromJson010, TestSize.Level1) { -nlohmann::json jsonComponent; - BuildPasteComponentInfo(jsonComponent); + nlohmann::json jsonComponent; + TestCommon::BuildPasteComponentInfo(jsonComponent); PasteButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); @@ -167,7 +111,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) PasteButton button2; nlohmann::json jsonComponent; - BuildPasteComponentInfo(jsonComponent); + TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_TRUE(button2.FromJson(jsonComponent)); @@ -198,7 +142,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) { nlohmann::json jsonComponent; PasteButton comp1; - BuildPasteComponentInfo(jsonComponent); + TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); PasteButton comp2 = comp1; @@ -251,4 +195,4 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) comp1.iconColor_.value = TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); -} \ No newline at end of file +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index 8771c78..257b5cb 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -13,9 +13,11 @@ * limitations under the License. */ #include "save_button_test.h" + #include #include "sec_comp_log.h" #include "sec_comp_err.h" +#include "test_common.h" using namespace testing::ext; using namespace OHOS; @@ -24,63 +26,6 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SaveButtonTest"}; - -static constexpr float TEST_SIZE = 100.0; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_DIMENSION = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0x7ffff00; -static constexpr uint32_t TEST_COLOR_RED = 0xff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0x0000ff; -static constexpr uint32_t TEST_DIFF_COLOR = 0; - -static void BuildSaveComponentInfo(nlohmann::json& jsonComponent) -{ - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, - }; - - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } - }; - - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; -} } void SaveButtonTest::SetUpTestCase() @@ -105,8 +50,8 @@ void SaveButtonTest::TearDown() */ HWTEST_F(SaveButtonTest, FromJson010, TestSize.Level1) { -nlohmann::json jsonComponent; - BuildSaveComponentInfo(jsonComponent); + nlohmann::json jsonComponent; + TestCommon::BuildSaveComponentInfo(jsonComponent); SaveButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); @@ -166,7 +111,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) SaveButton button2; nlohmann::json jsonComponent; - BuildSaveComponentInfo(jsonComponent); + TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_TRUE(button2.FromJson(jsonComponent)); @@ -197,7 +142,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) { nlohmann::json jsonComponent; SaveButton comp1; - BuildSaveComponentInfo(jsonComponent); + TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); SaveButton comp2 = comp1; @@ -250,4 +195,4 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) comp1.iconColor_.value = TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); -} \ No newline at end of file +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index c43a610..8128924 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_kit_test.h" + #include "location_button.h" #define private public #include "sec_comp_caller_authorization.h" @@ -21,6 +22,7 @@ #include "sec_comp_info.h" #include "sec_comp_log.h" #include "sec_comp_tool.h" +#include "test_common.h" using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; @@ -28,11 +30,6 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompKitTest"}; -static constexpr float TEST_SIZE = 100.0; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_DIMENSION = 100.0; -static constexpr uint32_t TEST_COLOR = 0xffffffff; -static constexpr size_t MAX_CALLER_SIZE = 10; static void TestInCallerNotCheckList() { @@ -175,3 +172,20 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) SecCompCallerAuthorization::GetInstance().isInit_ = false; } +/** + * @tc.name: RegisterWithoutCallback001 + * @tc.desc: test register without callback. + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + + int32_t scId; + ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, + SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(-1, scId); +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index e907048..07d339a 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -13,19 +13,21 @@ * limitations under the License. */ #include - #include "i_sec_comp_probe.h" #include "location_button.h" -#include "location_button_sample_build.h" #define private public #include "sec_comp_caller_authorization.h" #undef private +#include "sec_comp_client.h" +#include "sec_comp_enhance_adapter.h" #include "sec_comp_err.h" #include "sec_comp_info.h" #include "sec_comp_kit.h" #include "sec_comp_log.h" #include "sec_comp_tool.h" #include "sec_comp_ui_register.h" +#include "test_common.h" +#include "token_setproc.h" using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; @@ -87,6 +89,25 @@ void SecCompRegisterCallbackTest::TearDown() SC_LOG_INFO(LABEL, "TearDown."); } +/** + * @tc.name: RegisterWithoutPreprocess001 + * @tc.desc: test register without preprocess + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterCallbackTest, RegisterWithoutPreprocess001, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + + SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); + int32_t scId; + ASSERT_EQ(SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL, + SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(-1, scId); +} + /** * @tc.name: Register001 * @tc.desc: test register security component success. @@ -95,9 +116,8 @@ void SecCompRegisterCallbackTest::TearDown() */ HWTEST_F(SecCompRegisterCallbackTest, Register001, TestSize.Level1) { - SC_LOG_INFO(LABEL, "Register001."); nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = jsonRes.dump(); g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -117,10 +137,8 @@ HWTEST_F(SecCompRegisterCallbackTest, Register001, TestSize.Level1) */ HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) { - SC_LOG_INFO(LABEL, "Register002."); - nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = jsonRes.dump(); g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = -1; @@ -139,10 +157,8 @@ HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) */ HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) { - SC_LOG_INFO(LABEL, "Register003."); - nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = jsonRes.dump(); g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -151,3 +167,141 @@ HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); } +/** + * @tc.name: RegisterSecurityComponent001 + * @tc.desc: Test register security component check touch info failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) + }; + touchInfo.extraInfo.dataSize = MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: RegisterSecurityComponent002 + * @tc.desc: Test register security component permission grant failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.desc: Test register security component success + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + + ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + uint32_t selfTokenId = GetSelfTokenID(); + ASSERT_TRUE(SecCompKit::ReduceAfterVerifySavePermission(selfTokenId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: ExceptCall001 + * @tc.desc: Test update security component success + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompRegisterCallbackTest, ExceptCall001, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId)); + ASSERT_NE(-1, scId); + ASSERT_EQ(SC_OK, SecCompKit::UpdateSecurityComponent(scId, saveInfo)); +} + +/** + * @tc.name: ReportClickWithoutHmac001 + * @tc.desc: Test report click event permission denied + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildLocationComponentInfo(jsonRes); + std::string locationInfo = jsonRes.dump(); + g_probe.mockComponentInfo = locationInfo; + g_probe.mockRes = 0; + + int32_t scId; + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_NE(-1, scId); + uint8_t data[MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touch = { + .touchX = TEST_COORDINATE, + .touchY = TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + }; + touch.extraInfo.dataSize = MAX_HMAC_SIZE; + touch.extraInfo.data = data; + EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, + SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); + system("param set sec.comp.enhance 0"); +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp deleted file mode 100644 index c7e0273..0000000 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) 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. - */ -#include - -#include "i_sec_comp_probe.h" -#include "location_button.h" -#include "location_button_sample_build.h" -#define private public -#include "sec_comp_caller_authorization.h" -#undef private -#include "sec_comp_client.h" -#include "sec_comp_enhance_adapter.h" -#include "sec_comp_err.h" -#include "sec_comp_info.h" -#include "sec_comp_kit.h" -#include "sec_comp_log.h" -#include "sec_comp_tool.h" -#include "sec_comp_ui_register.h" - -using namespace testing::ext; -using namespace OHOS::Security::SecurityComponent; - -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterChallengeTest"}; - -class MockUiSecCompProbe : public ISecCompProbe { -public: - int32_t GetComponentInfo(int32_t nodeId, std::string& componentInfo) override - { - componentInfo = mockComponentInfo; - return mockRes; - } - std::string mockComponentInfo; - int32_t mockRes; -}; - -static MockUiSecCompProbe g_probe; - -static void InitUiRegister() -{ - std::vector callerList; - SecCompUiRegister registerCallback(callerList, &g_probe); - SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); - SecCompCallerAuthorization::GetInstance().isInit_ = false; -} -} // namespace - -class SecCompRegisterChallengeTest : public testing::Test { -public: - static void SetUpTestCase(); - - static void TearDownTestCase(); - - void SetUp() override; - - void TearDown() override; -}; - -void SecCompRegisterChallengeTest::SetUpTestCase() -{ - InitUiRegister(); - SC_LOG_INFO(LABEL, "SecCompRegisterChallengeTest."); -} - -void SecCompRegisterChallengeTest::TearDownTestCase() -{ - SC_LOG_INFO(LABEL, "SecCompRegisterChallengeTest."); -} - -void SecCompRegisterChallengeTest::SetUp() -{ - SC_LOG_INFO(LABEL, "SetUp ok."); -} - -void SecCompRegisterChallengeTest::TearDown() -{ - SC_LOG_INFO(LABEL, "TearDown."); -} - -/** - * @tc.name: RegisterWithoutPreprocess001 - * @tc.desc: test register without preprocess - * @tc.type: FUNC - * @tc.require: AR000HO9JM - */ -HWTEST_F(SecCompRegisterChallengeTest, RegisterWithoutPreprocess001, TestSize.Level1) -{ - nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); - - SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); - int32_t scId; - ASSERT_EQ(SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL, - SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); - ASSERT_EQ(-1, scId); -} - diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp deleted file mode 100644 index 8c835ca..0000000 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (c) 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. - */ -#include - -#include "i_sec_comp_probe.h" -#include "location_button.h" -#include "location_button_sample_build.h" -#define private public -#include "sec_comp_caller_authorization.h" -#undef private -#include "sec_comp_err.h" -#include "sec_comp_info.h" -#include "sec_comp_kit.h" -#include "sec_comp_log.h" -#include "sec_comp_tool.h" -#include "sec_comp_ui_register.h" - -using namespace testing::ext; -using namespace OHOS::Security::SecurityComponent; - -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterWithoutCallbackTest"}; - -static void InitUiRegister() -{ - std::vector callerList; - SecCompUiRegister registerCallback(callerList, nullptr); - SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); - SecCompCallerAuthorization::GetInstance().isInit_ = false; -} -} // namespace - -class SecCompRegisterWithoutCallbackTest : public testing::Test { -public: - static void SetUpTestCase(); - - static void TearDownTestCase(); - - void SetUp() override; - - void TearDown() override; -}; - -void SecCompRegisterWithoutCallbackTest::SetUpTestCase() -{ - InitUiRegister(); - SC_LOG_INFO(LABEL, "SecCompRegisterWithoutCallbackTest."); -} - -void SecCompRegisterWithoutCallbackTest::TearDownTestCase() -{ - SC_LOG_INFO(LABEL, "SecCompRegisterWithoutCallbackTest."); -} - -void SecCompRegisterWithoutCallbackTest::SetUp() -{ - SC_LOG_INFO(LABEL, "SetUp ok."); -} - -void SecCompRegisterWithoutCallbackTest::TearDown() -{ - SC_LOG_INFO(LABEL, "TearDown."); -} - -/** - * @tc.name: RegisterWithoutCallback001 - * @tc.desc: test register without callback - * @tc.type: FUNC - * @tc.require: AR000HO9JM - */ -HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback001, TestSize.Level1) -{ - nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); - - int32_t scId; - ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, - SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); - ASSERT_EQ(-1, scId); -} - -/** - * @tc.name: RegisterWithoutCallback002 - * @tc.desc: test register in MaliciousAppList. - * @tc.type: FUNC - * @tc.require: AR000HO9JM - */ -HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback002, TestSize.Level1) -{ - nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); - - int32_t scId; - ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, - SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); - ASSERT_EQ(-1, scId); -} diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp deleted file mode 100644 index 7c59fb2..0000000 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_report_click_without_hmac_test.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 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. - */ -#include - -#include "i_sec_comp_probe.h" -#include "location_button.h" -#include "location_button_sample_build.h" -#define private public -#include "sec_comp_caller_authorization.h" -#undef private -#include "sec_comp_err.h" -#include "sec_comp_info.h" -#include "sec_comp_kit.h" -#include "sec_comp_log.h" -#include "sec_comp_tool.h" -#include "sec_comp_ui_register.h" - -using namespace testing::ext; -using namespace OHOS::Security::SecurityComponent; - -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompReportClickWithoutHmacTest"}; -static constexpr double TEST_COORDINATE = 100.0; - -class MockUiSecCompProbe : public ISecCompProbe { -public: - int32_t GetComponentInfo(int32_t nodeId, std::string& componentInfo) override - { - componentInfo = mockComponentInfo; - return mockRes; - } - std::string mockComponentInfo; - int32_t mockRes; -}; - -static MockUiSecCompProbe g_probe; -static void InitUiRegister() -{ - std::vector callerList; - SecCompUiRegister registerCallback(callerList, &g_probe); - SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); - SecCompCallerAuthorization::GetInstance().isInit_ = false; -} -} // namespace - -class SecCompReportClickWithoutHmacTest : public testing::Test { -public: - static void SetUpTestCase(); - - static void TearDownTestCase(); - - void SetUp() override; - - void TearDown() override; -}; - -void SecCompReportClickWithoutHmacTest::SetUpTestCase() -{ - InitUiRegister(); - SC_LOG_INFO(LABEL, "SecCompReportClickWithoutHmacTest."); -} - -void SecCompReportClickWithoutHmacTest::TearDownTestCase() -{ - SC_LOG_INFO(LABEL, "SecCompReportClickWithoutHmacTest."); -} - -void SecCompReportClickWithoutHmacTest::SetUp() -{ - SC_LOG_INFO(LABEL, "SetUp ok."); -} - -void SecCompReportClickWithoutHmacTest::TearDown() -{ - SC_LOG_INFO(LABEL, "TearDown."); -} - -/** - * @tc.name: ReportClickWithoutHmac001 - * @tc.desc: test report click event without hmac data - * @tc.type: FUNC - * @tc.require: AR000HO9JM - */ -HWTEST_F(SecCompReportClickWithoutHmacTest, ReportClickWithoutHmac001, TestSize.Level1) -{ - nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); - g_probe.mockComponentInfo = locationInfo; - g_probe.mockRes = 0; - - int32_t scId; - ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); - ASSERT_NE(-1, scId); - - struct SecCompClickEvent touch = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, - .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) - }; - EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, - SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); -} diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp similarity index 38% rename from interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp rename to interfaces/inner_api/security_component/test/unittest/src/test_common.cpp index 6cdb68d..067bfe9 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp @@ -12,20 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "location_button_sample_build.h" +#include "test_common.h" namespace OHOS { namespace Security { namespace SecurityComponent { -static const std::string WRONG_TYPE = "wrongType"; -static constexpr float TEST_SIZE = 100.0; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_DIMENSION = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0xffffff00; -static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; - -void LocationButtonSampleBuild::BuildLocationComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::BuildLocationComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { @@ -73,6 +65,104 @@ void LocationButtonSampleBuild::BuildLocationComponentInfo(nlohmann::json& jsonC { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; } + +void TestCommon::BuildSaveComponentInfo(nlohmann::json& jsonComponent) +{ + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; +} + +void TestCommon::BuildPasteComponentInfo(nlohmann::json& jsonComponent) +{ + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h similarity index 44% rename from services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h rename to interfaces/inner_api/security_component/test/unittest/src/test_common.h index 36bbaaa..34f3a18 100644 --- a/services/security_component_service/sa/test/mock/include/sec_comp_enhance_adapter.h +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.h @@ -12,25 +12,36 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef SECURITY_COMPONENT_ENHANCE_ADAPTER_H -#define SECURITY_COMPONENT_ENHANCE_ADAPTER_H -#include -#include "parcel.h" -#include "sec_comp_info.h" +#ifndef TEST_COMMON_H +#define TEST_COMMON_H +#include "location_button.h" +#include "paste_button.h" +#include "save_button.h" namespace OHOS { namespace Security { namespace SecurityComponent { -struct SecCompEnhanceAdapter { - static int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen); - static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen, - uint8_t* enhanceData, uint32_t& enHancedataLen); - static int32_t CheckExtraInfo(const SecCompClickEvent& touchInfo); +const std::string WRONG_TYPE = "wrongType"; +constexpr float TEST_SIZE = 100.0; +constexpr double TEST_COORDINATE = 100.0; +constexpr double TEST_DIMENSION = 100.0; +constexpr uint32_t TEST_COLOR = 0xffffffff; +constexpr uint32_t TEST_COLOR_YELLOW = 0xffffff00; +constexpr uint32_t TEST_COLOR_RED = 0xffff0000; +constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; +constexpr uint32_t TEST_DIFF_COLOR = 0; +constexpr uint64_t TIME_CONVERSION_UNIT = 1000; +constexpr uint32_t HAP_TOKEN_ID = 537715419; +constexpr uint32_t MAX_HMAC_SIZE = 64; +constexpr size_t MAX_CALLER_SIZE = 10; - static int32_t EnableInputEnhance(); - static int32_t DisableInputEnhance(); +class TestCommon { +public: + static void BuildLocationComponentInfo(nlohmann::json& jsonComponent); + static void BuildSaveComponentInfo(nlohmann::json& jsonComponent); + static void BuildPasteComponentInfo(nlohmann::json& jsonComponent); }; } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // SECURITY_COMPONENT_ENHANCE_ADAPTER_H +#endif // TEST_COMMON_H diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index aec385a..91ec015 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -69,7 +69,7 @@ bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const int32_t res = SecCompEnhanceAdapter::CheckExtraInfo(touchInfo); if ((res != SC_OK) && (res != SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE)) { - SC_LOG_ERROR(LABEL, "HMAC checkout failed" + SC_LOG_ERROR(LABEL, "HMAC checkout failed," "touchX:%{public}f, touchY:%{public}f, timestamp:%{public}lu, dataSize:%{public}d", touchInfo.touchX, touchInfo.touchY, touchInfo.timestamp, touchInfo.extraInfo.dataSize); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index 23f9d1a..7c10b7a 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -70,7 +70,8 @@ int32_t SecCompPermManager::GrantLocationPermission(AccessToken::AccessTokenID t { int32_t res = AccessToken::AccessTokenKit::GrantPermission(tokenId, permissionName, AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); - SC_LOG_INFO(LABEL, "grant permission: %{public}s, tokenId:%{public}d", permissionName.c_str(), tokenId); + SC_LOG_INFO(LABEL, "grant permission res: %{public}d, permission: %{public}s, tokenId:%{public}d", + res, permissionName.c_str(), tokenId); return res; } @@ -79,7 +80,8 @@ int32_t SecCompPermManager::RevokeLocationPermission(AccessToken::AccessTokenID { int32_t res = AccessToken::AccessTokenKit::RevokePermission(tokenId, permissionName, AccessToken::PermissionFlag::PERMISSION_COMPONENT_SET); - SC_LOG_INFO(LABEL, "revoke permission: %{public}s, tokenId:%{public}d", permissionName.c_str(), tokenId); + SC_LOG_INFO(LABEL, "revoke permission res: %{public}d, permission: %{public}s, tokenId:%{public}d", + res, permissionName.c_str(), tokenId); return res; } diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 04537f3..84cd54b 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -36,9 +36,9 @@ ohos_unittest("sec_comp_service_test") { "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", - "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_manager.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", @@ -50,6 +50,7 @@ ohos_unittest("sec_comp_service_test") { "unittest/src/sec_comp_manager_test.cpp", "unittest/src/sec_comp_service_test.cpp", "unittest/src/sec_comp_stub_test.cpp", + "unittest/src/service_test_common.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] @@ -99,9 +100,9 @@ ohos_unittest("sec_comp_service_mock_test") { "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_entity.cpp", - "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_manager.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", @@ -109,6 +110,7 @@ ohos_unittest("sec_comp_service_mock_test") { "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp", "unittest/src/sec_comp_service_mock_test.cpp", + "unittest/src/service_test_common.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] @@ -139,7 +141,7 @@ ohos_unittest("sec_comp_service_mock_test") { group("unittest") { testonly = true deps = [ - ":sec_comp_service_test", ":sec_comp_service_mock_test", + ":sec_comp_service_test", ] } diff --git a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp index 7c5538e..812f046 100644 --- a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp +++ b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_enhance_adapter.h" + #include "sec_comp_err.h" #include "sec_comp_log.h" diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp index 4923cdb..946262a 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -13,8 +13,10 @@ * limitations under the License. */ #include "app_state_observer_test.h" + #include "sec_comp_log.h" #include "sec_comp_err.h" +#include "service_test_common.h" using namespace testing::ext; using namespace OHOS; @@ -23,11 +25,6 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "AppStateObserverTest"}; - -static constexpr int32_t TEST_UID = 1; -static constexpr int32_t TEST_UID_2 = 2; -static constexpr int32_t TEST_PID = 1; -static constexpr int32_t TEST_PID_2 = 2; } void AppStateObserverTest::SetUpTestCase() @@ -60,13 +57,13 @@ void AppStateObserverTest::TearDown() */ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) { - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); AppExecFwk::AppStateData stateData = { - .pid = TEST_PID, - .uid = TEST_UID, + .pid = TEST_PID_1, + .uid = TEST_UID_1, }; observer_->AddProcessToForegroundSet(stateData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); } /** @@ -78,12 +75,12 @@ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) { AppExecFwk::AppStateData stateData = { - .pid = TEST_PID, - .uid = TEST_UID, + .pid = TEST_PID_1, + .uid = TEST_UID_1, }; observer_->AddProcessToForegroundSet(stateData); observer_->AddProcessToForegroundSet(stateData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); AppExecFwk::ProcessData procData = { .pid = TEST_PID_2, @@ -103,14 +100,14 @@ HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level1) { AppExecFwk::ProcessData procData = { - .pid = TEST_PID, - .uid = TEST_UID, + .pid = TEST_PID_1, + .uid = TEST_UID_1, }; observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); observer_->RemoveProcessFromForegroundSet(procData); observer_->RemoveProcessFromForegroundSet(procData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); } /** @@ -122,14 +119,14 @@ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet002, TestSize.Level1) { AppExecFwk::ProcessData procData = { - .pid = TEST_PID, - .uid = TEST_UID, + .pid = TEST_PID_1, + .uid = TEST_UID_1, }; observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); procData.pid = TEST_PID_2; observer_->RemoveProcessFromForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); } /** @@ -146,14 +143,14 @@ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) ASSERT_EQ(observer_->foregrandProcList_.size(), static_cast(0)); processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; - processData.pid = TEST_PID; + processData.pid = TEST_PID_1; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); processData.state = AppExecFwk::AppProcessState::APP_STATE_BACKGROUND; - processData.pid = TEST_PID; + processData.pid = TEST_PID_1; observer_->OnProcessStateChanged(processData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); } /** @@ -166,9 +163,9 @@ HWTEST_F(AppStateObserverTest, OnProcessDied001, TestSize.Level1) { AppExecFwk::ProcessData processData; processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; - processData.pid = TEST_PID; + processData.pid = TEST_PID_1; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); // if last process died, the sec_comp service will exit. processData.pid = TEST_PID_2; @@ -176,7 +173,7 @@ HWTEST_F(AppStateObserverTest, OnProcessDied001, TestSize.Level1) ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_2, TEST_UID_2)); // notify process 1 died - processData.pid = TEST_PID; + processData.pid = TEST_PID_1; observer_->OnProcessDied(processData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID, TEST_UID)); + ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 66d2831..2304db6 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -13,12 +13,14 @@ * limitations under the License. */ #include "sec_comp_entity_test.h" + #include "sec_comp_log.h" #include "location_button.h" #include "paste_button.h" #include "save_button.h" #include "sec_comp_err.h" #include "sec_comp_tool.h" +#include "service_test_common.h" using namespace testing::ext; using namespace OHOS; @@ -28,9 +30,6 @@ using namespace OHOS::Security::AccessToken; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntityTest"}; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_DIFF_COORDINATE = 200.0; -static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; } void SecCompEntityTest::SetUpTestCase() @@ -67,16 +66,16 @@ void SecCompEntityTest::TearDown() HWTEST_F(SecCompEntityTest, RevokeTempPermission001, TestSize.Level1) { entity_->isGrant_ = false; - ASSERT_EQ(entity_->RevokeTempPermission(), SC_OK); + ASSERT_EQ(SC_OK, entity_->RevokeTempPermission()); entity_->isGrant_ = true; entity_->componentInfo_->type_ = UNKNOWN_SC_TYPE; - ASSERT_EQ(entity_->RevokeTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, entity_->RevokeTempPermission()); ASSERT_FALSE(entity_->isGrant_); entity_->isGrant_ = true; entity_->componentInfo_->type_ = LOCATION_COMPONENT; - ASSERT_EQ(entity_->RevokeTempPermission(), SC_OK); + ASSERT_EQ(SC_OK, entity_->RevokeTempPermission()); ASSERT_FALSE(entity_->isGrant_); } @@ -90,17 +89,17 @@ HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level1) { entity_->isGrant_ = false; entity_->componentInfo_->type_ = UNKNOWN_SC_TYPE; - ASSERT_EQ(entity_->GrantTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, entity_->GrantTempPermission()); ASSERT_TRUE(entity_->isGrant_); entity_->isGrant_ = false; entity_->componentInfo_->type_ = LOCATION_COMPONENT; - ASSERT_EQ(entity_->GrantTempPermission(), SC_OK); + ASSERT_EQ(SC_OK, entity_->GrantTempPermission()); ASSERT_TRUE(entity_->isGrant_); entity_->isGrant_ = false; entity_->componentInfo_->type_ = PASTE_COMPONENT; - ASSERT_EQ(entity_->GrantTempPermission(), SC_OK); + ASSERT_EQ(SC_OK, entity_->GrantTempPermission()); ASSERT_TRUE(entity_->isGrant_); } @@ -118,8 +117,8 @@ HWTEST_F(SecCompEntityTest, GrantTempPermission002, TestSize.Level1) entity_ = std::make_shared(pasteComponent, 0, 1); ASSERT_NE(nullptr, entity_); - ASSERT_EQ(entity_->GrantTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); - ASSERT_EQ(entity_->RevokeTempPermission(), SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, entity_->GrantTempPermission()); + ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, entity_->RevokeTempPermission()); } /** diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index ac86c7f..8c6c251 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -14,14 +14,16 @@ */ #include "sec_comp_info_helper_test.h" + +#include "display.h" +#include "display_info.h" +#include "display_manager.h" #include "location_button.h" #include "paste_button.h" #include "save_button.h" #include "sec_comp_log.h" #include "sec_comp_err.h" -#include "display.h" -#include "display_info.h" -#include "display_manager.h" +#include "service_test_common.h" using namespace testing::ext; using namespace OHOS; @@ -30,18 +32,6 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelperTest"}; - -static constexpr float TEST_SIZE = 100.0; -static constexpr double TEST_DIMENSION = 100.0; -static constexpr double TEST_INVALID_DIMENSION = -100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; -static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; -static constexpr uint32_t TEST_COLOR_BLACK = 0x00000000; -static constexpr uint32_t TEST_COLOR_WHITE = 0xffffffff; -static constexpr uint32_t QUARTER = 4; -static constexpr double ZERO_OFFSET = 0.0F; static double g_curScreenWidth = 0.0F; static double g_curScreenHeight = 0.0F; static double g_testWidth = 0.0F; @@ -64,55 +54,6 @@ static bool GetScreenSize() g_curScreenHeight = static_cast(info->GetHeight()); return true; } - -static void BuildLocationComponentInfo(nlohmann::json& jsonComponent) -{ - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, g_testWidth }, - {JsonTagConstants::JSON_RECT_Y, g_testHeight }, - {JsonTagConstants::JSON_RECT_WIDTH, g_testWidth }, - {JsonTagConstants::JSON_RECT_HEIGHT, g_testHeight } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - { JsonTagConstants::JSON_RECT_X, g_testWidth }, - { JsonTagConstants::JSON_RECT_Y, g_testHeight }, - { JsonTagConstants::JSON_RECT_WIDTH, g_testWidth }, - { JsonTagConstants::JSON_RECT_HEIGHT, g_testHeight } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, - }; - - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } - }; - - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; -} } void SecCompInfoHelperTest::SetUpTestCase() @@ -142,9 +83,9 @@ void SecCompInfoHelperTest::TearDown() HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_NE(comp, nullptr); + ASSERT_NE(nullptr, comp); ASSERT_TRUE(comp->GetValid()); } @@ -157,11 +98,11 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) { nlohmann::json jsonComponent; - ASSERT_EQ(SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent), nullptr); + ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent)); - ASSERT_EQ(SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent), nullptr); - ASSERT_EQ(SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent), nullptr); - ASSERT_EQ(SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent), nullptr); + ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent)); + ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent)); + ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent)); } /** @@ -173,14 +114,14 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); jsonComponent[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); - ASSERT_EQ(comp, nullptr); + ASSERT_EQ(nullptr, comp); } /** @@ -268,7 +209,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -289,7 +230,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -352,7 +293,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -398,7 +339,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -444,7 +385,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -482,7 +423,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -504,7 +445,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -557,7 +498,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -608,7 +549,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); @@ -661,7 +602,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { @@ -681,7 +622,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->text_ = UNKNOWN_TEXT; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); @@ -703,7 +644,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) { nlohmann::json jsonComponent; - BuildLocationComponentInfo(jsonComponent); + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->type_ = SecCompType::UNKNOWN_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 737c938..be5d04b 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -14,10 +14,12 @@ */ #include "sec_comp_manager_test.h" + #include "sec_comp_log.h" #include "location_button.h" #include "save_button.h" #include "sec_comp_err.h" +#include "service_test_common.h" using namespace testing::ext; using namespace OHOS; @@ -28,26 +30,6 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompManagerTest"}; -static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_INVALID_SIZE = 0; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr uint32_t TEST_COLOR = 0xffffffff; -static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; - -static constexpr int32_t TEST_PID_1 = 1; -static constexpr int32_t TEST_PID_2 = 2; -static constexpr int32_t TEST_PID_3 = 3; - -static constexpr AccessTokenID TEST_TOKEN_ID = 1; -static constexpr int32_t TEST_SC_ID_1 = 1; -static constexpr int32_t TEST_SC_ID_2 = 2; -static constexpr int32_t TEST_INVALID_SC_ID = -1; - -static constexpr int32_t MAX_INT_NUM = 0x7fffffff; -static constexpr int32_t SC_ID_START = 1000; - static LocationButton BuildInvalidLocationComponent() { LocationButton button; @@ -58,9 +40,9 @@ static LocationButton BuildInvalidLocationComponent() button.padding_.bottom = TEST_INVALID_SIZE; button.padding_.left = TEST_INVALID_SIZE; button.textIconSpace_ = TEST_INVALID_SIZE; - button.fontColor_.value = TEST_COLOR; - button.iconColor_.value = TEST_COLOR; - button.bgColor_.value = TEST_COLOR; + button.fontColor_.value = TEST_COLOR_WHITE; + button.iconColor_.value = TEST_COLOR_WHITE; + button.bgColor_.value = TEST_COLOR_WHITE; button.borderWidth_ = TEST_INVALID_SIZE; button.type_ = LOCATION_COMPONENT; button.rect_.x_ = TEST_COORDINATE; @@ -133,10 +115,10 @@ void SecCompManagerTest::TearDown() HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) { SecCompManager::GetInstance().scIdStart_ = MAX_INT_NUM; - ASSERT_EQ(SecCompManager::GetInstance().CreateScId(), SC_ID_START); + ASSERT_EQ(SC_ID_START, SecCompManager::GetInstance().CreateScId()); SecCompManager::GetInstance().scIdStart_ = SC_ID_START; - ASSERT_EQ(SecCompManager::GetInstance().CreateScId(), SC_ID_START + 1); + ASSERT_EQ(SC_ID_START + 1, SecCompManager::GetInstance().CreateScId()); } /** @@ -148,23 +130,23 @@ HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) { std::shared_ptr compPtr = std::make_shared(); - ASSERT_NE(compPtr, nullptr); + ASSERT_NE(nullptr, compPtr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entity), SC_OK); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, entity)); std::shared_ptr compPtrNew = std::make_shared(); - ASSERT_NE(compPtrNew, nullptr); + ASSERT_NE(nullptr, compPtrNew); compPtrNew->rect_.x_ = TEST_COORDINATE * 2; // not overlap compPtrNew->rect_.y_ = TEST_COORDINATE * 2; // not overlap compPtrNew->rect_.width_ = TEST_COORDINATE; compPtrNew->rect_.height_ = TEST_COORDINATE; SecCompEntity entityNew(compPtrNew, TEST_TOKEN_ID, TEST_SC_ID_2); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(1, entityNew), SC_OK); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, entityNew)); } /** @@ -179,18 +161,18 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); std::shared_ptr compPtr = std::make_shared(); - ASSERT_NE(compPtr, nullptr); + ASSERT_NE(nullptr, compPtr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); - ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2), - SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, + SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2)); - ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), SC_OK); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); } /** @@ -201,18 +183,18 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 */ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) { - ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); + ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); std::shared_ptr compPtr = std::make_shared(); - ASSERT_NE(compPtr, nullptr); + ASSERT_NE(nullptr, compPtr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); - ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2), nullptr); - ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); + ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2)); + ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); } /** @@ -226,15 +208,15 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) SecCompManager::GetInstance().NotifyProcessBackground(TEST_PID_1); std::shared_ptr compPtr = std::make_shared(); - ASSERT_NE(compPtr, nullptr); + ASSERT_NE(nullptr, compPtr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1); - ASSERT_NE(component, nullptr); + ASSERT_NE(nullptr, component); SecCompManager::GetInstance().NotifyProcessForeground(TEST_PID_1); ASSERT_TRUE(SecCompManager::GetInstance().IsForegroundCompExist()); @@ -251,28 +233,28 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) { std::shared_ptr compPtr = std::make_shared(); - ASSERT_NE(compPtr, nullptr); + ASSERT_NE(nullptr, compPtr); compPtr->rect_.x_ = TEST_COORDINATE; compPtr->rect_.y_ = TEST_COORDINATE; compPtr->rect_.width_ = TEST_COORDINATE; compPtr->rect_.height_ = TEST_COORDINATE; SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity), SC_OK); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); std::shared_ptr compPtr2 = std::make_shared(); - ASSERT_NE(compPtr2, nullptr); + ASSERT_NE(nullptr, compPtr2); compPtr2->rect_.x_ = TEST_COORDINATE * 2; // not overlap compPtr2->rect_.y_ = TEST_COORDINATE * 2; // not overlap compPtr2->rect_.width_ = TEST_COORDINATE; compPtr2->rect_.height_ = TEST_COORDINATE; SecCompEntity entity2(compPtr2, TEST_TOKEN_ID, TEST_SC_ID_2); - ASSERT_EQ(SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_2, entity2), SC_OK); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_2, entity2)); SecCompManager::GetInstance().NotifyProcessDied(TEST_PID_3); - ASSERT_NE(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); + ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); SecCompManager::GetInstance().NotifyProcessDied(TEST_PID_1); - ASSERT_EQ(SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), nullptr); + ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); } /** @@ -291,16 +273,16 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) .tokenId = TEST_TOKEN_ID, .pid = TEST_PID_1 }; - ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId), - SC_SERVICE_ERROR_COMPONENT_INFO_INVALID); + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, + SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); nlohmann::json jsonValid; LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); // callback check failed - ASSERT_EQ(SecCompManager::GetInstance().RegisterSecurityComponent( - LOCATION_COMPONENT, jsonValid, caller, scId), SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST); + ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, + SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId)); SecCompManager::GetInstance().maliciousAppList_.clear(); } @@ -319,8 +301,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) .tokenId = TEST_TOKEN_ID, .pid = TEST_PID_1 }; - ASSERT_NE(SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller), - SC_OK); + ASSERT_NE(SC_OK, SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller)); } /** @@ -336,8 +317,8 @@ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) .pid = TEST_PID_1 }; - ASSERT_EQ(SecCompManager::GetInstance().UnregisterSecurityComponent(TEST_INVALID_SC_ID, caller), - SC_SERVICE_ERROR_VALUE_INVALID); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, + SecCompManager::GetInstance().UnregisterSecurityComponent(TEST_INVALID_SC_ID, caller)); } /** @@ -356,6 +337,6 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve nlohmann::json jsonVaild; LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); - ASSERT_NE(SecCompManager::GetInstance(). - ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo, nullptr), SC_OK); + ASSERT_NE(SC_OK, + SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo, nullptr)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp index c430ebe..554cceb 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "sec_comp_service_test.h" +#include "sec_comp_service_mock_test.h" + #include "ipc_skeleton.h" #include "iservice_registry.h" #include "location_button.h" @@ -24,6 +25,7 @@ #include "sec_comp_log.h" #include "sec_comp_tool.h" #include "sec_comp_enhance_adapter.h" +#include "service_test_common.h" #include "system_ability.h" #include "token_setproc.h" @@ -34,58 +36,17 @@ using namespace OHOS::Security::AccessToken; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompServiceTest"}; - -static constexpr uint32_t HAP_TOKEN_ID = 537715419; -static constexpr int32_t SA_ID = 3506; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; -static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompServiceMockTest"}; static AccessTokenID g_selfTokenId = 0; - -static std::string BuildSaveComponentInfo() -{ - SaveButton button; - button.fontSize_ = TEST_SIZE; - button.iconSize_ = TEST_SIZE; - button.padding_.top = TEST_SIZE; - button.padding_.right = TEST_SIZE; - button.padding_.bottom = TEST_SIZE; - button.padding_.left = TEST_SIZE; - button.textIconSpace_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_YELLOW; - button.iconColor_.value = TEST_COLOR_RED; - button.bgColor_.value = TEST_COLOR_BLUE; - button.borderWidth_ = TEST_SIZE; - button.type_ = SAVE_COMPONENT; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.windowRect_.x_ = TEST_COORDINATE; - button.windowRect_.y_ = TEST_COORDINATE; - button.windowRect_.width_ = TEST_COORDINATE; - button.windowRect_.height_ = TEST_COORDINATE; - button.text_ = static_cast(SaveDesc::DOWNLOAD); - button.icon_ = static_cast(SaveIcon::LINE_ICON); - button.bg_ = SecCompBackground::CIRCLE; - - nlohmann::json jsonRes; - button.ToJson(jsonRes); - return jsonRes.dump(); -} } -void SecCompServiceTest::SetUpTestCase() +void SecCompServiceMockTest::SetUpTestCase() {} -void SecCompServiceTest::TearDownTestCase() +void SecCompServiceMockTest::TearDownTestCase() {} -void SecCompServiceTest::SetUp() +void SecCompServiceMockTest::SetUp() { SC_LOG_INFO(LABEL, "setup"); if (secCompService_ != nullptr) { @@ -95,11 +56,11 @@ void SecCompServiceTest::SetUp() secCompService_ = sptr(ptr); ASSERT_NE(nullptr, secCompService_); secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); - ASSERT_TRUE(secCompService_->appStateObserver_ != nullptr); + ASSERT_NE(nullptr, secCompService_->appStateObserver_); g_selfTokenId = GetSelfTokenID(); } -void SecCompServiceTest::TearDown() +void SecCompServiceMockTest::TearDown() { if (secCompService_ != nullptr) { secCompService_->appStateObserver_ = nullptr; @@ -114,29 +75,29 @@ void SecCompServiceTest::TearDown() * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) +HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) { // get caller fail int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId), - SC_SERVICE_ERROR_VALUE_INVALID); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId)); + nlohmann::json jsonRes; + ServiceTestCommon::BuildSaveComponentJson(jsonRes); + std::string saveInfo = jsonRes.dump(); // parse component json fail - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "{a=", scId), - SC_SERVICE_ERROR_VALUE_INVALID); // wrong json + // wrong json + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "{a=", scId)); // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); - EXPECT_EQ(secCompService_->UpdateSecurityComponent(scId, BuildSaveComponentInfo()), - SC_OK); + EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + EXPECT_EQ(SC_OK, secCompService_->UpdateSecurityComponent(scId, saveInfo)); struct SecCompClickEvent touch = { .touchX = 100, .touchY = 100, @@ -144,10 +105,9 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touch, nullptr), - SC_OK); + EXPECT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); sleep(5); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); + EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); } /** @@ -156,29 +116,31 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompServiceTest, RegisterSecurityComponent002, TestSize.Level1) +HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) { int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); + nlohmann::json jsonRes; + ServiceTestCommon::BuildSaveComponentJson(jsonRes); + std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); + EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); struct SecCompClickEvent touch = { .touchX = 100, .touchY = 100, .timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touch, nullptr), - SC_SERVICE_ERROR_CLICK_EVENT_INVALID); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); + EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); + EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); } /** @@ -187,29 +149,31 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent002, TestSize.Level1) * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompServiceTest, RegisterSecurityComponent003, TestSize.Level1) +HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) { int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); + nlohmann::json jsonRes; + ServiceTestCommon::BuildSaveComponentJson(jsonRes); + std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(SetSelfTokenID(0), 0); + ASSERT_EQ(0, SetSelfTokenID(0)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); + EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); struct SecCompClickEvent touch = { .touchX = 100, .touchY = 100, .timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touch, nullptr), - SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); + EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, + secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); + EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); } /** @@ -218,20 +182,22 @@ HWTEST_F(SecCompServiceTest, RegisterSecurityComponent003, TestSize.Level1) * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1) +HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); + nlohmann::json jsonRes; + ServiceTestCommon::BuildSaveComponentJson(jsonRes); + std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); + EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); struct SecCompClickEvent touchInfo = { .touchX = 100, .touchY = 100, @@ -239,18 +205,16 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - ASSERT_EQ(SC_OK, - secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr)); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(6); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); + EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); } /** @@ -259,20 +223,22 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve * @tc.type: FUNC * @tc.require: AR000HO9IN */ -HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Level1) +HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize.Level1) { int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); + nlohmann::json jsonRes; + ServiceTestCommon::BuildSaveComponentJson(jsonRes); + std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); + EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); struct SecCompClickEvent touchInfo = { .touchX = 100, .touchY = 100, @@ -280,35 +246,29 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Leve std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); sleep(3); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); @@ -321,20 +281,22 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent002, TestSize.Leve * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent003, TestSize.Level1) { int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); + nlohmann::json jsonRes; + ServiceTestCommon::BuildSaveComponentJson(jsonRes); + std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); // register security component ok - EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, BuildSaveComponentInfo(), scId), - SC_OK); + EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); struct SecCompClickEvent touchInfo = { .touchX = 100, .touchY = 100, @@ -342,23 +304,19 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent003, TestSize.Leve std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT }; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(6); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); touchInfo.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildSaveComponentInfo(), touchInfo, nullptr), - SC_OK); + ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.h similarity index 65% rename from interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h rename to services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.h index 9bde892..f2f7ae7 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.h @@ -13,19 +13,30 @@ * limitations under the License. */ -#ifndef LOCATION_BUTTON_SAMPLE_BUILD_H -#define LOCATION_BUTTON_SAMPLE_BUILD_H -#include "location_button.h" +#ifndef SEC_COMP_SERVICE_TEST_H +#define SEC_COMP_SERVICE_TEST_H + +#include +#define private public +#include "sec_comp_service.h" +#undef private namespace OHOS { namespace Security { namespace SecurityComponent { - -class LocationButtonSampleBuild { +class SecCompServiceMockTest : public testing::Test { public: - static void BuildLocationComponentInfo(nlohmann::json& jsonComponent); + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); + + sptr secCompService_ = nullptr; }; } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // LOCATION_BUTTON_SAMPLE_BUILD_H +#endif // SEC_COMP_SERVICE_TEST_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 5b6fe8e..afd77a2 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "sec_comp_service_test.h" + #include "ipc_skeleton.h" #include "iservice_registry.h" #include "location_button.h" @@ -23,6 +24,7 @@ #include "sec_comp_err.h" #include "sec_comp_log.h" #include "sec_comp_tool.h" +#include "service_test_common.h" #include "system_ability.h" #include "token_setproc.h" @@ -34,49 +36,7 @@ using namespace OHOS::Security::AccessToken; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompServiceTest"}; - -static constexpr uint32_t HAP_TOKEN_ID = 537715419; -static constexpr int32_t SA_ID = 3506; -static constexpr int32_t TEST_SC_ID = 1; static AccessTokenID g_selfTokenId = 0; -static constexpr double TEST_COORDINATE = 100.0; -static constexpr double TEST_SIZE = 100.0; -static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; -static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; -static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; - -static std::string BuildLocationComponentInfo() -{ - LocationButton button; - button.fontSize_ = TEST_SIZE; - button.iconSize_ = TEST_SIZE; - button.padding_.top = TEST_SIZE; - button.padding_.right = TEST_SIZE; - button.padding_.bottom = TEST_SIZE; - button.padding_.left = TEST_SIZE; - button.textIconSpace_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_YELLOW; - button.iconColor_.value = TEST_COLOR_RED; - button.bgColor_.value = TEST_COLOR_BLUE; - button.borderWidth_ = TEST_SIZE; - button.type_ = LOCATION_COMPONENT; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.windowRect_.x_ = TEST_COORDINATE; - button.windowRect_.y_ = TEST_COORDINATE; - button.windowRect_.width_ = TEST_COORDINATE; - button.windowRect_.height_ = TEST_COORDINATE; - button.text_ = static_cast(LocationDesc::SELECT_LOCATION); - button.icon_ = static_cast(LocationIcon::LINE_ICON); - button.bg_ = SecCompBackground::CIRCLE; - - nlohmann::json jsonRes; - button.ToJson(jsonRes); - return jsonRes.dump(); -} } void SecCompServiceTest::SetUpTestCase() @@ -95,7 +55,7 @@ void SecCompServiceTest::SetUp() secCompService_ = sptr(ptr); ASSERT_NE(nullptr, secCompService_); secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); - ASSERT_TRUE(secCompService_->appStateObserver_ != nullptr); + ASSERT_NE(nullptr, secCompService_->appStateObserver_); g_selfTokenId = GetSelfTokenID(); } @@ -119,16 +79,16 @@ HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1) secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->appStateObserver_ = nullptr; secCompService_->OnStart(); - ASSERT_EQ(secCompService_->appStateObserver_, nullptr); + ASSERT_EQ(nullptr, secCompService_->appStateObserver_); EXPECT_CALL(*secCompService_, Publish(testing::_)).WillOnce(testing::Return(false)); secCompService_->state_ = ServiceRunningState::STATE_NOT_START; secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); secCompService_->OnStart(); - ASSERT_EQ(secCompService_->state_, ServiceRunningState::STATE_RUNNING); + ASSERT_EQ(ServiceRunningState::STATE_RUNNING, secCompService_->state_); secCompService_->OnStop(); - ASSERT_EQ(secCompService_->appStateObserver_, nullptr); + ASSERT_EQ(nullptr, secCompService_->appStateObserver_); } /** @@ -142,7 +102,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) // GetSystemAbilityManager get failed secCompService_->appStateObserver_ = nullptr; std::shared_ptr saClient = std::make_shared(); - ASSERT_NE(saClient, nullptr); + ASSERT_NE(nullptr, saClient); SystemAbilityManagerClient::clientInstance = saClient.get(); EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr)); EXPECT_FALSE(secCompService_->RegisterAppStateObserver()); @@ -173,7 +133,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, GetForegroundApplications(testing::_)).WillOnce(testing::Return(-1)); EXPECT_TRUE(secCompService_->RegisterAppStateObserver()); - EXPECT_EQ(secCompService_->appStateObserver_->foregrandProcList_.size(), static_cast(0)); + EXPECT_EQ(static_cast(0), secCompService_->appStateObserver_->foregrandProcList_.size()); // get one foreground app secCompService_->appStateObserver_ = nullptr; @@ -189,7 +149,7 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) return 0; }); EXPECT_TRUE(secCompService_->RegisterAppStateObserver()); - EXPECT_EQ(secCompService_->appStateObserver_->foregrandProcList_.size(), static_cast(1)); + EXPECT_EQ(static_cast(1), secCompService_->appStateObserver_->foregrandProcList_.size()); secCompService_->UnregisterAppStateObserver(); SystemAbilityManagerClient::clientInstance = nullptr; } @@ -227,7 +187,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) // set token id to hap token, but uid is not in foreground EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); setuid(0); - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); // add local uid to foreground. AppExecFwk::AppStateData stateData = { .uid = getuid() @@ -245,7 +205,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) { // get caller fail - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(TEST_SC_ID), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, secCompService_->UnregisterSecurityComponent(TEST_SC_ID_1)); } /** @@ -257,14 +217,14 @@ HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) { // get caller fail - EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, ""), SC_SERVICE_ERROR_VALUE_INVALID); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->UpdateSecurityComponent(TEST_SC_ID_1, "")); - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - EXPECT_EQ(secCompService_->UpdateSecurityComponent(TEST_SC_ID, "{a"), SC_SERVICE_ERROR_VALUE_INVALID); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->UpdateSecurityComponent(TEST_SC_ID_1, "{a")); } /** @@ -278,19 +238,23 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve auto uid = getuid(); // get caller fail int32_t scId; - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId), - SC_SERVICE_ERROR_VALUE_INVALID); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, + secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId)); + + nlohmann::json jsonRes; + ServiceTestCommon::BuildLocationComponentJson(jsonRes); + std::string locationInfo = jsonRes.dump(); // parse component json fail - ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); setuid(100); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - EXPECT_EQ(secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, BuildLocationComponentInfo(), scId), - SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST); + EXPECT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, + secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); uint8_t data[16] = { 0 }; struct SecCompClickEvent touch = { .touchX = 100, @@ -300,8 +264,8 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve .extraInfo.data = data, .extraInfo.dataSize = 16, }; - EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, BuildLocationComponentInfo(), touch, nullptr), - SC_ENHANCE_ERROR_IN_MALICIOUS_LIST); - EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, + secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); + EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, secCompService_->UnregisterSecurityComponent(scId)); setuid(uid); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp index d29720d..3693fb9 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.cpp @@ -14,9 +14,11 @@ */ #include "sec_comp_stub_test.h" + #include "sec_comp_log.h" #include "sec_comp_err.h" #include "sec_comp_click_event_parcel.h" +#include "service_test_common.h" using namespace testing::ext; using namespace OHOS; @@ -62,17 +64,15 @@ HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1) MessageOption option; data.WriteInterfaceToken(u"wrong"); - ASSERT_EQ(stub_->OnRemoteRequest( - static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), - data, reply, option), SC_SERVICE_ERROR_IPC_REQUEST_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_IPC_REQUEST_FAIL, stub_->OnRemoteRequest(static_cast( + SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option)); data.WriteInterfaceToken(u"ohos.security.ISecCompService"); - ASSERT_EQ(stub_->OnRemoteRequest( - static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), - data, reply, option), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->OnRemoteRequest(static_cast( + SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option)); data.WriteInterfaceToken(u"ohos.security.ISecCompService"); - ASSERT_EQ(stub_->OnRemoteRequest(1000, data, reply, option), 305); + ASSERT_EQ(305, stub_->OnRemoteRequest(1000, data, reply, option)); } /** @@ -86,20 +86,20 @@ HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level1) MessageParcel data; MessageParcel reply; - ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->RegisterSecurityComponentInner(data, reply)); data.WriteUint32(UNKNOWN_SC_TYPE); - ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->RegisterSecurityComponentInner(data, reply)); data.WriteUint32(MAX_SC_TYPE); - ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->RegisterSecurityComponentInner(data, reply)); data.WriteUint32(LOCATION_COMPONENT); - ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->RegisterSecurityComponentInner(data, reply)); data.WriteUint32(LOCATION_COMPONENT); data.WriteString(""); - ASSERT_EQ(stub_->RegisterSecurityComponentInner(data, reply), SC_OK); + ASSERT_EQ(SC_OK, stub_->RegisterSecurityComponentInner(data, reply)); } /** @@ -113,17 +113,17 @@ HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level1) MessageParcel data; MessageParcel reply; - ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UpdateSecurityComponentInner(data, reply)); data.WriteInt32(-1); - ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->UpdateSecurityComponentInner(data, reply)); data.WriteInt32(1); - ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UpdateSecurityComponentInner(data, reply)); data.WriteInt32(1); data.WriteString(""); - ASSERT_EQ(stub_->UpdateSecurityComponentInner(data, reply), SC_OK); + ASSERT_EQ(SC_OK, stub_->UpdateSecurityComponentInner(data, reply)); } /** @@ -136,13 +136,13 @@ HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level1) { MessageParcel data; MessageParcel reply; - ASSERT_EQ(stub_->UnregisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UnregisterSecurityComponentInner(data, reply)); data.WriteInt32(-1); - ASSERT_EQ(stub_->UnregisterSecurityComponentInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->UnregisterSecurityComponentInner(data, reply)); data.WriteInt32(1); - ASSERT_EQ(stub_->UnregisterSecurityComponentInner(data, reply), SC_OK); + ASSERT_EQ(SC_OK, stub_->UnregisterSecurityComponentInner(data, reply)); } /** @@ -155,17 +155,17 @@ HWTEST_F(SecCompStubTest, ReportSecurityComponentClickEventInner001, TestSize.Le { MessageParcel data; MessageParcel reply; - ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->ReportSecurityComponentClickEventInner(data, reply)); data.WriteInt32(-1); - ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_VALUE_INVALID); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->ReportSecurityComponentClickEventInner(data, reply)); data.WriteInt32(1); - ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->ReportSecurityComponentClickEventInner(data, reply)); data.WriteInt32(1); data.WriteString(""); - ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL); + ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->ReportSecurityComponentClickEventInner(data, reply)); data.WriteInt32(1); data.WriteString(""); @@ -173,5 +173,5 @@ HWTEST_F(SecCompStubTest, ReportSecurityComponentClickEventInner001, TestSize.Le sptr parcel = new (std::nothrow) SecCompClickEventParcel(); parcel->touchInfoParams_ = touchInfo; data.WriteParcelable(parcel); - ASSERT_EQ(stub_->ReportSecurityComponentClickEventInner(data, reply), SC_OK); + ASSERT_EQ(SC_OK, stub_->ReportSecurityComponentClickEventInner(data, reply)); } diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp new file mode 100644 index 0000000..1b159a1 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 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. + */ +#include "service_test_common.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +void ServiceTestCommon::BuildLocationComponentJson(nlohmann::json& jsonComponent) +{ + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + { JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + { JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + { JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + { JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; +} + +void ServiceTestCommon::BuildSaveComponentJson(nlohmann::json& jsonComponent) +{ + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, + { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; +} + +void ServiceTestCommon::BuildPasteComponentJson(nlohmann::json& jsonComponent) +{ + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; + jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; + jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + }; + nlohmann::json jsonPadding = nlohmann::json { + { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + }; + + jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, + }; + + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + }; + + jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + }; + jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { + { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, + }; + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, + { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, + }; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.h b/services/security_component_service/sa/test/unittest/src/service_test_common.h new file mode 100644 index 0000000..26368f4 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 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 SERVICE_TEST_COMMON_H +#define SERVICE_TEST_COMMON_H +#include "location_button.h" +#include "paste_button.h" +#include "save_button.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +constexpr int32_t TEST_UID_1 = 1; +constexpr int32_t TEST_UID_2 = 2; +constexpr int32_t TEST_PID_1 = 1; +constexpr int32_t TEST_PID_2 = 2; +constexpr int32_t TEST_PID_3 = 3; +constexpr int32_t TEST_SC_ID_1 = 1; +constexpr int32_t TEST_SC_ID_2 = 2; +constexpr int32_t TEST_INVALID_SC_ID = -1; +constexpr int32_t SC_ID_START = 1000; +constexpr int32_t SA_ID = 3506; + +constexpr float TEST_SIZE = 100.0; +constexpr double TEST_COORDINATE = 100.0; +constexpr double TEST_DIFF_COORDINATE = 200.0; +constexpr double TEST_DIMENSION = 100.0; +constexpr double TEST_INVALID_DIMENSION = -100.0; +constexpr double ZERO_OFFSET = 0.0F; +constexpr uint32_t TEST_INVALID_SIZE = 0; +constexpr uint32_t QUARTER = 4; + +constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; +constexpr uint32_t TEST_COLOR_BLACK = 0x00000000; +constexpr uint32_t TEST_COLOR_WHITE = 0xffffffff; +constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; +constexpr uint32_t TEST_COLOR_RED = 0xffff0000; +constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; + +constexpr uint64_t TIME_CONVERSION_UNIT = 1000; +constexpr uint32_t MAX_INT_NUM = 0x7fffffff; +constexpr uint32_t HAP_TOKEN_ID = 537715419; +constexpr uint32_t TEST_TOKEN_ID = 1; +constexpr uint32_t MAX_HMAC_SIZE = 64; + +class ServiceTestCommon { +public: + static void BuildLocationComponentJson(nlohmann::json& jsonComponent); + static void BuildSaveComponentJson(nlohmann::json& jsonComponent); + static void BuildPasteComponentJson(nlohmann::json& jsonComponent); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SERVICE_TEST_COMMON_H -- Gitee From ad7ff3a7101e77a06ca0f676e507119e395e1429 Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 22 Aug 2023 12:50:14 +0800 Subject: [PATCH 55/59] Description: CheckRect Match-id-2ca6728a956f58df3a36f8d4efad481a4b577094 --- .../unittest/src/sec_comp_enhance_test.cpp | 2 +- .../unittest/src/location_button_test.cpp | 133 +++++----- .../test/unittest/src/paste_button_test.cpp | 28 +-- .../test/unittest/src/save_button_test.cpp | 28 +-- .../test/unittest/src/sec_comp_kit_test.cpp | 36 +-- .../src/sec_comp_register_callback_test.cpp | 38 +-- .../test/unittest/src/test_common.cpp | 114 ++++----- .../test/unittest/src/test_common.h | 26 +- .../sa/sa_main/sec_comp_entity.cpp | 9 +- .../sa/sa_main/sec_comp_stub.cpp | 22 ++ .../sa/sa_main/sec_comp_stub.h | 2 + .../sa/test/mock/include/accesstoken_kit.h | 5 + .../unittest/src/app_state_observer_test.cpp | 58 ++--- .../unittest/src/sec_comp_entity_test.cpp | 20 +- .../src/sec_comp_info_helper_test.cpp | 184 +++++++------- .../unittest/src/sec_comp_manager_test.cpp | 232 +++++++++--------- .../src/sec_comp_service_mock_test.cpp | 72 +++--- .../unittest/src/sec_comp_service_test.cpp | 25 +- .../test/unittest/src/service_test_common.cpp | 114 ++++----- .../test/unittest/src/service_test_common.h | 61 +++-- 20 files changed, 625 insertions(+), 584 deletions(-) diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index cb3c2ab..0dad6f0 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -45,7 +45,7 @@ void SecCompEnhanceTest::SetUpTestCase() if (access(ENHANCE_SRV_INTERFACE_LIB.c_str(), F_OK) == 0) { g_srvEnhanceExist = true; } - + system("kill -9 `pidof security_compon`"); SC_LOG_INFO(LABEL, "SetUpTestCase."); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 972d4c2..f4be631 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -26,6 +26,7 @@ using namespace OHOS::Security::SecurityComponent; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButtonTest"}; +static const std::string WRONG_TYPE = "wrongType"; } void LocationButtonTest::SetUpTestCase() @@ -109,32 +110,32 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { {JsonTagConstants::JSON_RECT_X, WRONG_TYPE}, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE}, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE}, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE} + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE}, {JsonTagConstants::JSON_RECT_Y, WRONG_TYPE}, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE}, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE} + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE}, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE}, {JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE}, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE} + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE}, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE}, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE}, {JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE} }; ASSERT_FALSE(comp.FromJson(jsonComponent)); @@ -154,31 +155,31 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { { JsonTagConstants::JSON_FONT_SIZE_TAG, WRONG_TYPE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_ICON_SIZE_TAG, WRONG_TYPE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; @@ -200,28 +201,28 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) nlohmann::json jsonPadding = nlohmann::json { { JsonTagConstants::JSON_PADDING_TOP_TAG, WRONG_TYPE }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, { JsonTagConstants::JSON_PADDING_RIGHT_TAG, WRONG_TYPE }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); @@ -241,29 +242,29 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) ASSERT_TRUE(comp.FromJson(jsonComponent)); nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, WRONG_TYPE }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, { JsonTagConstants::JSON_PADDING_LEFT_TAG, WRONG_TYPE }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; ASSERT_FALSE(comp.FromJson(jsonComponent)); @@ -344,21 +345,21 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { { JsonTagConstants::JSON_FONT_COLOR_TAG, WRONG_TYPE }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } }; ASSERT_FALSE(button.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, + { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, { JsonTagConstants::JSON_ICON_COLOR_TAG, WRONG_TYPE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } }; ASSERT_FALSE(button.FromJson(jsonComponent)); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, { JsonTagConstants::JSON_BG_COLOR_TAG, WRONG_TYPE } }; ASSERT_FALSE(button.FromJson(jsonComponent)); @@ -505,47 +506,47 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) comp1.fontSize_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.fontSize_ = TEST_SIZE; + comp1.fontSize_ = TestCommon::TEST_SIZE; comp1.iconSize_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.iconSize_ = TEST_SIZE; + comp1.iconSize_ = TestCommon::TEST_SIZE; comp1.padding_.top = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.top = TEST_DIMENSION; + comp1.padding_.top = TestCommon::TEST_DIMENSION; comp1.padding_.right = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.right = TEST_DIMENSION; + comp1.padding_.right = TestCommon::TEST_DIMENSION; comp1.padding_.bottom = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.bottom = TEST_DIMENSION; + comp1.padding_.bottom = TestCommon::TEST_DIMENSION; comp1.padding_.left = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.left = TEST_DIMENSION; + comp1.padding_.left = TestCommon::TEST_DIMENSION; comp1.textIconSpace_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.textIconSpace_ = TEST_SIZE; + comp1.textIconSpace_ = TestCommon::TEST_SIZE; comp1.borderWidth_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.borderWidth_ = TEST_SIZE; + comp1.borderWidth_ = TestCommon::TEST_SIZE; - comp1.fontColor_.value = TEST_DIFF_COLOR; + comp1.fontColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.fontColor_.value = TEST_COLOR_RED; + comp1.fontColor_.value = TestCommon::TEST_COLOR_RED; - comp1.bgColor_.value = TEST_DIFF_COLOR; + comp1.bgColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.bgColor_.value = TEST_COLOR_YELLOW; + comp1.bgColor_.value = TestCommon::TEST_COLOR_YELLOW; - comp1.iconColor_.value = TEST_DIFF_COLOR; + comp1.iconColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.iconColor_.value = TEST_COLOR_BLUE; + comp1.iconColor_.value = TestCommon::TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index 4faa121..e724f35 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -152,47 +152,47 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) comp1.fontSize_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.fontSize_ = TEST_SIZE; + comp1.fontSize_ = TestCommon::TEST_SIZE; comp1.iconSize_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.iconSize_ = TEST_SIZE; + comp1.iconSize_ = TestCommon::TEST_SIZE; comp1.padding_.top = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.top = TEST_DIMENSION; + comp1.padding_.top = TestCommon::TEST_DIMENSION; comp1.padding_.right = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.right = TEST_DIMENSION; + comp1.padding_.right = TestCommon::TEST_DIMENSION; comp1.padding_.bottom = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.bottom = TEST_DIMENSION; + comp1.padding_.bottom = TestCommon::TEST_DIMENSION; comp1.padding_.left = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.left = TEST_DIMENSION; + comp1.padding_.left = TestCommon::TEST_DIMENSION; comp1.textIconSpace_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.textIconSpace_ = TEST_SIZE; + comp1.textIconSpace_ = TestCommon::TEST_SIZE; comp1.borderWidth_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.borderWidth_ = TEST_SIZE; + comp1.borderWidth_ = TestCommon::TEST_SIZE; - comp1.fontColor_.value = TEST_DIFF_COLOR; + comp1.fontColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.fontColor_.value = TEST_COLOR_RED; + comp1.fontColor_.value = TestCommon::TEST_COLOR_RED; - comp1.bgColor_.value = TEST_DIFF_COLOR; + comp1.bgColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.bgColor_.value = TEST_COLOR_YELLOW; + comp1.bgColor_.value = TestCommon::TEST_COLOR_YELLOW; - comp1.iconColor_.value = TEST_DIFF_COLOR; + comp1.iconColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.iconColor_.value = TEST_COLOR_BLUE; + comp1.iconColor_.value = TestCommon::TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index 257b5cb..0c48e62 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -152,47 +152,47 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) comp1.fontSize_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.fontSize_ = TEST_SIZE; + comp1.fontSize_ = TestCommon::TEST_SIZE; comp1.iconSize_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.iconSize_ = TEST_SIZE; + comp1.iconSize_ = TestCommon::TEST_SIZE; comp1.padding_.top = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.top = TEST_DIMENSION; + comp1.padding_.top = TestCommon::TEST_DIMENSION; comp1.padding_.right = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.right = TEST_DIMENSION; + comp1.padding_.right = TestCommon::TEST_DIMENSION; comp1.padding_.bottom = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.bottom = TEST_DIMENSION; + comp1.padding_.bottom = TestCommon::TEST_DIMENSION; comp1.padding_.left = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.padding_.left = TEST_DIMENSION; + comp1.padding_.left = TestCommon::TEST_DIMENSION; comp1.textIconSpace_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.textIconSpace_ = TEST_SIZE; + comp1.textIconSpace_ = TestCommon::TEST_SIZE; comp1.borderWidth_ = DEFAULT_DIMENSION; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.borderWidth_ = TEST_SIZE; + comp1.borderWidth_ = TestCommon::TEST_SIZE; - comp1.fontColor_.value = TEST_DIFF_COLOR; + comp1.fontColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.fontColor_.value = TEST_COLOR_RED; + comp1.fontColor_.value = TestCommon::TEST_COLOR_RED; - comp1.bgColor_.value = TEST_DIFF_COLOR; + comp1.bgColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.bgColor_.value = TEST_COLOR_YELLOW; + comp1.bgColor_.value = TestCommon::TEST_COLOR_YELLOW; - comp1.iconColor_.value = TEST_DIFF_COLOR; + comp1.iconColor_.value = TestCommon::TEST_DIFF_COLOR; ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true)); - comp1.iconColor_.value = TEST_COLOR_BLUE; + comp1.iconColor_.value = TestCommon::TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 8128924..58dc275 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -89,23 +89,23 @@ void SecCompKitTest::TearDown() HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) { LocationButton comp; - comp.fontSize_ = TEST_SIZE; - comp.iconSize_ = TEST_SIZE; - comp.padding_.top = TEST_DIMENSION; - comp.padding_.right = TEST_DIMENSION; - comp.padding_.bottom = TEST_DIMENSION; - comp.padding_.left = TEST_DIMENSION; - comp.textIconSpace_ = TEST_SIZE; - comp.bgColor_.value = TEST_COLOR; - comp.fontColor_.value = TEST_COLOR; - comp.iconColor_.value = TEST_COLOR; - comp.borderWidth_ = TEST_SIZE; + comp.fontSize_ = TestCommon::TEST_SIZE; + comp.iconSize_ = TestCommon::TEST_SIZE; + comp.padding_.top = TestCommon::TEST_DIMENSION; + comp.padding_.right = TestCommon::TEST_DIMENSION; + comp.padding_.bottom = TestCommon::TEST_DIMENSION; + comp.padding_.left = TestCommon::TEST_DIMENSION; + comp.textIconSpace_ = TestCommon::TEST_SIZE; + comp.bgColor_.value = TestCommon::TEST_COLOR; + comp.fontColor_.value = TestCommon::TEST_COLOR; + comp.iconColor_.value = TestCommon::TEST_COLOR; + comp.borderWidth_ = TestCommon::TEST_SIZE; comp.parentEffect_ = true; comp.type_ = LOCATION_COMPONENT; - comp.rect_.x_ = TEST_COORDINATE; - comp.rect_.y_ = TEST_COORDINATE; - comp.rect_.width_ = TEST_COORDINATE; - comp.rect_.height_ = TEST_COORDINATE; + comp.rect_.x_ = TestCommon::TEST_COORDINATE; + comp.rect_.y_ = TestCommon::TEST_COORDINATE; + comp.rect_.width_ = TestCommon::TEST_COORDINATE; + comp.rect_.height_ = TestCommon::TEST_COORDINATE; nlohmann::json jsonRes; comp.ToJson(jsonRes); @@ -116,8 +116,8 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonStr)); struct SecCompClickEvent touch = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonStr, touch, nullptr)); @@ -163,7 +163,7 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); SecCompCallerAuthorization::GetInstance().isInit_ = false; - for (size_t i = 0; i < MAX_CALLER_SIZE + 1; i++) { + for (size_t i = 0; i < TestCommon::MAX_CALLER_SIZE + 1; i++) { callerList.emplace_back(reinterpret_cast(TestInCallerNotCheckList)); } SecCompUiRegister registerCallback2(callerList, nullptr); diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index 07d339a..b38dd25 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -182,14 +182,14 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Lev int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); - uint8_t data[MAX_HMAC_SIZE] = { 0 }; + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent touchInfo = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, .timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) }; - touchInfo.extraInfo.dataSize = MAX_HMAC_SIZE; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; touchInfo.extraInfo.data = data; EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); @@ -212,14 +212,14 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Lev int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); - uint8_t data[MAX_HMAC_SIZE] = { 0 }; + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent touchInfo = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT }; - touchInfo.extraInfo.dataSize = MAX_HMAC_SIZE; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; touchInfo.extraInfo.data = data; EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); @@ -241,14 +241,14 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); - uint8_t data[MAX_HMAC_SIZE] = { 0 }; + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent touchInfo = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT }; - touchInfo.extraInfo.dataSize = MAX_HMAC_SIZE; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; touchInfo.extraInfo.data = data; ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); @@ -292,14 +292,14 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 int32_t scId; ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_NE(-1, scId); - uint8_t data[MAX_HMAC_SIZE] = { 0 }; + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent touch = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT }; - touch.extraInfo.dataSize = MAX_HMAC_SIZE; + touch.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; touch.extraInfo.data = data; EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp index 067bfe9..ab8f3ad 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp @@ -21,40 +21,40 @@ void TestCommon::BuildLocationComponentInfo(nlohmann::json& jsonComponent) { jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } }; jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } }; jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, }; jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, @@ -71,39 +71,39 @@ void TestCommon::BuildSaveComponentInfo(nlohmann::json& jsonComponent) jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } }; jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } }; jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, }; jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, @@ -120,39 +120,39 @@ void TestCommon::BuildPasteComponentInfo(nlohmann::json& jsonComponent) jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } }; jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } }; jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, }; jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h index 34f3a18..f3b97b9 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.h +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.h @@ -21,22 +21,20 @@ namespace OHOS { namespace Security { namespace SecurityComponent { -const std::string WRONG_TYPE = "wrongType"; -constexpr float TEST_SIZE = 100.0; -constexpr double TEST_COORDINATE = 100.0; -constexpr double TEST_DIMENSION = 100.0; -constexpr uint32_t TEST_COLOR = 0xffffffff; -constexpr uint32_t TEST_COLOR_YELLOW = 0xffffff00; -constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; -constexpr uint32_t TEST_DIFF_COLOR = 0; -constexpr uint64_t TIME_CONVERSION_UNIT = 1000; -constexpr uint32_t HAP_TOKEN_ID = 537715419; -constexpr uint32_t MAX_HMAC_SIZE = 64; -constexpr size_t MAX_CALLER_SIZE = 10; - class TestCommon { public: + static constexpr float TEST_SIZE = 100.0; + static constexpr double TEST_COORDINATE = 100.0; + static constexpr double TEST_DIMENSION = 100.0; + static constexpr uint32_t TEST_COLOR = 0xffffffff; + static constexpr uint32_t TEST_COLOR_YELLOW = 0xffffff00; + static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; + static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; + static constexpr uint32_t TEST_DIFF_COLOR = 0; + static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; + static constexpr uint32_t MAX_HMAC_SIZE = 64; + static constexpr size_t MAX_CALLER_SIZE = 10; + static void BuildLocationComponentInfo(nlohmann::json& jsonComponent); static void BuildSaveComponentInfo(nlohmann::json& jsonComponent); static void BuildPasteComponentInfo(nlohmann::json& jsonComponent); diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index 91ec015..e1bc9e4 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp @@ -57,8 +57,8 @@ bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const auto current = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; if (touchInfo.timestamp < current - MAX_TOUCH_INTERVAL || touchInfo.timestamp > current) { - SC_LOG_ERROR(LABEL, "touch timestamp invalid touchInfo. timestamp: %{public}lu, current: %{public}lu", - touchInfo.timestamp, current); + SC_LOG_ERROR(LABEL, "touch timestamp invalid touchInfo. timestamp: %{public}llu, current: %{public}llu", + static_cast(touchInfo.timestamp), static_cast(current)); return false; } @@ -70,8 +70,9 @@ bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const int32_t res = SecCompEnhanceAdapter::CheckExtraInfo(touchInfo); if ((res != SC_OK) && (res != SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE)) { SC_LOG_ERROR(LABEL, "HMAC checkout failed," - "touchX:%{public}f, touchY:%{public}f, timestamp:%{public}lu, dataSize:%{public}d", - touchInfo.touchX, touchInfo.touchY, touchInfo.timestamp, touchInfo.extraInfo.dataSize); + "touchX:%{public}f, touchY:%{public}f, timestamp:%{public}llu, dataSize:%{public}d", + touchInfo.touchX, touchInfo.touchY, static_cast(touchInfo.timestamp), + touchInfo.extraInfo.dataSize); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED", HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(), "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId_, "SC_TYPE", componentInfo_->type_); diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index c492afd..b91d428 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -14,6 +14,7 @@ */ #include "sec_comp_stub.h" +#include "accesstoken_kit.h" #include "ipc_skeleton.h" #include "sec_comp_click_event_parcel.h" #include "sec_comp_err.h" @@ -24,6 +25,8 @@ namespace Security { namespace SecurityComponent { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompStub"}; +static constexpr int32_t ROOT_UID = 0; +static constexpr int32_t BASE_USER_RANGE = 200000; } // namespace int32_t SecCompStub::OnRemoteRequest( @@ -165,6 +168,10 @@ int32_t SecCompStub::ReportSecurityComponentClickEventInner(MessageParcel& data, int32_t SecCompStub::ReduceAfterVerifySavePermissionInner(MessageParcel& data, MessageParcel& reply) { + if (!IsMediaLibraryCalling()) { + SC_LOG_ERROR(LABEL, "Not medialibrary called"); + return SC_SERVICE_ERROR_CALLER_INVALID; + } uint32_t tokenId; if (!data.ReadUint32(tokenId)) { SC_LOG_ERROR(LABEL, "Read component id fail"); @@ -194,6 +201,21 @@ int32_t SecCompStub::GetEnhanceRemoteObjectInner(MessageParcel& data, MessagePar return SC_OK; } +bool SecCompStub::IsMediaLibraryCalling() +{ + int32_t uid = IPCSkeleton::GetCallingUid(); + if (uid == ROOT_UID) { + return true; + } + int32_t userId = uid / BASE_USER_RANGE; + uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID(); + if (mediaLibraryTokenId_ == 0) { + mediaLibraryTokenId_ = AccessToken::AccessTokenKit::GetHapTokenID( + userId, "com.ohos.medialibrary.medialibrarydata", 0); + } + return tokenCaller == mediaLibraryTokenId_; +} + SecCompStub::SecCompStub() { requestFuncMap_[static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT)] = diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.h b/services/security_component_service/sa/sa_main/sec_comp_stub.h index c435876..8c5adda 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.h +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.h @@ -37,9 +37,11 @@ private: int32_t ReportSecurityComponentClickEventInner(MessageParcel& data, MessageParcel& reply); int32_t ReduceAfterVerifySavePermissionInner(MessageParcel& data, MessageParcel& reply); int32_t GetEnhanceRemoteObjectInner(MessageParcel& data, MessageParcel& reply); + bool IsMediaLibraryCalling(); using RequestFuncType = int32_t (SecCompStub::*)(MessageParcel& data, MessageParcel& reply); std::map requestFuncMap_; + AccessToken::AccessTokenID mediaLibraryTokenId_ = 0; }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/test/mock/include/accesstoken_kit.h b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h index f4a2af7..0a746c2 100644 --- a/services/security_component_service/sa/test/mock/include/accesstoken_kit.h +++ b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h @@ -41,6 +41,11 @@ public: return 0; }; + static AccessTokenID GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex) + { + return 0; + }; + static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID) { AccessTokenIDInner *idInner = reinterpret_cast(&tokenID); diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp index 946262a..b132c4f 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -57,13 +57,13 @@ void AppStateObserverTest::TearDown() */ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) { - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_FALSE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); AppExecFwk::AppStateData stateData = { - .pid = TEST_PID_1, - .uid = TEST_UID_1, + .pid = ServiceTestCommon::TEST_PID_1, + .uid = ServiceTestCommon::TEST_UID_1, }; observer_->AddProcessToForegroundSet(stateData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } /** @@ -75,20 +75,20 @@ HWTEST_F(AppStateObserverTest, IsProcessForeground001, TestSize.Level1) HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) { AppExecFwk::AppStateData stateData = { - .pid = TEST_PID_1, - .uid = TEST_UID_1, + .pid = ServiceTestCommon::TEST_PID_1, + .uid = ServiceTestCommon::TEST_UID_1, }; observer_->AddProcessToForegroundSet(stateData); observer_->AddProcessToForegroundSet(stateData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); AppExecFwk::ProcessData procData = { - .pid = TEST_PID_2, - .uid = TEST_UID_2, + .pid = ServiceTestCommon::TEST_PID_2, + .uid = ServiceTestCommon::TEST_UID_2, }; observer_->AddProcessToForegroundSet(procData); observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_2, TEST_UID_2)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_2, ServiceTestCommon::TEST_UID_2)); } /** @@ -100,14 +100,14 @@ HWTEST_F(AppStateObserverTest, AddProcessToForegroundSet001, TestSize.Level1) HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level1) { AppExecFwk::ProcessData procData = { - .pid = TEST_PID_1, - .uid = TEST_UID_1, + .pid = ServiceTestCommon::TEST_PID_1, + .uid = ServiceTestCommon::TEST_UID_1, }; observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); observer_->RemoveProcessFromForegroundSet(procData); observer_->RemoveProcessFromForegroundSet(procData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_FALSE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } /** @@ -119,14 +119,14 @@ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet002, TestSize.Level1) { AppExecFwk::ProcessData procData = { - .pid = TEST_PID_1, - .uid = TEST_UID_1, + .pid = ServiceTestCommon::TEST_PID_1, + .uid = ServiceTestCommon::TEST_UID_1, }; observer_->AddProcessToForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); - procData.pid = TEST_PID_2; + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); + procData.pid = ServiceTestCommon::TEST_PID_2; observer_->RemoveProcessFromForegroundSet(procData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } /** @@ -143,14 +143,14 @@ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) ASSERT_EQ(observer_->foregrandProcList_.size(), static_cast(0)); processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; - processData.pid = TEST_PID_1; + processData.pid = ServiceTestCommon::TEST_PID_1; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); processData.state = AppExecFwk::AppProcessState::APP_STATE_BACKGROUND; - processData.pid = TEST_PID_1; + processData.pid = ServiceTestCommon::TEST_PID_1; observer_->OnProcessStateChanged(processData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_FALSE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } /** @@ -163,17 +163,17 @@ HWTEST_F(AppStateObserverTest, OnProcessDied001, TestSize.Level1) { AppExecFwk::ProcessData processData; processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; - processData.pid = TEST_PID_1; + processData.pid = ServiceTestCommon::TEST_PID_1; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); // if last process died, the sec_comp service will exit. - processData.pid = TEST_PID_2; + processData.pid = ServiceTestCommon::TEST_PID_2; observer_->OnProcessStateChanged(processData); - ASSERT_TRUE(observer_->IsProcessForeground(TEST_PID_2, TEST_UID_2)); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_2, ServiceTestCommon::TEST_UID_2)); // notify process 1 died - processData.pid = TEST_PID_1; + processData.pid = ServiceTestCommon::TEST_PID_1; observer_->OnProcessDied(processData); - ASSERT_FALSE(observer_->IsProcessForeground(TEST_PID_1, TEST_UID_1)); + ASSERT_FALSE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 2304db6..8a9b861 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -130,8 +130,8 @@ HWTEST_F(SecCompEntityTest, GrantTempPermission002, TestSize.Level1) HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) { SecCompClickEvent touch = { - .touchX = TEST_COORDINATE, - .touchY = TEST_COORDINATE, + .touchX = ServiceTestCommon::TEST_COORDINATE, + .touchY = ServiceTestCommon::TEST_COORDINATE, .timestamp = 0, }; ASSERT_FALSE(entity_->CheckTouchInfo(touch)); @@ -140,17 +140,17 @@ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) touch.timestamp = current + 10000L; // 10s ASSERT_FALSE(entity_->CheckTouchInfo(touch)); - entity_->componentInfo_->rect_.x_ = TEST_DIFF_COORDINATE; // click event will not hit this rect - entity_->componentInfo_->rect_.y_ = TEST_DIFF_COORDINATE; - entity_->componentInfo_->rect_.width_ = TEST_DIFF_COORDINATE; - entity_->componentInfo_->rect_.height_ = TEST_DIFF_COORDINATE; + entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_DIFF_COORDINATE; // click event will not hit this rect + entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_DIFF_COORDINATE; + entity_->componentInfo_->rect_.width_ = ServiceTestCommon::TEST_DIFF_COORDINATE; + entity_->componentInfo_->rect_.height_ = ServiceTestCommon::TEST_DIFF_COORDINATE; touch.timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; ASSERT_FALSE(entity_->CheckTouchInfo(touch)); - entity_->componentInfo_->rect_.x_ = TEST_COORDINATE; - entity_->componentInfo_->rect_.y_ = TEST_COORDINATE; + entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; touch.timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; ASSERT_FALSE(entity_->CheckTouchInfo(touch)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 8c6c251..ddcef69 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -59,8 +59,8 @@ static bool GetScreenSize() void SecCompInfoHelperTest::SetUpTestCase() { ASSERT_TRUE(GetScreenSize()); - g_testWidth = (ZERO_OFFSET + g_curScreenWidth) / QUARTER; - g_testHeight = (ZERO_OFFSET + g_curScreenHeight) / QUARTER; + g_testWidth = (ServiceTestCommon::ZERO_OFFSET + g_curScreenWidth) / ServiceTestCommon::QUARTER; + g_testHeight = (ServiceTestCommon::ZERO_OFFSET + g_curScreenHeight) / ServiceTestCommon::QUARTER; } void SecCompInfoHelperTest::TearDownTestCase() @@ -146,9 +146,9 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) }; ASSERT_TRUE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); - rect.x_ = TEST_INVALID_DIMENSION; + rect.x_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); - rect.y_ = TEST_INVALID_DIMENSION; + rect.y_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); rect.x_ = g_curScreenWidth + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); @@ -194,9 +194,9 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); windowRect.height_ = g_testHeight - 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); - windowRect.width_ = TEST_INVALID_DIMENSION; + windowRect.width_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); - windowRect.height_ = TEST_INVALID_DIMENSION; + windowRect.height_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); } @@ -236,48 +236,48 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_INVALID_DIMENSION }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_INVALID_DIMENSION }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -298,32 +298,32 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_INVALID_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_INVALID_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -344,32 +344,32 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_INVALID_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_INVALID_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -390,25 +390,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_INVALID }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_INVALID }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_INVALID }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_INVALID }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_INVALID } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_INVALID } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -455,9 +455,9 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_YELLOW }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -468,9 +468,9 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -481,9 +481,9 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { JsonTagConstants::JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_YELLOW }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); @@ -508,16 +508,16 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) { JsonTagConstants::JSON_BG_TAG, SecCompBackground::NO_BG_TYPE }, }; nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, { JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, { JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -525,15 +525,15 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) jsonPadding = nlohmann::json { { JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, { JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -562,14 +562,14 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) nlohmann::json jsonPadding = nlohmann::json { { JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, { JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, { JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -579,13 +579,13 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) { JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG }, { JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG }, { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -606,9 +606,9 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLACK }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_WHITE } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_WHITE } }; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index be5d04b..5ad5d90 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -33,26 +33,26 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static LocationButton BuildInvalidLocationComponent() { LocationButton button; - button.fontSize_ = TEST_INVALID_SIZE; - button.iconSize_ = TEST_INVALID_SIZE; - button.padding_.top = TEST_INVALID_SIZE; - button.padding_.right = TEST_INVALID_SIZE; - button.padding_.bottom = TEST_INVALID_SIZE; - button.padding_.left = TEST_INVALID_SIZE; - button.textIconSpace_ = TEST_INVALID_SIZE; - button.fontColor_.value = TEST_COLOR_WHITE; - button.iconColor_.value = TEST_COLOR_WHITE; - button.bgColor_.value = TEST_COLOR_WHITE; - button.borderWidth_ = TEST_INVALID_SIZE; + button.fontSize_ = ServiceTestCommon::TEST_INVALID_SIZE; + button.iconSize_ = ServiceTestCommon::TEST_INVALID_SIZE; + button.padding_.top = ServiceTestCommon::TEST_INVALID_SIZE; + button.padding_.right = ServiceTestCommon::TEST_INVALID_SIZE; + button.padding_.bottom = ServiceTestCommon::TEST_INVALID_SIZE; + button.padding_.left = ServiceTestCommon::TEST_INVALID_SIZE; + button.textIconSpace_ = ServiceTestCommon::TEST_INVALID_SIZE; + button.fontColor_.value = ServiceTestCommon::TEST_COLOR_WHITE; + button.iconColor_.value = ServiceTestCommon::TEST_COLOR_WHITE; + button.bgColor_.value = ServiceTestCommon::TEST_COLOR_WHITE; + button.borderWidth_ = ServiceTestCommon::TEST_INVALID_SIZE; button.type_ = LOCATION_COMPONENT; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.windowRect_.x_ = TEST_COORDINATE; - button.windowRect_.y_ = TEST_COORDINATE; - button.windowRect_.width_ = TEST_COORDINATE; - button.windowRect_.height_ = TEST_COORDINATE; + button.rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + button.rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + button.rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + button.rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.x_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.y_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.width_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.height_ = ServiceTestCommon::TEST_COORDINATE; button.text_ = UNKNOWN_TEXT; button.icon_ = UNKNOWN_ICON; button.bg_ = SecCompBackground::UNKNOWN_BG; @@ -62,26 +62,26 @@ static LocationButton BuildInvalidLocationComponent() static LocationButton BuildValidLocationComponent() { LocationButton button; - button.fontSize_ = TEST_SIZE; - button.iconSize_ = TEST_SIZE; - button.padding_.top = TEST_SIZE; - button.padding_.right = TEST_SIZE; - button.padding_.bottom = TEST_SIZE; - button.padding_.left = TEST_SIZE; - button.textIconSpace_ = TEST_SIZE; - button.fontColor_.value = TEST_COLOR_YELLOW; - button.iconColor_.value = TEST_COLOR_RED; - button.bgColor_.value = TEST_COLOR_BLUE; - button.borderWidth_ = TEST_SIZE; + button.fontSize_ = ServiceTestCommon::TEST_SIZE; + button.iconSize_ = ServiceTestCommon::TEST_SIZE; + button.padding_.top = ServiceTestCommon::TEST_SIZE; + button.padding_.right = ServiceTestCommon::TEST_SIZE; + button.padding_.bottom = ServiceTestCommon::TEST_SIZE; + button.padding_.left = ServiceTestCommon::TEST_SIZE; + button.textIconSpace_ = ServiceTestCommon::TEST_SIZE; + button.fontColor_.value = ServiceTestCommon::TEST_COLOR_YELLOW; + button.iconColor_.value = ServiceTestCommon::TEST_COLOR_RED; + button.bgColor_.value = ServiceTestCommon::TEST_COLOR_BLUE; + button.borderWidth_ = ServiceTestCommon::TEST_SIZE; button.type_ = LOCATION_COMPONENT; - button.rect_.x_ = TEST_COORDINATE; - button.rect_.y_ = TEST_COORDINATE; - button.rect_.width_ = TEST_COORDINATE; - button.rect_.height_ = TEST_COORDINATE; - button.windowRect_.x_ = TEST_COORDINATE; - button.windowRect_.y_ = TEST_COORDINATE; - button.windowRect_.width_ = TEST_COORDINATE; - button.windowRect_.height_ = TEST_COORDINATE; + button.rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + button.rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + button.rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + button.rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.x_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.y_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.width_ = ServiceTestCommon::TEST_COORDINATE; + button.windowRect_.height_ = ServiceTestCommon::TEST_COORDINATE; button.text_ = static_cast(LocationDesc::SELECT_LOCATION); button.icon_ = static_cast(LocationIcon::LINE_ICON); button.bg_ = SecCompBackground::CIRCLE; @@ -114,11 +114,11 @@ void SecCompManagerTest::TearDown() */ HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1) { - SecCompManager::GetInstance().scIdStart_ = MAX_INT_NUM; - ASSERT_EQ(SC_ID_START, SecCompManager::GetInstance().CreateScId()); + SecCompManager::GetInstance().scIdStart_ = ServiceTestCommon::MAX_INT_NUM; + ASSERT_EQ(ServiceTestCommon::SC_ID_START, SecCompManager::GetInstance().CreateScId()); - SecCompManager::GetInstance().scIdStart_ = SC_ID_START; - ASSERT_EQ(SC_ID_START + 1, SecCompManager::GetInstance().CreateScId()); + SecCompManager::GetInstance().scIdStart_ = ServiceTestCommon::SC_ID_START; + ASSERT_EQ(ServiceTestCommon::SC_ID_START + 1, SecCompManager::GetInstance().CreateScId()); } /** @@ -131,21 +131,21 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) { std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(nullptr, compPtr); - compPtr->rect_.x_ = TEST_COORDINATE; - compPtr->rect_.y_ = TEST_COORDINATE; - compPtr->rect_.width_ = TEST_COORDINATE; - compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); + compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, entity)); std::shared_ptr compPtrNew = std::make_shared(); ASSERT_NE(nullptr, compPtrNew); - compPtrNew->rect_.x_ = TEST_COORDINATE * 2; // not overlap - compPtrNew->rect_.y_ = TEST_COORDINATE * 2; // not overlap - compPtrNew->rect_.width_ = TEST_COORDINATE; - compPtrNew->rect_.height_ = TEST_COORDINATE; - SecCompEntity entityNew(compPtrNew, TEST_TOKEN_ID, TEST_SC_ID_2); + compPtrNew->rect_.x_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap + compPtrNew->rect_.y_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap + compPtrNew->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + compPtrNew->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + SecCompEntity entityNew(compPtrNew, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_2); ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, entityNew)); } @@ -157,22 +157,23 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) */ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1) { - ASSERT_EQ(SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1), - SC_SERVICE_ERROR_COMPONENT_NOT_EXIST); + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, SecCompManager::GetInstance().DeleteSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(nullptr, compPtr); - compPtr->rect_.x_ = TEST_COORDINATE; - compPtr->rect_.y_ = TEST_COORDINATE; - compPtr->rect_.width_ = TEST_COORDINATE; - compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); - - ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, - SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2)); - - ASSERT_EQ(SC_OK, SecCompManager::GetInstance().DeleteSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); + compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); + + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, SecCompManager::GetInstance().DeleteSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_2)); + + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().DeleteSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); } /** @@ -183,18 +184,21 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1 */ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) { - ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); + ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(nullptr, compPtr); - compPtr->rect_.x_ = TEST_COORDINATE; - compPtr->rect_.y_ = TEST_COORDINATE; - compPtr->rect_.width_ = TEST_COORDINATE; - compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); - ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_2)); - ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); + compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); + ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_2)); + ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); } /** @@ -205,22 +209,23 @@ HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1) */ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) { - SecCompManager::GetInstance().NotifyProcessBackground(TEST_PID_1); + SecCompManager::GetInstance().NotifyProcessBackground(ServiceTestCommon::TEST_PID_1); std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(nullptr, compPtr); - compPtr->rect_.x_ = TEST_COORDINATE; - compPtr->rect_.y_ = TEST_COORDINATE; - compPtr->rect_.width_ = TEST_COORDINATE; - compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); - auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1); + compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); + auto component = SecCompManager::GetInstance().GetSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1); ASSERT_NE(nullptr, component); - SecCompManager::GetInstance().NotifyProcessForeground(TEST_PID_1); + SecCompManager::GetInstance().NotifyProcessForeground(ServiceTestCommon::TEST_PID_1); ASSERT_TRUE(SecCompManager::GetInstance().IsForegroundCompExist()); - SecCompManager::GetInstance().NotifyProcessBackground(TEST_PID_1); + SecCompManager::GetInstance().NotifyProcessBackground(ServiceTestCommon::TEST_PID_1); ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); } @@ -234,27 +239,29 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) { std::shared_ptr compPtr = std::make_shared(); ASSERT_NE(nullptr, compPtr); - compPtr->rect_.x_ = TEST_COORDINATE; - compPtr->rect_.y_ = TEST_COORDINATE; - compPtr->rect_.width_ = TEST_COORDINATE; - compPtr->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity(compPtr, TEST_TOKEN_ID, TEST_SC_ID_1); - ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_1, entity)); + compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); std::shared_ptr compPtr2 = std::make_shared(); ASSERT_NE(nullptr, compPtr2); - compPtr2->rect_.x_ = TEST_COORDINATE * 2; // not overlap - compPtr2->rect_.y_ = TEST_COORDINATE * 2; // not overlap - compPtr2->rect_.width_ = TEST_COORDINATE; - compPtr2->rect_.height_ = TEST_COORDINATE; - SecCompEntity entity2(compPtr2, TEST_TOKEN_ID, TEST_SC_ID_2); - ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(TEST_PID_2, entity2)); - - SecCompManager::GetInstance().NotifyProcessDied(TEST_PID_3); - ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); - - SecCompManager::GetInstance().NotifyProcessDied(TEST_PID_1); - ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(TEST_PID_1, TEST_SC_ID_1)); + compPtr2->rect_.x_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap + compPtr2->rect_.y_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap + compPtr2->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + compPtr2->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + SecCompEntity entity2(compPtr2, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_2); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_2, entity2)); + + SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_3); + ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); + + SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1); + ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList( + ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); } /** @@ -270,8 +277,8 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) buttonInvalid.ToJson(jsonInvalid); int32_t scId; SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID, - .pid = TEST_PID_1 + .tokenId = ServiceTestCommon::TEST_TOKEN_ID, + .pid = ServiceTestCommon::TEST_PID_1 }; ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); @@ -298,10 +305,11 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID, - .pid = TEST_PID_1 + .tokenId = ServiceTestCommon::TEST_TOKEN_ID, + .pid = ServiceTestCommon::TEST_PID_1 }; - ASSERT_NE(SC_OK, SecCompManager::GetInstance().UpdateSecurityComponent(TEST_SC_ID_1, jsonValid, caller)); + ASSERT_NE(SC_OK, SecCompManager::GetInstance().UpdateSecurityComponent( + ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller)); } /** @@ -313,12 +321,12 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) { SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID, - .pid = TEST_PID_1 + .tokenId = ServiceTestCommon::TEST_TOKEN_ID, + .pid = ServiceTestCommon::TEST_PID_1 }; ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, - SecCompManager::GetInstance().UnregisterSecurityComponent(TEST_INVALID_SC_ID, caller)); + SecCompManager::GetInstance().UnregisterSecurityComponent(ServiceTestCommon::TEST_INVALID_SC_ID, caller)); } /** @@ -330,8 +338,8 @@ HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { SecCompCallerInfo caller = { - .tokenId = TEST_TOKEN_ID, - .pid = TEST_PID_1 + .tokenId = ServiceTestCommon::TEST_TOKEN_ID, + .pid = ServiceTestCommon::TEST_PID_1 }; SecCompClickEvent touchInfo; nlohmann::json jsonVaild; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp index 554cceb..533adec 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -52,7 +52,7 @@ void SecCompServiceMockTest::SetUp() if (secCompService_ != nullptr) { return; } - SecCompService* ptr = new (std::nothrow) SecCompService(SA_ID, true); + SecCompService* ptr = new (std::nothrow) SecCompService(ServiceTestCommon::SA_ID, true); secCompService_ = sptr(ptr); ASSERT_NE(nullptr, secCompService_); secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); @@ -87,7 +87,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) std::string saveInfo = jsonRes.dump(); // parse component json fail - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; @@ -101,8 +101,8 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) struct SecCompClickEvent touch = { .touchX = 100, .touchY = 100, - .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) / + ServiceTestCommon::TIME_CONVERSION_UNIT }; EXPECT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); @@ -125,7 +125,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; @@ -168,8 +168,8 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) struct SecCompClickEvent touch = { .touchX = 100, .touchY = 100, - .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) / + ServiceTestCommon::TIME_CONVERSION_UNIT }; EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); @@ -191,7 +191,7 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; @@ -201,19 +201,19 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. struct SecCompClickEvent touchInfo = { .touchX = 100, .touchY = 100, - .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) / + ServiceTestCommon::TIME_CONVERSION_UNIT }; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(6); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); } @@ -232,7 +232,7 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; @@ -242,37 +242,37 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. struct SecCompClickEvent touchInfo = { .touchX = 100, .touchY = 100, - .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) / + ServiceTestCommon::TIME_CONVERSION_UNIT }; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); touchInfo.timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); touchInfo.timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); touchInfo.timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;; + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); sleep(3); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); } /** @@ -290,7 +290,7 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent003, TestSize. ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = jsonRes.dump(); - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; @@ -300,24 +300,24 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent003, TestSize. struct SecCompClickEvent touchInfo = { .touchX = 100, .touchY = 100, - .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) / + ServiceTestCommon::TIME_CONVERSION_UNIT }; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); touchInfo.timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(6); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); touchInfo.timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT; + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); sleep(3); - ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); - ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index afd77a2..3ca3a7f 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -40,7 +40,9 @@ static AccessTokenID g_selfTokenId = 0; } void SecCompServiceTest::SetUpTestCase() -{} +{ + system("kill -9 `pidof security_compon`"); +} void SecCompServiceTest::TearDownTestCase() {} @@ -51,7 +53,7 @@ void SecCompServiceTest::SetUp() if (secCompService_ != nullptr) { return; } - SecCompService* ptr = new (std::nothrow) SecCompService(SA_ID, true); + SecCompService* ptr = new (std::nothrow) SecCompService(ServiceTestCommon::SA_ID, true); secCompService_ = sptr(ptr); ASSERT_NE(nullptr, secCompService_); secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); @@ -187,7 +189,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) // set token id to hap token, but uid is not in foreground EXPECT_FALSE(secCompService_->GetCallerInfo(caller)); setuid(0); - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); // add local uid to foreground. AppExecFwk::AppStateData stateData = { .uid = getuid() @@ -205,7 +207,8 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1) HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) { // get caller fail - EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, secCompService_->UnregisterSecurityComponent(TEST_SC_ID_1)); + EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, + secCompService_->UnregisterSecurityComponent(ServiceTestCommon::TEST_SC_ID_1)); } /** @@ -217,14 +220,16 @@ HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1) { // get caller fail - EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->UpdateSecurityComponent(TEST_SC_ID_1, "")); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, + secCompService_->UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, "")); - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { .uid = getuid() }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); - EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->UpdateSecurityComponent(TEST_SC_ID_1, "{a")); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, + secCompService_->UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, "{a")); } /** @@ -246,7 +251,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve std::string locationInfo = jsonRes.dump(); // parse component json fail - ASSERT_EQ(0, SetSelfTokenID(HAP_TOKEN_ID)); + ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); setuid(100); AppExecFwk::AppStateData stateData = { .uid = getuid() @@ -259,8 +264,8 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve struct SecCompClickEvent touch = { .touchX = 100, .touchY = 100, - .timestamp = static_cast( - std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT, + .timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()) / + ServiceTestCommon::TIME_CONVERSION_UNIT, .extraInfo.data = data, .extraInfo.dataSize = 16, }; diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp index 1b159a1..62a1545 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp @@ -22,39 +22,39 @@ void ServiceTestCommon::BuildLocationComponentJson(nlohmann::json& jsonComponent jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } }; jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - { JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + { JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, + { JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, + { JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, + { JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, }; jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, @@ -71,39 +71,39 @@ void ServiceTestCommon::BuildSaveComponentJson(nlohmann::json& jsonComponent) jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } }; jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, }; jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, @@ -120,39 +120,39 @@ void ServiceTestCommon::BuildPasteComponentJson(nlohmann::json& jsonComponent) jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } }; jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TEST_COORDINATE } + {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, + {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } }; nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, + { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, }; jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, + { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, }; jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TEST_COLOR_YELLOW } + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } }; jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TEST_SIZE }, + { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, }; jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.h b/services/security_component_service/sa/test/unittest/src/service_test_common.h index 26368f4..078189f 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.h +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.h @@ -21,41 +21,40 @@ namespace OHOS { namespace Security { namespace SecurityComponent { -constexpr int32_t TEST_UID_1 = 1; -constexpr int32_t TEST_UID_2 = 2; -constexpr int32_t TEST_PID_1 = 1; -constexpr int32_t TEST_PID_2 = 2; -constexpr int32_t TEST_PID_3 = 3; -constexpr int32_t TEST_SC_ID_1 = 1; -constexpr int32_t TEST_SC_ID_2 = 2; -constexpr int32_t TEST_INVALID_SC_ID = -1; -constexpr int32_t SC_ID_START = 1000; -constexpr int32_t SA_ID = 3506; +class ServiceTestCommon { +public: + static constexpr int32_t TEST_UID_1 = 1; + static constexpr int32_t TEST_UID_2 = 2; + static constexpr int32_t TEST_PID_1 = 1; + static constexpr int32_t TEST_PID_2 = 2; + static constexpr int32_t TEST_PID_3 = 3; + static constexpr int32_t TEST_SC_ID_1 = 1; + static constexpr int32_t TEST_SC_ID_2 = 2; + static constexpr int32_t TEST_INVALID_SC_ID = -1; + static constexpr int32_t SC_ID_START = 1000; + static constexpr int32_t SA_ID = 3506; -constexpr float TEST_SIZE = 100.0; -constexpr double TEST_COORDINATE = 100.0; -constexpr double TEST_DIFF_COORDINATE = 200.0; -constexpr double TEST_DIMENSION = 100.0; -constexpr double TEST_INVALID_DIMENSION = -100.0; -constexpr double ZERO_OFFSET = 0.0F; -constexpr uint32_t TEST_INVALID_SIZE = 0; -constexpr uint32_t QUARTER = 4; + static constexpr float TEST_SIZE = 100.0; + static constexpr double TEST_COORDINATE = 100.0; + static constexpr double TEST_DIFF_COORDINATE = 200.0; + static constexpr double TEST_DIMENSION = 100.0; + static constexpr double TEST_INVALID_DIMENSION = -100.0; + static constexpr double ZERO_OFFSET = 0.0F; + static constexpr uint32_t TEST_INVALID_SIZE = 0; + static constexpr uint32_t QUARTER = 4; -constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; -constexpr uint32_t TEST_COLOR_BLACK = 0x00000000; -constexpr uint32_t TEST_COLOR_WHITE = 0xffffffff; -constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; -constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; + static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; + static constexpr uint32_t TEST_COLOR_BLACK = 0x00000000; + static constexpr uint32_t TEST_COLOR_WHITE = 0xffffffff; + static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; + static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; + static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; -constexpr uint64_t TIME_CONVERSION_UNIT = 1000; -constexpr uint32_t MAX_INT_NUM = 0x7fffffff; -constexpr uint32_t HAP_TOKEN_ID = 537715419; -constexpr uint32_t TEST_TOKEN_ID = 1; -constexpr uint32_t MAX_HMAC_SIZE = 64; + static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; + static constexpr uint32_t MAX_INT_NUM = 0x7fffffff; + static constexpr uint32_t HAP_TOKEN_ID = 537715419; + static constexpr uint32_t TEST_TOKEN_ID = 1; -class ServiceTestCommon { -public: static void BuildLocationComponentJson(nlohmann::json& jsonComponent); static void BuildSaveComponentJson(nlohmann::json& jsonComponent); static void BuildPasteComponentJson(nlohmann::json& jsonComponent); -- Gitee From a4ba25979242b1fd5aeab8cd265e02da9a13406d Mon Sep 17 00:00:00 2001 From: xxx Date: Tue, 22 Aug 2023 16:47:12 +0800 Subject: [PATCH 56/59] Description:delete googletest from bundle.json Match-id-3d36f88b07fa0d9f0ef4d699debae8915bb752a1 --- bundle.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index f9134f4..0d2b697 100644 --- a/bundle.json +++ b/bundle.json @@ -39,8 +39,7 @@ "window_manager" ], "third_party": [ - "cJSON", - "googletest" + "cJSON" ] }, "build": { -- Gitee From 2f67b7d229efc7440cd2c3974414ebfb90bcebae Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 24 Aug 2023 09:55:54 +0800 Subject: [PATCH 57/59] Description:modify unit test when enhance is not exist Match-id-34e2ec72766fe3d407856346a6fc78b39d2c2074 --- .../inner_api/security_component/test/BUILD.gn | 9 +++++++++ .../test/unittest/src/sec_comp_kit_test.cpp | 6 ++++++ .../unittest/src/sec_comp_register_callback_test.cpp | 12 ++++++++++++ security_component.gni | 7 +++++++ services/security_component_service/sa/test/BUILD.gn | 5 +++++ .../sa/test/unittest/src/sec_comp_entity_test.cpp | 4 ++++ .../sa/test/unittest/src/sec_comp_manager_test.cpp | 5 +++++ .../sa/test/unittest/src/sec_comp_service_test.cpp | 11 +++++++++++ 8 files changed, 59 insertions(+) diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index fdba91b..66892b6 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/test.gni") +import("../../../../security_component.gni") sec_comp_root_dir = "../../../.." @@ -36,6 +37,10 @@ ohos_unittest("sec_comp_sdk_test") { configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] + if (security_component_enhance_enable) { + cflags_cc += [ "-DSECURITY_COMPONENT_ENHANCE_ENABLE" ] + } + deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", @@ -68,6 +73,10 @@ ohos_unittest("sec_comp_register_callback_test") { configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] + if (security_component_enhance_enable) { + cflags_cc += [ "-DSECURITY_COMPONENT_ENHANCE_ENABLE" ] + } + deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 58dc275..ba3d51d 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -185,7 +185,13 @@ HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1) std::string locationInfo = jsonRes.dump(); int32_t scId; +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_EQ(-1, scId); +#else + ASSERT_EQ(SC_OK, + SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_NE(-1, scId); +#endif } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index b38dd25..9aee39d 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -103,9 +103,15 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterWithoutPreprocess001, TestSize.Lev SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); int32_t scId; +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE ASSERT_EQ(SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL, SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_EQ(-1, scId); +#else + ASSERT_EQ(SC_OK, + SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_NE(-1, scId); +#endif } /** @@ -144,9 +150,15 @@ HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) g_probe.mockRes = -1; int32_t scId; +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_EQ(-1, scId); +#else + ASSERT_EQ(SC_OK, + SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_NE(-1, scId); +#endif } /** diff --git a/security_component.gni b/security_component.gni index f92bdb1..9d13f50 100644 --- a/security_component.gni +++ b/security_component.gni @@ -12,3 +12,10 @@ # limitations under the License. sec_comp_dir = "//base/security/security_component" + +if (!defined(global_parts_info) || + defined(global_parts_info.security_security_component_enhance)) { + security_component_enhance_enable = true +} else { + security_component_enhance_enable = false +} diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 84cd54b..7e2e468 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/test.gni") +import("../../../../security_component.gni") sec_comp_root_dir = "../../../.." @@ -56,6 +57,10 @@ ohos_unittest("sec_comp_service_test") { configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] + if (security_component_enhance_enable) { + cflags_cc += [ "-DSECURITY_COMPONENT_ENHANCE_ENABLE" ] + } + deps = [ "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 8a9b861..f306dc5 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -152,5 +152,9 @@ HWTEST_F(SecCompEntityTest, CheckTouchInfo001, TestSize.Level1) entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; touch.timestamp = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT; +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE ASSERT_FALSE(entity_->CheckTouchInfo(touch)); +#else + ASSERT_TRUE(entity_->CheckTouchInfo(touch)); +#endif } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 5ad5d90..4e9e5f6 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -287,9 +287,14 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE // callback check failed ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId)); +#else + ASSERT_EQ(SC_OK, + SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId)); +#endif SecCompManager::GetInstance().maliciousAppList_.clear(); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 3ca3a7f..8efe7c4 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -258,8 +258,13 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve }; secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData); +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE EXPECT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST, secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); +#else + EXPECT_EQ(SC_OK, + secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); +#endif uint8_t data[16] = { 0 }; struct SecCompClickEvent touch = { .touchX = 100, @@ -269,8 +274,14 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve .extraInfo.data = data, .extraInfo.dataSize = 16, }; +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, secCompService_->UnregisterSecurityComponent(scId)); +#else + EXPECT_EQ(SC_OK, + secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); + EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); +#endif setuid(uid); } -- Gitee From 59dcc9fa2bb85eaa82c45d39bc1d765ff51115e4 Mon Sep 17 00:00:00 2001 From: baoyang Date: Thu, 24 Aug 2023 21:02:19 +0800 Subject: [PATCH 58/59] add patches.json to setup pipeline Signed-off-by: baoyang --- patches/patches.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 patches/patches.json diff --git a/patches/patches.json b/patches/patches.json new file mode 100644 index 0000000..ee83bc1 --- /dev/null +++ b/patches/patches.json @@ -0,0 +1,9 @@ +{ + "patches": [ + { + "project":"productdefine_common", + "path":"productdefine/common", + "pr_url":"https://gitee.com/openharmony/productdefine_common/pulls/693" + } + ] +} -- Gitee From 8f170cd8a0e2671f34ef54b8bc440aa39d1df382 Mon Sep 17 00:00:00 2001 From: y1585740638 Date: Fri, 25 Aug 2023 11:46:13 +0800 Subject: [PATCH 59/59] =?UTF-8?q?=E8=A1=A5=E5=85=85tdd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: y1585740638 Change-Id: I53e23d59f31254ab6c0aa8c46a7d1110ad174753 --- .../unittest/src/location_button_test.cpp | 77 +++++- .../src/sec_comp_register_callback_test.cpp | 254 ++++++++++++++++-- .../test/unittest/src/test_common.h | 1 + .../unittest/src/app_state_observer_test.cpp | 20 ++ .../src/sec_comp_info_helper_test.cpp | 39 ++- .../unittest/src/sec_comp_manager_test.cpp | 60 ++++- .../unittest/src/sec_comp_service_test.cpp | 46 ++++ .../test/unittest/src/service_test_common.h | 2 +- 8 files changed, 464 insertions(+), 35 deletions(-) diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index f4be631..09fda9c 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -143,11 +143,62 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) /** * @tc.name: FromJson005 - * @tc.desc: Test location button from wrong size json + * @tc.desc: Test location button from wrong rect json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp; + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = wrongJson; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + TestCommon::BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, WRONG_TYPE}, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, WRONG_TYPE}, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE}, + {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { + {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE}, + {JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE} + }; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson006 + * @tc.desc: Test location button from wrong size json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -187,12 +238,12 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) } /** - * @tc.name: FromJson006 + * @tc.name: FromJson007 * @tc.desc: Test location button from wrong size json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -229,12 +280,12 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) } /** - * @tc.name: FromJson007 + * @tc.name: FromJson008 * @tc.desc: Test location button from wrong size json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -271,12 +322,12 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) } /** - * @tc.name: FromJson008 + * @tc.name: FromJson009 * @tc.desc: Test location button from wrong border and parent json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -296,12 +347,12 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) } /** - * @tc.name: FromJson009 + * @tc.name: FromJson010 * @tc.desc: Test location button from wrong type params json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) { nlohmann::json jsonComponent; TestCommon::BuildLocationComponentInfo(jsonComponent); @@ -331,12 +382,12 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) } /** - * @tc.name: FromJson010 + * @tc.name: FromJson011 * @tc.desc: Test location button from wrong type params json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { nlohmann::json jsonComponent; TestCommon::BuildLocationComponentInfo(jsonComponent); @@ -366,12 +417,12 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) } /** - * @tc.name: FromJson011 + * @tc.name: FromJson012 * @tc.desc: Test location button from wrong value params json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) { nlohmann::json jsonComponent; TestCommon::BuildLocationComponentInfo(jsonComponent); diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index 9aee39d..50aabd5 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -31,10 +31,13 @@ using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterCallbackTest"}; +static AccessTokenID g_selfTokenId = 0; +static int32_t g_selfUid = 0; class MockUiSecCompProbe : public ISecCompProbe { public: @@ -81,11 +84,15 @@ void SecCompRegisterCallbackTest::TearDownTestCase() void SecCompRegisterCallbackTest::SetUp() { + g_selfUid = getuid(); + g_selfTokenId = GetSelfTokenID(); SC_LOG_INFO(LABEL, "SetUp ok."); } void SecCompRegisterCallbackTest::TearDown() { + setuid(g_selfUid); + EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId)); SC_LOG_INFO(LABEL, "TearDown."); } @@ -111,16 +118,17 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterWithoutPreprocess001, TestSize.Lev ASSERT_EQ(SC_OK, SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_NE(-1, scId); + EXPECT_EQ(SC_OK, SecCompClient::GetInstance().UnregisterSecurityComponent(scId)); #endif } /** - * @tc.name: Register001 + * @tc.name: RegisterSecurityComponent001 * @tc.desc: test register security component success. * @tc.type: FUNC * @tc.require: AR000HO9JM */ -HWTEST_F(SecCompRegisterCallbackTest, Register001, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) { nlohmann::json jsonRes; TestCommon::BuildLocationComponentInfo(jsonRes); @@ -136,12 +144,12 @@ HWTEST_F(SecCompRegisterCallbackTest, Register001, TestSize.Level1) } /** - * @tc.name: Register002 + * @tc.name: RegisterSecurityComponent002 * @tc.desc: test register callback failed. * @tc.type: FUNC * @tc.require: AR000HO9JM */ -HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) { nlohmann::json jsonRes; TestCommon::BuildLocationComponentInfo(jsonRes); @@ -158,16 +166,17 @@ HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_NE(-1, scId); + ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); #endif } /** - * @tc.name: Register003 + * @tc.name: RegisterSecurityComponent003 * @tc.desc: test register in MaliciousAppList. * @tc.type: FUNC * @tc.require: AR000HO9JM */ -HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1) { nlohmann::json jsonRes; TestCommon::BuildLocationComponentInfo(jsonRes); @@ -177,15 +186,16 @@ HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) int32_t scId; ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } /** - * @tc.name: RegisterSecurityComponent001 + * @tc.name: RegisterSecurityComponent004 * @tc.desc: Test register security component check touch info failed * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1) { system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; @@ -210,12 +220,12 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Lev } /** - * @tc.name: RegisterSecurityComponent002 + * @tc.name: RegisterSecurityComponent005 * @tc.desc: Test register security component permission grant failed * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1) { system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; @@ -239,12 +249,28 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Lev } /** - * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.name: RegisterSecurityComponent006 + * @tc.desc: Test register security component permission grant failed caller error + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + setuid(100); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent001 * @tc.desc: Test register security component success * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; @@ -266,24 +292,74 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); uint32_t selfTokenId = GetSelfTokenID(); ASSERT_TRUE(SecCompKit::ReduceAfterVerifySavePermission(selfTokenId)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); } /** - * @tc.name: ExceptCall001 - * @tc.desc: Test update security component success + * @tc.name: ReportSecurityComponentClickEvent002 + * @tc.desc: Test report security component caller error * @tc.type: FUNC - * @tc.require: AR000HO9IN + * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, ExceptCall001, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1) { + system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = jsonRes.dump(); int32_t scId; - ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId)); - ASSERT_NE(-1, scId); - ASSERT_EQ(SC_OK, SecCompKit::UpdateSecurityComponent(scId, saveInfo)); + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + + setuid(100); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.desc: Test report security component data is empty + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE + ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); +#else + ASSERT_EQ(SC_OK, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); +#endif + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } /** @@ -315,5 +391,145 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 touch.extraInfo.data = data; EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: ReduceAfterVerifySavePermission001 + * @tc.desc: Test register security component wrong hap + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, ReduceAfterVerifySavePermission001, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + + ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + setuid(100); + ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(TestCommon::HAP_TOKEN_ID)); + // mediaLibraryTokenId_ != 0 + ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(TestCommon::HAP_TOKEN_ID)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: ReduceAfterVerifySavePermission002 + * @tc.desc: Test register security component invalid tokenId + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, ReduceAfterVerifySavePermission002, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + + ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(0)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: UnregisterSecurityComponent001 + * @tc.desc: Test unregister security component caller error + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + setuid(100); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UnregisterSecurityComponent(scId)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); } + +/** + * @tc.name: UpdateSecurityComponent001 + * @tc.desc: Test update security component success + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId)); + ASSERT_NE(-1, scId); + ASSERT_EQ(SC_OK, SecCompKit::UpdateSecurityComponent(scId, saveInfo)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); +} + +/** + * @tc.name: UpdateSecurityComponent002 + * @tc.desc: Test update security component caller error + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId)); + ASSERT_NE(-1, scId); + setuid(100); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UpdateSecurityComponent(scId, saveInfo)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h index f3b97b9..b42b47a 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.h +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.h @@ -32,6 +32,7 @@ public: static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; static constexpr uint32_t TEST_DIFF_COLOR = 0; static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; + static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr uint32_t MAX_HMAC_SIZE = 64; static constexpr size_t MAX_CALLER_SIZE = 10; diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp index b132c4f..df46a14 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -153,6 +153,26 @@ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) ASSERT_FALSE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } +/** + * @tc.name: OnProcessStateChanged002 + * @tc.desc: Test add process to foregroundset twice + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(AppStateObserverTest, OnProcessStateChanged002, TestSize.Level1) +{ + AppExecFwk::ProcessData processData; + processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; + processData.pid = ServiceTestCommon::TEST_PID_1; + observer_->OnProcessStateChanged(processData); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); + + processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; + processData.pid = ServiceTestCommon::TEST_PID_1; + observer_->OnProcessStateChanged(processData); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); +} + /** * @tc.name: OnProcessDied001 * @tc.desc: Test recieve process state died diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index ddcef69..3bd8099 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -148,20 +148,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) rect.x_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_testWidth; + rect.y_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_testHeight; + rect.x_ = g_curScreenWidth + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_testWidth; + rect.y_ = g_curScreenHeight + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_testHeight; + rect.width_ = g_curScreenWidth; - ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); rect.height_ = g_curScreenHeight; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.width_ = g_testWidth; + rect.height_ = g_testHeight; + rect.x_ = g_curScreenWidth - g_testWidth; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_testWidth; rect.y_ = g_curScreenHeight - g_testHeight; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_testHeight; } /** @@ -188,16 +201,27 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) windowRect.x_ = g_testWidth + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.x_ = g_testWidth; + windowRect.y_ = g_testHeight + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.y_ = g_testHeight; + windowRect.width_ = g_testWidth - 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.width_ = g_testWidth; + windowRect.height_ = g_testHeight - 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.height_ = g_testHeight; + windowRect.width_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.width_ = g_testWidth; + windowRect.height_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.height_ = g_testHeight; } /** @@ -605,11 +629,13 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); + ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_WHITE } }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); } @@ -652,3 +678,14 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) comp->type_ = SecCompType::MAX_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); } + +/** + * @tc.name: RevokeTempPermission001 + * @tc.desc: Test revoke temp permission componentInfo is null + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, RevokeTempPermission001, TestSize.Level1) +{ + ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, SecCompInfoHelper::RevokeTempPermission(0, nullptr)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 4e9e5f6..a52e23b 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -16,6 +16,9 @@ #include "sec_comp_manager_test.h" #include "sec_comp_log.h" +#define private public +#include "sec_comp_manager.h" +#undef private #include "location_button.h" #include "save_button.h" #include "sec_comp_err.h" @@ -149,6 +152,24 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, entityNew)); } +/** + * @tc.name: AddSecurityComponentToList002 + * @tc.desc: Test add security component to list sa not exit + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompManagerTest, AddSecurityComponentToList002, TestSize.Level1) +{ + bool isSaExit = SecCompManager::GetInstance().isSaExit_; + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(nullptr, compPtr); + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + + ASSERT_NE(SC_SERVICE_ERROR_SERVICE_NOT_EXIST, + SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); + SecCompManager::GetInstance().isSaExit_ = isSaExit; +} + /** * @tc.name: DeleteSecurityComponentFromList001 * @tc.desc: Test delete security component @@ -224,9 +245,10 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) ASSERT_NE(nullptr, component); SecCompManager::GetInstance().NotifyProcessForeground(ServiceTestCommon::TEST_PID_1); ASSERT_TRUE(SecCompManager::GetInstance().IsForegroundCompExist()); - SecCompManager::GetInstance().NotifyProcessBackground(ServiceTestCommon::TEST_PID_1); ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); + SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1); + ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); } /** @@ -353,3 +375,39 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve ASSERT_NE(SC_OK, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo, nullptr)); } + +/** + * @tc.name: CheckClickSecurityComponentInfo001 + * @tc.desc: Test check click security component info failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompManagerTest, CheckClickSecurityComponentInfo001, TestSize.Level1) +{ + SecCompCallerInfo caller = { + .tokenId = ServiceTestCommon::TEST_TOKEN_ID, + .pid = ServiceTestCommon::TEST_PID_1 + }; + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(nullptr, compPtr); + + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); + + SecCompClickEvent touchInfo; + nlohmann::json jsonVaild; + LocationButton buttonValid = BuildValidLocationComponent(); + buttonValid.ToJson(jsonVaild); + jsonVaild[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( + ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, touchInfo, nullptr)); + + jsonVaild[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( + ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, touchInfo, nullptr)); + + jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_COORDINATE - 1; + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( + ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, touchInfo, nullptr)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 8efe7c4..9e49992 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -275,6 +275,9 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve .extraInfo.dataSize = 16, }; #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE + EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, + secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); + // already in malicious list EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, secCompService_->UnregisterSecurityComponent(scId)); @@ -285,3 +288,46 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve #endif setuid(uid); } + +/** + * @tc.name: Dump001 + * @tc.desc: Test dump + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, Dump001, TestSize.Level1) +{ + int fd = -1; + std::vector args; + + ASSERT_EQ(ERR_INVALID_VALUE, secCompService_->Dump(fd, args)); + + fd = 0; + + // hidumper + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + // hidumper -h + args.emplace_back(Str8ToStr16("-h")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -p + args.emplace_back(Str8ToStr16("-p")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -a + args.emplace_back(Str8ToStr16("-a")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -"" + args.emplace_back(Str8ToStr16("")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -t + args.emplace_back(Str8ToStr16("-t")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); +} diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.h b/services/security_component_service/sa/test/unittest/src/service_test_common.h index 078189f..64989b2 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.h +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.h @@ -44,7 +44,7 @@ public: static constexpr uint32_t QUARTER = 4; static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; - static constexpr uint32_t TEST_COLOR_BLACK = 0x00000000; + static constexpr uint32_t TEST_COLOR_BLACK = 0xff000000; static constexpr uint32_t TEST_COLOR_WHITE = 0xffffffff; static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; static constexpr uint32_t TEST_COLOR_RED = 0xffff0000; -- Gitee