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: AR000HO9J7
69  */
70 HWTEST_F(SecCompEntityTest, GrantTempPermission001, TestSize.Level1)
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: AR000HO9J7
93  */
94 HWTEST_F(SecCompEntityTest, GrantTempPermission002, TestSize.Level1)
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: AR000HO9J7
110  */
111 HWTEST_F(SecCompEntityTest, CheckClickInfo001, TestSize.Level1)
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     ASSERT_NE(entity_->CheckClickInfo(touch), SC_OK);
120 
121     uint64_t current = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
122     touch.point.timestamp = current + 10000L; // 10s
123     ASSERT_NE(entity_->CheckClickInfo(touch), SC_OK);
124 
125     entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_DIFF_COORDINATE; // click event will not hit this rect
126     entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_DIFF_COORDINATE;
127     entity_->componentInfo_->rect_.width_ = ServiceTestCommon::TEST_DIFF_COORDINATE;
128     entity_->componentInfo_->rect_.height_ = ServiceTestCommon::TEST_DIFF_COORDINATE;
129     touch.point.timestamp = static_cast<uint64_t>(
130         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
131     ASSERT_NE(entity_->CheckClickInfo(touch), SC_OK);
132 
133     entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
134     entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
135     uint8_t buffer[1] = { 0 };
136     touch.extraInfo.dataSize = 1;
137     touch.extraInfo.data = buffer;
138     touch.point.timestamp = static_cast<uint64_t>(
139         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
140     ASSERT_EQ(entity_->CheckClickInfo(touch), SC_SERVICE_ERROR_CLICK_EVENT_INVALID);
141 }
142 
143 /**
144  * @tc.name: CheckClickInfo002
145  * @tc.desc: Test touch info with window check failed
146  * @tc.type: FUNC
147  * @tc.require:
148  */
149 HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level1)
150 {
151     SecCompClickEvent touch = {
152         .type = ClickEventType::POINT_EVENT_TYPE,
153         .point.touchX = ServiceTestCommon::TEST_COORDINATE,
154         .point.touchY = ServiceTestCommon::TEST_COORDINATE,
155         .point.timestamp = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()),
156     };
157     ASSERT_NE(entity_->CheckClickInfo(touch), SC_OK);
158 
159     entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
160     entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
161     entity_->componentInfo_->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
162     entity_->componentInfo_->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
163 
164     // GetAccessibilityWindowInfo failed
165     OHOS::Rosen::WindowManager::GetInstance().result_ = static_cast<OHOS::Rosen::WMError>(-1);
166     ASSERT_EQ(entity_->CheckClickInfo(touch), SC_SERVICE_ERROR_CLICK_EVENT_INVALID);
167 }
168 
169 /**
170  * @tc.name: CompareComponentBasicInfo001
171  * @tc.desc: Test Basic info
172  * @tc.type: FUNC
173  * @tc.require: AR000HO9J7
174  */
175 HWTEST_F(SecCompEntityTest, CompareComponentBasicInfo001, TestSize.Level1)
176 {
177     nlohmann::json jsonComponent;
178     ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
179     SecCompBase* other = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent);
180     bool isRectCheck = true;
181     ASSERT_FALSE(entity_->CompareComponentBasicInfo(other, isRectCheck));
182 }
183 
184 /**
185  * @tc.name: CheckKeyEvent001
186  * @tc.desc: Test CheckKeyEvent
187  * @tc.type: FUNC
188  * @tc.require: AR000HO9J7
189  */
190 HWTEST_F(SecCompEntityTest, CheckKeyEvent001, TestSize.Level1)
191 {
192     SecCompClickEvent clickInfo;
193     clickInfo.type = ClickEventType::KEY_EVENT_TYPE;
194     auto current = static_cast<uint64_t>(
195         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / 1000;
196     clickInfo.key.timestamp = current - 1000000L - 1;
197     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo));
198 
199     clickInfo.key.timestamp = current + 1;
200     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo));
201 
202     clickInfo.key.timestamp = current - 1;
203     clickInfo.key.keyCode = 1;
204     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo));
205 
206     clickInfo.key.keyCode = KEY_SPACE;
207     ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
208     clickInfo.key.keyCode = KEY_ENTER;
209     ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
210     clickInfo.key.keyCode = KEY_NUMPAD_ENTER;
211     ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
212 }
213