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 96019d0..6782c7f 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 @@ -252,7 +252,8 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Lev auto proxy = SecCompClient::GetInstance().GetProxy(true); ASSERT_NE(proxy, nullptr); auto token = proxy->AsObject(); - EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); + EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); } @@ -302,9 +303,10 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, Test auto proxy = SecCompClient::GetInstance().GetProxy(true); ASSERT_NE(proxy, nullptr); auto token = proxy->AsObject(); - ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); + ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); uint32_t selfTokenId = GetSelfTokenID(); - ASSERT_TRUE(SecCompKit::VerifySavePermission(selfTokenId)); + ASSERT_FALSE(SecCompKit::VerifySavePermission(selfTokenId)); EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); } @@ -373,13 +375,8 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test auto proxy = SecCompClient::GetInstance().GetProxy(true); ASSERT_NE(proxy, nullptr); auto token = proxy->AsObject(); -#ifdef SECURITY_COMPONENT_ENHANCE_ENABLE ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); -#else - ASSERT_EQ(SC_OK, - SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); -#endif EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); } @@ -414,7 +411,7 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 auto proxy = SecCompClient::GetInstance().GetProxy(true); ASSERT_NE(proxy, nullptr); auto token = proxy->AsObject(); - EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, + EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, clickInfo, token)); EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); @@ -449,7 +446,8 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) auto proxy = SecCompClient::GetInstance().GetProxy(true); ASSERT_NE(proxy, nullptr); auto token = proxy->AsObject(); - ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); + ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); setuid(100); ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID)); // mediaLibraryTokenId_ != 0 @@ -487,7 +485,8 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) auto proxy = SecCompClient::GetInstance().GetProxy(true); ASSERT_NE(proxy, nullptr); auto token = proxy->AsObject(); - ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); + ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); ASSERT_FALSE(SecCompKit::VerifySavePermission(0)); EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId)); system("param set sec.comp.enhance 0"); @@ -521,7 +520,8 @@ HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.L auto proxy = SecCompClient::GetInstance().GetProxy(true); ASSERT_NE(proxy, nullptr); auto token = proxy->AsObject(); - EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); + EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, + SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token)); setuid(100); EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UnregisterSecurityComponent(scId)); setuid(g_selfUid); diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 804cbca..a906600 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -62,6 +62,7 @@ ohos_shared_library("security_component_service") { "sa_main/sec_comp_service.cpp", "sa_main/sec_comp_stub.cpp", "sa_main/sec_event_handler.cpp", + "sa_main/window_info_helper.cpp", ] cflags_cc = [ "-DHILOG_ENABLE" ] 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 f1a3f9b..98dc023 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 @@ -21,6 +21,7 @@ #include "sec_comp_enhance_adapter.h" #include "sec_comp_info_helper.h" #include "sec_comp_log.h" +#include "window_info_helper.h" namespace OHOS { namespace Security { @@ -57,6 +58,7 @@ int32_t SecCompEntity::CheckPointEvent(const SecCompClickEvent& clickInfo) const clickInfo.point.touchX, clickInfo.point.touchY); return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; } + return SC_OK; } @@ -79,6 +81,12 @@ int32_t SecCompEntity::CheckKeyEvent(const SecCompClickEvent& clickInfo) const int32_t SecCompEntity::CheckClickInfo(const SecCompClickEvent& clickInfo) const { + if (!WindowInfoHelper::CheckOtherWindowCoverComp(componentInfo_->windowId_, + componentInfo_->rect_)) { + SC_LOG_ERROR(LABEL, "Component may be covered by other window"); + return SC_SERVICE_ERROR_CLICK_EVENT_INVALID; + } + int32_t res = SC_SERVICE_ERROR_CLICK_EVENT_INVALID; if (clickInfo.type == ClickEventType::POINT_EVENT_TYPE) { res = CheckPointEvent(clickInfo); diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 4cdd330..f9a4b3c 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -24,7 +24,7 @@ #include "sec_comp_log.h" #include "sec_comp_service.h" #include "sec_comp_tool.h" -#include "window_manager.h" +#include "window_info_helper.h" namespace OHOS { namespace Security { @@ -33,29 +33,9 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelper"}; static constexpr double MAX_RECT_PERCENT = 0.1F; // 10% static constexpr double ZERO_OFFSET = 0.0F; -static constexpr float FULL_SCREEN_SCALE = 1.0F; static std::mutex g_renderLock; } -float SecCompInfoHelper::GetWindowScale(int32_t windowId) -{ - float scale = FULL_SCREEN_SCALE; - std::vector> infos; - if (Rosen::WindowManager::GetInstance().GetAccessibilityWindowInfo(infos) != Rosen::WMError::WM_OK) { - SC_LOG_ERROR(LABEL, "Get AccessibilityWindowInfo failed"); - return scale; - } - auto iter = std::find_if(infos.begin(), infos.end(), [windowId](const sptr info) { - return windowId == info->wid_; - }); - if (iter == infos.end()) { - return scale; - } - scale = (*iter)->scaleVal_; - SC_LOG_INFO(LABEL, "Get scale %{public}f", scale); - return scale; -} - void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale) { comp->rect_.width_ *= scale; @@ -222,8 +202,8 @@ bool SecCompInfoHelper::CheckComponentValid(SecCompBase* comp) return false; } - float scale = GetWindowScale(comp->windowId_); - if (!IsEqual(scale, FULL_SCREEN_SCALE) && !IsEqual(scale, 0.0)) { + float scale = WindowInfoHelper::GetWindowScale(comp->windowId_); + if (!IsEqual(scale, WindowInfoHelper::FULL_SCREEN_SCALE) && !IsEqual(scale, 0.0)) { AdjustSecCompRect(comp, scale); } diff --git a/services/security_component_service/sa/sa_main/window_info_helper.cpp b/services/security_component_service/sa/sa_main/window_info_helper.cpp new file mode 100644 index 0000000..d1c3862 --- /dev/null +++ b/services/security_component_service/sa/sa_main/window_info_helper.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "window_info_helper.h" + +#include +#include "sec_comp_log.h" +#include "window_manager.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "WindowInfoHelper"}; +constexpr int32_t INVALID_WINDOW_LAYER = -1; +} + +float WindowInfoHelper::GetWindowScale(int32_t windowId) +{ + float scale = FULL_SCREEN_SCALE; + std::vector> infos; + if (Rosen::WindowManager::GetInstance().GetAccessibilityWindowInfo(infos) != Rosen::WMError::WM_OK) { + SC_LOG_ERROR(LABEL, "Get AccessibilityWindowInfo failed"); + return scale; + } + auto iter = std::find_if(infos.begin(), infos.end(), [windowId](const sptr info) { + return windowId == info->wid_; + }); + if (iter == infos.end()) { + return scale; + } + scale = (*iter)->scaleVal_; + SC_LOG_INFO(LABEL, "Get scale %{public}f", scale); + return scale; +} + +static bool IsRectInWindRect(Rosen::Rect& windRect, const SecCompRect& secRect) +{ + // left or right + if ((secRect.x_ + secRect.width_ < windRect.posX_) || (secRect.x_ > windRect.posX_ + windRect.width_)) { + return false; + } + // top or bottom + if ((secRect.y_ + secRect.height_ < windRect.posY_) || (secRect.y_ > windRect.posY_ + windRect.height_)) { + return false; + } + + return true; +} + +bool WindowInfoHelper::CheckOtherWindowCoverComp(int32_t compWinId, const SecCompRect& secRect) +{ + std::vector> infos; + if (Rosen::WindowManager::GetInstance().GetAccessibilityWindowInfo(infos) != Rosen::WMError::WM_OK) { + SC_LOG_ERROR(LABEL, "Get AccessibilityWindowInfo failed"); + return false; + } + + int32_t compLayer = INVALID_WINDOW_LAYER; + std::vector layerList; + for (auto& info : infos) { + if (info == nullptr) { + continue; + } + + if (info->wid_ == compWinId) { + compLayer = info->layer_; + continue; + } + if (info->scaleVal_ != 0.0) { + info->windowRect_.width_ *= info->scaleVal_; + info->windowRect_.height_ *= info->scaleVal_; + } + if (IsRectInWindRect(info->windowRect_, secRect)) { + layerList.emplace_back(info->layer_); + } + } + + if (compLayer == INVALID_WINDOW_LAYER) { + SC_LOG_ERROR(LABEL, "windId %{public}d component layer is wrong", compWinId); + return false; + } + + if (layerList.size() == static_cast(0)) { + return true; + } + + auto iter = std::find_if(layerList.begin(), layerList.end(), [compLayer](const int layer) { + return layer >= compLayer; + }); + if (iter != layerList.end()) { + SC_LOG_ERROR(LABEL, "component window %{public}d is covered, click check failed", compWinId); + return false; + } + return true; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/window_info_helper.h b/services/security_component_service/sa/sa_main/window_info_helper.h new file mode 100644 index 0000000..ad60af2 --- /dev/null +++ b/services/security_component_service/sa/sa_main/window_info_helper.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef I_SECURITY_COMPONENT_WINDOW_INFO_HELPER_H +#define I_SECURITY_COMPONENT_WINDOW_INFO_HELPER_H + +#include +#include "sec_comp_info.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class WindowInfoHelper { +public: + static float GetWindowScale(int32_t windowId); + static bool CheckOtherWindowCoverComp(int32_t compWinId, const SecCompRect& secRect); +public: + static constexpr float FULL_SCREEN_SCALE = 1.0F; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // I_SECURITY_COMPONENT_WINDOW_INFO_HELPER_H diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index ad0e754..d4cf894 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("sec_comp_service_test") { "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/window_info_helper.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", @@ -54,6 +55,7 @@ ohos_unittest("sec_comp_service_test") { "unittest/src/sec_comp_service_test.cpp", "unittest/src/sec_comp_stub_test.cpp", "unittest/src/service_test_common.cpp", + "unittest/src/window_info_helper_test.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] @@ -83,7 +85,6 @@ ohos_unittest("sec_comp_service_test") { "hitrace:hitrace_meter", "ipc:ipc_core", "window_manager:libdm", - "window_manager:libwm", ] } @@ -120,6 +121,7 @@ ohos_unittest("sec_comp_service_mock_test") { "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/window_info_helper.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", @@ -151,7 +153,6 @@ ohos_unittest("sec_comp_service_mock_test") { "hitrace:hitrace_meter", "ipc:ipc_core", "window_manager:libdm", - "window_manager:libwm", ] } diff --git a/services/security_component_service/sa/test/mock/include/window_manager.h b/services/security_component_service/sa/test/mock/include/window_manager.h new file mode 100644 index 0000000..92969a0 --- /dev/null +++ b/services/security_component_service/sa/test/mock/include/window_manager.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SECURITY_COMPONENT_MOCK_WINDOW_MANAGER_H +#define SECURITY_COMPONENT_MOCK_WINDOW_MANAGER_H +#include +#include +#include +#include + +namespace OHOS { +namespace Rosen { +using DisplayId = uint64_t; +using WindowMode = uint32_t; +using WindowType = uint32_t; +enum class WMError : int32_t { + WM_OK = 0, +}; + +struct Rect { + int32_t posX_; + int32_t posY_; + uint32_t width_; + uint32_t height_; +}; + +class AccessibilityWindowInfo : public Parcelable { +public: + AccessibilityWindowInfo() = default; + ~AccessibilityWindowInfo() = default; + virtual bool Marshalling(Parcel& parcel) const override + { + return true; + }; + + int32_t wid_; + int32_t innerWid_; + int32_t uiNodeId_; + Rect windowRect_; + bool focused_ { false }; + bool isDecorEnable_ { false }; + DisplayId displayId_; + uint32_t layer_; + WindowMode mode_; + WindowType type_; + float scaleVal_; +}; + +class WindowManager { +public: + static WindowManager& GetInstance() + { + static WindowManager instance; + return instance; + }; + + WMError GetAccessibilityWindowInfo(std::vector>& list) + { + list = list_; + return result_; + }; + + WindowManager() {}; + + void SetDefaultSecCompScene() + { + result_ = OHOS::Rosen::WMError::WM_OK; + std::vector> list; + sptr compWin = new AccessibilityWindowInfo(); + compWin->wid_ = 0; + compWin->layer_ = 0; + compWin->scaleVal_ = 0.0; + list.emplace_back(compWin); + list_ = list; + }; + + std::vector> list_; + WMError result_ = OHOS::Rosen::WMError::WM_OK; +private: + ~WindowManager() {}; +}; +} // namespace Security +} // namespace OHOS +#endif // SECURITY_COMPONENT_MOCK_WINDOW_MANAGER_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 4809666..d2a8275 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 @@ -22,6 +22,7 @@ #include "sec_comp_tool.h" #include "service_test_common.h" #include "sec_comp_info_helper_test.h" +#include "window_manager.h" using namespace testing::ext; using namespace OHOS; @@ -51,6 +52,8 @@ void SecCompEntityTest::SetUp() entity_ = std::make_shared(component, 1, 1); ASSERT_NE(nullptr, entity_); + + Rosen::WindowManager::GetInstance().SetDefaultSecCompScene(); } void SecCompEntityTest::TearDown() @@ -138,6 +141,32 @@ HWTEST_F(SecCompEntityTest, CheckClickInfo001, TestSize.Level1) #endif } +/** + * @tc.name: CheckClickInfo002 + * @tc.desc: Test touch info with window check failed + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level1) +{ + SecCompClickEvent touch = { + .type = ClickEventType::POINT_EVENT_TYPE, + .point.touchX = ServiceTestCommon::TEST_COORDINATE, + .point.touchY = ServiceTestCommon::TEST_COORDINATE, + .point.timestamp = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count()), + }; + ASSERT_NE(entity_->CheckClickInfo(touch), SC_OK); + + entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_COORDINATE; + entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE; + entity_->componentInfo_->rect_.width_ = ServiceTestCommon::TEST_COORDINATE; + entity_->componentInfo_->rect_.height_ = ServiceTestCommon::TEST_COORDINATE; + + // GetAccessibilityWindowInfo failed + OHOS::Rosen::WindowManager::GetInstance().result_ = static_cast(-1); + ASSERT_EQ(entity_->CheckClickInfo(touch), SC_SERVICE_ERROR_CLICK_EVENT_INVALID); +} + /** * @tc.name: CompareComponentBasicInfo001 * @tc.desc: Test Basic info 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 ff302ba..fba7cd1 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 @@ -24,6 +24,7 @@ #include "sec_comp_log.h" #include "sec_comp_err.h" #include "service_test_common.h" +#include "window_manager.h" using namespace testing::ext; using namespace OHOS; @@ -69,6 +70,7 @@ void SecCompInfoHelperTest::TearDownTestCase() void SecCompInfoHelperTest::SetUp() { SC_LOG_INFO(LABEL, "setup"); + Rosen::WindowManager::GetInstance().SetDefaultSecCompScene(); } void SecCompInfoHelperTest::TearDown() @@ -548,3 +550,48 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) comp->type_ = SecCompType::MAX_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); } + +/** + * @tc.name: CheckComponentValid004 + * @tc.desc: Test CheckComponentValid with window scale changed + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) +{ + nlohmann::json jsonComponent; + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + + Rosen::WindowManager::GetInstance().result_ = Rosen::WMError::WM_OK; + std::vector> list; + sptr compWin = new Rosen::AccessibilityWindowInfo(); + compWin->wid_ = 0; + compWin->layer_ = 1; + compWin->scaleVal_ = 0.99; // change window scale to 0.99 + list.emplace_back(compWin); + + sptr otherWin = new Rosen::AccessibilityWindowInfo(); + otherWin->wid_ = 1; + otherWin->layer_ = 0; + otherWin->scaleVal_ = 0.0; + list.emplace_back(otherWin); + Rosen::WindowManager::GetInstance().list_ = list; + ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); +} + +/** + * @tc.name: CheckComponentValid005 + * @tc.desc: Test CheckComponentValid with get GetWindowScale failed + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, CheckComponentValid005, TestSize.Level1) +{ + nlohmann::json jsonComponent; + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); + + Rosen::WindowManager::GetInstance().result_ = static_cast(-1); + ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); +} 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 e38ee11..c66b058 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 @@ -27,6 +27,7 @@ #include "service_test_common.h" #include "system_ability.h" #include "token_setproc.h" +#include "window_manager.h" using namespace testing::ext; using namespace OHOS; @@ -57,6 +58,8 @@ void SecCompServiceMockTest::SetUp() secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); ASSERT_NE(nullptr, secCompService_->appStateObserver_); g_selfTokenId = GetSelfTokenID(); + + Rosen::WindowManager::GetInstance().SetDefaultSecCompScene(); } void SecCompServiceMockTest::TearDown() 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 3f40e5c..8f3bc96 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 @@ -27,6 +27,7 @@ #include "service_test_common.h" #include "system_ability.h" #include "token_setproc.h" +#include "window_manager.h" using namespace testing::ext; using namespace OHOS; @@ -59,6 +60,8 @@ void SecCompServiceTest::SetUp() secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver(); ASSERT_NE(nullptr, secCompService_->appStateObserver_); g_selfTokenId = GetSelfTokenID(); + + Rosen::WindowManager::GetInstance().SetDefaultSecCompScene(); } void SecCompServiceTest::TearDown() diff --git a/services/security_component_service/sa/test/unittest/src/window_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/window_info_helper_test.cpp new file mode 100644 index 0000000..1e13ac0 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/window_info_helper_test.cpp @@ -0,0 +1,153 @@ +/* + * 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 "location_button.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" +#include "service_test_common.h" +#include "window_info_helper.h" +#include "window_manager.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Rosen; +using namespace OHOS::Security::SecurityComponent; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "WindowInfoHelperTest"}; +} + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class WindowInfoHelperTest : public testing::Test { +public: + static void SetUpTestCase() {}; + + static void TearDownTestCase() {}; + + void SetUp() + { + SC_LOG_INFO(LABEL, "setup"); + }; + + void TearDown() {}; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + +/** + * @tc.name: CheckOtherWindowCoverComp001 + * @tc.desc: Test pinter event cross other window + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(WindowInfoHelperTest, CheckOtherWindowCoverComp001, TestSize.Level1) +{ + WindowManager::GetInstance().result_ = WMError::WM_OK; + std::vector> list; + sptr compWin = new AccessibilityWindowInfo(); + compWin->wid_ = 0; + compWin->layer_ = 1; + compWin->scaleVal_ = 1.0; // window did not scale + list.emplace_back(compWin); + list.emplace_back(nullptr); + + sptr downNotCrossWin = new AccessibilityWindowInfo(); + downNotCrossWin->wid_ = 1; + downNotCrossWin->layer_ = 0; + downNotCrossWin->scaleVal_ = 0.0; + downNotCrossWin->windowRect_ = Rosen::Rect{ 0, 0, 0, 0 }; + list.emplace_back(downNotCrossWin); + + sptr downCrossWin = new AccessibilityWindowInfo(); + downCrossWin->wid_ = 1; + downCrossWin->layer_ = 0; + downCrossWin->scaleVal_ = 0.9; // window is scaled to 90% + downCrossWin->windowRect_ = Rosen::Rect { + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE, + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE + }; + list.emplace_back(downCrossWin); + + sptr upWin = new AccessibilityWindowInfo(); + upWin->wid_ = 1; + upWin->layer_ = 2; // layer larger than comp window + upWin->scaleVal_ = 1.0; // window did not scale + upWin->windowRect_ = Rosen::Rect { + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE, + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE + }; + list.emplace_back(upWin); + WindowManager::GetInstance().list_ = list; + + SecCompRect compRect = { + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE, + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE + }; + + ASSERT_FALSE(WindowInfoHelper::CheckOtherWindowCoverComp(0, compRect)); +} + +/** + * @tc.name: CheckOtherWindowCoverComp002 + * @tc.desc: Test component window is not found in list + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(WindowInfoHelperTest, CheckOtherWindowCoverComp002, TestSize.Level1) +{ + WindowManager::GetInstance().result_ = WMError::WM_OK; + std::vector> list; + sptr compWin = new AccessibilityWindowInfo(); + compWin->wid_ = 1; + compWin->layer_ = 1; + compWin->scaleVal_ = 1.0; + list.emplace_back(compWin); + WindowManager::GetInstance().list_ = list; + + SecCompRect compRect = { + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE, + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE + }; + ASSERT_FALSE(WindowInfoHelper::CheckOtherWindowCoverComp(0, compRect)); +} + +/** + * @tc.name: CheckOtherWindowCoverComp003 + * @tc.desc: Test component window is not covered + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(WindowInfoHelperTest, CheckOtherWindowCoverComp003, TestSize.Level1) +{ + WindowManager::GetInstance().result_ = WMError::WM_OK; + std::vector> list; + sptr compWin = new AccessibilityWindowInfo(); + compWin->wid_ = 0; + compWin->layer_ = 1; + compWin->scaleVal_ = 1.0; + list.emplace_back(compWin); + WindowManager::GetInstance().list_ = list; + + SecCompRect compRect = { + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE, + ServiceTestCommon::TEST_COORDINATE, ServiceTestCommon::TEST_COORDINATE + }; + ASSERT_TRUE(WindowInfoHelper::CheckOtherWindowCoverComp(0, compRect)); +}