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 263e8701aed919ef5291c7cca511b1f5aa5ba8bf..ce832db0930bf45e35d7474056c34243870e4140 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 @@ -127,11 +127,46 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) /** * @tc.name: FromJson005 - * @tc.desc: Test location button from wrong size json + * @tc.desc: Test location button from wrong rect json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) +{ + nlohmann::json jsonComponent; + LocationButton comp; + jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); + jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = wrongJson; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + TestCommon::BuildLocationComponentInfo(jsonComponent); + ASSERT_TRUE(comp.FromJson(jsonComponent)); + + auto& rectJson = jsonComponent[JsonTagConstants::JSON_WINDOW_RECT]; + rectJson[JsonTagConstants::JSON_RECT_X] = WRONG_TYPE; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + rectJson[JsonTagConstants::JSON_RECT_X] = TestCommon::TEST_COORDINATE; + rectJson[JsonTagConstants::JSON_RECT_Y] = WRONG_TYPE; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE; + rectJson[JsonTagConstants::JSON_RECT_WIDTH] = WRONG_TYPE; + ASSERT_FALSE(comp.FromJson(jsonComponent)); + + rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE; + rectJson[JsonTagConstants::JSON_RECT_HEIGHT] = WRONG_TYPE; + ASSERT_FALSE(comp.FromJson(jsonComponent)); +} + +/** + * @tc.name: FromJson006 + * @tc.desc: Test location button from wrong size json + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -152,12 +187,12 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) } /** - * @tc.name: FromJson006 + * @tc.name: FromJson007 * @tc.desc: Test location button from wrong size json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -175,12 +210,12 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) } /** - * @tc.name: FromJson007 + * @tc.name: FromJson008 * @tc.desc: Test location button from wrong size json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -198,12 +233,12 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) } /** - * @tc.name: FromJson008 + * @tc.name: FromJson009 * @tc.desc: Test location button from wrong border and parent json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { nlohmann::json jsonComponent; LocationButton comp; @@ -223,12 +258,12 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) } /** - * @tc.name: FromJson009 + * @tc.name: FromJson010 * @tc.desc: Test location button from wrong type params json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) { nlohmann::json jsonComponent; TestCommon::BuildLocationComponentInfo(jsonComponent); @@ -249,12 +284,12 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) } /** - * @tc.name: FromJson010 + * @tc.name: FromJson011 * @tc.desc: Test location button from wrong type params json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { nlohmann::json jsonComponent; TestCommon::BuildLocationComponentInfo(jsonComponent); @@ -275,12 +310,12 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) } /** - * @tc.name: FromJson011 + * @tc.name: FromJson012 * @tc.desc: Test location button from wrong value params json * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) +HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) { nlohmann::json jsonComponent; TestCommon::BuildLocationComponentInfo(jsonComponent); @@ -441,3 +476,20 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } + +/** + * @tc.name: CompareLocationButton003 + * @tc.desc: Test LocationButton compare + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(LocationButtonTest, CompareLocationButton003, TestSize.Level1) +{ + LocationButton button1; + SaveButton button2; + nlohmann::json jsonComponent; + TestCommon::BuildLocationComponentInfo(jsonComponent); + + ASSERT_TRUE(button1.FromJson(jsonComponent)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, false)); +} 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 35cb14fc466dad174fbd681fadc89aed25b8a6cc..d79349ddb51e352852008fe32485db266f0a9f43 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 @@ -146,3 +146,20 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } + +/** + * @tc.name: ComparePasteButton003 + * @tc.desc: Test PasteButton compare + * @tc.type: FUNC + * @tc.require: AR000HO9JB + */ +HWTEST_F(PasteButtonTest, ComparePasteButton003, TestSize.Level1) +{ + PasteButton button1; + SaveButton button2; + nlohmann::json jsonComponent; + TestCommon::BuildPasteComponentInfo(jsonComponent); + + ASSERT_TRUE(button1.FromJson(jsonComponent)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index 1c57f421fc1c655f275be473bce879ec137b0af5..6c710884bed298b7f7b49fd0b2e02fdc9d0dda24 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 @@ -146,3 +146,20 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); } + +/** + * @tc.name: CompareSaveButton003 + * @tc.desc: Test SaveButton compare + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SaveButtonTest, CompareSaveButton003, TestSize.Level1) +{ + SaveButton button1; + PasteButton button2; + nlohmann::json jsonComponent; + TestCommon::BuildSaveComponentInfo(jsonComponent); + + ASSERT_TRUE(button1.FromJson(jsonComponent)); + ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); +} 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 9aee39d1b9d09ab97f6ccb4eaef339142e276e6a..c549f54a7a7c820b8ff5bd48ff1ac5cfc8864f2d 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -31,10 +31,13 @@ using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterCallbackTest"}; +static AccessTokenID g_selfTokenId = 0; +static int32_t g_selfUid = 0; class MockUiSecCompProbe : public ISecCompProbe { public: @@ -81,11 +84,15 @@ void SecCompRegisterCallbackTest::TearDownTestCase() void SecCompRegisterCallbackTest::SetUp() { + g_selfUid = getuid(); + g_selfTokenId = GetSelfTokenID(); SC_LOG_INFO(LABEL, "SetUp ok."); } void SecCompRegisterCallbackTest::TearDown() { + setuid(g_selfUid); + EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId)); SC_LOG_INFO(LABEL, "TearDown."); } @@ -111,16 +118,17 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterWithoutPreprocess001, TestSize.Lev ASSERT_EQ(SC_OK, SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_NE(-1, scId); + EXPECT_EQ(SC_OK, SecCompClient::GetInstance().UnregisterSecurityComponent(scId)); #endif } /** - * @tc.name: Register001 + * @tc.name: RegisterSecurityComponent001 * @tc.desc: test register security component success. * @tc.type: FUNC * @tc.require: AR000HO9JM */ -HWTEST_F(SecCompRegisterCallbackTest, Register001, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) { nlohmann::json jsonRes; TestCommon::BuildLocationComponentInfo(jsonRes); @@ -136,12 +144,12 @@ HWTEST_F(SecCompRegisterCallbackTest, Register001, TestSize.Level1) } /** - * @tc.name: Register002 + * @tc.name: RegisterSecurityComponent002 * @tc.desc: test register callback failed. * @tc.type: FUNC * @tc.require: AR000HO9JM */ -HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) { nlohmann::json jsonRes; TestCommon::BuildLocationComponentInfo(jsonRes); @@ -158,16 +166,17 @@ HWTEST_F(SecCompRegisterCallbackTest, Register002, TestSize.Level1) ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); ASSERT_NE(-1, scId); + ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); #endif } /** - * @tc.name: Register003 + * @tc.name: RegisterSecurityComponent003 * @tc.desc: test register in MaliciousAppList. * @tc.type: FUNC * @tc.require: AR000HO9JM */ -HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1) { nlohmann::json jsonRes; TestCommon::BuildLocationComponentInfo(jsonRes); @@ -177,15 +186,16 @@ HWTEST_F(SecCompRegisterCallbackTest, Register003, TestSize.Level1) int32_t scId; ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId)); + ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } /** - * @tc.name: RegisterSecurityComponent001 + * @tc.name: RegisterSecurityComponent004 * @tc.desc: Test register security component check touch info failed * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1) { system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; @@ -210,12 +220,12 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Lev } /** - * @tc.name: RegisterSecurityComponent002 + * @tc.name: RegisterSecurityComponent005 * @tc.desc: Test register security component permission grant failed * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1) { system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; @@ -239,12 +249,28 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Lev } /** - * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.name: RegisterSecurityComponent006 + * @tc.desc: Test register security component permission grant failed caller error + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + setuid(100); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent001 * @tc.desc: Test register security component success * @tc.type: FUNC * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; @@ -266,24 +292,74 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); uint32_t selfTokenId = GetSelfTokenID(); ASSERT_TRUE(SecCompKit::ReduceAfterVerifySavePermission(selfTokenId)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); } /** - * @tc.name: ExceptCall001 - * @tc.desc: Test update security component success + * @tc.name: ReportSecurityComponentClickEvent002 + * @tc.desc: Test report security component caller error * @tc.type: FUNC - * @tc.require: AR000HO9IN + * @tc.require: AR000HO9J7 */ -HWTEST_F(SecCompRegisterCallbackTest, ExceptCall001, TestSize.Level1) +HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1) { + system("param set sec.comp.enhance 1"); nlohmann::json jsonRes; TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = jsonRes.dump(); int32_t scId; - ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId)); - ASSERT_NE(-1, scId); - ASSERT_EQ(SC_OK, SecCompKit::UpdateSecurityComponent(scId, saveInfo)); + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + + setuid(100); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: ReportSecurityComponentClickEvent003 + * @tc.desc: Test report security component data is empty + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; +#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE + ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); +#else + ASSERT_EQ(SC_OK, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); +#endif + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } /** @@ -315,5 +391,145 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 touch.extraInfo.data = data; EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); } + +/** + * @tc.name: ReduceAfterVerifySavePermission001 + * @tc.desc: Test register security component wrong hap + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, ReduceAfterVerifySavePermission001, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + + ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + setuid(100); + ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(TestCommon::HAP_TOKEN_ID)); + // mediaLibraryTokenId_ != 0 + ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(TestCommon::HAP_TOKEN_ID)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: ReduceAfterVerifySavePermission002 + * @tc.desc: Test register security component invalid tokenId + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, ReduceAfterVerifySavePermission002, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + + ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(0)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: UnregisterSecurityComponent001 + * @tc.desc: Test unregister security component caller error + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1) +{ + system("param set sec.comp.enhance 1"); + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); + uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; + struct SecCompClickEvent touchInfo = { + .touchX = TestCommon::TEST_COORDINATE, + .touchY = TestCommon::TEST_COORDINATE, + .timestamp = static_cast( + std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT + }; + touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE; + touchInfo.extraInfo.data = data; + EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, nullptr)); + setuid(100); + EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UnregisterSecurityComponent(scId)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); + system("param set sec.comp.enhance 0"); +} + +/** + * @tc.name: UpdateSecurityComponent001 + * @tc.desc: Test update security component success + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId)); + ASSERT_NE(-1, scId); + ASSERT_EQ(SC_OK, SecCompKit::UpdateSecurityComponent(scId, saveInfo)); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); +} + +/** + * @tc.name: UpdateSecurityComponent002 + * @tc.desc: Test update security component caller error + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1) +{ + nlohmann::json jsonRes; + TestCommon::BuildSaveComponentInfo(jsonRes); + std::string saveInfo = jsonRes.dump(); + int32_t scId; + + ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); + ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId)); + ASSERT_NE(-1, scId); + setuid(100); + ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UpdateSecurityComponent(scId, saveInfo)); + setuid(g_selfUid); + EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); +} diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h index f3b97b9bb2ff97eb3fe9656e08bcc9a36d968f31..b42b47ae448fcf9801ad66b04632270e5637fb4d 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.h +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.h @@ -32,6 +32,7 @@ public: static constexpr uint32_t TEST_COLOR_BLUE = 0xff0000ff; static constexpr uint32_t TEST_DIFF_COLOR = 0; static constexpr uint64_t TIME_CONVERSION_UNIT = 1000; + static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr uint32_t MAX_HMAC_SIZE = 64; static constexpr size_t MAX_CALLER_SIZE = 10; diff --git a/services/security_component_service/sa/sa_main/sec_comp_entity.cpp b/services/security_component_service/sa/sa_main/sec_comp_entity.cpp index e1bc9e4715a9c359519d661ca135bc3533a735e0..f1b361c39b51f83817a559456a39d84904321f24 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 @@ -63,7 +63,8 @@ bool SecCompEntity::CheckTouchInfo(const SecCompClickEvent& touchInfo) const } if (!componentInfo_->rect_.IsInRect(touchInfo.touchX, touchInfo.touchY)) { - SC_LOG_ERROR(LABEL, "touch point is not in component rect"); + SC_LOG_ERROR(LABEL, "touch point is not in component rect, %{public}lf, %{public}lf", + touchInfo.touchX, touchInfo.touchY); return false; } 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 b132c4f687de7f637c133f985a375b4697797dee..df46a148af34bb6465aefdf6756529e55af7b08d 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -153,6 +153,26 @@ HWTEST_F(AppStateObserverTest, OnProcessStateChanged001, TestSize.Level1) ASSERT_FALSE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } +/** + * @tc.name: OnProcessStateChanged002 + * @tc.desc: Test add process to foregroundset twice + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(AppStateObserverTest, OnProcessStateChanged002, TestSize.Level1) +{ + AppExecFwk::ProcessData processData; + processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; + processData.pid = ServiceTestCommon::TEST_PID_1; + observer_->OnProcessStateChanged(processData); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); + + processData.state = AppExecFwk::AppProcessState::APP_STATE_FOREGROUND; + processData.pid = ServiceTestCommon::TEST_PID_1; + observer_->OnProcessStateChanged(processData); + ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); +} + /** * @tc.name: OnProcessDied001 * @tc.desc: Test recieve process state died diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 64dc4d73c5883a55e1c3d845698624ff0f5b5e0c..19b3e534238bfa9c9d8693fb50fc1f50cf38bdd8 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 @@ -149,20 +149,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1) rect.x_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_testWidth; + rect.y_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_testHeight; + rect.x_ = g_curScreenWidth + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_testWidth; + rect.y_ = g_curScreenHeight + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_testHeight; + rect.width_ = g_curScreenWidth; - ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); rect.height_ = g_curScreenHeight; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.width_ = g_testWidth; + rect.height_ = g_testHeight; + rect.x_ = g_curScreenWidth - g_testWidth; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.x_ = g_testWidth; rect.y_ = g_curScreenHeight - g_testHeight; + ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + rect.y_ = g_testHeight; } /** @@ -179,16 +192,27 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) windowRect.x_ = g_testWidth + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.x_ = g_testWidth; + windowRect.y_ = g_testHeight + 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.y_ = g_testHeight; + windowRect.width_ = g_testWidth - 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.width_ = g_testWidth; + windowRect.height_ = g_testHeight - 1; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.height_ = g_testHeight; + windowRect.width_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.width_ = g_testWidth; + windowRect.height_ = ServiceTestCommon::TEST_INVALID_DIMENSION; ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect)); + windowRect.height_ = g_testHeight; } /** @@ -436,6 +460,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) } +/** + * @tc.name: ParseComponent015 + * @tc.desc: Test parse component info with similar color + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) +{ + nlohmann::json jsonComponent; + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_TRUE(comp->GetValid()); + + jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { + { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, + { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, + { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE } + }; + jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { + { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW }, + { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, + { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } + }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + ASSERT_FALSE(comp->GetValid()); +} + /** * @tc.name: CheckComponentValid001 * @tc.desc: Test CheckComponentValid with invalid color @@ -448,11 +499,13 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); + ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_WHITE } }; + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); } @@ -495,3 +548,14 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) comp->type_ = SecCompType::MAX_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); } + +/** + * @tc.name: RevokeTempPermission001 + * @tc.desc: Test revoke temp permission componentInfo is null + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompInfoHelperTest, RevokeTempPermission001, TestSize.Level1) +{ + ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, SecCompInfoHelper::RevokeTempPermission(0, nullptr)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 4e9e5f61f3a207b2ead9683aca6826bdf7c641cb..a52e23b861e563b39163661245ce00222d8b1ccd 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -16,6 +16,9 @@ #include "sec_comp_manager_test.h" #include "sec_comp_log.h" +#define private public +#include "sec_comp_manager.h" +#undef private #include "location_button.h" #include "save_button.h" #include "sec_comp_err.h" @@ -149,6 +152,24 @@ HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1) ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, entityNew)); } +/** + * @tc.name: AddSecurityComponentToList002 + * @tc.desc: Test add security component to list sa not exit + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompManagerTest, AddSecurityComponentToList002, TestSize.Level1) +{ + bool isSaExit = SecCompManager::GetInstance().isSaExit_; + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(nullptr, compPtr); + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + + ASSERT_NE(SC_SERVICE_ERROR_SERVICE_NOT_EXIST, + SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); + SecCompManager::GetInstance().isSaExit_ = isSaExit; +} + /** * @tc.name: DeleteSecurityComponentFromList001 * @tc.desc: Test delete security component @@ -224,9 +245,10 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) ASSERT_NE(nullptr, component); SecCompManager::GetInstance().NotifyProcessForeground(ServiceTestCommon::TEST_PID_1); ASSERT_TRUE(SecCompManager::GetInstance().IsForegroundCompExist()); - SecCompManager::GetInstance().NotifyProcessBackground(ServiceTestCommon::TEST_PID_1); ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); + SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1); + ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); } /** @@ -353,3 +375,39 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve ASSERT_NE(SC_OK, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo, nullptr)); } + +/** + * @tc.name: CheckClickSecurityComponentInfo001 + * @tc.desc: Test check click security component info failed + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompManagerTest, CheckClickSecurityComponentInfo001, TestSize.Level1) +{ + SecCompCallerInfo caller = { + .tokenId = ServiceTestCommon::TEST_TOKEN_ID, + .pid = ServiceTestCommon::TEST_PID_1 + }; + std::shared_ptr compPtr = std::make_shared(); + ASSERT_NE(nullptr, compPtr); + + SecCompEntity entity(compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1); + ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, entity)); + + SecCompClickEvent touchInfo; + nlohmann::json jsonVaild; + LocationButton buttonValid = BuildValidLocationComponent(); + buttonValid.ToJson(jsonVaild); + jsonVaild[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( + ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, touchInfo, nullptr)); + + jsonVaild[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; + jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( + ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, touchInfo, nullptr)); + + jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_COORDINATE - 1; + ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( + ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, touchInfo, nullptr)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 8efe7c47fc0b92d76ae2a6115d317eeddd8e3dd0..9e49992b46c067b749ed481228be3447bf76b6de 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -275,6 +275,9 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve .extraInfo.dataSize = 16, }; #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE + EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, + secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); + // already in malicious list EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, secCompService_->UnregisterSecurityComponent(scId)); @@ -285,3 +288,46 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve #endif setuid(uid); } + +/** + * @tc.name: Dump001 + * @tc.desc: Test dump + * @tc.type: FUNC + * @tc.require: AR000HO9J7 + */ +HWTEST_F(SecCompServiceTest, Dump001, TestSize.Level1) +{ + int fd = -1; + std::vector args; + + ASSERT_EQ(ERR_INVALID_VALUE, secCompService_->Dump(fd, args)); + + fd = 0; + + // hidumper + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + // hidumper -h + args.emplace_back(Str8ToStr16("-h")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -p + args.emplace_back(Str8ToStr16("-p")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -a + args.emplace_back(Str8ToStr16("-a")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -"" + args.emplace_back(Str8ToStr16("")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); + + args.clear(); + // hidumper -t + args.emplace_back(Str8ToStr16("-t")); + ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args)); +} diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.h b/services/security_component_service/sa/test/unittest/src/service_test_common.h index 078189f9b7d314e3569566e5d3765f796d4cd6eb..64989b23ffd1504663b02b4b5919f37ba0eb14b1 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.h +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.h @@ -44,7 +44,7 @@ public: static constexpr uint32_t QUARTER = 4; static constexpr uint32_t TEST_COLOR_INVALID = 0x66000000; - static constexpr uint32_t TEST_COLOR_BLACK = 0x00000000; + static constexpr uint32_t TEST_COLOR_BLACK = 0xff000000; static constexpr uint32_t TEST_COLOR_WHITE = 0xffffffff; static constexpr uint32_t TEST_COLOR_YELLOW = 0xff7fff00; static constexpr uint32_t TEST_COLOR_RED = 0xffff0000;