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
16 #include "sec_comp_manager_test.h"
17
18 #include "sec_comp_log.h"
19 #define private public
20 #include "sec_comp_manager.h"
21 #undef private
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "location_button.h"
25 #include "mock_system_ability_proxy.h"
26 #include "save_button.h"
27 #include "sec_comp_err.h"
28 #include "service_test_common.h"
29 #include "system_ability.h"
30
31 using namespace testing::ext;
32 using namespace OHOS;
33 using namespace OHOS::Security::SecurityComponent;
34 using namespace OHOS::Security::AccessToken;
35
36 namespace {
37 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
38 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompManagerTest"};
39
BuildInvalidLocationComponent()40 static LocationButton BuildInvalidLocationComponent()
41 {
42 LocationButton button;
43 button.fontSize_ = ServiceTestCommon::TEST_INVALID_SIZE;
44 button.iconSize_ = ServiceTestCommon::TEST_INVALID_SIZE;
45 button.padding_.top = ServiceTestCommon::TEST_INVALID_SIZE;
46 button.padding_.right = ServiceTestCommon::TEST_INVALID_SIZE;
47 button.padding_.bottom = ServiceTestCommon::TEST_INVALID_SIZE;
48 button.padding_.left = ServiceTestCommon::TEST_INVALID_SIZE;
49 button.textIconSpace_ = ServiceTestCommon::TEST_INVALID_SIZE;
50 button.fontColor_.value = ServiceTestCommon::TEST_COLOR_WHITE;
51 button.iconColor_.value = ServiceTestCommon::TEST_COLOR_WHITE;
52 button.bgColor_.value = ServiceTestCommon::TEST_COLOR_WHITE;
53 button.borderWidth_ = ServiceTestCommon::TEST_INVALID_SIZE;
54 button.type_ = LOCATION_COMPONENT;
55 button.rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
56 button.rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
57 button.rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
58 button.rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
59 button.windowRect_.x_ = ServiceTestCommon::TEST_COORDINATE;
60 button.windowRect_.y_ = ServiceTestCommon::TEST_COORDINATE;
61 button.windowRect_.width_ = ServiceTestCommon::TEST_COORDINATE;
62 button.windowRect_.height_ = ServiceTestCommon::TEST_COORDINATE;
63 button.text_ = UNKNOWN_TEXT;
64 button.icon_ = UNKNOWN_ICON;
65 button.bg_ = SecCompBackground::UNKNOWN_BG;
66 return button;
67 }
68
BuildValidLocationComponent()69 static LocationButton BuildValidLocationComponent()
70 {
71 LocationButton button;
72 button.fontSize_ = ServiceTestCommon::TEST_SIZE;
73 button.iconSize_ = ServiceTestCommon::TEST_SIZE;
74 button.padding_.top = ServiceTestCommon::TEST_SIZE;
75 button.padding_.right = ServiceTestCommon::TEST_SIZE;
76 button.padding_.bottom = ServiceTestCommon::TEST_SIZE;
77 button.padding_.left = ServiceTestCommon::TEST_SIZE;
78 button.textIconSpace_ = ServiceTestCommon::TEST_SIZE;
79 button.fontColor_.value = ServiceTestCommon::TEST_COLOR_YELLOW;
80 button.iconColor_.value = ServiceTestCommon::TEST_COLOR_RED;
81 button.bgColor_.value = ServiceTestCommon::TEST_COLOR_BLUE;
82 button.borderWidth_ = ServiceTestCommon::TEST_SIZE;
83 button.type_ = LOCATION_COMPONENT;
84 button.rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
85 button.rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
86 button.rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
87 button.rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
88 button.windowRect_.x_ = ServiceTestCommon::TEST_COORDINATE;
89 button.windowRect_.y_ = ServiceTestCommon::TEST_COORDINATE;
90 button.windowRect_.width_ = ServiceTestCommon::TEST_COORDINATE;
91 button.windowRect_.height_ = ServiceTestCommon::TEST_COORDINATE;
92 button.text_ = static_cast<int32_t>(LocationDesc::SELECT_LOCATION);
93 button.icon_ = static_cast<int32_t>(LocationIcon::LINE_ICON);
94 button.bg_ = SecCompBackground::CIRCLE;
95 return button;
96 }
97 }
98
SetUpTestCase()99 void SecCompManagerTest::SetUpTestCase()
100 {}
101
TearDownTestCase()102 void SecCompManagerTest::TearDownTestCase()
103 {}
104
SetUp()105 void SecCompManagerTest::SetUp()
106 {
107 SC_LOG_INFO(LABEL, "setup");
108 }
109
TearDown()110 void SecCompManagerTest::TearDown()
111 {
112 SecCompManager::GetInstance().componentMap_.clear();
113 }
114
115
116 /**
117 * @tc.name: CreateScId001
118 * @tc.desc: Test create sc id
119 * @tc.type: FUNC
120 * @tc.require: AR000HO9J7
121 */
122 HWTEST_F(SecCompManagerTest, CreateScId001, TestSize.Level1)
123 {
124 SecCompManager::GetInstance().scIdStart_ = ServiceTestCommon::MAX_INT_NUM;
125 ASSERT_EQ(ServiceTestCommon::SC_ID_START, SecCompManager::GetInstance().CreateScId());
126
127 SecCompManager::GetInstance().scIdStart_ = ServiceTestCommon::SC_ID_START;
128 ASSERT_EQ(ServiceTestCommon::SC_ID_START + 1, SecCompManager::GetInstance().CreateScId());
129 }
130
131 /**
132 * @tc.name: AddSecurityComponentToList001
133 * @tc.desc: Test add security component to list
134 * @tc.type: FUNC
135 * @tc.require: AR000HO9J7
136 */
137 HWTEST_F(SecCompManagerTest, AddSecurityComponentToList001, TestSize.Level1)
138 {
139 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
140 ASSERT_NE(nullptr, compPtr);
141 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
142 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
143 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
144 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
145 std::shared_ptr<SecCompEntity> entity =
146 std::make_shared<SecCompEntity>(
147 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
148
149 ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, 0, entity));
150
151 std::shared_ptr<LocationButton> compPtrNew = std::make_shared<LocationButton>();
152 ASSERT_NE(nullptr, compPtrNew);
153 compPtrNew->rect_.x_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap
154 compPtrNew->rect_.y_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap
155 compPtrNew->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
156 compPtrNew->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
157 std::shared_ptr<SecCompEntity> entityNew =
158 std::make_shared<SecCompEntity>(
159 compPtrNew, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_2, 1, 1);
160 ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(1, 0, entityNew));
161 }
162
163 /**
164 * @tc.name: AddSecurityComponentToList002
165 * @tc.desc: Test add security component to list sa not exit
166 * @tc.type: FUNC
167 * @tc.require: AR000HO9J7
168 */
169 HWTEST_F(SecCompManagerTest, AddSecurityComponentToList002, TestSize.Level1)
170 {
171 bool isSaExit = SecCompManager::GetInstance().isSaExit_;
172 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
173 ASSERT_NE(nullptr, compPtr);
174 std::shared_ptr<SecCompEntity> entity =
175 std::make_shared<SecCompEntity>(
176 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
177
178 ASSERT_NE(SC_SERVICE_ERROR_SERVICE_NOT_EXIST,
179 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
180 SecCompManager::GetInstance().isSaExit_ = isSaExit;
181 }
182
183 /**
184 * @tc.name: DeleteSecurityComponentFromList001
185 * @tc.desc: Test delete security component
186 * @tc.type: FUNC
187 * @tc.require: AR000HO9J7
188 */
189 HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList001, TestSize.Level1)
190 {
191 ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, SecCompManager::GetInstance().DeleteSecurityComponentFromList(
192 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1));
193
194 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
195 ASSERT_NE(nullptr, compPtr);
196 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
197 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
198 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
199 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
200 std::shared_ptr<SecCompEntity> entity =
201 std::make_shared<SecCompEntity>(
202 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
203 ASSERT_EQ(SC_OK,
204 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
205
206 ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST, SecCompManager::GetInstance().DeleteSecurityComponentFromList(
207 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_2));
208
209 ASSERT_EQ(SC_OK, SecCompManager::GetInstance().DeleteSecurityComponentFromList(
210 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1));
211 }
212
213 /**
214 * @tc.name: GetSecurityComponentFromList001
215 * @tc.desc: Test get security component
216 * @tc.type: FUNC
217 * @tc.require: AR000HO9J7
218 */
219 HWTEST_F(SecCompManagerTest, GetSecurityComponentFromList001, TestSize.Level1)
220 {
221 ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(
222 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1));
223
224 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
225 ASSERT_NE(nullptr, compPtr);
226 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
227 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
228 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
229 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
230 std::shared_ptr<SecCompEntity> entity =
231 std::make_shared<SecCompEntity>(
232 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
233 ASSERT_EQ(SC_OK,
234 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
235 ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(
236 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_2));
237 ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(
238 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1));
239 }
240
241 /**
242 * @tc.name: NotifyProcessBackground001
243 * @tc.desc: Test notify process background
244 * @tc.type: FUNC
245 * @tc.require: AR000HO9J7
246 */
247 HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1)
248 {
249 SecCompManager::GetInstance().NotifyProcessBackground(ServiceTestCommon::TEST_PID_1);
250
251 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
252 ASSERT_NE(nullptr, compPtr);
253 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
254 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
255 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
256 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
257 std::shared_ptr<SecCompEntity> entity =
258 std::make_shared<SecCompEntity>(
259 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
260 ASSERT_EQ(SC_OK,
261 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
262 auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(
263 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1);
264 ASSERT_NE(nullptr, component);
265 SecCompManager::GetInstance().NotifyProcessForeground(ServiceTestCommon::TEST_PID_1);
266 ASSERT_TRUE(SecCompManager::GetInstance().IsForegroundCompExist());
267 SecCompManager::GetInstance().NotifyProcessBackground(ServiceTestCommon::TEST_PID_1);
268 ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist());
269 SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1);
270 ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist());
271 }
272
273 /**
274 * @tc.name: NotifyProcessDied001
275 * @tc.desc: Test notify process died
276 * @tc.type: FUNC
277 * @tc.require: AR000HO9J7
278 */
279 HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1)
280 {
281 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
282 ASSERT_NE(nullptr, compPtr);
283 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
284 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
285 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
286 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
287 std::shared_ptr<SecCompEntity> entity =
288 std::make_shared<SecCompEntity>(
289 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
290 ASSERT_EQ(SC_OK,
291 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
292
293 std::shared_ptr<LocationButton> compPtr2 = std::make_shared<LocationButton>();
294 ASSERT_NE(nullptr, compPtr2);
295 compPtr2->rect_.x_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap
296 compPtr2->rect_.y_ = ServiceTestCommon::TEST_COORDINATE * 2; // not overlap
297 compPtr2->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
298 compPtr2->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
299 std::shared_ptr<SecCompEntity> entity2 =
300 std::make_shared<SecCompEntity>(
301 compPtr2, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_2, 1, 1);
302 ASSERT_EQ(SC_OK,
303 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_2, 0, entity2));
304
305 SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_3);
306 ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(
307 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1));
308
309 SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1);
310 ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList(
311 ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1));
312 }
313
314 /**
315 * @tc.name: RegisterSecurityComponent001
316 * @tc.desc: Test register security component
317 * @tc.type: FUNC
318 * @tc.require: AR000HO9J7
319 */
320 HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1)
321 {
322 SecCompCallerInfo caller = {
323 .tokenId = ServiceTestCommon::TEST_TOKEN_ID,
324 .uid = 1,
325 .pid = ServiceTestCommon::TEST_PID_1
326 };
327 int32_t scId;
328 SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1);
329 nlohmann::json jsonInvalid;
330 EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST,
331 SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId));
332 SecCompManager::GetInstance().malicious_.maliciousAppList_.clear();
333
334 LocationButton buttonInvalid = BuildInvalidLocationComponent();
335 buttonInvalid.ToJson(jsonInvalid);
336 EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID,
337 SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId));
338
339 nlohmann::json jsonValid;
340 LocationButton buttonValid = BuildValidLocationComponent();
341 buttonValid.ToJson(jsonValid);
342
343 EXPECT_EQ(SC_OK,
344 SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId));
345 SecCompManager::GetInstance().malicious_.maliciousAppList_.clear();
346 }
347
348 /**
349 * @tc.name: UpdateSecurityComponent001
350 * @tc.desc: Test update security component
351 * @tc.type: FUNC
352 * @tc.require: AR000HO9J7
353 */
354 HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1)
355 {
356 SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1);
357 nlohmann::json jsonValid;
358 LocationButton buttonValid = BuildValidLocationComponent();
359 buttonValid.ToJson(jsonValid);
360 SecCompCallerInfo caller = {
361 .tokenId = ServiceTestCommon::TEST_TOKEN_ID,
362 .uid = 1,
363 .pid = ServiceTestCommon::TEST_PID_1
364 };
365 EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST,
366 SecCompManager::GetInstance().UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller));
367 SecCompManager::GetInstance().malicious_.maliciousAppList_.clear();
368
369 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
370 compPtr->type_ = LOCATION_COMPONENT;
371 std::shared_ptr<SecCompEntity> entity =
372 std::make_shared<SecCompEntity>(
373 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
374 EXPECT_EQ(SC_OK,
375 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
376
377 // wrong json
378 nlohmann::json jsonInvalid;
379 LocationButton buttonInvalid = BuildInvalidLocationComponent();
380 buttonInvalid.ToJson(jsonInvalid);
381 EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID,
382 SecCompManager::GetInstance().UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, jsonInvalid, caller));
383
384 // no enhance data
385 EXPECT_EQ(SC_OK,
386 SecCompManager::GetInstance().UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller));
387 SecCompManager::GetInstance().malicious_.maliciousAppList_.clear();
388 }
389
390 /**
391 * @tc.name: UnregisterSecurityComponent001
392 * @tc.desc: Test unregister security component
393 * @tc.type: FUNC
394 * @tc.require: AR000HO9J7
395 */
396 HWTEST_F(SecCompManagerTest, UnregisterSecurityComponent001, TestSize.Level1)
397 {
398 SecCompCallerInfo caller = {
399 .tokenId = ServiceTestCommon::TEST_TOKEN_ID,
400 .pid = ServiceTestCommon::TEST_PID_1
401 };
402
403 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
404 SecCompManager::GetInstance().UnregisterSecurityComponent(ServiceTestCommon::TEST_INVALID_SC_ID, caller));
405 }
406
407 /**
408 * @tc.name: ReportSecurityComponentClickEvent001
409 * @tc.desc: Test report security component click
410 * @tc.type: FUNC
411 * @tc.require: AR000HO9J7
412 */
413 HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
414 {
415 SecCompCallerInfo caller = {
416 .tokenId = ServiceTestCommon::TEST_TOKEN_ID,
417 .pid = ServiceTestCommon::TEST_PID_1
418 };
419 SecCompClickEvent clickInfo;
420 nlohmann::json jsonVaild;
421 LocationButton buttonValid = BuildValidLocationComponent();
422 buttonValid.ToJson(jsonVaild);
423 std::vector<sptr<IRemoteObject>> remote = { nullptr, nullptr };
424 ASSERT_NE(SC_OK,
425 SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1,
426 jsonVaild, caller, clickInfo, remote));
427 }
428
429 /**
430 * @tc.name: AddSecurityComponentToList004
431 * @tc.desc: Test add security component too many components
432 * @tc.type: FUNC
433 * @tc.require:
434 */
435 HWTEST_F(SecCompManagerTest, AddSecurityComponentToList004, TestSize.Level1)
436 {
437 std::shared_ptr<SecCompManager> managerInstance = std::make_shared<SecCompManager>();
438 managerInstance->isSaExit_ = false;
439 int pid = 1;
440 std::shared_ptr<SecCompEntity> entity =
441 std::make_shared<SecCompEntity>(
442 nullptr, 0, 0, 1, 1);
443 const int MAX_COMPONENT_SIZE = 500;
444 for (int i = 0; i < MAX_COMPONENT_SIZE; i++) {
445 managerInstance->componentMap_[pid].compList.emplace_back(entity);
446 }
447
448 ASSERT_NE(managerInstance->AddSecurityComponentToList(pid, 0, entity), SC_SERVICE_ERROR_VALUE_INVALID);
449 }
450
451 /**
452 * @tc.name: CheckClickSecurityComponentInfo001
453 * @tc.desc: Test check click security component info failed
454 * @tc.type: FUNC
455 * @tc.require: AR000HO9J7
456 */
457 HWTEST_F(SecCompManagerTest, CheckClickSecurityComponentInfo001, TestSize.Level1)
458 {
459 SecCompCallerInfo caller = {
460 .tokenId = ServiceTestCommon::TEST_TOKEN_ID,
461 .pid = ServiceTestCommon::TEST_PID_1
462 };
463 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
464 ASSERT_NE(nullptr, compPtr);
465
466 std::shared_ptr<SecCompEntity> entity =
467 std::make_shared<SecCompEntity>(
468 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
469 ASSERT_EQ(SC_OK,
470 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
471
472 SecCompClickEvent clickInfo;
473 nlohmann::json jsonVaild;
474 LocationButton buttonValid = BuildValidLocationComponent();
475 buttonValid.ToJson(jsonVaild);
476 jsonVaild[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE;
477 std::vector<sptr<IRemoteObject>> remote = { nullptr, nullptr };
478 ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(
479 ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, remote));
480
481 jsonVaild[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT;
482 jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_INVALID_DIMENSION;
483 ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(
484 ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, remote));
485
486 jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_COORDINATE - 1;
487 ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(
488 ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, remote));
489 }
490
491 /**
492 * @tc.name: AddSecurityComponentToList003
493 * @tc.desc: Test add security component to list sa not exit
494 * @tc.type: FUNC
495 * @tc.require: AR000HO9J7
496 */
497 HWTEST_F(SecCompManagerTest, AddSecurityComponentToList003, TestSize.Level1)
498 {
499 SecCompManager::GetInstance().isSaExit_ = true;
500 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
501 std::shared_ptr<SecCompEntity> entity =
502 std::make_shared<SecCompEntity>(
503 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
504 ASSERT_EQ(SC_SERVICE_ERROR_SERVICE_NOT_EXIST,
505 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
506 SecCompManager::GetInstance().isSaExit_ = false;
507 }
508
509 /**
510 * @tc.name: DeleteSecurityComponentFromList002
511 * @tc.desc: Test delete security component
512 * @tc.type: FUNC
513 * @tc.require: AR000HO9J7
514 */
515 HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList002, TestSize.Level1)
516 {
517 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
518 ASSERT_NE(nullptr, compPtr);
519 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
520 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
521 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
522 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
523 std::shared_ptr<SecCompEntity> entity =
524 std::make_shared<SecCompEntity>(
525 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
526 ASSERT_EQ(SC_OK,
527 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
528
529 auto component = SecCompManager::GetInstance().GetSecurityComponentFromList(1, 1);
530 ASSERT_NE(nullptr, component);
531 ASSERT_EQ(SC_OK, SecCompManager::GetInstance().DeleteSecurityComponentFromList(1, 1));
532 }
533
534 /**
535 * @tc.name: UpdateSecurityComponent002
536 * @tc.desc: Test update security component
537 * @tc.type: FUNC
538 * @tc.require: AR000HO9J7
539 */
540 HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1)
541 {
542 nlohmann::json jsonValid;
543 SecCompCallerInfo caller = {
544 .tokenId = ServiceTestCommon::TEST_TOKEN_ID,
545 .pid = ServiceTestCommon::TEST_PID_1
546 };
547 ASSERT_NE(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().UpdateSecurityComponent(
548 ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller));
549
550 SecCompManager::GetInstance().malicious_.AddAppToMaliciousAppList(ServiceTestCommon::TEST_PID_1);
551 LocationButton buttonValid = BuildValidLocationComponent();
552 buttonValid.ToJson(jsonValid);
553 ASSERT_NE(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().UpdateSecurityComponent(
554 ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller));
555 }
556
557 /**
558 * @tc.name: ExitSaProcess001
559 * @tc.desc: Test check ExitSaProcess
560 * @tc.type: FUNC
561 * @tc.require: AR000HO9J7
562 */
563 HWTEST_F(SecCompManagerTest, ExitSaProcess001, TestSize.Level1)
564 {
565 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
566 ASSERT_NE(nullptr, compPtr);
567 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
568 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
569 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
570 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
571 std::shared_ptr<SecCompEntity> entity =
572 std::make_shared<SecCompEntity>(
573 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
574 std::shared_ptr<SecCompManager> instance = std::make_shared<SecCompManager>();
575 ASSERT_EQ(SC_OK,
576 instance->AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
577 instance->ExitSaProcess();
578 EXPECT_FALSE(instance->isSaExit_);
579
580 instance->componentMap_.clear();
581 instance->ExitSaProcess();
582 EXPECT_TRUE(instance->isSaExit_);
583 instance->isSaExit_ = false;
584
585 std::shared_ptr<SystemAbilityManagerClient> saClient = std::make_shared<SystemAbilityManagerClient>();
586 ASSERT_NE(nullptr, saClient);
587 SystemAbilityManagerClient::clientInstance = saClient.get();
588 sptr<SystemAbilityManagerProxy> proxy = new SystemAbilityManagerProxy(nullptr);
589 EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
590 instance->ExitSaProcess();
591 EXPECT_TRUE(instance->isSaExit_);
592 instance->isSaExit_ = false;
593
594 EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
595 EXPECT_CALL(*proxy, UnloadSystemAbility(testing::_)).WillOnce(testing::Return(-1));
596 instance->ExitSaProcess();
597 EXPECT_TRUE(instance->isSaExit_);
598 }
599
600 /**
601 * @tc.name: ExitWhenAppMgrDied001
602 * @tc.desc: Test check ExitWhenAppMgrDied
603 * @tc.type: FUNC
604 * @tc.require: AR000HO9J7
605 */
606 HWTEST_F(SecCompManagerTest, ExitWhenAppMgrDied001, TestSize.Level1)
607 {
608 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
609 ASSERT_NE(nullptr, compPtr);
610 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
611 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
612 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
613 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
614 std::shared_ptr<SecCompEntity> entity =
615 std::make_shared<SecCompEntity>(
616 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
617
618 std::shared_ptr<SystemAbilityManagerClient> saClient = std::make_shared<SystemAbilityManagerClient>();
619 ASSERT_NE(nullptr, saClient);
620 SystemAbilityManagerClient::clientInstance = saClient.get();
621
622 EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr));
623 std::shared_ptr<SecCompManager> instance = std::make_shared<SecCompManager>();
624 EXPECT_EQ(SC_OK, instance->AddSecurityComponentToList(1, 0, entity));
625 instance->ExitWhenAppMgrDied();
626 EXPECT_TRUE(instance->isSaExit_);
627
628 sptr<SystemAbilityManagerProxy> proxy = new SystemAbilityManagerProxy(nullptr);
629 EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
630 instance->ExitWhenAppMgrDied();
631 EXPECT_TRUE(instance->isSaExit_);
632
633 EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
634 instance->ExitWhenAppMgrDied();
635 EXPECT_TRUE(instance->isSaExit_);
636
637 EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
638 EXPECT_CALL(*proxy, UnloadSystemAbility(testing::_)).WillOnce(testing::Return(-1));
639 instance->ExitWhenAppMgrDied();
640 EXPECT_TRUE(instance->isSaExit_);
641 }
642
643 /**
644 * @tc.name: SendCheckInfoEnhanceSysEvent001
645 * @tc.desc: Test check SendCheckInfoEnhanceSysEvent
646 * @tc.type: FUNC
647 * @tc.require: AR000HO9J7
648 */
649 HWTEST_F(SecCompManagerTest, SendCheckInfoEnhanceSysEvent001, TestSize.Level1)
650 {
651 SecCompManager::GetInstance().malicious_.maliciousAppList_.clear();
652 ASSERT_TRUE(SecCompManager::GetInstance().malicious_.IsMaliciousAppListEmpty());
653 int32_t scId = INVALID_SC_ID;
654 const std::string scene = "";
655 int32_t res = SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL;
656 SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res);
657 }
658
659 /**
660 * @tc.name: DumpSecComp001
661 * @tc.desc: Test check DumpSecComp
662 * @tc.type: FUNC
663 * @tc.require: AR000HO9J7
664 */
665 HWTEST_F(SecCompManagerTest, DumpSecComp001, TestSize.Level1)
666 {
667 std::shared_ptr<LocationButton> compPtr = std::make_shared<LocationButton>();
668 ASSERT_NE(nullptr, compPtr);
669 compPtr->rect_.x_ = ServiceTestCommon::TEST_COORDINATE;
670 compPtr->rect_.y_ = ServiceTestCommon::TEST_COORDINATE;
671 compPtr->rect_.width_ = ServiceTestCommon::TEST_COORDINATE;
672 compPtr->rect_.height_ = ServiceTestCommon::TEST_COORDINATE;
673 std::shared_ptr<SecCompEntity> entity =
674 std::make_shared<SecCompEntity>(
675 compPtr, ServiceTestCommon::TEST_TOKEN_ID, ServiceTestCommon::TEST_SC_ID_1, 1, 1);
676 SecCompManager::GetInstance().isSaExit_ = false;
677 ASSERT_EQ(SC_OK,
678 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity));
679 std::string dumpStr;
680 SecCompManager::GetInstance().DumpSecComp(dumpStr);
681 }
682
683 /**
684 * @tc.name: TransformCallBackResult001
685 * @tc.desc: Test check TransformCallBackResult
686 * @tc.type: FUNC
687 * @tc.require: AR000HO9J7
688 */
689 HWTEST_F(SecCompManagerTest, TransformCallBackResult001, TestSize.Level1)
690 {
691 int32_t scId = INVALID_SC_ID;
692 const std::string scene = "REGISTER";
693 int32_t res = SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE;
694 SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res);
695 res = SC_ENHANCE_ERROR_VALUE_INVALID;
696 SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res);
697 res = SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL;
698 SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res);
699 res = SC_SERVICE_ERROR_COMPONENT_INFO_INVALID;
700 SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res);
701 res = SC_ENHANCE_ERROR_CALLBACK_CHECK_FAIL;
702 SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res);
703 res = SC_SERVICE_ERROR_VALUE_INVALID;
704 SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res);
705 }
706
707 /**
708 * @tc.name: AddSecurityComponentProcess001
709 * @tc.desc: Test AddSecurityComponentProcess
710 * @tc.type: FUNC
711 * @tc.require:
712 */
713 HWTEST_F(SecCompManagerTest, AddSecurityComponentProcess001, TestSize.Level1)
714 {
715 bool isSaExit = SecCompManager::GetInstance().isSaExit_;
716 SecCompManager::GetInstance().isSaExit_ = true;
717 SecCompCallerInfo info = {
718 .pid = 0,
719 .tokenId = 0,
720 .uid = 0,
721 };
722 EXPECT_EQ(SC_SERVICE_ERROR_SERVICE_NOT_EXIST,
723 SecCompManager::GetInstance().AddSecurityComponentProcess(info));
724 auto oldmap = SecCompManager::GetInstance().componentMap_;
725 SecCompManager::GetInstance().componentMap_.clear();
726 SecCompManager::GetInstance().isSaExit_ = false;
727 EXPECT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentProcess(info));
728 SecCompManager::GetInstance().componentMap_ = oldmap;
729 SecCompManager::GetInstance().isSaExit_ = isSaExit;
730 }