diff --git a/BUILD.gn b/BUILD.gn index 58080d8750b729b6f7ab316c7b8a670d0741ef81..1f7cd3c983eae56b15f2f1145ddf5ae3f1da311d 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 866b79a515ce770466828e46393575776d5dfe74..c0647d2d1af3beae7fb765ce5cb9496b492b50b9 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 03d5acf48dd20aabcb847dad2c4de37cc34a8a7d..4acbcce2de99b03ec301837e608eb975f7c4006c 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 a286b4c905db87950d243b7098a67e17d6471cde..117f24ae7594d5767943b3ca2a79f7d552c427f7 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 0aabcdc2703c15c74ddd94b5a1a5d4c06d4d58af..fdba91b8a0e40d27607119904555702266a0bcc3 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 ef7503ae2e55710b103f182aeee5665bc329ebdf..972d4c2b1412d011acc0ffa1102e4ff58cb25c8c 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 625c4acebcd80e1432943020717745b1498cfab3..4faa121ddbfb861e2a014a3a0d1e045e3fb63f36 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 8771c78b7015ac2b04dc478cefe19c7f35b27ec6..257b5cbd452937888674ee06ef3a87519e528d26 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 c43a610b1d2d319ad2966ca12c99be41b0a4f9c0..812892457003e954e627ac840a239ce3b735ef76 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 e90704811ea325c66b05057c6b7e50c60a3d5342..07d339afc1b217c5b1edaa11def23e80e0e6ead8 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 c7e0273d2e504e8419e657370e747579e1456c9b..0000000000000000000000000000000000000000 --- 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 8c835ca8521ca52f5f360f7c0853b9adfc95579e..0000000000000000000000000000000000000000 --- 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 7c59fb21f0f2ea7afe8ae55b58914352a7197e29..0000000000000000000000000000000000000000 --- 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 6cdb68d20c056bf4f20a045acdc622b826481606..067bfe9d74b45da3dd40ac2efb1f4fa9dbbbd36b 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 36bbaaa5505326ec3ccfee16d805f99c58c90a87..34f3a1889460fe1cded0600e57313305e66ed120 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 aec385abdd7ba2caa599f12295268fac6147b6c3..e1bc9e4715a9c359519d661ca135bc3533a735e0 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; } @@ -69,9 +69,10 @@ 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); + SC_LOG_ERROR(LABEL, "HMAC checkout failed," + "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_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index 23f9d1a215a737f964e9995f2eebb6fff32d9771..7c10b7adb4f1cc408c581f1d715f674a9aaaf547 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/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index c492afd7e0ed0413e42133b2d19a2c098979c552..1a66227d3e5ae18a7b49b344e806896fb5532546 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 @@ -24,6 +24,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 +167,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 +200,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::GerCallingTokenID(); + 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 c435876f0855c8ecb10e01ccdff86a2d0a29be8c..f0f2f2f3cbce72b7fd4300c313fc0a6adb3c2468 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;111 }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 04537f303379dd49ce1eca29627896c53a27bb52..84cd54b72a554b9e23184a8fb35bbc40ce848709 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/include/accesstoken_kit.h b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h index f4a2af765841197735074308ecc879e47eb29f5e..0a746c250c2b5fb77119ab5dea9b9bdbe43492dd 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/mock/src/sec_comp_enhance_adapter.cpp b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp index 7c5538ef39a91bf4a393e73ae15ff7bb29591ff5..812f046124c763bdd5fa3ce8e5ca5067188a639d 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 4923cdb83f168b09662c888907b0e539383e142e..946262a2479e9bd150eec392a03d3229491632ae 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 66d2831b294ff79601e4955bae1d60add2e7b11d..2304db67af8abc84618d9c74a1bb048ee0c4be15 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 ac86c7feb30145510cd8e3e4eb4f612393a7ff33..8c6c2511912dd9be09da20879eb91241a13ce046 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 737c9388c8de0eeaee0391a8f6c2674ea034b90d..be5d04bdb01bba6e094f8cd52ca8aaae3eda5a34 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 c430ebec42f25856ef2fe3ac965e0348a7a7af2b..554cceb2a45ee6f8f8400257f31becb793d7c155 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 9bde8927e0d48848b3093570ffddce1dadfe6c5a..f2f7ae7f9050b43fa653262e838805d5e818966b 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 5b6fe8eae2385291d3566907dba00372317d3822..afd77a236806951f718089ef4a5214f413dd7a6e 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 d29720da6719fa09df0278f40855e820dcf77a3a..3693fb9a8fbceb4909c4747c6b10b9f3ed588765 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 0000000000000000000000000000000000000000..1b159a152a11bff6c8ad295f2920bc05f0f58195 --- /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 0000000000000000000000000000000000000000..26368f430a034ecc5c135f0732c60bf95f43a944 --- /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