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: AR000HO9JM
130 */
131 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1)
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: AR000HO9JM
152 */
153 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1)
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: AR000HO9JM
179 */
180 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1)
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: AR000HO9J7
198 */
199 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1)
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();
__anon05e931780202(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: AR000HO9J7
236 */
237 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1)
238 {
239 system("param set sec.comp.enhance 1");
240 nlohmann::json jsonRes;
241 TestCommon::BuildSaveComponentInfo(jsonRes);
242 std::string saveInfo = jsonRes.dump();
243 int32_t scId;
244
245 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, 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();
__anon05e931780302(int32_t) 259 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
260 SecCompInfo secCompInfo{ scId, saveInfo, 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: AR000HO9J7
273 */
274 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1)
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: AR000HO9J7
289 */
290 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
291 {
292 system("param set sec.comp.enhance 1");
293 nlohmann::json jsonRes;
294 TestCommon::BuildSaveComponentInfo(jsonRes);
295 std::string saveInfo = jsonRes.dump();
296 int32_t scId;
297
298 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, 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();
__anon05e931780402(int32_t) 313 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
314 SecCompInfo secCompInfo{ scId, saveInfo, 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: AR000HO9J7
327 */
328 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1)
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();
__anon05e931780502(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: AR000HO9J7
369 */
370 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1)
371 {
372 nlohmann::json jsonRes;
373 TestCommon::BuildSaveComponentInfo(jsonRes);
374 std::string saveInfo = 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(SAVE_COMPONENT, saveInfo, 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();
__anon05e931780602(int32_t) 391 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
392 SecCompInfo secCompInfo{ scId, saveInfo, 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: AR000HO9J7
409 */
410 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent004, TestSize.Level1)
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: AR000HO9JM
444 */
445 HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1)
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();
__anon05e931780702(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: AR000HO9J7
484 */
485 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1)
486 {
487 system("param set sec.comp.enhance 1");
488 nlohmann::json jsonRes;
489 TestCommon::BuildSaveComponentInfo(jsonRes);
490 std::string saveInfo = jsonRes.dump();
491 int32_t scId;
492 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
493 g_token_sum ++;
494
495 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
496 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
497 struct SecCompClickEvent clickInfo = {
498 .type = ClickEventType::POINT_EVENT_TYPE,
499 .point.touchX = TestCommon::TEST_COORDINATE,
500 .point.touchY = TestCommon::TEST_COORDINATE,
501 .point.timestamp = static_cast<uint64_t>(
502 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
503 };
504 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
505 clickInfo.extraInfo.data = data;
506 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
507 ASSERT_NE(callback, nullptr);
508 auto token = callback->AsObject();
__anon05e931780802(int32_t) 509 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
510 SecCompInfo secCompInfo{ scId, saveInfo, clickInfo };
511 std::string message;
512 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
513 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
514 setuid(100);
515 ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
516 // mediaLibraryTokenId_ != 0
517 ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
518 setuid(g_selfUid);
519 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
520 system("param set sec.comp.enhance 0");
521 }
522
523 /**
524 * @tc.name: VerifySavePermission002
525 * @tc.desc: Test register security component invalid tokenId
526 * @tc.type: FUNC
527 * @tc.require: AR000HO9J7
528 */
529 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1)
530 {
531 system("param set sec.comp.enhance 1");
532 nlohmann::json jsonRes;
533 TestCommon::BuildSaveComponentInfo(jsonRes);
534 std::string saveInfo = jsonRes.dump();
535 int32_t scId;
536
537 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
538 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
539 struct SecCompClickEvent clickInfo = {
540 .type = ClickEventType::POINT_EVENT_TYPE,
541 .point.touchX = TestCommon::TEST_COORDINATE,
542 .point.touchY = TestCommon::TEST_COORDINATE,
543 .point.timestamp = static_cast<uint64_t>(
544 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
545 };
546 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
547 clickInfo.extraInfo.data = data;
548 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
549 ASSERT_NE(callback, nullptr);
550 auto token = callback->AsObject();
__anon05e931780902(int32_t) 551 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
552 SecCompInfo secCompInfo{ scId, saveInfo, clickInfo };
553 std::string message;
554 ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
555 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
556 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
557 system("param set sec.comp.enhance 0");
558 }
559
560 /**
561 * @tc.name: UnregisterSecurityComponent001
562 * @tc.desc: Test unregister security component caller error
563 * @tc.type: FUNC
564 * @tc.require: AR000HO9J7
565 */
566 HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1)
567 {
568 system("param set sec.comp.enhance 1");
569 nlohmann::json jsonRes;
570 TestCommon::BuildSaveComponentInfo(jsonRes);
571 std::string saveInfo = jsonRes.dump();
572 int32_t scId;
573
574 EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
575 uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
576 struct SecCompClickEvent clickInfo = {
577 .type = ClickEventType::POINT_EVENT_TYPE,
578 .point.touchX = TestCommon::TEST_COORDINATE,
579 .point.touchY = TestCommon::TEST_COORDINATE,
580 .point.timestamp = static_cast<uint64_t>(
581 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
582 };
583 clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
584 clickInfo.extraInfo.data = data;
585 sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
586 ASSERT_NE(callback, nullptr);
587 auto token = callback->AsObject();
__anon05e931780a02(int32_t) 588 OnFirstUseDialogCloseFunc func = [] (int32_t) {};
589 SecCompInfo secCompInfo{ scId, saveInfo, clickInfo };
590 std::string message;
591 EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
592 ReportSecurityComponentClickEvent(secCompInfo, token, std::move(func), message));
593 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
594 system("param set sec.comp.enhance 0");
595 }
596
597 /**
598 * @tc.name: UpdateSecurityComponent001
599 * @tc.desc: Test update security component success
600 * @tc.type: FUNC
601 * @tc.require: AR000HO9IN
602 */
603 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1)
604 {
605 nlohmann::json jsonRes;
606 TestCommon::BuildSaveComponentInfo(jsonRes);
607 std::string saveInfo = jsonRes.dump();
608 int32_t scId;
609 ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
610 ASSERT_NE(-1, scId);
611 ASSERT_EQ(SC_OK, UpdateSecurityComponent(scId, saveInfo));
612 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
613 }
614
615 /**
616 * @tc.name: UpdateSecurityComponent002
617 * @tc.desc: Test update security component caller error
618 * @tc.type: FUNC
619 * @tc.require: AR000HO9IN
620 */
621 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1)
622 {
623 nlohmann::json jsonRes;
624 TestCommon::BuildSaveComponentInfo(jsonRes);
625 std::string saveInfo = jsonRes.dump();
626 int32_t scId;
627
628 ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
629 g_token_sum ++;
630 ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
631 ASSERT_NE(-1, scId);
632 setuid(100);
633 ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, UpdateSecurityComponent(scId, saveInfo));
634 setuid(g_selfUid);
635 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
636 }
637
638 /**
639 * @tc.name: CheckCipherPrint001
640 * @tc.desc: Test cipher info print
641 * @tc.type: FUNC
642 * @tc.require:
643 */
644 HWTEST_F(SecCompRegisterCallbackTest, CheckCipherPrint001, TestSize.Level1)
645 {
646 nlohmann::json jsonRes;
647 TestCommon::BuildLocationComponentInfo(jsonRes);
648 std::string locationInfo = jsonRes.dump();
649 g_probe.mockComponentInfo = locationInfo;
650 g_probe.mockRes = 0;
651 int32_t scId;
652
653 system("param set sec.comp.enhance 3");
654 ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
655 ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
656 system("param set sec.comp.enhance -1");
657 }
658