• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(int32_t scId,std::string & componentInfo,const SecCompClickEvent & clickInfo,sptr<IRemoteObject> callerToken,OnFirstUseDialogCloseFunc dialogCall)62 static __attribute__((noinline)) int32_t ReportSecurityComponentClickEvent(
63     int32_t scId, std::string& componentInfo,
64     const SecCompClickEvent& clickInfo, sptr<IRemoteObject> callerToken, OnFirstUseDialogCloseFunc dialogCall)
65 {
66     SC_LOG_INFO(LABEL, "ReportSecurityComponentClickEvent enter");
67     return SecCompKit::ReportSecurityComponentClickEvent(scId, componentInfo, clickInfo, callerToken,
68         std::move(dialogCall));
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     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, 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         RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
164     ASSERT_EQ(-1, scId);
165 #else
166     ASSERT_EQ(SC_OK,
167         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, 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, 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     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
219     ASSERT_NE(callback, nullptr);
220     auto token = callback->AsObject();
__anon700d4dfc0202(int32_t) 221     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
222     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
223         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
224     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
225     system("param set sec.comp.enhance 0");
226 }
227 
228 /**
229  * @tc.name: RegisterSecurityComponent005
230  * @tc.desc: Test register security component permission grant failed
231  * @tc.type: FUNC
232  * @tc.require: AR000HO9J7
233  */
234 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1)
235 {
236     system("param set sec.comp.enhance 1");
237     nlohmann::json jsonRes;
238     TestCommon::BuildSaveComponentInfo(jsonRes);
239     std::string saveInfo = jsonRes.dump();
240     int32_t scId;
241 
242     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
243     uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
244     struct SecCompClickEvent clickInfo = {
245         .type = ClickEventType::POINT_EVENT_TYPE,
246         .point.touchX = TestCommon::TEST_COORDINATE,
247         .point.touchY = TestCommon::TEST_COORDINATE,
248         .point.timestamp = static_cast<uint64_t>(
249             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
250     };
251     clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
252     clickInfo.extraInfo.data = data;
253     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
254     ASSERT_NE(callback, nullptr);
255     auto token = callback->AsObject();
__anon700d4dfc0302(int32_t) 256     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
257     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
258         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
259     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
260     system("param set sec.comp.enhance 0");
261 }
262 
263 /**
264  * @tc.name: RegisterSecurityComponent006
265  * @tc.desc: Test register security component permission grant failed caller error
266  * @tc.type: FUNC
267  * @tc.require: AR000HO9J7
268  */
269 HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1)
270 {
271     nlohmann::json jsonRes;
272     TestCommon::BuildSaveComponentInfo(jsonRes);
273     std::string saveInfo = jsonRes.dump();
274     int32_t scId;
275     setuid(100);
276     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
277 }
278 
279 /**
280  * @tc.name: ReportSecurityComponentClickEvent001
281  * @tc.desc: Test register security component success
282  * @tc.type: FUNC
283  * @tc.require: AR000HO9J7
284  */
285 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
286 {
287     system("param set sec.comp.enhance 1");
288     nlohmann::json jsonRes;
289     TestCommon::BuildSaveComponentInfo(jsonRes);
290     std::string saveInfo = jsonRes.dump();
291     int32_t scId;
292 
293     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
294     uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
295     struct SecCompClickEvent clickInfo = {
296         .type = ClickEventType::POINT_EVENT_TYPE,
297         .point.touchX = TestCommon::TEST_COORDINATE,
298         .point.touchY = TestCommon::TEST_COORDINATE,
299         .point.timestamp = static_cast<uint64_t>(
300             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
301     };
302     clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
303     clickInfo.extraInfo.data = data;
304 
305     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
306     ASSERT_NE(callback, nullptr);
307     auto token = callback->AsObject();
__anon700d4dfc0402(int32_t) 308     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
309     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
310         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
311     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
312     system("param set sec.comp.enhance 0");
313 }
314 
315 /**
316  * @tc.name: ReportSecurityComponentClickEvent002
317  * @tc.desc: Test report security component caller error
318  * @tc.type: FUNC
319  * @tc.require: AR000HO9J7
320  */
321 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1)
322 {
323     system("param set sec.comp.enhance 1");
324     nlohmann::json jsonRes;
325     TestCommon::BuildSaveComponentInfo(jsonRes);
326     std::string saveInfo = jsonRes.dump();
327     int32_t scId;
328     ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
329     g_token_sum ++;
330 
331     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
332     uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
333     struct SecCompClickEvent clickInfo = {
334         .type = ClickEventType::POINT_EVENT_TYPE,
335         .point.touchX = TestCommon::TEST_COORDINATE,
336         .point.touchY = TestCommon::TEST_COORDINATE,
337         .point.timestamp = static_cast<uint64_t>(
338             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
339     };
340     clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
341     clickInfo.extraInfo.data = data;
342 
343     setuid(100);
344     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
345     ASSERT_NE(callback, nullptr);
346     auto token = callback->AsObject();
__anon700d4dfc0502(int32_t) 347     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
348     ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
349         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
350     setuid(g_selfUid);
351     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
352     system("param set sec.comp.enhance 0");
353 }
354 
355 /**
356  * @tc.name: ReportSecurityComponentClickEvent003
357  * @tc.desc: Test report security component enhance data is empty
358  * @tc.type: FUNC
359  * @tc.require: AR000HO9J7
360  */
361 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1)
362 {
363     nlohmann::json jsonRes;
364     TestCommon::BuildSaveComponentInfo(jsonRes);
365     std::string saveInfo = jsonRes.dump();
366     int32_t scId;
367     ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
368     g_token_sum ++;
369 
370     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
371 
372     struct SecCompClickEvent clickInfo = {
373         .type = ClickEventType::POINT_EVENT_TYPE,
374         .point.touchX = TestCommon::TEST_COORDINATE,
375         .point.touchY = TestCommon::TEST_COORDINATE,
376         .point.timestamp = static_cast<uint64_t>(
377             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
378     };
379     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
380     ASSERT_NE(callback, nullptr);
381     auto token = callback->AsObject();
__anon700d4dfc0602(int32_t) 382     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
383 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
384     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
385         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
386 #else
387     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
388         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
389 #endif
390     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
391 }
392 
393 /**
394  * @tc.name: ReportSecurityComponentClickEvent004
395  * @tc.desc: Test report security component dialog callback is nullptr
396  * @tc.type: FUNC
397  * @tc.require: AR000HO9J7
398  */
399 HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent004, TestSize.Level1)
400 {
401     nlohmann::json jsonRes;
402     TestCommon::BuildSaveComponentInfo(jsonRes);
403     std::string saveInfo = jsonRes.dump();
404     int32_t scId;
405     ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
406     g_token_sum ++;
407 
408     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
409 
410     struct SecCompClickEvent clickInfo = {
411         .type = ClickEventType::POINT_EVENT_TYPE,
412         .point.touchX = TestCommon::TEST_COORDINATE,
413         .point.touchY = TestCommon::TEST_COORDINATE,
414         .point.timestamp = static_cast<uint64_t>(
415             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
416     };
417     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
418     ASSERT_NE(callback, nullptr);
419     auto token = callback->AsObject();
420     OnFirstUseDialogCloseFunc func = nullptr;
421     ASSERT_EQ(SC_ENHANCE_ERROR_VALUE_INVALID,
422         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
423     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
424 }
425 
426 /**
427  * @tc.name: ReportClickWithoutHmac001
428  * @tc.desc: Test report click event permission denied
429  * @tc.type: FUNC
430  * @tc.require: AR000HO9JM
431  */
432 HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1)
433 {
434     system("param set sec.comp.enhance 1");
435     nlohmann::json jsonRes;
436     TestCommon::BuildLocationComponentInfo(jsonRes);
437     std::string locationInfo = jsonRes.dump();
438     g_probe.mockComponentInfo = locationInfo;
439     g_probe.mockRes = 0;
440 
441     int32_t scId;
442     ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
443     ASSERT_NE(-1, scId);
444     uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
445     struct SecCompClickEvent clickInfo = {
446         .type = ClickEventType::POINT_EVENT_TYPE,
447         .point.touchX = TestCommon::TEST_COORDINATE,
448         .point.touchY = TestCommon::TEST_COORDINATE,
449         .point.timestamp = static_cast<uint64_t>(
450             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
451     };
452     clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
453     clickInfo.extraInfo.data = data;
454     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
455     ASSERT_NE(callback, nullptr);
456     auto token = callback->AsObject();
__anon700d4dfc0702(int32_t) 457     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
458     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
459         ReportSecurityComponentClickEvent(scId, locationInfo, clickInfo, token, std::move(func)));
460     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
461     system("param set sec.comp.enhance 0");
462 }
463 
464 /**
465  * @tc.name: VerifySavePermission001
466  * @tc.desc: Test register security component wrong hap
467  * @tc.type: FUNC
468  * @tc.require: AR000HO9J7
469  */
470 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1)
471 {
472     system("param set sec.comp.enhance 1");
473     nlohmann::json jsonRes;
474     TestCommon::BuildSaveComponentInfo(jsonRes);
475     std::string saveInfo = jsonRes.dump();
476     int32_t scId;
477     ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
478     g_token_sum ++;
479 
480     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
481     uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
482     struct SecCompClickEvent clickInfo = {
483         .type = ClickEventType::POINT_EVENT_TYPE,
484         .point.touchX = TestCommon::TEST_COORDINATE,
485         .point.touchY = TestCommon::TEST_COORDINATE,
486         .point.timestamp = static_cast<uint64_t>(
487             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
488     };
489     clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
490     clickInfo.extraInfo.data = data;
491     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
492     ASSERT_NE(callback, nullptr);
493     auto token = callback->AsObject();
__anon700d4dfc0802(int32_t) 494     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
495     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
496         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
497     setuid(100);
498     ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
499     // mediaLibraryTokenId_ != 0
500     ASSERT_FALSE(SecCompKit::VerifySavePermission(TestCommon::HAP_TOKEN_ID));
501     setuid(g_selfUid);
502     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
503     system("param set sec.comp.enhance 0");
504 }
505 
506 /**
507  * @tc.name: VerifySavePermission002
508  * @tc.desc: Test register security component invalid tokenId
509  * @tc.type: FUNC
510  * @tc.require: AR000HO9J7
511  */
512 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1)
513 {
514     system("param set sec.comp.enhance 1");
515     nlohmann::json jsonRes;
516     TestCommon::BuildSaveComponentInfo(jsonRes);
517     std::string saveInfo = jsonRes.dump();
518     int32_t scId;
519 
520     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
521     uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
522     struct SecCompClickEvent clickInfo = {
523         .type = ClickEventType::POINT_EVENT_TYPE,
524         .point.touchX = TestCommon::TEST_COORDINATE,
525         .point.touchY = TestCommon::TEST_COORDINATE,
526         .point.timestamp = static_cast<uint64_t>(
527             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
528     };
529     clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
530     clickInfo.extraInfo.data = data;
531     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
532     ASSERT_NE(callback, nullptr);
533     auto token = callback->AsObject();
__anon700d4dfc0902(int32_t) 534     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
535     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
536         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
537     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
538     system("param set sec.comp.enhance 0");
539 }
540 
541 /**
542  * @tc.name: UnregisterSecurityComponent001
543  * @tc.desc: Test unregister security component caller error
544  * @tc.type: FUNC
545  * @tc.require: AR000HO9J7
546  */
547 HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1)
548 {
549     system("param set sec.comp.enhance 1");
550     nlohmann::json jsonRes;
551     TestCommon::BuildSaveComponentInfo(jsonRes);
552     std::string saveInfo = jsonRes.dump();
553     int32_t scId;
554 
555     EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
556     uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 };
557     struct SecCompClickEvent clickInfo = {
558         .type = ClickEventType::POINT_EVENT_TYPE,
559         .point.touchX = TestCommon::TEST_COORDINATE,
560         .point.touchY = TestCommon::TEST_COORDINATE,
561         .point.timestamp = static_cast<uint64_t>(
562             std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TestCommon::TIME_CONVERSION_UNIT
563     };
564     clickInfo.extraInfo.dataSize = TestCommon::MAX_HMAC_SIZE;
565     clickInfo.extraInfo.data = data;
566     sptr<SystemAbilityLoadCallbackStub> callback = new (std::nothrow) SystemAbilityLoadCallbackStub();
567     ASSERT_NE(callback, nullptr);
568     auto token = callback->AsObject();
__anon700d4dfc0a02(int32_t) 569     OnFirstUseDialogCloseFunc func = [] (int32_t) {};
570     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
571         ReportSecurityComponentClickEvent(scId, saveInfo, clickInfo, token, std::move(func)));
572     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
573     system("param set sec.comp.enhance 0");
574 }
575 
576 /**
577  * @tc.name: UpdateSecurityComponent001
578  * @tc.desc: Test update security component success
579  * @tc.type: FUNC
580  * @tc.require: AR000HO9IN
581  */
582 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1)
583 {
584     nlohmann::json jsonRes;
585     TestCommon::BuildSaveComponentInfo(jsonRes);
586     std::string saveInfo = jsonRes.dump();
587     int32_t scId;
588     ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
589     ASSERT_NE(-1, scId);
590     ASSERT_EQ(SC_OK, UpdateSecurityComponent(scId, saveInfo));
591     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
592 }
593 
594 /**
595  * @tc.name: UpdateSecurityComponent002
596  * @tc.desc: Test update security component caller error
597  * @tc.type: FUNC
598  * @tc.require: AR000HO9IN
599  */
600 HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1)
601 {
602     nlohmann::json jsonRes;
603     TestCommon::BuildSaveComponentInfo(jsonRes);
604     std::string saveInfo = jsonRes.dump();
605     int32_t scId;
606 
607     ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID + g_token_sum));
608     g_token_sum ++;
609     ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
610     ASSERT_NE(-1, scId);
611     setuid(100);
612     ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, UpdateSecurityComponent(scId, saveInfo));
613     setuid(g_selfUid);
614     EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
615 }
616 
617 /**
618  * @tc.name: CheckCipherPrint001
619  * @tc.desc: Test cipher info print
620  * @tc.type: FUNC
621  * @tc.require:
622  */
623 HWTEST_F(SecCompRegisterCallbackTest, CheckCipherPrint001, TestSize.Level1)
624 {
625     nlohmann::json jsonRes;
626     TestCommon::BuildLocationComponentInfo(jsonRes);
627     std::string locationInfo = jsonRes.dump();
628     g_probe.mockComponentInfo = locationInfo;
629     g_probe.mockRes = 0;
630     int32_t scId;
631 
632     system("param set sec.comp.enhance 3");
633     ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
634     ASSERT_EQ(SC_OK, RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
635     system("param set sec.comp.enhance -1");
636 }
637