• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 
16 #include "el5_filekey_manager_service_mock_unittest.h"
17 
18 #include "accesstoken_kit.h"
19 #include "el5_filekey_callback_interface_stub.h"
20 #include "el5_filekey_manager_error.h"
21 #include "el5_test_common.h"
22 #include "mock_ipc.h"
23 #include "token_setproc.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS::Security::AccessToken;
27 namespace {
28 constexpr uint32_t SCREEN_ON_DELAY_TIME = 30;
29 } // namespace
30 
SetUpTestCase()31 void El5FilekeyManagerServiceMockTest::SetUpTestCase()
32 {
33 }
34 
TearDownTestCase()35 void El5FilekeyManagerServiceMockTest::TearDownTestCase()
36 {
37     sleep(SCREEN_ON_DELAY_TIME);
38 }
39 
SetUp()40 void El5FilekeyManagerServiceMockTest::SetUp()
41 {
42     el5FilekeyManagerService_ = DelayedSingleton<El5FilekeyManagerService>::GetInstance();
43     el5FilekeyManagerService_->Init();
44     el5FilekeyManagerService_->service_ = nullptr;
45 }
46 
TearDown()47 void El5FilekeyManagerServiceMockTest::TearDown()
48 {
49 }
50 
51 class TestEl5FilekeyCallback : public El5FilekeyCallbackInterfaceStub {
52 public:
OnRegenerateAppKey(std::vector<AppKeyInfo> & infos)53     OHOS::ErrCode OnRegenerateAppKey(std::vector<AppKeyInfo> &infos)
54     {
55         GTEST_LOG_(INFO) << "OnRegenerateAppKey.";
56         return OHOS::ERR_OK;
57     }
58 };
59 
60 class TestEl5FilekeyServiceExt : public El5FilekeyServiceExtInterface {
61 public:
AcquireAccess(DataLockType type,bool isApp)62     int32_t AcquireAccess(DataLockType type, bool isApp)
63     {
64         return EFM_SUCCESS;
65     }
66 
ReleaseAccess(DataLockType type,bool isApp)67     int32_t ReleaseAccess(DataLockType type, bool isApp)
68     {
69         return EFM_SUCCESS;
70     }
71 
GenerateAppKey(uint32_t uid,const std::string & bundleName,std::string & keyId)72     int32_t GenerateAppKey(uint32_t uid, const std::string& bundleName, std::string& keyId)
73     {
74         return EFM_SUCCESS;
75     }
76 
DeleteAppKey(const std::string & bundleName,int32_t userId)77     int32_t DeleteAppKey(const std::string& bundleName, int32_t userId)
78     {
79         return EFM_SUCCESS;
80     }
81 
GetUserAppKey(int32_t userId,bool getAllFlag,std::vector<UserAppKeyInfo> & keyInfos)82     int32_t GetUserAppKey(int32_t userId, bool getAllFlag, std::vector<UserAppKeyInfo> &keyInfos)
83     {
84         int32_t key = 111;
85         std::string info = "test";
86         keyInfos.emplace_back(UserAppKeyInfo(key, info));
87         return EFM_SUCCESS;
88     }
89 
ChangeUserAppkeysLoadInfo(int32_t userId,const std::vector<AppKeyLoadInfo> & loadInfos)90     int32_t ChangeUserAppkeysLoadInfo(int32_t userId, const std::vector<AppKeyLoadInfo> &loadInfos)
91     {
92         return EFM_SUCCESS;
93     }
94 
SetFilePathPolicy(int32_t userId)95     int32_t SetFilePathPolicy(int32_t userId)
96     {
97         return EFM_SUCCESS;
98     }
99 
RegisterCallback(const OHOS::sptr<El5FilekeyCallbackInterface> & callback)100     int32_t RegisterCallback(const OHOS::sptr<El5FilekeyCallbackInterface> &callback)
101     {
102         return EFM_SUCCESS;
103     }
104 
GenerateGroupIDKey(uint32_t uid,const std::string & groupID,std::string & keyId)105     int32_t GenerateGroupIDKey(uint32_t uid, const std::string &groupID, std::string &keyId)
106     {
107         return EFM_SUCCESS;
108     }
109 
DeleteGroupIDKey(uint32_t uid,const std::string & groupID)110     int32_t DeleteGroupIDKey(uint32_t uid, const std::string &groupID)
111     {
112         return EFM_SUCCESS;
113     }
114 
QueryAppKeyState(DataLockType type,bool isApp)115     int32_t QueryAppKeyState(DataLockType type, bool isApp)
116     {
117         return EFM_SUCCESS;
118     }
119 
HandleUserCommonEvent(const std::string & eventName,int32_t userId)120     int32_t HandleUserCommonEvent(const std::string &eventName, int32_t userId)
121     {
122         return EFM_SUCCESS;
123     }
124 
SetPolicyScreenLocked()125     int32_t SetPolicyScreenLocked()
126     {
127         return EFM_SUCCESS;
128     }
129 
DumpData(int fd,const std::vector<std::u16string> & args)130     int32_t DumpData(int fd, const std::vector<std::u16string>& args)
131     {
132         return EFM_SUCCESS;
133     }
134 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)135     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
136     {
137         GTEST_LOG_(INFO) << "OnAddSystemAbility.";
138     }
139 
UnInit()140     void UnInit()
141     {
142         GTEST_LOG_(INFO) << "UnInit.";
143     }
144 };
145 
146 /**
147  * @tc.name: AcquireAccess001
148  * @tc.desc: Acquire default type data access.
149  * @tc.type: FUNC
150  * @tc.require: issueIAD2MD
151  */
152 HWTEST_F(El5FilekeyManagerServiceMockTest, AcquireAccess001, TestSize.Level1)
153 {
154     el5FilekeyManagerService_->service_ = nullptr;
155 
156     MockIpc::SetCallingUid(20020025);
157     uint64_t tokenId = GetTokenIdFromBundleName("com.ohos.medialibrary.medialibrarydata");
158     // if medialibrarydata not exist, try contactsdataability
159     if (tokenId == INVALID_TOKENID) {
160         tokenId = GetTokenIdFromBundleName("com.ohos.contactsdataability");
161     }
162     ASSERT_NE(tokenId, INVALID_TOKENID);
163     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
164 
165     ASSERT_EQ(el5FilekeyManagerService_->AcquireAccess(DataLockType::DEFAULT_DATA), EFM_SUCCESS);
166 }
167 
168 /**
169  * @tc.name: AcquireAccess002
170  * @tc.desc: Acquire default type data access.
171  * @tc.type: FUNC
172  * @tc.require: issueIAD2MD
173  */
174 HWTEST_F(El5FilekeyManagerServiceMockTest, AcquireAccess002, TestSize.Level1)
175 {
176     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
177 
178     MockIpc::SetCallingUid(20020025);
179     uint64_t tokenId = GetTokenIdFromBundleName("com.ohos.medialibrary.medialibrarydata");
180     // if medialibrarydata not exist, try contactsdataability
181     if (tokenId == INVALID_TOKENID) {
182         tokenId = GetTokenIdFromBundleName("com.ohos.contactsdataability");
183     }
184     ASSERT_NE(tokenId, INVALID_TOKENID);
185     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
186 
187     ASSERT_EQ(el5FilekeyManagerService_->AcquireAccess(DataLockType::DEFAULT_DATA), EFM_SUCCESS);
188 }
189 
190 /**
191  * @tc.name: ReleaseAccess001
192  * @tc.desc: Release default type data access.
193  * @tc.type: FUNC
194  * @tc.require: issueIAD2MD
195  */
196 HWTEST_F(El5FilekeyManagerServiceMockTest, ReleaseAccess001, TestSize.Level1)
197 {
198     el5FilekeyManagerService_->service_ = nullptr;
199 
200     MockIpc::SetCallingUid(20020025);
201     uint64_t tokenId = GetTokenIdFromBundleName("com.ohos.medialibrary.medialibrarydata");
202     // if medialibrarydata not exist, try contactsdataability
203     if (tokenId == INVALID_TOKENID) {
204         tokenId = GetTokenIdFromBundleName("com.ohos.contactsdataability");
205     }
206     ASSERT_NE(tokenId, INVALID_TOKENID);
207     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
208 
209     ASSERT_EQ(el5FilekeyManagerService_->ReleaseAccess(DataLockType::DEFAULT_DATA), EFM_SUCCESS);
210 }
211 
212 /**
213  * @tc.name: ReleaseAccess002
214  * @tc.desc: Release default type data access.
215  * @tc.type: FUNC
216  * @tc.require: issueIAD2MD
217  */
218 HWTEST_F(El5FilekeyManagerServiceMockTest, ReleaseAccess002, TestSize.Level1)
219 {
220     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
221 
222     MockIpc::SetCallingUid(20020025);
223     uint64_t tokenId = GetTokenIdFromBundleName("com.ohos.medialibrary.medialibrarydata");
224     // if medialibrarydata not exist, try contactsdataability
225     if (tokenId == INVALID_TOKENID) {
226         tokenId = GetTokenIdFromBundleName("com.ohos.contactsdataability");
227     }
228     ASSERT_NE(tokenId, INVALID_TOKENID);
229     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
230 
231     ASSERT_EQ(el5FilekeyManagerService_->ReleaseAccess(DataLockType::DEFAULT_DATA), EFM_SUCCESS);
232 }
233 
234 /**
235  * @tc.name: GenerateAppKey001
236  * @tc.desc: Generate app key by uid and bundle name.
237  * @tc.type: FUNC
238  * @tc.require: issueIAD2MD
239  */
240 HWTEST_F(El5FilekeyManagerServiceMockTest, GenerateAppKey001, TestSize.Level1)
241 {
242     el5FilekeyManagerService_->service_ = nullptr;
243 
244     int32_t uid = 12345;
245     std::string bundleName = "com.ohos.el5_test";
246     std::string keyId;
247 
248     MockIpc::SetCallingUid(3060);
249 
250     ASSERT_EQ(el5FilekeyManagerService_->GenerateAppKey(uid, bundleName, keyId), EFM_SUCCESS);
251 }
252 
253 /**
254  * @tc.name: GenerateAppKey002
255  * @tc.desc: Generate app key by uid and bundle name.
256  * @tc.type: FUNC
257  * @tc.require: issueIAD2MD
258  */
259 HWTEST_F(El5FilekeyManagerServiceMockTest, GenerateAppKey002, TestSize.Level1)
260 {
261     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
262 
263     int32_t uid = 12345;
264     std::string bundleName = "com.ohos.el5_test";
265     std::string keyId;
266 
267     MockIpc::SetCallingUid(3060);
268 
269     ASSERT_EQ(el5FilekeyManagerService_->GenerateAppKey(uid, bundleName, keyId), EFM_SUCCESS);
270 }
271 
272 /**
273  * @tc.name: DeleteAppKey001
274  * @tc.desc: Delete app key by bundle name and user id.
275  * @tc.type: FUNC
276  * @tc.require: issueIAD2MD
277  */
278 HWTEST_F(El5FilekeyManagerServiceMockTest, DeleteAppKey001, TestSize.Level1)
279 {
280     el5FilekeyManagerService_->service_ = nullptr;
281 
282     std::string bundleName = "";
283     int32_t userId = 100;
284 
285     MockIpc::SetCallingUid(3060);
286 
287     ASSERT_EQ(el5FilekeyManagerService_->DeleteAppKey(bundleName, userId), EFM_SUCCESS);
288 }
289 
290 /**
291  * @tc.name: DeleteAppKey002
292  * @tc.desc: Delete app key by bundle name and user id.
293  * @tc.type: FUNC
294  * @tc.require: issueIAD2MD
295  */
296 HWTEST_F(El5FilekeyManagerServiceMockTest, DeleteAppKey002, TestSize.Level1)
297 {
298     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
299 
300     std::string bundleName = "";
301     int32_t userId = 100;
302 
303     MockIpc::SetCallingUid(3060);
304 
305     ASSERT_EQ(el5FilekeyManagerService_->DeleteAppKey(bundleName, userId), EFM_SUCCESS);
306 }
307 
308 /**
309  * @tc.name: GetUserAppKey001
310  * @tc.desc: Find key infos of the specified user id.
311  * @tc.type: FUNC
312  * @tc.require: issueIAD2MD
313  */
314 HWTEST_F(El5FilekeyManagerServiceMockTest, GetUserAppKey001, TestSize.Level1)
315 {
316     el5FilekeyManagerService_->service_ = nullptr;
317 
318     int32_t userId = 100;
319     std::vector<UserAppKeyInfo> keyInfos;
320 
321     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
322     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
323 
324     ASSERT_EQ(el5FilekeyManagerService_->GetUserAppKey(userId, false, keyInfos), EFM_SUCCESS);
325 }
326 
327 /**
328  * @tc.name: GetUserAppKey002
329  * @tc.desc: Find key infos of the specified user id.
330  * @tc.type: FUNC
331  * @tc.require: issueIAD2MD
332  */
333 HWTEST_F(El5FilekeyManagerServiceMockTest, GetUserAppKey002, TestSize.Level1)
334 {
335     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
336 
337     int32_t userId = 100;
338     std::vector<UserAppKeyInfo> keyInfos;
339 
340     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
341     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
342 
343     ASSERT_EQ(el5FilekeyManagerService_->GetUserAppKey(userId, false, keyInfos), EFM_SUCCESS);
344 }
345 
346 /**
347  * @tc.name: GetUserAppKey003
348  * @tc.desc: Find key infos of the specified user id.
349  * @tc.type: FUNC
350  * @tc.require: issueIAD2MD
351  */
352 HWTEST_F(El5FilekeyManagerServiceMockTest, GetUserAppKey003, TestSize.Level1)
353 {
354     OHOS::MessageParcel data;
355     OHOS::MessageParcel reply;
356     OHOS::MessageOption option(OHOS::MessageOption::TF_SYNC);
357 
358     ASSERT_EQ(true, data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor()));
359 
360     data.WriteInt32(100);
361     data.WriteBool(false);
362 
363     ASSERT_EQ(el5FilekeyManagerService_->OnRemoteRequest(
364         static_cast<uint32_t>(El5FilekeyManagerInterfaceIpcCode::COMMAND_GET_USER_APP_KEY), data, reply, option),
365         OHOS::NO_ERROR);
366 }
367 
368 /**
369  * @tc.name: ChangeUserAppkeysLoadInfo001
370  * @tc.desc: Change key infos of the specified user id.
371  * @tc.type: FUNC
372  * @tc.require: issueIAD2MD
373  */
374 HWTEST_F(El5FilekeyManagerServiceMockTest, ChangeUserAppkeysLoadInfo001, TestSize.Level1)
375 {
376     el5FilekeyManagerService_->service_ = nullptr;
377 
378     int32_t userId = 100;
379     std::vector<AppKeyLoadInfo> loadInfos;
380     std::string emptyStr("");
381     loadInfos.emplace_back(AppKeyLoadInfo(emptyStr, true));
382 
383     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
384     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
385 
386     ASSERT_EQ(el5FilekeyManagerService_->ChangeUserAppkeysLoadInfo(userId, loadInfos), EFM_SUCCESS);
387 }
388 
389 /**
390  * @tc.name: ChangeUserAppkeysLoadInfo002
391  * @tc.desc: Change key infos of the specified user id.
392  * @tc.type: FUNC
393  * @tc.require: issueIAD2MD
394  */
395 HWTEST_F(El5FilekeyManagerServiceMockTest, ChangeUserAppkeysLoadInfo002, TestSize.Level1)
396 {
397     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
398 
399     int32_t userId = 100;
400     std::vector<AppKeyLoadInfo> loadInfos;
401     std::string emptyStr("");
402     loadInfos.emplace_back(AppKeyLoadInfo(emptyStr, true));
403 
404     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
405     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
406 
407     ASSERT_EQ(el5FilekeyManagerService_->ChangeUserAppkeysLoadInfo(userId, loadInfos), EFM_SUCCESS);
408 }
409 
410 /**
411  * @tc.name: SetFilePathPolicy001
412  * @tc.desc: Set path policy.
413  * @tc.type: FUNC
414  * @tc.require: issueIAD2MD
415  */
416 HWTEST_F(El5FilekeyManagerServiceMockTest, SetFilePathPolicy001, TestSize.Level1)
417 {
418     el5FilekeyManagerService_->service_ = nullptr;
419 
420     MockIpc::SetCallingUid(20020025);
421     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
422     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
423 
424     ASSERT_EQ(el5FilekeyManagerService_->SetFilePathPolicy(), EFM_SUCCESS);
425 }
426 
427 /**
428  * @tc.name: SetFilePathPolicy002
429  * @tc.desc: Set path policy.
430  * @tc.type: FUNC
431  * @tc.require: issueIAD2MD
432  */
433 HWTEST_F(El5FilekeyManagerServiceMockTest, SetFilePathPolicy002, TestSize.Level1)
434 {
435     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
436 
437     MockIpc::SetCallingUid(20020025);
438     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
439     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
440 
441     ASSERT_EQ(el5FilekeyManagerService_->SetFilePathPolicy(), EFM_SUCCESS);
442 }
443 
444 /**
445  * @tc.name: RegisterCallback001
446  * @tc.desc: Register app key generation callback.
447  * @tc.type: FUNC
448  * @tc.require: issueIAD2MD
449  */
450 HWTEST_F(El5FilekeyManagerServiceMockTest, RegisterCallback001, TestSize.Level1)
451 {
452     el5FilekeyManagerService_->service_ = nullptr;
453 
454     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("foundation");
455     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
456 
457     ASSERT_EQ(el5FilekeyManagerService_->RegisterCallback((new TestEl5FilekeyCallback())), EFM_SUCCESS);
458 }
459 
460 /**
461  * @tc.name: RegisterCallback002
462  * @tc.desc: Register app key generation callback.
463  * @tc.type: FUNC
464  * @tc.require: issueIAD2MD
465  */
466 HWTEST_F(El5FilekeyManagerServiceMockTest, RegisterCallback002, TestSize.Level1)
467 {
468     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
469 
470     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("foundation");
471     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
472 
473     ASSERT_EQ(el5FilekeyManagerService_->RegisterCallback((new TestEl5FilekeyCallback())), EFM_SUCCESS);
474 }
475 
476 /**
477  * @tc.name: GenerateGroupIDKey001
478  * @tc.desc: Generate data group key by user id and group id.
479  * @tc.type: FUNC
480  * @tc.require: issueIAD2MD
481  */
482 HWTEST_F(El5FilekeyManagerServiceMockTest, GenerateGroupIDKey001, TestSize.Level1)
483 {
484     el5FilekeyManagerService_->service_ = nullptr;
485 
486     uint32_t uid = 100;
487     std::string groupID = "abcdefghijklmn";
488     std::string keyId;
489 
490     MockIpc::SetCallingUid(3060);
491 
492     ASSERT_EQ(el5FilekeyManagerService_->GenerateGroupIDKey(uid, groupID, keyId), EFM_SUCCESS);
493 }
494 
495 /**
496  * @tc.name: GenerateGroupIDKey002
497  * @tc.desc: Generate data group key by user id and group id.
498  * @tc.type: FUNC
499  * @tc.require: issueIAD2MD
500  */
501 HWTEST_F(El5FilekeyManagerServiceMockTest, GenerateGroupIDKey002, TestSize.Level1)
502 {
503     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
504 
505     uint32_t uid = 100;
506     std::string groupID = "abcdefghijklmn";
507     std::string keyId;
508 
509     MockIpc::SetCallingUid(3060);
510 
511     ASSERT_EQ(el5FilekeyManagerService_->GenerateGroupIDKey(uid, groupID, keyId), EFM_SUCCESS);
512 }
513 
514 /**
515  * @tc.name: DeleteGroupIDKey001
516  * @tc.desc: Delete data group key by user id and group id.
517  * @tc.type: FUNC
518  * @tc.require: issueIAD2MD
519  */
520 HWTEST_F(El5FilekeyManagerServiceMockTest, DeleteGroupIDKey001, TestSize.Level1)
521 {
522     el5FilekeyManagerService_->service_ = nullptr;
523 
524     uint32_t uid = 100;
525     std::string groupID = "";
526 
527     MockIpc::SetCallingUid(3060);
528 
529     ASSERT_EQ(el5FilekeyManagerService_->DeleteGroupIDKey(uid, groupID), EFM_SUCCESS);
530 }
531 
532 /**
533  * @tc.name: DeleteGroupIDKey002
534  * @tc.desc: Delete data group key by user id and group id.
535  * @tc.type: FUNC
536  * @tc.require: issueIAD2MD
537  */
538 HWTEST_F(El5FilekeyManagerServiceMockTest, DeleteGroupIDKey002, TestSize.Level1)
539 {
540     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
541 
542     uint32_t uid = 100;
543     std::string groupID = "";
544 
545     MockIpc::SetCallingUid(3060);
546 
547     ASSERT_EQ(el5FilekeyManagerService_->DeleteGroupIDKey(uid, groupID), EFM_SUCCESS);
548 }
549 
550 /**
551  * @tc.name: QueryAppKeyState001
552  * @tc.desc: Query default type app key.
553  * @tc.type: FUNC
554  * @tc.require: issueIAD2MD
555  */
556 HWTEST_F(El5FilekeyManagerServiceMockTest, QueryAppKeyState001, TestSize.Level1)
557 {
558     el5FilekeyManagerService_->service_ = nullptr;
559 
560     MockIpc::SetCallingUid(20020025);
561     uint64_t tokenId = GetTokenIdFromBundleName("com.ohos.medialibrary.medialibrarydata");
562     // if medialibrarydata not exist, try contactsdataability
563     if (tokenId == INVALID_TOKENID) {
564         tokenId = GetTokenIdFromBundleName("com.ohos.contactsdataability");
565     }
566     ASSERT_NE(tokenId, INVALID_TOKENID);
567     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
568 
569     ASSERT_EQ(el5FilekeyManagerService_->QueryAppKeyState(DataLockType::DEFAULT_DATA), EFM_SUCCESS);
570 }
571 
572 /**
573  * @tc.name: QueryAppKeyState002
574  * @tc.desc: Query default type app key.
575  * @tc.type: FUNC
576  * @tc.require: issueIAD2MD
577  */
578 HWTEST_F(El5FilekeyManagerServiceMockTest, QueryAppKeyState002, TestSize.Level1)
579 {
580     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
581 
582     MockIpc::SetCallingUid(20020025);
583     uint64_t tokenId = GetTokenIdFromBundleName("com.ohos.medialibrary.medialibrarydata");
584     // if medialibrarydata not exist, try contactsdataability
585     if (tokenId == INVALID_TOKENID) {
586         tokenId = GetTokenIdFromBundleName("com.ohos.contactsdataability");
587     }
588     ASSERT_NE(tokenId, INVALID_TOKENID);
589     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
590 
591     ASSERT_EQ(el5FilekeyManagerService_->QueryAppKeyState(DataLockType::DEFAULT_DATA), EFM_SUCCESS);
592 }
593 
594 /**
595  * @tc.name: SetPolicyScreenLocked001
596  * @tc.desc: SetPolicyScreenLocked
597  * @tc.type: FUNC
598  * @tc.require: issueIAD2MD
599  */
600 HWTEST_F(El5FilekeyManagerServiceMockTest, SetPolicyScreenLocked001, TestSize.Level1)
601 {
602     el5FilekeyManagerService_->service_ = nullptr;
603 
604     ASSERT_EQ(el5FilekeyManagerService_->SetPolicyScreenLocked(), EFM_SUCCESS);
605 }
606 
607 /**
608  * @tc.name: SetPolicyScreenLocked002
609  * @tc.desc: SetPolicyScreenLocked
610  * @tc.type: FUNC
611  * @tc.require: issueIAD2MD
612  */
613 HWTEST_F(El5FilekeyManagerServiceMockTest, SetPolicyScreenLocked002, TestSize.Level1)
614 {
615     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
616 
617     ASSERT_EQ(el5FilekeyManagerService_->SetPolicyScreenLocked(), EFM_SUCCESS);
618 }
619 
620 /**
621  * @tc.name: Dump001
622  * @tc.desc: Dump fd > 0
623  * @tc.type: FUNC
624  * @tc.require: issueIAD2MD
625  */
626 HWTEST_F(El5FilekeyManagerServiceMockTest, Dump001, TestSize.Level1)
627 {
628     el5FilekeyManagerService_->service_ = nullptr;
629 
630     int fd = 1;
631     std::vector<std::u16string> args = {};
632     ASSERT_EQ(el5FilekeyManagerService_->Dump(fd, args), EFM_SUCCESS);
633 }
634 
635 /**
636  * @tc.name: Dump002
637  * @tc.desc: Dump fd > 0
638  * @tc.type: FUNC
639  * @tc.require: issueIAD2MD
640  */
641 HWTEST_F(El5FilekeyManagerServiceMockTest, Dump002, TestSize.Level1)
642 {
643     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
644 
645     int fd = 1;
646     std::vector<std::u16string> args = {};
647     ASSERT_EQ(el5FilekeyManagerService_->Dump(fd, args), EFM_SUCCESS);
648 }
649 
650 /**
651  * @tc.name: HandleUserCommonEvent001
652  * @tc.desc: HandleUserCommonEvent func test, service_ == null.
653  * @tc.type: FUNC
654  * @tc.require: issueIAD2MD
655  */
656 HWTEST_F(El5FilekeyManagerServiceMockTest, HandleUserCommonEvent001, TestSize.Level1)
657 {
658     el5FilekeyManagerService_->service_ = nullptr;
659     std::string eventName = "abc";
660     int userId = 1;
661     ASSERT_EQ(el5FilekeyManagerService_->HandleUserCommonEvent(eventName, userId), EFM_SUCCESS);
662 }
663 
664 /**
665  * @tc.name: HandleUserCommonEvent002
666  * @tc.desc: HandleUserCommonEvent func test, service_ != null.
667  * @tc.type: FUNC
668  * @tc.require: issueIAD2MD
669  */
670 HWTEST_F(El5FilekeyManagerServiceMockTest, HandleUserCommonEvent002, TestSize.Level1)
671 {
672     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
673     std::string eventName = "abc";
674     int userId = 1;
675     ASSERT_EQ(el5FilekeyManagerService_->HandleUserCommonEvent(eventName, userId), EFM_SUCCESS);
676 }
677 
678 /**
679  * @tc.name: OnRemoteRequest001
680  * @tc.desc: El5FilekeyCallbackStub function test OnRemoteRequest001.
681  * @tc.type: FUNC
682  * @tc.require: Issue Number
683  */
684 HWTEST_F(El5FilekeyManagerServiceMockTest, OnRemoteRequest001, TestSize.Level1)
685 {
686     TestEl5FilekeyCallback testEl5FilekeyCallback;
687     OHOS::MessageParcel data;
688     OHOS::MessageParcel reply;
689     OHOS::MessageOption option;
690     uint32_t code = static_cast<uint32_t>(El5FilekeyCallbackInterfaceIpcCode::COMMAND_ON_REGENERATE_APP_KEY);
691 
692     ASSERT_EQ(data.WriteInterfaceToken(El5FilekeyCallbackInterface::GetDescriptor()), true);
693     data.WriteUint32(1); // infosSize
694     data.WriteInt32(1);  // AppKeyInfo size
695     data.WriteUint32(static_cast<uint32_t>(AppKeyType::APP));
696     data.WriteUint32(1000);
697     std::string bundleName = "ohos.permission.test";
698     data.WriteString(bundleName);
699     data.WriteInt32(100);
700     data.WriteString("testGroupId");
701     ASSERT_EQ(testEl5FilekeyCallback.OnRemoteRequest(code, data, reply, option), OHOS::NO_ERROR);
702 }
703 
704 /**
705  * @tc.name: Marshalling001
706  * @tc.desc: AppKeyInfo function test Marshalling.
707  * @tc.type: FUNC
708  * @tc.require: Issue Number
709  */
710 HWTEST_F(El5FilekeyManagerServiceMockTest, Marshalling001, TestSize.Level1)
711 {
712     AppKeyInfo appKeyInfo;
713     appKeyInfo.uid = 1000;
714     appKeyInfo.bundleName = "test";
715     appKeyInfo.userId = 200;
716     appKeyInfo.groupID = "testGroupId";
717     OHOS::Parcel parcel;
718     ASSERT_EQ(appKeyInfo.Marshalling(parcel), true);
719 }
720 
721 /**
722  * @tc.name: Unmarshalling001
723  * @tc.desc: AppKeyInfo function test Marshalling.
724  * @tc.type: FUNC
725  * @tc.require: Issue Number
726  */
727 HWTEST_F(El5FilekeyManagerServiceMockTest, Unmarshalling001, TestSize.Level1)
728 {
729     AppKeyInfo appKeyInfo;
730     OHOS::Parcel parcel;
731     parcel.WriteUint32(static_cast<uint32_t>(AppKeyType::GROUPID));
732     parcel.WriteUint32(1000);
733     parcel.WriteString("ohos.permission.test");
734     parcel.WriteInt32(100);
735     parcel.WriteString("testGroupId");
736     auto info = appKeyInfo.Unmarshalling(parcel);
737     ASSERT_EQ(info != nullptr, true);
738     ASSERT_EQ(info->bundleName, "ohos.permission.test");
739 }
740