From 32619eb6a9b7455365a035ef304f20dfd29e38b6 Mon Sep 17 00:00:00 2001 From: y1585740638 Date: Fri, 18 Aug 2023 15:28:37 +0800 Subject: [PATCH] =?UTF-8?q?tdd=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: y1585740638 --- .../src/sec_comp_client.cpp | 2 +- .../security_component/test/BUILD.gn | 20 +- .../mock/include/sec_comp_enhance_adapter.h | 36 +++ .../mock/src/sec_comp_enhance_adapter.cpp | 109 +++++++ .../unittest/src/location_button_test.cpp | 92 ++---- .../test/unittest/src/paste_button_test.cpp | 70 +--- .../test/unittest/src/save_button_test.cpp | 69 +--- .../unittest/src/sec_comp_kit_mock_test.cpp | 298 ++++++++++++++++++ ...ample_build.h => sec_comp_kit_mock_test.h} | 20 +- .../test/unittest/src/sec_comp_kit_test.cpp | 8 +- .../src/sec_comp_register_callback_test.cpp | 9 +- .../src/sec_comp_register_challenge_test.cpp | 31 +- ...ec_comp_register_without_callback_test.cpp | 5 +- ...ec_comp_report_click_without_hmac_test.cpp | 116 ------- ...utton_sample_build.cpp => test_common.cpp} | 109 ++++++- .../test/unittest/src/test_common.h | 40 +++ .../unittest/src/app_state_observer_test.cpp | 52 ++- .../unittest/src/sec_comp_entity_test.cpp | 4 +- .../src/sec_comp_info_helper_test.cpp | 19 +- .../unittest/src/sec_comp_manager_test.cpp | 27 +- .../src/sec_comp_service_mock_test.cpp | 10 +- .../unittest/src/sec_comp_service_test.cpp | 11 +- .../test/unittest/src/sec_comp_stub_test.cpp | 1 + .../sa/test/unittest/src/test_common_param.h | 54 ++++ 24 files changed, 767 insertions(+), 445 deletions(-) create mode 100644 interfaces/inner_api/security_component/test/mock/include/sec_comp_enhance_adapter.h create mode 100644 interfaces/inner_api/security_component/test/mock/src/sec_comp_enhance_adapter.cpp create mode 100644 interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_mock_test.cpp rename interfaces/inner_api/security_component/test/unittest/src/{location_button_sample_build.h => sec_comp_kit_mock_test.h} (69%) 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%) create mode 100644 interfaces/inner_api/security_component/test/unittest/src/test_common.h create mode 100644 services/security_component_service/sa/test/unittest/src/test_common_param.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 index 0e20cee..9ce69af 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -52,7 +52,7 @@ SecCompClient::~SecCompClient() int32_t SecCompClient::RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId) { - auto proxy = GetProxy(true); + auto proxy = GetProxy(true);111 if (proxy == nullptr) { SC_LOG_ERROR(LABEL, "Proxy is null"); return SC_SERVICE_ERROR_VALUE_INVALID; diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index 0aabcdc..a5bb319 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -49,26 +49,26 @@ ohos_unittest("sec_comp_sdk_test") { ] } -ohos_unittest("sec_comp_register_test") { +ohos_unittest("sec_comp_sdk_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/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_callback_test.cpp", + "${sec_comp_root_dir}/interfaces/inner_api/security_component/test/mock/src/sec_comp_enhance_adapter.cpp", + "unittest/src/sec_comp_kit_mock_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", ] @@ -81,7 +81,7 @@ ohos_unittest("sec_comp_register_test") { ] } -ohos_unittest("sec_comp_report_click_without_hmac_test") { +ohos_unittest("sec_comp_register_test") { subsystem_name = "security" part_name = "security_component" module_out_path = part_name + "/" + part_name @@ -93,8 +93,8 @@ ohos_unittest("sec_comp_report_click_without_hmac_test") { ] sources = [ - "unittest/src/location_button_sample_build.cpp", - "unittest/src/sec_comp_report_click_without_hmac_test.cpp", + "unittest/src/sec_comp_register_callback_test.cpp", + "unittest/src/test_common.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -125,8 +125,8 @@ ohos_unittest("sec_comp_register_without_callback_test") { ] sources = [ - "unittest/src/location_button_sample_build.cpp", "unittest/src/sec_comp_register_without_callback_test.cpp", + "unittest/src/test_common.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -158,8 +158,8 @@ ohos_unittest("sec_comp_register_challenge_test") { ] 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" ] @@ -183,9 +183,9 @@ group("unittest") { testonly = true deps = [ ":sec_comp_sdk_test", + ":sec_comp_sdk_mock_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/mock/include/sec_comp_enhance_adapter.h b/interfaces/inner_api/security_component/test/mock/include/sec_comp_enhance_adapter.h new file mode 100644 index 0000000..36bbaaa --- /dev/null +++ b/interfaces/inner_api/security_component/test/mock/include/sec_comp_enhance_adapter.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_ENHANCE_ADAPTER_H +#define SECURITY_COMPONENT_ENHANCE_ADAPTER_H +#include +#include "parcel.h" +#include "sec_comp_info.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); + + static int32_t EnableInputEnhance(); + static int32_t DisableInputEnhance(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_ENHANCE_ADAPTER_H diff --git a/interfaces/inner_api/security_component/test/mock/src/sec_comp_enhance_adapter.cpp b/interfaces/inner_api/security_component/test/mock/src/sec_comp_enhance_adapter.cpp new file mode 100644 index 0000000..7c5538e --- /dev/null +++ b/interfaces/inner_api/security_component/test/mock/src/sec_comp_enhance_adapter.cpp @@ -0,0 +1,109 @@ +/* + * 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 "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, "MockSecCompEnhanceAdapter"}; +} + +int32_t SecCompEnhanceAdapter::SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) +{ + SC_LOG_DEBUG(LABEL, "SetEnhanceCfg success"); + return SC_OK; +} + +int32_t SecCompEnhanceAdapter::GetPointerEventEnhanceData(void* data, uint32_t dataLen, + uint8_t* enhanceData, uint32_t& enHancedataLen) +{ + SC_LOG_DEBUG(LABEL, "GetPointerEventEnhanceData success"); + return SC_OK; +} + +int32_t SecCompEnhanceAdapter::CheckExtraInfo(const SecCompClickEvent& touchInfo) +{ + SC_LOG_DEBUG(LABEL, "CheckExtraInfo success"); + return SC_OK; +} + +int32_t SecCompEnhanceAdapter::EnableInputEnhance() +{ + SC_LOG_DEBUG(LABEL, "EnableInputEnhance success"); + return SC_OK; +} + +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/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..3986fcb 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_param.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)); @@ -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..78428b8 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_param.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..fd7a62f 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_param.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_mock_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_mock_test.cpp new file mode 100644 index 0000000..209f864 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_mock_test.cpp @@ -0,0 +1,298 @@ +/* + * 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 "test_common_param.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 AccessTokenID g_selfTokenId = 0; +} + +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 check touch info failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, RegisterSecurityComponent001, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + 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, saveInfo, 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, saveInfo, touch, nullptr), + SC_SERVICE_ERROR_CLICK_EVENT_INVALID); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: RegisterSecurityComponent002 + * @tc.desc: Test register security component permission grant failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, RegisterSecurityComponent002, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + 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, saveInfo, 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, saveInfo, touch, nullptr), + SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.desc: Test register security component + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + 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, saveInfo, 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, saveInfo, touchInfo, nullptr)); + + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr), + SC_OK); + sleep(6); + ASSERT_FALSE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + EXPECT_EQ(secCompService_->UnregisterSecurityComponent(scId), SC_OK); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent004 + * @tc.desc: Test report security component click with save button + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent004, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + 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, saveInfo, 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, saveInfo, touchInfo, nullptr), + SC_OK); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, 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, saveInfo, 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, saveInfo, touchInfo, nullptr), + 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, saveInfo, 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, saveInfo, touchInfo, nullptr), + 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: ReportSecurityComponentClickEvent005 + * @tc.desc: Test report security component click twice with save button + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent005, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + // secCompService_->state_ = ServiceRunningState::STATE_RUNNING; + // secCompService_->Initialize(); + + // ASSERT_EQ(SetSelfTokenID(HAP_TOKEN_ID), 0); + // AppExecFwk::AppStateData stateData = { + // .uid = getuid() + // }; + // secCompService_->appSta2teObserver_->AddProcessToForegroundSet(stateData); + // register security component ok + EXPECT_EQ(secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, 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, saveInfo, 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, saveInfo, 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, saveInfo, touchInfo, nullptr), + SC_OK); + ASSERT_TRUE(secCompService_->ReduceAfterVerifySavePermission(HAP_TOKEN_ID)); + ASSERT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr), + SC_OK); + 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/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_mock_test.h similarity index 69% rename from interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h rename to interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_mock_test.h index 9bde892..3bd1241 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_sample_build.h +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_mock_test.h @@ -12,20 +12,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef SECURITY_COMPONENT_KIT_TEST +#define SECURITY_COMPONENT_KIT_TEST -#ifndef LOCATION_BUTTON_SAMPLE_BUILD_H -#define LOCATION_BUTTON_SAMPLE_BUILD_H -#include "location_button.h" +#include +#include "sec_comp_kit.h" namespace OHOS { namespace Security { namespace SecurityComponent { - -class LocationButtonSampleBuild { +class SecCompKitTest : public testing::Test { public: - static void BuildLocationComponentInfo(nlohmann::json& jsonComponent); + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; }; } // namespace SecurityComponent } // namespace Security } // namespace OHOS -#endif // LOCATION_BUTTON_SAMPLE_BUILD_H +#endif // SECURITY_COMPONENT_KIT_TEST \ 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..3534cd1 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_param.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() { @@ -174,4 +171,3 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) SecCompCallerAuthorization::GetInstance().kitCallerList_.clear(); SecCompCallerAuthorization::GetInstance().isInit_ = false; } - 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..03bda10 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,7 +13,6 @@ * limitations under the License. */ #include - #include "i_sec_comp_probe.h" #include "location_button.h" #include "location_button_sample_build.h" @@ -26,6 +25,7 @@ #include "sec_comp_log.h" #include "sec_comp_tool.h" #include "sec_comp_ui_register.h" +#include "test_common_param.h" using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; @@ -97,7 +97,7 @@ 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; @@ -120,7 +120,7 @@ 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; @@ -142,7 +142,7 @@ 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; @@ -150,4 +150,3 @@ HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) int32_t 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_challenge_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_challenge_test.cpp index c7e0273..3a31952 100644 --- 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 @@ -13,7 +13,6 @@ * limitations under the License. */ #include - #include "i_sec_comp_probe.h" #include "location_button.h" #include "location_button_sample_build.h" @@ -28,6 +27,7 @@ #include "sec_comp_log.h" #include "sec_comp_tool.h" #include "sec_comp_ui_register.h" +#include "test_common_param.h" using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; @@ -99,7 +99,7 @@ void SecCompRegisterChallengeTest::TearDown() HWTEST_F(SecCompRegisterChallengeTest, RegisterWithoutPreprocess001, TestSize.Level1) { nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = jsonRes.dump(); SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); @@ -109,3 +109,30 @@ HWTEST_F(SecCompRegisterChallengeTest, RegisterWithoutPreprocess001, TestSize.Le ASSERT_EQ(-1, scId); } +/** + * @tc.name: ReportClickWithoutHmac001 + * @tc.desc: test report click event without hmac data + * @tc.type: FUNC + * @tc.require: AR000HO9JM + */ +HWTEST_F(SecCompReportClickWithoutHmacTest, 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); + + 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_OK, 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_without_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_without_callback_test.cpp index 8c835ca..3b2213c 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 @@ -26,6 +26,7 @@ #include "sec_comp_log.h" #include "sec_comp_tool.h" #include "sec_comp_ui_register.h" +#include "test_common_param.h" using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; @@ -84,7 +85,7 @@ void SecCompRegisterWithoutCallbackTest::TearDown() HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback001, TestSize.Level1) { nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = jsonRes.dump(); int32_t scId; @@ -102,7 +103,7 @@ HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback001, TestSiz HWTEST_F(SecCompRegisterWithoutCallbackTest, RegisterWithoutCallback002, TestSize.Level1) { nlohmann::json jsonRes; - LocationButtonSampleBuild::BuildLocationComponentInfo(jsonRes); + TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = jsonRes.dump(); int32_t 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..862d382 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,13 @@ * 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 +66,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/interfaces/inner_api/security_component/test/unittest/src/test_common.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h new file mode 100644 index 0000000..d572400 --- /dev/null +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.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 TEST_COMMON_H +#define 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 = 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 size_t MAX_CALLER_SIZE = 10; +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 // TEST_COMMON_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..4d5b336 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 @@ -15,6 +15,7 @@ #include "app_state_observer_test.h" #include "sec_comp_log.h" #include "sec_comp_err.h" +#include "test_common_param.h" using namespace testing::ext; using namespace OHOS; @@ -23,11 +24,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 +56,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 +74,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 +99,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 +118,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 +142,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 +162,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 +172,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..59f53b5 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 @@ -19,6 +19,7 @@ #include "save_button.h" #include "sec_comp_err.h" #include "sec_comp_tool.h" +#include "test_common_param.h" using namespace testing::ext; using namespace OHOS; @@ -28,9 +29,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() 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..51aeb7a 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,15 @@ */ #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 "test_common_param.h" using namespace testing::ext; using namespace OHOS; @@ -30,18 +31,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; 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..bc2fb89 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 @@ -18,6 +18,7 @@ #include "location_button.h" #include "save_button.h" #include "sec_comp_err.h" +#include "test_common_param.h" using namespace testing::ext; using namespace OHOS; @@ -28,26 +29,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 +39,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; 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..e9926c3 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 @@ -25,6 +25,7 @@ #include "sec_comp_tool.h" #include "sec_comp_enhance_adapter.h" #include "system_ability.h" +#include "test_common_param.h" #include "token_setproc.h" using namespace testing::ext; @@ -35,15 +36,6 @@ 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; static AccessTokenID g_selfTokenId = 0; static std::string BuildSaveComponentInfo() 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..29dc6c7 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 @@ -24,6 +24,7 @@ #include "sec_comp_log.h" #include "sec_comp_tool.h" #include "system_ability.h" +#include "test_common_param.h" #include "token_setproc.h" using namespace testing::ext; @@ -34,17 +35,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() { 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..868d8b2 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 @@ -17,6 +17,7 @@ #include "sec_comp_log.h" #include "sec_comp_err.h" #include "sec_comp_click_event_parcel.h" +#include "test_common_param.h" using namespace testing::ext; using namespace OHOS; diff --git a/services/security_component_service/sa/test/unittest/src/test_common_param.h b/services/security_component_service/sa/test/unittest/src/test_common_param.h new file mode 100644 index 0000000..52d2d21 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/test_common_param.h @@ -0,0 +1,54 @@ +/* + * 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 TEST_COMMON_PARAM_H +#define TEST_COMMON_PARAM_H + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +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; + +static constexpr float TEST_SIZE = 100.0; +static constexpr uint32_t TEST_INVALID_SIZE = 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 ZERO_OFFSET = 0.0F; +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_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; + +static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; +static constexpr int32_t MAX_INT_NUM = 0x7fffffff; +static constexpr uint32_t HAP_TOKEN_ID = 537715419; +static constexpr uint32_t TEST_TOKEN_ID = 1; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // TEST_COMMON_PARAM_H -- Gitee