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 <gtest/gtest.h>
16 #include "i_sec_comp_probe.h"
17 #include "location_button.h"
18 #include "sec_comp_enhance_adapter.h"
19 #include "sec_comp_enhance_kit.h"
20 #include "sec_comp_err.h"
21 #include "sec_comp_info.h"
22 #include "sec_comp_kit.h"
23 #include "sec_comp_log.h"
24 #include "sec_comp_tool.h"
25 #include "sec_comp_ui_register.h"
26 #include "system_ability_load_callback_stub.h"
27 #include "test_common.h"
28 #include "token_setproc.h"
29
30 using namespace testing::ext;
31 using namespace OHOS::Security::SecurityComponent;
32 using namespace OHOS::Security::AccessToken;
33 using namespace OHOS;
34
35 namespace {
36 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
37 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompRegisterCallbackTest"};
38 static AccessTokenID g_selfTokenId = 0;
39 static int32_t g_selfUid = 0;
40 static uint32_t g_token_sum = 0;
41
42 class MockUiSecCompProbe : public ISecCompProbe {
43 public:
GetComponentInfo(int32_t nodeId,std::string & componentInfo)44 int32_t GetComponentInfo(int32_t nodeId, std::string& componentInfo) override
45 {
46 componentInfo = mockComponentInfo;
47 return mockRes;
48 }
49 std::string mockComponentInfo;
50 int32_t mockRes;
51 };
52
53 static MockUiSecCompProbe g_probe;
54
RegisterSecurityComponent(SecCompType type,std::string & componentInfo,int32_t & scId)55 static __attribute__((noinline)) int32_t RegisterSecurityComponent(
56 SecCompType type, std::string& componentInfo, int32_t& scId)
57 {
58 SC_LOG_INFO(LABEL, "RegisterSecurityComponent enter");
59 return SecCompKit::RegisterSecurityComponent(type, componentInfo, scId);
60 }
61
ReportSecurityComponentClickEvent(SecCompInfo & secCompInfo,sptr<IRemoteObject> callerToken,OnFirstUseDialogCloseFunc dialogCall,std::string & message)62 static __attribute__((noinline)) int32_t ReportSecurityComponentClickEvent(
63 SecCompInfo& secCompInfo, sptr<IRemoteObject> callerToken, OnFirstUseDialogCloseFunc dialogCall,
64 std::string& message)
65 {
66 SC_LOG_INFO(LABEL, "ReportSecurityComponentClickEvent enter");
67 return SecCompKit::ReportSecurityComponentClickEvent(secCompInfo, callerToken,
68 std::move(dialogCall), message);
69 }
70
UpdateSecurityComponent(int32_t scId,std::string & componentInfo)71 static __attribute__((noinline)) int32_t UpdateSecurityComponent(int32_t scId, std::string& componentInfo)
72 {
73 SC_LOG_INFO(LABEL, "UpdateSecurityComponent enter");
74 return SecCompKit::UpdateSecurityComponent(scId, componentInfo);
75 }
76
InitUiRegister()77 static void InitUiRegister()
78 {
79 std::vector<uintptr_t> callerList = {
80 reinterpret_cast<uintptr_t>(RegisterSecurityComponent),
81 reinterpret_cast<uintptr_t>(ReportSecurityComponentClickEvent),
82 reinterpret_cast<uintptr_t>(UpdateSecurityComponent)
83 };
84 SecCompUiRegister registerCallback(callerList, &g_probe);
85 }
86 } // namespace
87
88 class SecCompRegisterCallbackTest : public testing::Test {
89 public:
90 static void SetUpTestCase();
91
92 static void TearDownTestCase();
93
94 void SetUp() override;
95
96 void TearDown() override;
97 };
98
SetUpTestCase()99 void SecCompRegisterCallbackTest::SetUpTestCase()
100 {
101 system("kill -9 `pidof security_component_service`");
102 InitUiRegister();
103 SC_LOG_INFO(LABEL, "SecCompRegisterCallbackTest.");
104 }
105
TearDownTestCase()106 void SecCompRegisterCallbackTest::TearDownTestCase()
107 {
108 SC_LOG_INFO(LABEL, "SecCompRegisterCallbackTest.");
109 }
110
SetUp()111 void SecCompRegisterCallbackTest::SetUp()
112 {
113 g_selfUid = getuid();
114 g_selfTokenId = GetSelfTokenID();
115 SC_LOG_INFO(LABEL, "SetUp ok.");
116 }
117
TearDown()118 void SecCompRegisterCallbackTest::TearDown()
119 {
120 setuid(g_selfUid);
121 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
122 SC_LOG_INFO(LABEL, "TearDown.");
123 }
124
125 /**
126 * @tc.name: RegisterSecurityComponent001
127 * @tc.desc: test register security component success.
128 * @tc.type: FUNC
129 * @tc.require:
130 */
131 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level0)
132 {
133 sleep(1);
134 nlohmann::json jsonRes;
135 TestCommon::BuildLocationComponentInfo(jsonRes);
136 std::string locationInfo = jsonRes.dump();
137 g_probe.mockComponentInfo = locationInfo;
138 g_probe.mockRes = 0;
139
140 int32_t scId;
141 ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
142 ASSERT_NE(-1, scId);
143
144 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
145 }
146
147 /**
148 * @tc.name: RegisterSecurityComponent002
149 * @tc.desc: test register callback failed.
150 * @tc.type: FUNC
151 * @tc.require:
152 */
153 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level0)
154 {
155 nlohmann::json jsonRes;
156 TestCommon::BuildLocationComponentInfo(jsonRes);
157 std::string locationInfo = jsonRes.dump();
158 g_probe.mockComponentInfo = locationInfo;
159 g_probe.mockRes = -1;
160
161 int32_t scId;
162 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
163 ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL,
164 RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
165 ASSERT_EQ(-1, scId);
166 #else
167 ASSERT_EQ(SC_OK,
168 RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
169 ASSERT_NE(-1, scId);
170 ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
171 #endif
172 }
173
174 /**
175 * @tc.name: RegisterSecurityComponent003
176 * @tc.desc: test register in MaliciousAppList.
177 * @tc.type: FUNC
178 * @tc.require:
179 */
180 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level0)
181 {
182 nlohmann::json jsonRes;
183 TestCommon::BuildLocationComponentInfo(jsonRes);
184 std::string locationInfo = jsonRes.dump();
185 g_probe.mockComponentInfo = locationInfo;
186 g_probe.mockRes = 0;
187
188 int32_t scId;
189 ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
190 ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
191 }
192
193 /**
194 * @tc.name: RegisterSecurityComponent004
195 * @tc.desc: Test register security component check touch info failed
196 * @tc.type: FUNC
197 * @tc.require:
198 */
199 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level0)
200 {
201 system("param set sec.comp.enhance 1");
202 nlohmann::json jsonRes;
203 TestCommon::BuildSaveComponentInfo(jsonRes);
204 std::string saveInfo = jsonRes.dump();
205 int32_t scId;
206
207 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
208 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
209 struct SecCompClickEvent clickInfo = {
210 .type = ClickEventType::POINT_EVENT_TYPE,
211 .point.touchX = TestCommon::TEST_COORDINATE,
212 .point.touchY = TestCommon::TEST_COORDINATE,
213 .point.timestamp = static_cast<uint64_t>(
214 std::chrono::high_resolution_clock::now().time_since_epoch().count())
215 };
216 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
217 clickInfo.extraInfo.data = data;
218
219 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
220 ASSERT_NE(callback, nullptr);
221 auto token = callback->AsObject();
__anonac79d3da0202(int32_t) 222 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
223 SecCompInfo secCompInfo{ scId, saveInfo, clickInfo };
224 std::string message;
225 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
226 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
227 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
228 system("param set sec.comp.enhance 0");
229 }
230
231 /**
232 * @tc.name: RegisterSecurityComponent005
233 * @tc.desc: Test register security component permission grant failed
234 * @tc.type: FUNC
235 * @tc.require:
236 */
237 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level0)
238 {
239 system("param set sec.comp.enhance 1");
240 nlohmann::json jsonRes;
241 TestCommon::BuildPasteComponentInfo(jsonRes);
242 std::string pasteInfo = jsonRes.dump();
243 int32_t scId;
244
245 EXPECT_EQ(SC_OK, RegisterSecurityComponent(PASTE_COMPONENT, pasteInfo, scId));
246 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
247 struct SecCompClickEvent clickInfo = {
248 .type = ClickEventType::POINT_EVENT_TYPE,
249 .point.touchX = TestCommon::TEST_COORDINATE,
250 .point.touchY = TestCommon::TEST_COORDINATE,
251 .point.timestamp = static_cast<uint64_t>(
252 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
253 };
254 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
255 clickInfo.extraInfo.data = data;
256 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
257 ASSERT_NE(callback, nullptr);
258 auto token = callback->AsObject();
__anonac79d3da0302(int32_t) 259 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
260 SecCompInfo secCompInfo{ scId, pasteInfo, clickInfo };
261 std::string message;
262 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
263 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
264 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
265 system("param set sec.comp.enhance 0");
266 }
267
268 /**
269 * @tc.name: RegisterSecurityComponent006
270 * @tc.desc: Test register security component permission grant failed caller error
271 * @tc.type: FUNC
272 * @tc.require:
273 */
274 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level0)
275 {
276 nlohmann::json jsonRes;
277 TestCommon::BuildSaveComponentInfo(jsonRes);
278 std::string saveInfo = jsonRes.dump();
279 int32_t scId;
280 setuid(100);
281 EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
282 }
283
284 /**
285 * @tc.name: ReportSecurityComponentClickEvent001
286 * @tc.desc: Test register security component success
287 * @tc.type: FUNC
288 * @tc.require:
289 */
290 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level0)
291 {
292 system("param set sec.comp.enhance 1");
293 nlohmann::json jsonRes;
294 TestCommon::BuildPasteComponentInfo(jsonRes);
295 std::string pasteInfo = jsonRes.dump();
296 int32_t scId;
297
298 EXPECT_EQ(SC_OK, RegisterSecurityComponent(PASTE_COMPONENT, pasteInfo, scId));
299 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
300 struct SecCompClickEvent clickInfo = {
301 .type = ClickEventType::POINT_EVENT_TYPE,
302 .point.touchX = TestCommon::TEST_COORDINATE,
303 .point.touchY = TestCommon::TEST_COORDINATE,
304 .point.timestamp = static_cast<uint64_t>(
305 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
306 };
307 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
308 clickInfo.extraInfo.data = data;
309
310 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
311 ASSERT_NE(callback, nullptr);
312 auto token = callback->AsObject();
__anonac79d3da0402(int32_t) 313 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
314 SecCompInfo secCompInfo{ scId, pasteInfo, clickInfo };
315 std::string message;
316 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
317 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
318 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
319 system("param set sec.comp.enhance 0");
320 }
321
322 /**
323 * @tc.name: ReportSecurityComponentClickEvent002
324 * @tc.desc: Test report security component caller error
325 * @tc.type: FUNC
326 * @tc.require:
327 */
328 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level0)
329 {
330 system("param set sec.comp.enhance 1");
331 nlohmann::json jsonRes;
332 TestCommon::BuildSaveComponentInfo(jsonRes);
333 std::string saveInfo = jsonRes.dump();
334 int32_t scId;
335 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
336 g_token_sum ++;
337
338 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
339 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
340 struct SecCompClickEvent clickInfo = {
341 .type = ClickEventType::POINT_EVENT_TYPE,
342 .point.touchX = TestCommon::TEST_COORDINATE,
343 .point.touchY = TestCommon::TEST_COORDINATE,
344 .point.timestamp = static_cast<uint64_t>(
345 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
346 };
347 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
348 clickInfo.extraInfo.data = data;
349
350 setuid(100);
351 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
352 ASSERT_NE(callback, nullptr);
353 auto token = callback->AsObject();
__anonac79d3da0502(int32_t) 354 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
355 SecCompInfo secCompInfo{ scId, saveInfo, clickInfo };
356 std::string message;
357 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
358 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
359 setuid(g_selfUid);
360 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
361 system("param set sec.comp.enhance 0");
362 }
363
364 /**
365 * @tc.name: ReportSecurityComponentClickEvent003
366 * @tc.desc: Test report security component enhance data is empty
367 * @tc.type: FUNC
368 * @tc.require:
369 */
370 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level0)
371 {
372 nlohmann::json jsonRes;
373 TestCommon::BuildPasteComponentInfo(jsonRes);
374 std::string pasteInfo = jsonRes.dump();
375 int32_t scId;
376 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
377 g_token_sum ++;
378
379 EXPECT_EQ(SC_OK, RegisterSecurityComponent(PASTE_COMPONENT, pasteInfo, scId));
380
381 struct SecCompClickEvent clickInfo = {
382 .type = ClickEventType::POINT_EVENT_TYPE,
383 .point.touchX = TestCommon::TEST_COORDINATE,
384 .point.touchY = TestCommon::TEST_COORDINATE,
385 .point.timestamp = static_cast<uint64_t>(
386 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
387 };
388 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
389 ASSERT_NE(callback, nullptr);
390 auto token = callback->AsObject();
__anonac79d3da0602(int32_t) 391 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
392 SecCompInfo secCompInfo{ scId, pasteInfo, clickInfo };
393 std::string message;
394 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
395 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
396 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
397 #else
398 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
399 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
400 #endif
401 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
402 }
403
404 /**
405 * @tc.name: ReportSecurityComponentClickEvent004
406 * @tc.desc: Test report security component dialog callback is nullptr
407 * @tc.type: FUNC
408 * @tc.require:
409 */
410 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent004, TestSize.Level0)
411 {
412 nlohmann::json jsonRes;
413 TestCommon::BuildSaveComponentInfo(jsonRes);
414 std::string saveInfo = jsonRes.dump();
415 int32_t scId;
416 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
417 g_token_sum ++;
418
419 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
420
421 struct SecCompClickEvent clickInfo = {
422 .type = ClickEventType::POINT_EVENT_TYPE,
423 .point.touchX = TestCommon::TEST_COORDINATE,
424 .point.touchY = TestCommon::TEST_COORDINATE,
425 .point.timestamp = static_cast<uint64_t>(
426 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
427 };
428 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
429 ASSERT_NE(callback, nullptr);
430 auto token = callback->AsObject();
431 OnFirstUseDialogCloseFunc func = nullptr;
432 SecCompInfo secCompInfo{ scId, saveInfo, clickInfo };
433 std::string message;
434 ASSERT_EQ(SC_ENHANCE_ERROR_VALUE_INVALID,
435 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
436 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
437 }
438
439 /**
440 * @tc.name: ReportClickWithoutHmac001
441 * @tc.desc: Test report click event permission denied
442 * @tc.type: FUNC
443 * @tc.require:
444 */
445 HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level0)
446 {
447 system("param set sec.comp.enhance 1");
448 nlohmann::json jsonRes;
449 TestCommon::BuildLocationComponentInfo(jsonRes);
450 std::string locationInfo = jsonRes.dump();
451 g_probe.mockComponentInfo = locationInfo;
452 g_probe.mockRes = 0;
453
454 int32_t scId;
455 ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
456 ASSERT_NE(-1, scId);
457 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
458 struct SecCompClickEvent clickInfo = {
459 .type = ClickEventType::POINT_EVENT_TYPE,
460 .point.touchX = TestCommon::TEST_COORDINATE,
461 .point.touchY = TestCommon::TEST_COORDINATE,
462 .point.timestamp = static_cast<uint64_t>(
463 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
464 };
465 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
466 clickInfo.extraInfo.data = data;
467 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
468 ASSERT_NE(callback, nullptr);
469 auto token = callback->AsObject();
__anonac79d3da0702(int32_t) 470 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
471 SecCompInfo secCompInfo{ scId, locationInfo, clickInfo };
472 std::string message;
473 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
474 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
475 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
476 system("param set sec.comp.enhance 0");
477 }
478
479 /**
480 * @tc.name: VerifySavePermission001
481 * @tc.desc: Test register security component wrong hap
482 * @tc.type: FUNC
483 * @tc.require:
484 */
485 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level0)
486 {
487 system("param set sec.comp.enhance 1");
488 nlohmann::json jsonRes;
489 TestCommon::BuildPasteComponentInfo(jsonRes);
490 std::string pasteInfo = jsonRes.dump();
491 int32_t scId;
492 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
493 g_token_sum ++;
494 ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
495
496 EXPECT_EQ(SC_OK, RegisterSecurityComponent(PASTE_COMPONENT, pasteInfo, scId));
497 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
498 struct SecCompClickEvent clickInfo = {
499 .type = ClickEventType::POINT_EVENT_TYPE,
500 .point.touchX = TestCommon::TEST_COORDINATE,
501 .point.touchY = TestCommon::TEST_COORDINATE,
502 .point.timestamp = static_cast<uint64_t>(
503 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
504 };
505 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
506 clickInfo.extraInfo.data = data;
507 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
508 ASSERT_NE(callback, nullptr);
509 auto token = callback->AsObject();
__anonac79d3da0802(int32_t) 510 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
511 SecCompInfo secCompInfo{ scId, pasteInfo, clickInfo };
512 std::string message;
513 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
514 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
515 setuid(100);
516 ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
517 // mediaLibraryTokenId_ != 0
518 ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
519 setuid(g_selfUid);
520 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
521 system("param set sec.comp.enhance 0");
522 }
523
524 /**
525 * @tc.name: VerifySavePermission002
526 * @tc.desc: Test register security component invalid tokenId
527 * @tc.type: FUNC
528 * @tc.require:
529 */
530 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level0)
531 {
532 system("param set sec.comp.enhance 1");
533 nlohmann::json jsonRes;
534 TestCommon::BuildPasteComponentInfo(jsonRes);
535 std::string pasteInfo = jsonRes.dump();
536 int32_t scId;
537
538 EXPECT_EQ(SC_OK, RegisterSecurityComponent(PASTE_COMPONENT, pasteInfo, scId));
539 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
540 struct SecCompClickEvent clickInfo = {
541 .type = ClickEventType::POINT_EVENT_TYPE,
542 .point.touchX = TestCommon::TEST_COORDINATE,
543 .point.touchY = TestCommon::TEST_COORDINATE,
544 .point.timestamp = static_cast<uint64_t>(
545 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
546 };
547 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
548 clickInfo.extraInfo.data = data;
549 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
550 ASSERT_NE(callback, nullptr);
551 auto token = callback->AsObject();
__anonac79d3da0902(int32_t) 552 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
553 SecCompInfo secCompInfo{ scId, pasteInfo, clickInfo };
554 std::string message;
555 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
556 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
557 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
558 system("param set sec.comp.enhance 0");
559 }
560
561 /**
562 * @tc.name: UnregisterSecurityComponent001
563 * @tc.desc: Test unregister security component caller error
564 * @tc.type: FUNC
565 * @tc.require:
566 */
567 HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level0)
568 {
569 system("param set sec.comp.enhance 1");
570 nlohmann::json jsonRes;
571 TestCommon::BuildPasteComponentInfo(jsonRes);
572 std::string pasteInfo = jsonRes.dump();
573 int32_t scId;
574
575 EXPECT_EQ(SC_OK, RegisterSecurityComponent(PASTE_COMPONENT, pasteInfo, scId));
576 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
577 struct SecCompClickEvent clickInfo = {
578 .type = ClickEventType::POINT_EVENT_TYPE,
579 .point.touchX = TestCommon::TEST_COORDINATE,
580 .point.touchY = TestCommon::TEST_COORDINATE,
581 .point.timestamp = static_cast<uint64_t>(
582 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
583 };
584 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
585 clickInfo.extraInfo.data = data;
586 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
587 ASSERT_NE(callback, nullptr);
588 auto token = callback->AsObject();
__anonac79d3da0a02(int32_t) 589 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
590 SecCompInfo secCompInfo{ scId, pasteInfo, clickInfo };
591 std::string message;
592 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
593 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
594 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
595 system("param set sec.comp.enhance 0");
596 }
597
598 /**
599 * @tc.name: UpdateSecurityComponent001
600 * @tc.desc: Test update security component success
601 * @tc.type: FUNC
602 * @tc.require:
603 */
604 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level0)
605 {
606 nlohmann::json jsonRes;
607 TestCommon::BuildSaveComponentInfo(jsonRes);
608 std::string saveInfo = jsonRes.dump();
609 int32_t scId;
610 ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
611 ASSERT_NE(-1, scId);
612 ASSERT_EQ(SC_OK, UpdateSecurityComponent(scId, saveInfo));
613 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
614 }
615
616 /**
617 * @tc.name: UpdateSecurityComponent002
618 * @tc.desc: Test update security component caller error
619 * @tc.type: FUNC
620 * @tc.require:
621 */
622 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level0)
623 {
624 nlohmann::json jsonRes;
625 TestCommon::BuildSaveComponentInfo(jsonRes);
626 std::string saveInfo = jsonRes.dump();
627 int32_t scId;
628
629 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
630 g_token_sum ++;
631 ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
632 ASSERT_NE(-1, scId);
633 setuid(100);
634 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, UpdateSecurityComponent(scId, saveInfo));
635 setuid(g_selfUid);
636 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
637 }
638
639 /**
640 * @tc.name: CheckCipherPrint001
641 * @tc.desc: Test cipher info print
642 * @tc.type: FUNC
643 * @tc.require:
644 */
645 HWTEST_F(SecCompRegisterCallbackTest, CheckCipherPrint001, TestSize.Level0)
646 {
647 nlohmann::json jsonRes;
648 TestCommon::BuildLocationComponentInfo(jsonRes);
649 std::string locationInfo = jsonRes.dump();
650 g_probe.mockComponentInfo = locationInfo;
651 g_probe.mockRes = 0;
652 int32_t scId;
653
654 system("param set sec.comp.enhance 3");
655 ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
656 ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
657 system("param set sec.comp.enhance -1");
658 }
659