1 /*
2 * Copyright (c) 2023 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);
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);
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 touch.point.timestamp = static_cast<uint64_t>(
136 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
137 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
138 ASSERT_NE(entity_->CheckClickInfo(touch), SC_OK);
139 #else
140 ASSERT_EQ(entity_->CheckClickInfo(touch), SC_OK);
141 #endif
142 }
143
144 /**
145 * @tc.name: CheckClickInfo002
146 * @tc.desc: Test touch info with window check failed
147 * @tc.type: FUNC
148 * @tc.require:
149 */
150 HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level1)
151 {
152 SecCompClickEvent touch = {
153 .type = ClickEventType::POINT_EVENT_TYPE,
154 .point.touchX = ServiceTestCommon::TEST_COORDINATE,
155 .point.touchY = ServiceTestCommon::TEST_COORDINATE,
156 .point.timestamp = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()),
157 };
158 ASSERT_NE(entity_->CheckClickInfo(touch), SC_OK);
159
160 entity_->componentInfo_->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
161 entity_->componentInfo_->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
162 entity_->componentInfo_->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
163 entity_->componentInfo_->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
164
165 // GetAccessibilityWindowInfo failed
166 OHOS::Rosen::WindowManager::GetInstance().result_ = static_cast<OHOS::Rosen::WMError>(-1);
167 ASSERT_EQ(entity_->CheckClickInfo(touch), SC_SERVICE_ERROR_CLICK_EVENT_INVALID);
168 }
169
170 /**
171 * @tc.name: CompareComponentBasicInfo001
172 * @tc.desc: Test Basic info
173 * @tc.type: FUNC
174 * @tc.require: AR000HO9J7
175 */
176 HWTEST_F(SecCompEntityTest, CompareComponentBasicInfo001, TestSize.Level1)
177 {
178 nlohmann::json jsonComponent;
179 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
180 SecCompBase* other = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent);
181 bool isRectCheck = true;
182 ASSERT_FALSE(entity_->CompareComponentBasicInfo(other, isRectCheck));
183 }
184
185 /**
186 * @tc.name: CheckKeyEvent001
187 * @tc.desc: Test CheckKeyEvent
188 * @tc.type: FUNC
189 * @tc.require: AR000HO9J7
190 */
191 HWTEST_F(SecCompEntityTest, CheckKeyEvent001, TestSize.Level1)
192 {
193 SecCompClickEvent clickInfo;
194 clickInfo.type = ClickEventType::KEY_EVENT_TYPE;
195 auto current = static_cast<uint64_t>(
196 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / 1000;
197 clickInfo.key.timestamp = current - 1000000L - 1;
198 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo));
199
200 clickInfo.key.timestamp = current + 1;
201 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo));
202
203 clickInfo.key.timestamp = current - 1;
204 clickInfo.key.keyCode = 1;
205 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, entity_->CheckClickInfo(clickInfo));
206
207 clickInfo.key.keyCode = KEY_SPACE;
208 ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
209 clickInfo.key.keyCode = KEY_ENTER;
210 ASSERT_EQ(SC_OK, entity_->CheckKeyEvent(clickInfo));
211 }
212