1 /*
2  * Copyright (c) 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 
16 #include "el5_filekey_manager_service_mock_unittest.h"
17 
18 #include "accesstoken_kit.h"
19 #include "el5_filekey_callback_stub.h"
20 #include "mock_ipc.h"
21 #include "token_setproc.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS::Security::AccessToken;
25 
SetUpTestCase()26 void El5FilekeyManagerServiceMockTest::SetUpTestCase()
27 {
28 }
29 
TearDownTestCase()30 void El5FilekeyManagerServiceMockTest::TearDownTestCase()
31 {
32 }
33 
SetUp()34 void El5FilekeyManagerServiceMockTest::SetUp()
35 {
36     el5FilekeyManagerService_ = DelayedSingleton<El5FilekeyManagerService>::GetInstance();
37     el5FilekeyManagerService_->Init();
38     el5FilekeyManagerService_->service_ = nullptr;
39 }
40 
TearDown()41 void El5FilekeyManagerServiceMockTest::TearDown()
42 {
43 }
44 
45 class TestEl5FilekeyCallback : public El5FilekeyCallbackStub {
46 public:
OnRegenerateAppKey(std::vector<AppKeyInfo> & infos)47     void OnRegenerateAppKey(std::vector<AppKeyInfo> &infos)
48     {
49         GTEST_LOG_(INFO) << "OnRegenerateAppKey.";
50     }
51 };
52 
53 class TestEl5FilekeyServiceExt : public El5FilekeyServiceExtInterface {
54 public:
AcquireAccess(DataLockType type,bool isApp)55     int32_t AcquireAccess(DataLockType type, bool isApp)
56     {
57         return EFM_SUCCESS;
58     }
59 
ReleaseAccess(DataLockType type,bool isApp)60     int32_t ReleaseAccess(DataLockType type, bool isApp)
61     {
62         return EFM_SUCCESS;
63     }
64 
GenerateAppKey(uint32_t uid,const std::string & bundleName,std::string & keyId)65     int32_t GenerateAppKey(uint32_t uid, const std::string& bundleName, std::string& keyId)
66     {
67         return EFM_SUCCESS;
68     }
69 
DeleteAppKey(const std::string & bundleName,int32_t userId)70     int32_t DeleteAppKey(const std::string& bundleName, int32_t userId)
71     {
72         return EFM_SUCCESS;
73     }
74 
GetUserAppKey(int32_t userId,bool getAllFlag,std::vector<std::pair<int32_t,std::string>> & keyInfos)75     int32_t GetUserAppKey(int32_t userId, bool getAllFlag, std::vector<std::pair<int32_t, std::string>> &keyInfos)
76     {
77         int32_t key = 111;
78         std::string info = "test";
79         keyInfos.emplace_back(std::make_pair(key, info));
80         return EFM_SUCCESS;
81     }
82 
ChangeUserAppkeysLoadInfo(int32_t userId,std::vector<std::pair<std::string,bool>> & loadInfos)83     int32_t ChangeUserAppkeysLoadInfo(int32_t userId, std::vector<std::pair<std::string, bool>> &loadInfos)
84     {
85         return EFM_SUCCESS;
86     }
87 
SetFilePathPolicy(int32_t userId)88     int32_t SetFilePathPolicy(int32_t userId)
89     {
90         return EFM_SUCCESS;
91     }
92 
HandleUserCommonEvent(const std::string & eventName,int32_t userId)93     int32_t HandleUserCommonEvent(const std::string &eventName, int32_t userId)
94     {
95         return EFM_SUCCESS;
96     }
97 
SetPolicyScreenLocked()98     int32_t SetPolicyScreenLocked()
99     {
100         return EFM_SUCCESS;
101     }
102 
DumpData(int fd,const std::vector<std::u16string> & args)103     int32_t DumpData(int fd, const std::vector<std::u16string>& args)
104     {
105         return EFM_SUCCESS;
106     }
107 
RegisterCallback(const OHOS::sptr<El5FilekeyCallbackInterface> & callback)108     int32_t RegisterCallback(const OHOS::sptr<El5FilekeyCallbackInterface> &callback)
109     {
110         return EFM_SUCCESS;
111     }
112 };
113 
114 /**
115  * @tc.name: AcquireAccess001
116  * @tc.desc: Acquire default type data access.
117  * @tc.type: FUNC
118  * @tc.require: issueIAD2MD
119  */
120 HWTEST_F(El5FilekeyManagerServiceMockTest, AcquireAccess001, TestSize.Level1)
121 {
122     el5FilekeyManagerService_->service_ = nullptr;
123 
124     MockIpc::SetCallingUid(20020025);
125     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
126     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
127 
128     ASSERT_EQ(el5FilekeyManagerService_->AcquireAccess(DEFAULT_DATA), EFM_SUCCESS);
129 }
130 
131 /**
132  * @tc.name: AcquireAccess002
133  * @tc.desc: Acquire default type data access.
134  * @tc.type: FUNC
135  * @tc.require: issueIAD2MD
136  */
137 HWTEST_F(El5FilekeyManagerServiceMockTest, AcquireAccess002, TestSize.Level1)
138 {
139     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
140 
141     MockIpc::SetCallingUid(20020025);
142     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
143     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
144 
145     ASSERT_EQ(el5FilekeyManagerService_->AcquireAccess(DEFAULT_DATA), EFM_SUCCESS);
146 }
147 
148 /**
149  * @tc.name: ReleaseAccess001
150  * @tc.desc: Release default type data access.
151  * @tc.type: FUNC
152  * @tc.require: issueIAD2MD
153  */
154 HWTEST_F(El5FilekeyManagerServiceMockTest, ReleaseAccess001, TestSize.Level1)
155 {
156     el5FilekeyManagerService_->service_ = nullptr;
157 
158     MockIpc::SetCallingUid(20020025);
159     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
160     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
161 
162     ASSERT_EQ(el5FilekeyManagerService_->ReleaseAccess(DEFAULT_DATA), EFM_SUCCESS);
163 }
164 
165 /**
166  * @tc.name: ReleaseAccess002
167  * @tc.desc: Release default type data access.
168  * @tc.type: FUNC
169  * @tc.require: issueIAD2MD
170  */
171 HWTEST_F(El5FilekeyManagerServiceMockTest, ReleaseAccess002, TestSize.Level1)
172 {
173     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
174 
175     MockIpc::SetCallingUid(20020025);
176     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
177     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
178 
179     ASSERT_EQ(el5FilekeyManagerService_->ReleaseAccess(DEFAULT_DATA), EFM_SUCCESS);
180 }
181 
182 /**
183  * @tc.name: GenerateAppKey001
184  * @tc.desc: Generate app key by uid and bundle name.
185  * @tc.type: FUNC
186  * @tc.require: issueIAD2MD
187  */
188 HWTEST_F(El5FilekeyManagerServiceMockTest, GenerateAppKey001, TestSize.Level1)
189 {
190     el5FilekeyManagerService_->service_ = nullptr;
191 
192     int32_t uid = 12345;
193     std::string bundleName = "com.ohos.el5_test";
194     std::string keyId;
195 
196     MockIpc::SetCallingUid(3060);
197 
198     ASSERT_EQ(el5FilekeyManagerService_->GenerateAppKey(uid, bundleName, keyId), EFM_SUCCESS);
199 }
200 
201 /**
202  * @tc.name: GenerateAppKey002
203  * @tc.desc: Generate app key by uid and bundle name.
204  * @tc.type: FUNC
205  * @tc.require: issueIAD2MD
206  */
207 HWTEST_F(El5FilekeyManagerServiceMockTest, GenerateAppKey002, TestSize.Level1)
208 {
209     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
210 
211     int32_t uid = 12345;
212     std::string bundleName = "com.ohos.el5_test";
213     std::string keyId;
214 
215     MockIpc::SetCallingUid(3060);
216 
217     ASSERT_EQ(el5FilekeyManagerService_->GenerateAppKey(uid, bundleName, keyId), EFM_SUCCESS);
218 }
219 
220 /**
221  * @tc.name: DeleteAppKey001
222  * @tc.desc: Delete app key by bundle name and user id.
223  * @tc.type: FUNC
224  * @tc.require: issueIAD2MD
225  */
226 HWTEST_F(El5FilekeyManagerServiceMockTest, DeleteAppKey001, TestSize.Level1)
227 {
228     el5FilekeyManagerService_->service_ = nullptr;
229 
230     std::string bundleName = "";
231     int32_t userId = 100;
232 
233     MockIpc::SetCallingUid(3060);
234 
235     ASSERT_EQ(el5FilekeyManagerService_->DeleteAppKey(bundleName, userId), EFM_SUCCESS);
236 }
237 
238 /**
239  * @tc.name: DeleteAppKey002
240  * @tc.desc: Delete app key by bundle name and user id.
241  * @tc.type: FUNC
242  * @tc.require: issueIAD2MD
243  */
244 HWTEST_F(El5FilekeyManagerServiceMockTest, DeleteAppKey002, TestSize.Level1)
245 {
246     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
247 
248     std::string bundleName = "";
249     int32_t userId = 100;
250 
251     MockIpc::SetCallingUid(3060);
252 
253     ASSERT_EQ(el5FilekeyManagerService_->DeleteAppKey(bundleName, userId), EFM_SUCCESS);
254 }
255 
256 /**
257  * @tc.name: GetUserAppKey001
258  * @tc.desc: Find key infos of the specified user id.
259  * @tc.type: FUNC
260  * @tc.require: issueIAD2MD
261  */
262 HWTEST_F(El5FilekeyManagerServiceMockTest, GetUserAppKey001, TestSize.Level1)
263 {
264     el5FilekeyManagerService_->service_ = nullptr;
265 
266     int32_t userId = 100;
267     std::vector<std::pair<int32_t, std::string>> keyInfos;
268 
269     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
270     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
271 
272     ASSERT_EQ(el5FilekeyManagerService_->GetUserAppKey(userId, false, keyInfos), EFM_SUCCESS);
273 }
274 
275 /**
276  * @tc.name: GetUserAppKey002
277  * @tc.desc: Find key infos of the specified user id.
278  * @tc.type: FUNC
279  * @tc.require: issueIAD2MD
280  */
281 HWTEST_F(El5FilekeyManagerServiceMockTest, GetUserAppKey002, TestSize.Level1)
282 {
283     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
284 
285     int32_t userId = 100;
286     std::vector<std::pair<int32_t, std::string>> keyInfos;
287 
288     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
289     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
290 
291     ASSERT_EQ(el5FilekeyManagerService_->GetUserAppKey(userId, false, keyInfos), EFM_SUCCESS);
292 }
293 
294 /**
295  * @tc.name: GetUserAppKey003
296  * @tc.desc: Find key infos of the specified user id.
297  * @tc.type: FUNC
298  * @tc.require: issueIAD2MD
299  */
300 HWTEST_F(El5FilekeyManagerServiceMockTest, GetUserAppKey003, TestSize.Level1)
301 {
302     OHOS::MessageParcel data;
303     OHOS::MessageParcel reply;
304     OHOS::MessageOption option(OHOS::MessageOption::TF_SYNC);
305 
306     ASSERT_EQ(true, data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor()));
307 
308     data.WriteInt32(100);
309     data.WriteBool(false);
310 
311     ASSERT_EQ(el5FilekeyManagerService_->OnRemoteRequest(
312         static_cast<uint32_t>(EFMInterfaceCode::GET_USER_APP_KEY), data, reply, option), OHOS::NO_ERROR);
313 }
314 
315 /**
316  * @tc.name: ChangeUserAppkeysLoadInfo001
317  * @tc.desc: Change key infos of the specified user id.
318  * @tc.type: FUNC
319  * @tc.require: issueIAD2MD
320  */
321 HWTEST_F(El5FilekeyManagerServiceMockTest, ChangeUserAppkeysLoadInfo001, TestSize.Level1)
322 {
323     el5FilekeyManagerService_->service_ = nullptr;
324 
325     int32_t userId = 100;
326     std::vector<std::pair<std::string, bool>> loadInfos;
327     loadInfos.emplace_back(std::make_pair("", true));
328 
329     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
330     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
331 
332     ASSERT_EQ(el5FilekeyManagerService_->ChangeUserAppkeysLoadInfo(userId, loadInfos), EFM_SUCCESS);
333 }
334 
335 /**
336  * @tc.name: ChangeUserAppkeysLoadInfo002
337  * @tc.desc: Change key infos of the specified user id.
338  * @tc.type: FUNC
339  * @tc.require: issueIAD2MD
340  */
341 HWTEST_F(El5FilekeyManagerServiceMockTest, ChangeUserAppkeysLoadInfo002, TestSize.Level1)
342 {
343     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
344 
345     int32_t userId = 100;
346     std::vector<std::pair<std::string, bool>> loadInfos;
347     loadInfos.emplace_back(std::make_pair("", true));
348 
349     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("storage_daemon");
350     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
351 
352     ASSERT_EQ(el5FilekeyManagerService_->ChangeUserAppkeysLoadInfo(userId, loadInfos), EFM_SUCCESS);
353 }
354 
355 /**
356  * @tc.name: SetFilePathPolicy001
357  * @tc.desc: Set path policy.
358  * @tc.type: FUNC
359  * @tc.require: issueIAD2MD
360  */
361 HWTEST_F(El5FilekeyManagerServiceMockTest, SetFilePathPolicy001, TestSize.Level1)
362 {
363     el5FilekeyManagerService_->service_ = nullptr;
364 
365     MockIpc::SetCallingUid(20020025);
366     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
367     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
368 
369     ASSERT_EQ(el5FilekeyManagerService_->SetFilePathPolicy(), EFM_SUCCESS);
370 }
371 
372 /**
373  * @tc.name: SetFilePathPolicy002
374  * @tc.desc: Set path policy.
375  * @tc.type: FUNC
376  * @tc.require: issueIAD2MD
377  */
378 HWTEST_F(El5FilekeyManagerServiceMockTest, SetFilePathPolicy002, TestSize.Level1)
379 {
380     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
381 
382     MockIpc::SetCallingUid(20020025);
383     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.medialibrary.medialibrarydata", 0);
384     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
385 
386     ASSERT_EQ(el5FilekeyManagerService_->SetFilePathPolicy(), EFM_SUCCESS);
387 }
388 
389 /**
390  * @tc.name: RegisterCallback001
391  * @tc.desc: Register app key generation callback.
392  * @tc.type: FUNC
393  * @tc.require: issueIAD2MD
394  */
395 HWTEST_F(El5FilekeyManagerServiceMockTest, RegisterCallback001, TestSize.Level1)
396 {
397     el5FilekeyManagerService_->service_ = nullptr;
398 
399     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("foundation");
400     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
401 
402     ASSERT_EQ(el5FilekeyManagerService_->RegisterCallback((new TestEl5FilekeyCallback())), EFM_SUCCESS);
403 }
404 
405 /**
406  * @tc.name: RegisterCallback002
407  * @tc.desc: Register app key generation callback.
408  * @tc.type: FUNC
409  * @tc.require: issueIAD2MD
410  */
411 HWTEST_F(El5FilekeyManagerServiceMockTest, RegisterCallback002, TestSize.Level1)
412 {
413     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
414 
415     AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId("foundation");
416     MockIpc::SetCallingTokenID(static_cast<uint32_t>(tokenId));
417 
418     ASSERT_EQ(el5FilekeyManagerService_->RegisterCallback((new TestEl5FilekeyCallback())), EFM_SUCCESS);
419 }
420 
421 /**
422  * @tc.name: SetPolicyScreenLocked001
423  * @tc.desc: SetPolicyScreenLocked
424  * @tc.type: FUNC
425  * @tc.require: issueIAD2MD
426  */
427 HWTEST_F(El5FilekeyManagerServiceMockTest, SetPolicyScreenLocked001, TestSize.Level1)
428 {
429     el5FilekeyManagerService_->service_ = nullptr;
430 
431     ASSERT_EQ(el5FilekeyManagerService_->SetPolicyScreenLocked(), EFM_SUCCESS);
432 }
433 
434 /**
435  * @tc.name: SetPolicyScreenLocked002
436  * @tc.desc: SetPolicyScreenLocked
437  * @tc.type: FUNC
438  * @tc.require: issueIAD2MD
439  */
440 HWTEST_F(El5FilekeyManagerServiceMockTest, SetPolicyScreenLocked002, TestSize.Level1)
441 {
442     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
443 
444     ASSERT_EQ(el5FilekeyManagerService_->SetPolicyScreenLocked(), EFM_SUCCESS);
445 }
446 
447 /**
448  * @tc.name: Dump001
449  * @tc.desc: Dump fd > 0
450  * @tc.type: FUNC
451  * @tc.require: issueIAD2MD
452  */
453 HWTEST_F(El5FilekeyManagerServiceMockTest, Dump001, TestSize.Level1)
454 {
455     el5FilekeyManagerService_->service_ = nullptr;
456 
457     int fd = 1;
458     std::vector<std::u16string> args = {};
459     ASSERT_EQ(el5FilekeyManagerService_->Dump(fd, args), EFM_SUCCESS);
460 }
461 
462 /**
463  * @tc.name: Dump002
464  * @tc.desc: Dump fd > 0
465  * @tc.type: FUNC
466  * @tc.require: issueIAD2MD
467  */
468 HWTEST_F(El5FilekeyManagerServiceMockTest, Dump002, TestSize.Level1)
469 {
470     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
471 
472     int fd = 1;
473     std::vector<std::u16string> args = {};
474     ASSERT_EQ(el5FilekeyManagerService_->Dump(fd, args), EFM_SUCCESS);
475 }
476 
477 /**
478  * @tc.name: HandleUserCommonEvent001
479  * @tc.desc: HandleUserCommonEvent func test, service_ == null.
480  * @tc.type: FUNC
481  * @tc.require: issueIAD2MD
482  */
483 HWTEST_F(El5FilekeyManagerServiceMockTest, HandleUserCommonEvent001, TestSize.Level1)
484 {
485     el5FilekeyManagerService_->service_ = nullptr;
486     std::string eventName = "abc";
487     int userId = 1;
488     ASSERT_EQ(el5FilekeyManagerService_->HandleUserCommonEvent(eventName, userId), EFM_SUCCESS);
489 }
490 
491 /**
492  * @tc.name: HandleUserCommonEvent002
493  * @tc.desc: HandleUserCommonEvent func test, service_ != null.
494  * @tc.type: FUNC
495  * @tc.require: issueIAD2MD
496  */
497 HWTEST_F(El5FilekeyManagerServiceMockTest, HandleUserCommonEvent002, TestSize.Level1)
498 {
499     el5FilekeyManagerService_->service_ = new TestEl5FilekeyServiceExt();
500     std::string eventName = "abc";
501     int userId = 1;
502     ASSERT_EQ(el5FilekeyManagerService_->HandleUserCommonEvent(eventName, userId), EFM_SUCCESS);
503 }
504