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_service_mock_test.h"
16
17 #include "accesstoken_kit.h"
18 #include "ipc_skeleton.h"
19 #include "iservice_registry.h"
20 #include "location_button.h"
21 #include "paste_button.h"
22 #include "save_button.h"
23 #include "sec_comp_err.h"
24 #include "sec_comp_log.h"
25 #include "sec_comp_tool.h"
26 #include "sec_comp_enhance_adapter.h"
27 #include "service_test_common.h"
28 #include "system_ability.h"
29 #include "token_setproc.h"
30 #include "window_manager.h"
31
32 using namespace testing::ext;
33 using namespace OHOS;
34 using namespace OHOS::Security::SecurityComponent;
35 using namespace OHOS::Security::AccessToken;
36
37 namespace {
38 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
39 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompServiceMockTest"};
40 static AccessTokenID g_selfTokenId = 0;
41 }
42
SetUpTestCase()43 void SecCompServiceMockTest::SetUpTestCase()
44 {}
45
TearDownTestCase()46 void SecCompServiceMockTest::TearDownTestCase()
47 {}
48
SetUp()49 void SecCompServiceMockTest::SetUp()
50 {
51 SC_LOG_INFO(LABEL, "setup");
52 if (secCompService_ != nullptr) {
53 return;
54 }
55 SecCompService* ptr = new (std::nothrow) SecCompService(ServiceTestCommon::SA_ID, true);
56 secCompService_ = sptr<SecCompService>(ptr);
57 ASSERT_NE(nullptr, secCompService_);
58 secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver();
59 ASSERT_NE(nullptr, secCompService_->appStateObserver_);
60 g_selfTokenId = GetSelfTokenID();
61
62 Rosen::WindowManager::GetInstance().SetDefaultSecCompScene();
63 }
64
TearDown()65 void SecCompServiceMockTest::TearDown()
66 {
67 if (secCompService_ != nullptr) {
68 secCompService_->appStateObserver_ = nullptr;
69 }
70 secCompService_ = nullptr;
71 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
72 }
73
74 /**
75 * @tc.name: RegisterSecurityComponent001
76 * @tc.desc: Test register security component
77 * @tc.type: FUNC
78 * @tc.require: AR000HO9J7
79 */
80 HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1)
81 {
82 // get caller fail
83 int32_t scId;
84 secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
85 secCompService_->Initialize();
86 EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId));
87 nlohmann::json jsonRes;
88 ServiceTestCommon::BuildSaveComponentJson(jsonRes);
89 std::string saveInfo = jsonRes.dump();
90
91 // parse component json fail
92 ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
93 AppExecFwk::AppStateData stateData = {
94 .uid = getuid()
95 };
96 secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
97 // wrong json
98 EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "{a=", scId));
99
100 // register security component ok
101 EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
102 EXPECT_EQ(SC_OK, secCompService_->UpdateSecurityComponent(scId, saveInfo));
103 uint8_t buffer[1] = { 0 };
104 struct SecCompClickEvent touch = {
105 .type = ClickEventType::POINT_EVENT_TYPE,
106 .point.touchX = 100,
107 .point.touchY = 100,
108 .point.timestamp =
109 static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
110 ServiceTestCommon::TIME_CONVERSION_UNIT,
111 .extraInfo = {
112 .data = buffer,
113 .dataSize = 1
114 },
115 };
116 SecCompInfo secCompInfo{ scId, saveInfo, touch };
117 std::string message;
118 EXPECT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(secCompInfo, nullptr, nullptr, message));
119 EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
120 SecCompPermManager::GetInstance().applySaveCountMap_.clear();
121 }
122
123 /**
124 * @tc.name: RegisterSecurityComponent002
125 * @tc.desc: Test register security component check touch info failed
126 * @tc.type: FUNC
127 * @tc.require: AR000HO9J7
128 */
129 HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1)
130 {
131 int32_t scId;
132 secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
133 secCompService_->Initialize();
134 nlohmann::json jsonRes;
135 ServiceTestCommon::BuildSaveComponentJson(jsonRes);
136 std::string saveInfo = jsonRes.dump();
137
138 ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
139 AppExecFwk::AppStateData stateData = {
140 .uid = getuid()
141 };
142 secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
143 // register security component ok
144 EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
145 struct SecCompClickEvent touch = {
146 .type = ClickEventType::POINT_EVENT_TYPE,
147 .point.touchX = 100,
148 .point.touchY = 100,
149 .point.timestamp = static_cast<uint64_t>(
150 std::chrono::high_resolution_clock::now().time_since_epoch().count())
151 };
152 SecCompInfo secCompInfo{ scId, saveInfo, touch };
153 std::string message;
154 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
155 secCompService_->ReportSecurityComponentClickEvent(secCompInfo, nullptr, nullptr, message));
156 EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
157 SecCompPermManager::GetInstance().applySaveCountMap_.clear();
158 }
159
160 /**
161 * @tc.name: RegisterSecurityComponent003
162 * @tc.desc: Test register security component permission grant failed
163 * @tc.type: FUNC
164 * @tc.require: AR000HO9J7
165 */
166 HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1)
167 {
168 int32_t scId;
169 secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
170 secCompService_->Initialize();
171 nlohmann::json jsonRes;
172 ServiceTestCommon::BuildSaveComponentJson(jsonRes);
173 std::string saveInfo = jsonRes.dump();
174
175 ASSERT_EQ(0, SetSelfTokenID(0));
176 AppExecFwk::AppStateData stateData = {
177 .uid = getuid()
178 };
179 secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
180 // register security component ok
181 EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
182 uint8_t buffer[1] = { 0 };
183 struct SecCompClickEvent touch = {
184 .type = ClickEventType::POINT_EVENT_TYPE,
185 .point.touchX = 100,
186 .point.touchY = 100,
187 .point.timestamp =
188 static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
189 ServiceTestCommon::TIME_CONVERSION_UNIT,
190 .extraInfo = {
191 .data = buffer,
192 .dataSize = 1
193 },
194 };
195 SecCompInfo secCompInfo{ scId, saveInfo, touch };
196 std::string message;
197 EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL,
198 secCompService_->ReportSecurityComponentClickEvent(secCompInfo, nullptr, nullptr, message));
199 EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
200 SecCompPermManager::GetInstance().applySaveCountMap_.clear();
201 }
202
203 /**
204 * @tc.name: ReportSecurityComponentClickEvent001
205 * @tc.desc: Test register security component
206 * @tc.type: FUNC
207 * @tc.require: AR000HO9J7
208 */
209 HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
210 {
211 int32_t scId;
212 secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
213 secCompService_->Initialize();
214 nlohmann::json jsonRes;
215 ServiceTestCommon::BuildSaveComponentJson(jsonRes);
216 std::string saveInfo = jsonRes.dump();
217
218 ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
219 AppExecFwk::AppStateData stateData = {
220 .uid = getuid()
221 };
222 secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
223 // register security component ok
224 EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
225 uint8_t buffer[1] = { 0 };
226 struct SecCompClickEvent clickInfo = {
227 .type = ClickEventType::POINT_EVENT_TYPE,
228 .point.touchX = 100,
229 .point.touchY = 100,
230 .point.timestamp =
231 static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
232 ServiceTestCommon::TIME_CONVERSION_UNIT,
233 .extraInfo = {
234 .data = buffer,
235 .dataSize = 1
236 },
237 };
238 SecCompInfo secCompInfo{ scId, saveInfo, clickInfo };
239 std::string message;
240 ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(secCompInfo, nullptr, nullptr, message));
241
242 // test 10s valid
243 ASSERT_TRUE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
244 ASSERT_TRUE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
245 sleep(11);
246 ASSERT_FALSE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
247
248 // test 10s multiple clicks
249 secCompInfo.clickInfo.point.timestamp = static_cast<uint64_t>(
250 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
251 ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(secCompInfo, nullptr, nullptr, message));
252 sleep(3);
253 secCompInfo.clickInfo.point.timestamp = static_cast<uint64_t>(
254 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
255 ASSERT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(secCompInfo, nullptr, nullptr, message));
256 sleep(8);
257 ASSERT_TRUE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
258 sleep(2);
259 EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
260 }
261
262 /**
263 * @tc.name: ReportSecurityComponentClickEvent002
264 * @tc.desc: Test verify location permission
265 * @tc.type: FUNC
266 * @tc.require: AR000HO9IN
267 */
268 HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize.Level1)
269 {
270 SC_LOG_INFO(LABEL, "ReportSecurityComponentClickEvent002");
271 int32_t scId;
272 secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
273 secCompService_->Initialize();
274 nlohmann::json jsonRes;
275 ServiceTestCommon::BuildLocationComponentJson(jsonRes);
276 std::string locationInfo = jsonRes.dump();
277
278 ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
279 AppExecFwk::AppStateData stateData = {
280 .uid = getuid()
281 };
282 secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
283 // register security component ok
284 EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
285 uint8_t buffer[1] = { 0 };
286 struct SecCompClickEvent clickInfo1 = {
287 .type = ClickEventType::POINT_EVENT_TYPE,
288 .point.touchX = 100,
289 .point.touchY = 100,
290 .point.timestamp =
291 static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
292 ServiceTestCommon::TIME_CONVERSION_UNIT,
293 .extraInfo = {
294 .data = buffer,
295 .dataSize = 1
296 },
297 };
298 SecCompInfo secCompInfo{ scId, locationInfo, clickInfo1 };
299 std::string message;
300 ASSERT_EQ(SC_OK,
301 secCompService_->ReportSecurityComponentClickEvent(secCompInfo, nullptr, nullptr, message));
302
303 // test 10s valid
304 ASSERT_EQ(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
305 ASSERT_EQ(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
306 "ohos.permission.APPROXIMATELY_LOCATION"), 0);
307 sleep(11);
308 ASSERT_EQ(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
309 ASSERT_EQ(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
310 "ohos.permission.APPROXIMATELY_LOCATION"), 0);
311
312 SecCompManager::GetInstance().NotifyProcessBackground(getpid());
313 ASSERT_EQ(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
314 ASSERT_EQ(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
315 "ohos.permission.APPROXIMATELY_LOCATION"), 0);
316 sleep(11);
317 ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
318 ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
319 "ohos.permission.APPROXIMATELY_LOCATION"), 0);
320 }
321