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 <gtest/gtest.h>
16 #include "i_sec_comp_probe.h"
17 #include "location_button.h"
18 #define private public
19 #include "sec_comp_caller_authorization.h"
20 #include "sec_comp_client.h"
21 #undef private
22 #include "sec_comp_enhance_adapter.h"
23 #include "sec_comp_err.h"
24 #include "sec_comp_info.h"
25 #include "sec_comp_kit.h"
26 #include "sec_comp_log.h"
27 #include "sec_comp_tool.h"
28 #include "sec_comp_ui_register.h"
29 #include "test_common.h"
30 #include "token_setproc.h"
31
32 using namespace testing::ext;
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, "SecCompRegisterCallbackTest"};
39 static AccessTokenID g_selfTokenId = 0;
40 static int32_t g_selfUid = 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;
InitUiRegister()54 static void InitUiRegister()
55 {
56 std::vector<uintptr_t> callerList;
57 SecCompUiRegister registerCallback(callerList, &g_probe);
58 SecCompCallerAuthorization::GetInstance().kitCallerList_.clear();
59 SecCompCallerAuthorization::GetInstance().isInit_ = false;
60 }
61 } // namespace
62
63 class SecCompRegisterCallbackTest : public testing::Test {
64 public:
65 static void SetUpTestCase();
66
67 static void TearDownTestCase();
68
69 void SetUp() override;
70
71 void TearDown() override;
72 };
73
SetUpTestCase()74 void SecCompRegisterCallbackTest::SetUpTestCase()
75 {
76 InitUiRegister();
77 SC_LOG_INFO(LABEL, "SecCompRegisterCallbackTest.");
78 }
79
TearDownTestCase()80 void SecCompRegisterCallbackTest::TearDownTestCase()
81 {
82 SC_LOG_INFO(LABEL, "SecCompRegisterCallbackTest.");
83 }
84
SetUp()85 void SecCompRegisterCallbackTest::SetUp()
86 {
87 g_selfUid = getuid();
88 g_selfTokenId = GetSelfTokenID();
89 SC_LOG_INFO(LABEL, "SetUp ok.");
90 }
91
TearDown()92 void SecCompRegisterCallbackTest::TearDown()
93 {
94 setuid(g_selfUid);
95 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
96 SC_LOG_INFO(LABEL, "TearDown.");
97 }
98
99 /**
100 * @tc.name: RegisterWithoutPreprocess001
101 * @tc.desc: test register without preprocess
102 * @tc.type: FUNC
103 * @tc.require: AR000HO9JM
104 */
105 HWTEST_F(SecCompRegisterCallbackTest, RegisterWithoutPreprocess001, TestSize.Level1)
106 {
107 nlohmann::json jsonRes;
108 TestCommon::BuildLocationComponentInfo(jsonRes);
109 std::string locationInfo = jsonRes.dump();
110
111 SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE);
112 int32_t scId;
113 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
114 ASSERT_EQ(SC_ENHANCE_ERROR_CHALLENGE_CHECK_FAIL,
115 SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
116 ASSERT_EQ(-1, scId);
117 #else
118 ASSERT_EQ(SC_OK,
119 SecCompClient::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
120 ASSERT_NE(-1, scId);
121 EXPECT_EQ(SC_OK, SecCompClient::GetInstance().UnregisterSecurityComponent(scId));
122 #endif
123 }
124
125 /**
126 * @tc.name: RegisterSecurityComponent001
127 * @tc.desc: test register security component success.
128 * @tc.type: FUNC
129 * @tc.require: AR000HO9JM
130 */
131 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1)
132 {
133 nlohmann::json jsonRes;
134 TestCommon::BuildLocationComponentInfo(jsonRes);
135 std::string locationInfo = jsonRes.dump();
136 g_probe.mockComponentInfo = locationInfo;
137 g_probe.mockRes = 0;
138
139 int32_t scId;
140 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
141 ASSERT_NE(-1, scId);
142
143 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
144 }
145
146 /**
147 * @tc.name: RegisterSecurityComponent002
148 * @tc.desc: test register callback failed.
149 * @tc.type: FUNC
150 * @tc.require: AR000HO9JM
151 */
152 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1)
153 {
154 nlohmann::json jsonRes;
155 TestCommon::BuildLocationComponentInfo(jsonRes);
156 std::string locationInfo = jsonRes.dump();
157 g_probe.mockComponentInfo = locationInfo;
158 g_probe.mockRes = -1;
159
160 int32_t scId;
161 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
162 ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_OPER_FAIL,
163 SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
164 ASSERT_EQ(-1, scId);
165 #else
166 ASSERT_EQ(SC_OK,
167 SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
168 ASSERT_NE(-1, scId);
169 ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
170 #endif
171 }
172
173 /**
174 * @tc.name: RegisterSecurityComponent003
175 * @tc.desc: test register in MaliciousAppList.
176 * @tc.type: FUNC
177 * @tc.require: AR000HO9JM
178 */
179 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1)
180 {
181 nlohmann::json jsonRes;
182 TestCommon::BuildLocationComponentInfo(jsonRes);
183 std::string locationInfo = jsonRes.dump();
184 g_probe.mockComponentInfo = locationInfo;
185 g_probe.mockRes = 0;
186
187 int32_t scId;
188 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
189 ASSERT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
190 }
191
192 /**
193 * @tc.name: RegisterSecurityComponent004
194 * @tc.desc: Test register security component check touch info failed
195 * @tc.type: FUNC
196 * @tc.require: AR000HO9J7
197 */
198 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1)
199 {
200 system("param set sec.comp.enhance 1");
201 nlohmann::json jsonRes;
202 TestCommon::BuildSaveComponentInfo(jsonRes);
203 std::string saveInfo = jsonRes.dump();
204 int32_t scId;
205
206 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
207 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
208 struct SecCompClickEvent touchInfo = {
209 .touchX = TestCommon::TEST_COORDINATE,
210 .touchY = TestCommon::TEST_COORDINATE,
211 .timestamp = static_cast<uint64_t>(
212 std::chrono::high_resolution_clock::now().time_since_epoch().count())
213 };
214 touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
215 touchInfo.extraInfo.data = data;
216 auto proxy = SecCompClient::GetInstance().GetProxy(true);
217 ASSERT_NE(proxy, nullptr);
218 auto token = proxy->AsObject();
219 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
220 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
221 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
222 system("param set sec.comp.enhance 0");
223 }
224
225 /**
226 * @tc.name: RegisterSecurityComponent005
227 * @tc.desc: Test register security component permission grant failed
228 * @tc.type: FUNC
229 * @tc.require: AR000HO9J7
230 */
231 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1)
232 {
233 system("param set sec.comp.enhance 1");
234 nlohmann::json jsonRes;
235 TestCommon::BuildSaveComponentInfo(jsonRes);
236 std::string saveInfo = jsonRes.dump();
237 int32_t scId;
238
239 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
240 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
241 struct SecCompClickEvent touchInfo = {
242 .touchX = TestCommon::TEST_COORDINATE,
243 .touchY = TestCommon::TEST_COORDINATE,
244 .timestamp = static_cast<uint64_t>(
245 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
246 };
247 touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
248 touchInfo.extraInfo.data = data;
249 auto proxy = SecCompClient::GetInstance().GetProxy(true);
250 ASSERT_NE(proxy, nullptr);
251 auto token = proxy->AsObject();
252 EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
253 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
254 system("param set sec.comp.enhance 0");
255 }
256
257 /**
258 * @tc.name: RegisterSecurityComponent006
259 * @tc.desc: Test register security component permission grant failed caller error
260 * @tc.type: FUNC
261 * @tc.require: AR000HO9J7
262 */
263 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1)
264 {
265 nlohmann::json jsonRes;
266 TestCommon::BuildSaveComponentInfo(jsonRes);
267 std::string saveInfo = jsonRes.dump();
268 int32_t scId;
269 setuid(100);
270 EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
271 }
272
273 /**
274 * @tc.name: ReportSecurityComponentClickEvent001
275 * @tc.desc: Test register security component success
276 * @tc.type: FUNC
277 * @tc.require: AR000HO9J7
278 */
279 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
280 {
281 system("param set sec.comp.enhance 1");
282 nlohmann::json jsonRes;
283 TestCommon::BuildSaveComponentInfo(jsonRes);
284 std::string saveInfo = jsonRes.dump();
285 int32_t scId;
286
287 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
288 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
289 struct SecCompClickEvent touchInfo = {
290 .touchX = TestCommon::TEST_COORDINATE,
291 .touchY = TestCommon::TEST_COORDINATE,
292 .timestamp = static_cast<uint64_t>(
293 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
294 };
295 touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
296 touchInfo.extraInfo.data = data;
297
298 auto proxy = SecCompClient::GetInstance().GetProxy(true);
299 ASSERT_NE(proxy, nullptr);
300 auto token = proxy->AsObject();
301 ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
302 uint32_t selfTokenId = GetSelfTokenID();
303 ASSERT_TRUE(SecCompKit::ReduceAfterVerifySavePermission(selfTokenId));
304 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
305 system("param set sec.comp.enhance 0");
306 }
307
308 /**
309 * @tc.name: ReportSecurityComponentClickEvent002
310 * @tc.desc: Test report security component caller error
311 * @tc.type: FUNC
312 * @tc.require: AR000HO9J7
313 */
314 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1)
315 {
316 system("param set sec.comp.enhance 1");
317 nlohmann::json jsonRes;
318 TestCommon::BuildSaveComponentInfo(jsonRes);
319 std::string saveInfo = jsonRes.dump();
320 int32_t scId;
321 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
322
323 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
324 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
325 struct SecCompClickEvent touchInfo = {
326 .touchX = TestCommon::TEST_COORDINATE,
327 .touchY = TestCommon::TEST_COORDINATE,
328 .timestamp = static_cast<uint64_t>(
329 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
330 };
331 touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
332 touchInfo.extraInfo.data = data;
333
334 setuid(100);
335 auto proxy = SecCompClient::GetInstance().GetProxy(true);
336 ASSERT_NE(proxy, nullptr);
337 auto token = proxy->AsObject();
338 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
339 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
340 setuid(g_selfUid);
341 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
342 system("param set sec.comp.enhance 0");
343 }
344
345 /**
346 * @tc.name: ReportSecurityComponentClickEvent003
347 * @tc.desc: Test report security component data is empty
348 * @tc.type: FUNC
349 * @tc.require: AR000HO9J7
350 */
351 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1)
352 {
353 nlohmann::json jsonRes;
354 TestCommon::BuildSaveComponentInfo(jsonRes);
355 std::string saveInfo = jsonRes.dump();
356 int32_t scId;
357 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
358
359 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
360
361 struct SecCompClickEvent touchInfo = {
362 .touchX = TestCommon::TEST_COORDINATE,
363 .touchY = TestCommon::TEST_COORDINATE,
364 .timestamp = static_cast<uint64_t>(
365 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
366 };
367
368 auto proxy = SecCompClient::GetInstance().GetProxy(true);
369 ASSERT_NE(proxy, nullptr);
370 auto token = proxy->AsObject();
371 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
372 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
373 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
374 #else
375 ASSERT_EQ(SC_OK,
376 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
377 #endif
378 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
379 }
380
381 /**
382 * @tc.name: ReportClickWithoutHmac001
383 * @tc.desc: Test report click event permission denied
384 * @tc.type: FUNC
385 * @tc.require: AR000HO9JM
386 */
387 HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1)
388 {
389 system("param set sec.comp.enhance 1");
390 nlohmann::json jsonRes;
391 TestCommon::BuildLocationComponentInfo(jsonRes);
392 std::string locationInfo = jsonRes.dump();
393 g_probe.mockComponentInfo = locationInfo;
394 g_probe.mockRes = 0;
395
396 int32_t scId;
397 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
398 ASSERT_NE(-1, scId);
399 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
400 struct SecCompClickEvent touch = {
401 .touchX = TestCommon::TEST_COORDINATE,
402 .touchY = TestCommon::TEST_COORDINATE,
403 .timestamp = static_cast<uint64_t>(
404 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
405 };
406 touch.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
407 touch.extraInfo.data = data;
408 auto proxy = SecCompClient::GetInstance().GetProxy(true);
409 ASSERT_NE(proxy, nullptr);
410 auto token = proxy->AsObject();
411 EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL,
412 SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, touch, token));
413 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
414 system("param set sec.comp.enhance 0");
415 }
416
417 /**
418 * @tc.name: ReduceAfterVerifySavePermission001
419 * @tc.desc: Test register security component wrong hap
420 * @tc.type: FUNC
421 * @tc.require: AR000HO9J7
422 */
423 HWTEST_F(SecCompRegisterCallbackTest, ReduceAfterVerifySavePermission001, TestSize.Level1)
424 {
425 system("param set sec.comp.enhance 1");
426 nlohmann::json jsonRes;
427 TestCommon::BuildSaveComponentInfo(jsonRes);
428 std::string saveInfo = jsonRes.dump();
429 int32_t scId;
430 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
431
432 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
433 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
434 struct SecCompClickEvent touchInfo = {
435 .touchX = TestCommon::TEST_COORDINATE,
436 .touchY = TestCommon::TEST_COORDINATE,
437 .timestamp = static_cast<uint64_t>(
438 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
439 };
440 touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
441 touchInfo.extraInfo.data = data;
442
443 auto proxy = SecCompClient::GetInstance().GetProxy(true);
444 ASSERT_NE(proxy, nullptr);
445 auto token = proxy->AsObject();
446 ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
447 setuid(100);
448 ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(TestCommon::HAP_TOKEN_ID));
449 // mediaLibraryTokenId_ != 0
450 ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(TestCommon::HAP_TOKEN_ID));
451 setuid(g_selfUid);
452 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
453 system("param set sec.comp.enhance 0");
454 }
455
456 /**
457 * @tc.name: ReduceAfterVerifySavePermission002
458 * @tc.desc: Test register security component invalid tokenId
459 * @tc.type: FUNC
460 * @tc.require: AR000HO9J7
461 */
462 HWTEST_F(SecCompRegisterCallbackTest, ReduceAfterVerifySavePermission002, TestSize.Level1)
463 {
464 system("param set sec.comp.enhance 1");
465 nlohmann::json jsonRes;
466 TestCommon::BuildSaveComponentInfo(jsonRes);
467 std::string saveInfo = jsonRes.dump();
468 int32_t scId;
469
470 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
471 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
472 struct SecCompClickEvent touchInfo = {
473 .touchX = TestCommon::TEST_COORDINATE,
474 .touchY = TestCommon::TEST_COORDINATE,
475 .timestamp = static_cast<uint64_t>(
476 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
477 };
478 touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
479 touchInfo.extraInfo.data = data;
480
481 auto proxy = SecCompClient::GetInstance().GetProxy(true);
482 ASSERT_NE(proxy, nullptr);
483 auto token = proxy->AsObject();
484 ASSERT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
485 ASSERT_FALSE(SecCompKit::ReduceAfterVerifySavePermission(0));
486 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
487 system("param set sec.comp.enhance 0");
488 }
489
490 /**
491 * @tc.name: UnregisterSecurityComponent001
492 * @tc.desc: Test unregister security component caller error
493 * @tc.type: FUNC
494 * @tc.require: AR000HO9J7
495 */
496 HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1)
497 {
498 system("param set sec.comp.enhance 1");
499 nlohmann::json jsonRes;
500 TestCommon::BuildSaveComponentInfo(jsonRes);
501 std::string saveInfo = jsonRes.dump();
502 int32_t scId;
503
504 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
505 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
506 struct SecCompClickEvent touchInfo = {
507 .touchX = TestCommon::TEST_COORDINATE,
508 .touchY = TestCommon::TEST_COORDINATE,
509 .timestamp = static_cast<uint64_t>(
510 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
511 };
512 touchInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
513 touchInfo.extraInfo.data = data;
514 auto proxy = SecCompClient::GetInstance().GetProxy(true);
515 ASSERT_NE(proxy, nullptr);
516 auto token = proxy->AsObject();
517 EXPECT_EQ(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, touchInfo, token));
518 setuid(100);
519 EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UnregisterSecurityComponent(scId));
520 setuid(g_selfUid);
521 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
522 system("param set sec.comp.enhance 0");
523 }
524
525 /**
526 * @tc.name: UpdateSecurityComponent001
527 * @tc.desc: Test update security component success
528 * @tc.type: FUNC
529 * @tc.require: AR000HO9IN
530 */
531 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1)
532 {
533 nlohmann::json jsonRes;
534 TestCommon::BuildSaveComponentInfo(jsonRes);
535 std::string saveInfo = jsonRes.dump();
536 int32_t scId;
537 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId));
538 ASSERT_NE(-1, scId);
539 ASSERT_EQ(SC_OK, SecCompKit::UpdateSecurityComponent(scId, saveInfo));
540 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
541 }
542
543 /**
544 * @tc.name: UpdateSecurityComponent002
545 * @tc.desc: Test update security component caller error
546 * @tc.type: FUNC
547 * @tc.require: AR000HO9IN
548 */
549 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1)
550 {
551 nlohmann::json jsonRes;
552 TestCommon::BuildSaveComponentInfo(jsonRes);
553 std::string saveInfo = jsonRes.dump();
554 int32_t scId;
555
556 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
557 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, saveInfo, scId));
558 ASSERT_NE(-1, scId);
559 setuid(100);
560 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UpdateSecurityComponent(scId, saveInfo));
561 setuid(g_selfUid);
562 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
563 }
564