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 clickInfo = {
209 .type = ClickEventType::POINT_EVENT_TYPE,
210 .point.touchX = TestCommon::TEST_COORDINATE,
211 .point.touchY = TestCommon::TEST_COORDINATE,
212 .point.timestamp = static_cast<uint64_t>(
213 std::chrono::high_resolution_clock::now().time_since_epoch().count())
214 };
215 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
216 clickInfo.extraInfo.data = data;
217
218 auto proxy = SecCompClient::GetInstance().GetProxy(true);
219 ASSERT_NE(proxy, nullptr);
220 auto token = proxy->AsObject();
221 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
222 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
223 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
224 system("param set sec.comp.enhance 0");
225 }
226
227 /**
228 * @tc.name: RegisterSecurityComponent005
229 * @tc.desc: Test register security component permission grant failed
230 * @tc.type: FUNC
231 * @tc.require: AR000HO9J7
232 */
233 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1)
234 {
235 system("param set sec.comp.enhance 1");
236 nlohmann::json jsonRes;
237 TestCommon::BuildSaveComponentInfo(jsonRes);
238 std::string saveInfo = jsonRes.dump();
239 int32_t scId;
240
241 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
242 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
243 struct SecCompClickEvent clickInfo = {
244 .type = ClickEventType::POINT_EVENT_TYPE,
245 .point.touchX = TestCommon::TEST_COORDINATE,
246 .point.touchY = TestCommon::TEST_COORDINATE,
247 .point.timestamp = static_cast<uint64_t>(
248 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
249 };
250 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
251 clickInfo.extraInfo.data = data;
252 auto proxy = SecCompClient::GetInstance().GetProxy(true);
253 ASSERT_NE(proxy, nullptr);
254 auto token = proxy->AsObject();
255 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
256 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
257 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
258 system("param set sec.comp.enhance 0");
259 }
260
261 /**
262 * @tc.name: RegisterSecurityComponent006
263 * @tc.desc: Test register security component permission grant failed caller error
264 * @tc.type: FUNC
265 * @tc.require: AR000HO9J7
266 */
267 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1)
268 {
269 nlohmann::json jsonRes;
270 TestCommon::BuildSaveComponentInfo(jsonRes);
271 std::string saveInfo = jsonRes.dump();
272 int32_t scId;
273 setuid(100);
274 EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
275 }
276
277 /**
278 * @tc.name: ReportSecurityComponentClickEvent001
279 * @tc.desc: Test register security component success
280 * @tc.type: FUNC
281 * @tc.require: AR000HO9J7
282 */
283 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
284 {
285 system("param set sec.comp.enhance 1");
286 nlohmann::json jsonRes;
287 TestCommon::BuildSaveComponentInfo(jsonRes);
288 std::string saveInfo = jsonRes.dump();
289 int32_t scId;
290
291 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
292 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
293 struct SecCompClickEvent clickInfo = {
294 .type = ClickEventType::POINT_EVENT_TYPE,
295 .point.touchX = TestCommon::TEST_COORDINATE,
296 .point.touchY = TestCommon::TEST_COORDINATE,
297 .point.timestamp = static_cast<uint64_t>(
298 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
299 };
300 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
301 clickInfo.extraInfo.data = data;
302
303 auto proxy = SecCompClient::GetInstance().GetProxy(true);
304 ASSERT_NE(proxy, nullptr);
305 auto token = proxy->AsObject();
306 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
307 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
308 uint32_t selfTokenId = GetSelfTokenID();
309 ASSERT_FALSE(SecCompKit::VerifySavePermission(selfTokenId));
310 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
311 system("param set sec.comp.enhance 0");
312 }
313
314 /**
315 * @tc.name: ReportSecurityComponentClickEvent002
316 * @tc.desc: Test report security component caller error
317 * @tc.type: FUNC
318 * @tc.require: AR000HO9J7
319 */
320 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1)
321 {
322 system("param set sec.comp.enhance 1");
323 nlohmann::json jsonRes;
324 TestCommon::BuildSaveComponentInfo(jsonRes);
325 std::string saveInfo = jsonRes.dump();
326 int32_t scId;
327 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
328
329 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
330 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
331 struct SecCompClickEvent clickInfo = {
332 .type = ClickEventType::POINT_EVENT_TYPE,
333 .point.touchX = TestCommon::TEST_COORDINATE,
334 .point.touchY = TestCommon::TEST_COORDINATE,
335 .point.timestamp = static_cast<uint64_t>(
336 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
337 };
338 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
339 clickInfo.extraInfo.data = data;
340
341 setuid(100);
342 auto proxy = SecCompClient::GetInstance().GetProxy(true);
343 ASSERT_NE(proxy, nullptr);
344 auto token = proxy->AsObject();
345 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
346 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
347 setuid(g_selfUid);
348 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
349 system("param set sec.comp.enhance 0");
350 }
351
352 /**
353 * @tc.name: ReportSecurityComponentClickEvent003
354 * @tc.desc: Test report security component data is empty
355 * @tc.type: FUNC
356 * @tc.require: AR000HO9J7
357 */
358 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1)
359 {
360 nlohmann::json jsonRes;
361 TestCommon::BuildSaveComponentInfo(jsonRes);
362 std::string saveInfo = jsonRes.dump();
363 int32_t scId;
364 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
365
366 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
367
368 struct SecCompClickEvent clickInfo = {
369 .type = ClickEventType::POINT_EVENT_TYPE,
370 .point.touchX = TestCommon::TEST_COORDINATE,
371 .point.touchY = TestCommon::TEST_COORDINATE,
372 .point.timestamp = static_cast<uint64_t>(
373 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
374 };
375 auto proxy = SecCompClient::GetInstance().GetProxy(true);
376 ASSERT_NE(proxy, nullptr);
377 auto token = proxy->AsObject();
378 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
379 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
380 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
381 }
382
383 /**
384 * @tc.name: ReportClickWithoutHmac001
385 * @tc.desc: Test report click event permission denied
386 * @tc.type: FUNC
387 * @tc.require: AR000HO9JM
388 */
389 HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1)
390 {
391 system("param set sec.comp.enhance 1");
392 nlohmann::json jsonRes;
393 TestCommon::BuildLocationComponentInfo(jsonRes);
394 std::string locationInfo = jsonRes.dump();
395 g_probe.mockComponentInfo = locationInfo;
396 g_probe.mockRes = 0;
397
398 int32_t scId;
399 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
400 ASSERT_NE(-1, scId);
401 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
402 struct SecCompClickEvent clickInfo = {
403 .type = ClickEventType::POINT_EVENT_TYPE,
404 .point.touchX = TestCommon::TEST_COORDINATE,
405 .point.touchY = TestCommon::TEST_COORDINATE,
406 .point.timestamp = static_cast<uint64_t>(
407 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
408 };
409 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
410 clickInfo.extraInfo.data = data;
411 auto proxy = SecCompClient::GetInstance().GetProxy(true);
412 ASSERT_NE(proxy, nullptr);
413 auto token = proxy->AsObject();
414 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
415 SecCompKit::ReportSecurityComponentClickEvent(scId, locationInfo, clickInfo, token));
416 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
417 system("param set sec.comp.enhance 0");
418 }
419
420 /**
421 * @tc.name: VerifySavePermission001
422 * @tc.desc: Test register security component wrong hap
423 * @tc.type: FUNC
424 * @tc.require: AR000HO9J7
425 */
426 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1)
427 {
428 system("param set sec.comp.enhance 1");
429 nlohmann::json jsonRes;
430 TestCommon::BuildSaveComponentInfo(jsonRes);
431 std::string saveInfo = jsonRes.dump();
432 int32_t scId;
433 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
434
435 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
436 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
437 struct SecCompClickEvent clickInfo = {
438 .type = ClickEventType::POINT_EVENT_TYPE,
439 .point.touchX = TestCommon::TEST_COORDINATE,
440 .point.touchY = TestCommon::TEST_COORDINATE,
441 .point.timestamp = static_cast<uint64_t>(
442 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
443 };
444 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
445 clickInfo.extraInfo.data = data;
446 auto proxy = SecCompClient::GetInstance().GetProxy(true);
447 ASSERT_NE(proxy, nullptr);
448 auto token = proxy->AsObject();
449 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
450 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
451 setuid(100);
452 ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
453 // mediaLibraryTokenId_ != 0
454 ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
455 setuid(g_selfUid);
456 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
457 system("param set sec.comp.enhance 0");
458 }
459
460 /**
461 * @tc.name: VerifySavePermission002
462 * @tc.desc: Test register security component invalid tokenId
463 * @tc.type: FUNC
464 * @tc.require: AR000HO9J7
465 */
466 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1)
467 {
468 system("param set sec.comp.enhance 1");
469 nlohmann::json jsonRes;
470 TestCommon::BuildSaveComponentInfo(jsonRes);
471 std::string saveInfo = jsonRes.dump();
472 int32_t scId;
473
474 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
475 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
476 struct SecCompClickEvent clickInfo = {
477 .type = ClickEventType::POINT_EVENT_TYPE,
478 .point.touchX = TestCommon::TEST_COORDINATE,
479 .point.touchY = TestCommon::TEST_COORDINATE,
480 .point.timestamp = static_cast<uint64_t>(
481 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
482 };
483 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
484 clickInfo.extraInfo.data = data;
485 auto proxy = SecCompClient::GetInstance().GetProxy(true);
486 ASSERT_NE(proxy, nullptr);
487 auto token = proxy->AsObject();
488 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
489 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
490 ASSERT_FALSE(SecCompKit::VerifySavePermission(0));
491 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
492 system("param set sec.comp.enhance 0");
493 }
494
495 /**
496 * @tc.name: UnregisterSecurityComponent001
497 * @tc.desc: Test unregister security component caller error
498 * @tc.type: FUNC
499 * @tc.require: AR000HO9J7
500 */
501 HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1)
502 {
503 system("param set sec.comp.enhance 1");
504 nlohmann::json jsonRes;
505 TestCommon::BuildSaveComponentInfo(jsonRes);
506 std::string saveInfo = jsonRes.dump();
507 int32_t scId;
508
509 EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
510 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
511 struct SecCompClickEvent clickInfo = {
512 .type = ClickEventType::POINT_EVENT_TYPE,
513 .point.touchX = TestCommon::TEST_COORDINATE,
514 .point.touchY = TestCommon::TEST_COORDINATE,
515 .point.timestamp = static_cast<uint64_t>(
516 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
517 };
518 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
519 clickInfo.extraInfo.data = data;
520 auto proxy = SecCompClient::GetInstance().GetProxy(true);
521 ASSERT_NE(proxy, nullptr);
522 auto token = proxy->AsObject();
523 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
524 SecCompKit::ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token));
525 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
526 system("param set sec.comp.enhance 0");
527 }
528
529 /**
530 * @tc.name: UpdateSecurityComponent001
531 * @tc.desc: Test update security component success
532 * @tc.type: FUNC
533 * @tc.require: AR000HO9IN
534 */
535 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1)
536 {
537 nlohmann::json jsonRes;
538 TestCommon::BuildSaveComponentInfo(jsonRes);
539 std::string saveInfo = jsonRes.dump();
540 int32_t scId;
541 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
542 ASSERT_NE(-1, scId);
543 ASSERT_EQ(SC_OK, SecCompKit::UpdateSecurityComponent(scId, saveInfo));
544 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
545 }
546
547 /**
548 * @tc.name: UpdateSecurityComponent002
549 * @tc.desc: Test update security component caller error
550 * @tc.type: FUNC
551 * @tc.require: AR000HO9IN
552 */
553 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1)
554 {
555 nlohmann::json jsonRes;
556 TestCommon::BuildSaveComponentInfo(jsonRes);
557 std::string saveInfo = jsonRes.dump();
558 int32_t scId;
559
560 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID));
561 ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
562 ASSERT_NE(-1, scId);
563 setuid(100);
564 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::UpdateSecurityComponent(scId, saveInfo));
565 setuid(g_selfUid);
566 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
567 }
568