• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "sec_comp_entity_test.h"
16 
17 #include "sec_comp_log.h"
18 #include "location_button.h"
19 #include "paste_button.h"
20 #include "save_button.h"
21 #include "sec_comp_err.h"
22 #include "sec_comp_tool.h"
23 #include "service_test_common.h"
24 #include "sec_comp_info_helper_test.h"
25 #include "window_manager.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::Security::SecurityComponent;
30 using namespace OHOS::Security::AccessToken;
31 
32 namespace {
33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
34     LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntityTest"};
35 }
36 
SetUpTestCase()37 void SecCompEntityTest::SetUpTestCase()
38 {}
39 
TearDownTestCase()40 void SecCompEntityTest::TearDownTestCase()
41 {}
42 
SetUp()43 void SecCompEntityTest::SetUp()
44 {
45     SC_LOG_INFO(LABEL, "setup");
46     if (entity_ != nullptr) {
47         return;
48     }
49 
50     std::shared_ptr<LocationButton> component = std::make_shared<LocationButton>();
51     ASSERT_NE(nullptr, component);
52 
53     entity_ = std::make_shared<SecCompEntity>(component, 1, 1, 1, 1);
54     ASSERT_NE(nullptr, entity_);
55 
56     Rosen::WindowManager::GetInstance().SetDefaultSecCompScene();
57 }
58 
TearDown()59 void SecCompEntityTest::TearDown()
60 {
61     entity_ = nullptr;
62 }
63 
64 /**
65  * @tc.name: GrantTempPermission001
66  * @tc.desc: Test grant location permission
67  * @tc.type: FUNC
68  * @tc.require:
69  */
70 HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level0)
71 {
72     entity_->isGrant_ = false;
73     entity_->componentInfo_->type_ = UNKNOWN_SC_TYPE;
74     ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, entity_->GrantTempPermission());
75     ASSERT_TRUE(entity_->isGrant_);
76 
77     entity_->isGrant_ = false;
78     entity_->componentInfo_->type_ = LOCATION_COMPONENT;
79     ASSERT_EQ(SC_OK, entity_->GrantTempPermission());
80     ASSERT_TRUE(entity_->isGrant_);
81 
82     entity_->isGrant_ = false;
83     entity_->componentInfo_->type_ = PASTE_COMPONENT;
84     ASSERT_EQ(SC_OK, entity_->GrantTempPermission());
85     ASSERT_TRUE(entity_->isGrant_);
86 }
87 
88 /**
89  * @tc.name: GrantTempPermission002
90  * @tc.desc: Test grant paste permission with invalid tokenId.
91  * @tc.type: FUNC
92  * @tc.require:
93  */
94 HWTEST_F(SecCompEntityTest, GrantTempPermission002, TestSize.Level0)
95 {
96     std::shared_ptr<PasteButton> pasteComponent = std::make_shared<PasteButton>();
97     ASSERT_NE(nullptr, pasteComponent);
98 
99     entity_ = std::make_shared<SecCompEntity>(pasteComponent, 0, 1, 1, 1);
100     ASSERT_NE(nullptr, entity_);
101 
102     ASSERT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, entity_->GrantTempPermission());
103 }
104 
105 /**
106  * @tc.name: CheckClickInfo001
107  * @tc.desc: Test touch info
108  * @tc.type: FUNC
109  * @tc.require:
110  */
111 HWTEST_F(SecCompEntityTest, CheckClickInfo001, TestSize.Level0)
112 {
113     SecCompClickEvent touch = {
114         .type = ClickEventType::POINT_EVENT_TYPE,
115         .point.touchX = ServiceTestCommon::TEST_COORDINATE,
116         .point.touchY = ServiceTestCommon::TEST_COORDINATE,
117         .point.timestamp = 0,
118     };
119     std::string message;
120     ASSERT_NE(entity_->CheckClickInfo(touch, 0, CrossAxisState::STATE_INVALID, message), SC_OK);
121 
122     uint64_t current = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
123     touch.point.timestamp = current + 10000L; // 10s
124     ASSERT_NE(entity_->CheckClickInfo(touch, 0, CrossAxisState::STATE_INVALID, message), SC_OK);
125 
126     entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_DIFF_COORDINATE; // click event will not hit this rect
127     entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_DIFF_COORDINATE;
128     entity_->componentInfo_->rect_.width_ = ServiceTestCommon::TEST_DIFF_COORDINATE;
129     entity_->componentInfo_->rect_.height_ = ServiceTestCommon::TEST_DIFF_COORDINATE;
130     touch.point.timestamp = static_cast<uint64_t>(
131         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
132     ASSERT_NE(entity_->CheckClickInfo(touch, 0, CrossAxisState::STATE_INVALID, message), SC_OK);
133 
134     entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
135     entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
136     uint8_t buffer[1] = { 0 };
137     touch.extraInfo.dataSize = 1;
138     touch.extraInfo.data = buffer;
139     touch.point.timestamp = static_cast<uint64_t>(
140         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
141     ASSERT_EQ(entity_->CheckClickInfo(touch, 0, CrossAxisState::STATE_INVALID, message), SC_OK);
142 
143     OHOS::Rosen::WindowManager::GetInstance().result_ = static_cast<OHOS::Rosen::WMError>(-1);
144     entity_->componentInfo_->type_ = SAVE_COMPONENT;
145     ASSERT_EQ(entity_->CheckClickInfo(touch, 0, CrossAxisState::STATE_INVALID, message), SC_OK);
146 }
147 
148 /**
149  * @tc.name: CheckClickInfo002
150  * @tc.desc: Test touch info with window check failed
151  * @tc.type: FUNC
152  * @tc.require:
153  */
154 HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level0)
155 {
156     SecCompClickEvent touch = {
157         .type = ClickEventType::POINT_EVENT_TYPE,
158         .point.touchX = ServiceTestCommon::TEST_COORDINATE,
159         .point.touchY = ServiceTestCommon::TEST_COORDINATE,
160         .point.timestamp = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()),
161     };
162     std::string message;
163     ASSERT_NE(entity_->CheckClickInfo(touch, 0, CrossAxisState::STATE_INVALID, message), SC_OK);
164 
165     entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
166     entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
167     entity_->componentInfo_->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
168     entity_->componentInfo_->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
169 
170     // GetAccessibilityWindowInfo failed
171     OHOS::Rosen::WindowManager::GetInstance().result_ = static_cast<OHOS::Rosen::WMError>(-1);
172     ASSERT_EQ(entity_->CheckClickInfo(touch, 0, CrossAxisState::STATE_INVALID, message),
173         SC_SERVICE_ERROR_CLICK_EVENT_INVALID);
174 }
175 
176 /**
177  * @tc.name: CompareComponentBasicInfo001
178  * @tc.desc: Test Basic info
179  * @tc.type: FUNC
180  * @tc.require:
181  */
182 HWTEST_F(SecCompEntityTest, CompareComponentBasicInfo001, TestSize.Level0)
183 {
184     nlohmann::json jsonComponent;
185     ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
186     std::string message;
187     SecCompBase* other = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
188     bool isRectCheck = true;
189     ASSERT_FALSE(entity_->CompareComponentBasicInfo(other, isRectCheck));
190 }
191 
192 /**
193  * @tc.name: CheckKeyEvent001
194  * @tc.desc: Test CheckKeyEvent
195  * @tc.type: FUNC
196  * @tc.require:
197  */
198 HWTEST_F(SecCompEntityTest, CheckKeyEvent001, TestSize.Level0)
199 {
200     SecCompClickEvent clickInfo = {};
201     clickInfo.type = ClickEventType::KEY_EVENT_TYPE;
202     auto current = static_cast<uint64_t>(
203         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / 1000;
204     clickInfo.key.timestamp = current - 1000000L - 1;
205     std::string message;
206     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo, 0,
207         CrossAxisState::STATE_INVALID, message));
208 
209     clickInfo.key.timestamp = current + 1;
210     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo, 0,
211         CrossAxisState::STATE_INVALID, message));
212 
213     clickInfo.key.timestamp = current - 1;
214     clickInfo.key.keyCode = 1;
215     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo, 0,
216         CrossAxisState::STATE_INVALID, message));
217 
218     clickInfo.key.keyCode = KEY_SPACE;
219     ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
220     clickInfo.key.keyCode = KEY_ENTER;
221     ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
222     clickInfo.key.keyCode = KEY_NUMPAD_ENTER;
223     ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
224 }
225