• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include <unistd.h>
18 #include <vector>
19 
20 #include "accesstoken_kit.h"
21 #include "account_error_no.h"
22 #include "account_iam_callback_stub.h"
23 #include "account_log_wrapper.h"
24 #include "account_test_common.h"
25 #include "iam_common_defines.h"
26 #define private public
27 #include "inner_account_iam_manager.h"
28 #undef private
29 #include "istorage_manager.h"
30 #include "parameter.h"
31 #include "token_setproc.h"
32 
33 using namespace testing;
34 using namespace testing::ext;
35 using namespace OHOS;
36 using namespace OHOS::AccountSA;
37 using namespace Security::AccessToken;
38 using namespace StorageManager;
39 
40 namespace OHOS {
41 namespace AccountTest {
42 namespace {
43     const int32_t TEST_EXIST_ID = 100;
44     const int32_t TEST_USER_ID = 101;
45     const int32_t UPDATE_USER_ID = 102;
46     const int32_t UPDATE_FAIL_USER_ID = 103;
47     const int32_t TEST_OTHER_ID = 112;
48     const int32_t TEST_ID = 2222;
49     const int32_t ERROR_STORAGE_KEY_NOT_EXIST = -2;
50     const std::vector<uint8_t> TEST_CHALLENGE = {1, 2, 3, 4};
51     static bool g_fscryptEnable = false;
52     const uid_t ACCOUNT_UID = 3058;
53     const int32_t WAIT_TIME = 20;
54 }
55 
56 class MockStorageMgrProxy : public StorageManager::IStorageManager {
57 public:
MockStorageMgrProxy()58     MockStorageMgrProxy()
59     {}
~MockStorageMgrProxy()60     ~MockStorageMgrProxy()
61     {}
62     int32_t UpdateUserAuth(uint32_t userId, uint64_t secureUid,
63                             const std::vector<uint8_t> &token,
64                             const std::vector<uint8_t> &oldSecret,
65                             const std::vector<uint8_t> &newSecret);
66 
UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> & authToken,const std::vector<uint8_t> & newSecret,uint64_t secureUid,uint32_t userId,std::vector<std::vector<uint8_t>> & plainText)67     int32_t UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> &authToken,
68         const std::vector<uint8_t> &newSecret, uint64_t secureUid,
69         uint32_t userId, std::vector<std::vector<uint8_t>> &plainText)
70     {
71         return 0;
72     }
73 
PrepareAddUser(int32_t userId,uint32_t flags)74     int32_t PrepareAddUser(int32_t userId, uint32_t flags)
75     {
76         return 0;
77     }
78 
RemoveUser(int32_t userId,uint32_t flags)79     int32_t RemoveUser(int32_t userId, uint32_t flags)
80     {
81         return 0;
82     }
83 
PrepareStartUser(int32_t userId)84     int32_t PrepareStartUser(int32_t userId)
85     {
86         return 0;
87     }
88 
StopUser(int32_t userId)89     int32_t StopUser(int32_t userId)
90     {
91         return 0;
92     }
93 
CompleteAddUser(int32_t userId)94     int32_t CompleteAddUser(int32_t userId)
95     {
96         return 0;
97     }
98 
NotifyMtpMounted(const std::string & id,const std::string & path,const std::string & desc,const std::string & uuid)99     int32_t NotifyMtpMounted(const std::string &id, const std::string &path, const std::string &desc,
100                          const std::string &uuid)
101     {
102         return 0;
103     }
104 
NotifyMtpUnmounted(const std::string & id,const std::string & path,const bool isBadRemove)105     int32_t NotifyMtpUnmounted(const std::string &id, const std::string &path, const bool isBadRemove)
106     {
107         return 0;
108     }
109 
GetFreeSizeOfVolume(std::string volumeUuid,int64_t & freeSize)110     int32_t GetFreeSizeOfVolume(std::string volumeUuid, int64_t &freeSize)
111     {
112         return 0;
113     }
114 
GetTotalSizeOfVolume(std::string volumeUuid,int64_t & totalSize)115     int32_t GetTotalSizeOfVolume(std::string volumeUuid, int64_t &totalSize)
116     {
117         return 0;
118     }
119 
GetBundleStats(std::string pkgName,BundleStats & bundleStats,int32_t appIndex,uint32_t statFlag)120     int32_t GetBundleStats(std::string pkgName, BundleStats &bundleStats, int32_t appIndex, uint32_t statFlag)
121     {
122         return 0;
123     }
124 
GetSystemSize(int64_t & systemSize)125     int32_t GetSystemSize(int64_t &systemSize)
126     {
127         return 0;
128     }
129 
GetTotalSize(int64_t & totalSize)130     int32_t GetTotalSize(int64_t &totalSize)
131     {
132         return 0;
133     }
134 
GetFreeSize(int64_t & freeSize)135     int32_t GetFreeSize(int64_t &freeSize)
136     {
137         return 0;
138     }
139 
GetUserStorageStats(StorageStats & storageStats)140     int32_t GetUserStorageStats(StorageStats &storageStats)
141     {
142         return 0;
143     }
144 
GetUserStorageStats(int32_t userId,StorageStats & storageStats)145     int32_t GetUserStorageStats(int32_t userId, StorageStats &storageStats)
146     {
147         return 0;
148     }
149 
GetUserStorageStatsByType(int32_t userId,StorageStats & storageStats,std::string type)150     int32_t GetUserStorageStatsByType(int32_t userId, StorageStats &storageStats, std::string type)
151     {
152         return 0;
153     }
154 
GetCurrentBundleStats(BundleStats & bundleStats,uint32_t statFlag)155     int32_t GetCurrentBundleStats(BundleStats &bundleStats, uint32_t statFlag)
156     {
157         return 0;
158     }
159 
NotifyVolumeCreated(VolumeCore vc)160     int32_t NotifyVolumeCreated(VolumeCore vc)
161     {
162         return 0;
163     }
164 
NotifyVolumeMounted(std::string volumeId,int fsType,std::string fsUuid,std::string path,std::string description)165     int32_t NotifyVolumeMounted(std::string volumeId, int fsType, std::string fsUuid,
166                             std::string path, std::string description)
167     {
168         return 0;
169     }
170 
NotifyVolumeStateChanged(std::string volumeId,VolumeState state)171     int32_t NotifyVolumeStateChanged(std::string volumeId, VolumeState state)
172     {
173         return 0;
174     }
175 
Mount(std::string volumeId)176     int32_t Mount(std::string volumeId)
177     {
178         return 0;
179     }
180 
Unmount(std::string volumeId)181     int32_t Unmount(std::string volumeId)
182     {
183         return 0;
184     }
185 
GetAllVolumes(std::vector<VolumeExternal> & vecOfVol)186     int32_t GetAllVolumes(std::vector<VolumeExternal> &vecOfVol)
187     {
188         return 0;
189     }
190 
NotifyDiskCreated(Disk disk)191     int32_t NotifyDiskCreated(Disk disk)
192     {
193         return 0;
194     }
195 
NotifyDiskDestroyed(std::string diskId)196     int32_t NotifyDiskDestroyed(std::string diskId)
197     {
198         return 0;
199     }
200 
Partition(std::string diskId,int32_t type)201     int32_t Partition(std::string diskId, int32_t type)
202     {
203         return 0;
204     }
205 
GetAllDisks(std::vector<Disk> & vecOfDisk)206     int32_t GetAllDisks(std::vector<Disk> &vecOfDisk)
207     {
208         return 0;
209     }
210 
GetVolumeByUuid(std::string fsUuid,VolumeExternal & vc)211     int32_t GetVolumeByUuid(std::string fsUuid, VolumeExternal &vc)
212     {
213         return 0;
214     }
215 
GetVolumeById(std::string volumeId,VolumeExternal & vc)216     int32_t GetVolumeById(std::string volumeId, VolumeExternal &vc)
217     {
218         return 0;
219     }
220 
SetVolumeDescription(std::string fsUuid,std::string description)221     int32_t SetVolumeDescription(std::string fsUuid, std::string description)
222     {
223         return 0;
224     }
225 
Format(std::string volumeId,std::string fsType)226     int32_t Format(std::string volumeId, std::string fsType)
227     {
228         return 0;
229     }
230 
GetDiskById(std::string diskId,Disk & disk)231     int32_t GetDiskById(std::string diskId, Disk &disk)
232     {
233         return 0;
234     }
235 
GenerateUserKeys(uint32_t userId,uint32_t flags)236     int32_t GenerateUserKeys(uint32_t userId, uint32_t flags)
237     {
238         return 0;
239     }
240 
DeleteUserKeys(uint32_t userId)241     int32_t DeleteUserKeys(uint32_t userId)
242     {
243         return 0;
244     }
245 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)246     int32_t ActiveUserKey(uint32_t userId, const std::vector<uint8_t> &token, const std::vector<uint8_t> &secret)
247     {
248         if (userId == TEST_USER_ID) {
249             return 0;
250         }
251         if (userId == TEST_OTHER_ID) {
252             return ERROR_STORAGE_KEY_NOT_EXIST;
253         }
254         return -1;
255     }
256 
InactiveUserKey(uint32_t userId)257     int32_t InactiveUserKey(uint32_t userId)
258     {
259         return 0;
260     }
261 
LockUserScreen(uint32_t userId)262     int32_t LockUserScreen(uint32_t userId)
263     {
264         return 0;
265     }
266 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)267     int32_t GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
268     {
269         return 0;
270     }
271 
GenerateAppkey(uint32_t hashId,uint32_t userId,std::string & keyId)272     int32_t GenerateAppkey(uint32_t hashId, uint32_t userId, std::string &keyId)
273     {
274         return 0;
275     }
276 
DeleteAppkey(const std::string keyId)277     int32_t DeleteAppkey(const std::string keyId)
278     {
279         return 0;
280     }
281 
CreateRecoverKey(uint32_t userId,uint32_t userType,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)282     int32_t CreateRecoverKey(uint32_t userId,
283                              uint32_t userType,
284                              const std::vector<uint8_t> &token,
285                              const std::vector<uint8_t> &secret)
286     {
287         return 0;
288     }
289 
SetRecoverKey(const std::vector<uint8_t> & key)290     int32_t SetRecoverKey(const std::vector<uint8_t> &key)
291     {
292         return 0;
293     }
294 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)295     int32_t UnlockUserScreen(uint32_t userId, const std::vector<uint8_t> &token, const std::vector<uint8_t> &secret)
296     {
297         return 0;
298     }
299 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)300     int32_t MountDfsDocs(int32_t userId, const std::string &relativePath,
301         const std::string &networkId, const std::string &deviceId)
302     {
303         return 0;
304     }
305 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)306     int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
307     {
308         return 0;
309     }
310 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)311     int32_t UMountDfsDocs(int32_t userId, const std::string &relativePath,
312         const std::string &networkId, const std::string &deviceId)
313     {
314         return 0;
315     }
316 
UpdateKeyContext(uint32_t userId,bool needRemoveTmpKey=false)317     int32_t UpdateKeyContext(uint32_t userId, bool needRemoveTmpKey = false)
318     {
319         return g_fscryptEnable ? ERROR_STORAGE_KEY_NOT_EXIST : 0;
320     }
321 
GetUserNeedActiveStatus(uint32_t userId,bool & needActive)322     int32_t GetUserNeedActiveStatus(uint32_t userId, bool &needActive)
323     {
324         return 0;
325     }
326 
CreateShareFile(const std::vector<std::string> & uriList,uint32_t tokenId,uint32_t flag)327     std::vector<int32_t> CreateShareFile(const std::vector<std::string> &uriList, uint32_t tokenId, uint32_t flag)
328     {
329         return std::vector<int32_t>{0};
330     }
331 
DeleteShareFile(uint32_t tokenId,const std::vector<std::string> & uriList)332     int32_t DeleteShareFile(uint32_t tokenId, const std::vector<std::string> &uriList)
333     {
334         return 0;
335     }
336 
SetBundleQuota(const std::string & bundleName,int32_t uid,const std::string & bundleDataDirPath,int32_t limitSizeMb)337     int32_t SetBundleQuota(const std::string &bundleName, int32_t uid, const std::string &bundleDataDirPath,
338         int32_t limitSizeMb)
339     {
340         return 0;
341     }
342 
UpdateMemoryPara(int32_t size,int32_t & oldSize)343     virtual int32_t UpdateMemoryPara(int32_t size, int32_t &oldSize)
344     {
345         return 0;
346     }
347 
GetBundleStatsForIncrease(uint32_t userId,const std::vector<std::string> & bundleNames,const std::vector<int64_t> & incrementalBackTimes,std::vector<int64_t> & pkgFileSizes,std::vector<int64_t> & incPkgFileSizes)348     int32_t GetBundleStatsForIncrease(uint32_t userId, const std::vector<std::string> &bundleNames,
349         const std::vector<int64_t> &incrementalBackTimes, std::vector<int64_t> &pkgFileSizes,
350         std::vector<int64_t> &incPkgFileSizes)
351     {
352         return 0;
353     }
354 
MountMediaFuse(int32_t userId,int32_t & devFd)355     int32_t MountMediaFuse(int32_t userId, int32_t &devFd)
356     {
357         return 0;
358     }
359 
UMountMediaFuse(int32_t userId)360     int32_t UMountMediaFuse(int32_t userId)
361     {
362         return 0;
363     }
364 
MountFileMgrFuse(int32_t userId,const std::string & path,int32_t & fuseFd)365     int32_t MountFileMgrFuse(int32_t userId, const std::string &path, int32_t &fuseFd)
366     {
367         return 0;
368     }
369 
UMountFileMgrFuse(int32_t userId,const std::string & path)370     int32_t UMountFileMgrFuse(int32_t userId, const std::string &path)
371     {
372         return 0;
373     }
374 
AsObject()375     sptr<IRemoteObject> AsObject()
376     {
377         return nullptr;
378     }
379 };
380 
UpdateUserAuth(uint32_t userId,uint64_t secureUid,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)381 int32_t MockStorageMgrProxy::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
382                                             const std::vector<uint8_t> &token,
383                                             const std::vector<uint8_t> &oldSecret,
384                                             const std::vector<uint8_t> &newSecret)
385 {
386     const uint32_t dataSize = 4;
387     if (userId == UPDATE_USER_ID) {
388         return 0;
389     }
390     if (userId == UPDATE_FAIL_USER_ID) {
391         return -1;
392     }
393     if (!g_fscryptEnable) {
394         return 0;
395     }
396     if (newSecret.size() == dataSize || userId == TEST_OTHER_ID) {
397         return g_fscryptEnable ? ERROR_STORAGE_KEY_NOT_EXIST : 0;
398     }
399     if (userId == TEST_ID) {
400         if (token.size() != dataSize) {
401             return ERROR_STORAGE_KEY_NOT_EXIST;
402         }
403     }
404     std::cout << "mock UpdateUserAuth enter" << std::endl;
405     return 0;
406 }
407 
408 class MockDeathRecipient : public IRemoteObject {
409 public:
410     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override;
411     int32_t GetObjectRefCount() override;
412     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
413     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override;
414     int Dump(int fd, const std::vector<std::u16string> &args) override;
415 };
416 
AddDeathRecipient(const sptr<DeathRecipient> & recipient)417 bool MockDeathRecipient::AddDeathRecipient(const sptr<DeathRecipient> &recipient)
418 {
419     return true;
420 }
421 
GetObjectRefCount()422 int32_t MockDeathRecipient::GetObjectRefCount()
423 {
424     return 0;
425 }
426 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)427 int MockDeathRecipient::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
428 {
429     return 0;
430 }
431 
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)432 bool MockDeathRecipient::RemoveDeathRecipient(const sptr<DeathRecipient> &recipient)
433 {
434     return true;
435 }
436 
Dump(int fd,const std::vector<std::u16string> & args)437 int MockDeathRecipient::Dump(int fd, const std::vector<std::u16string> &args)
438 {
439     return 0;
440 }
441 
442 class MockIIDMCallback : public IDMCallbackStub {
443 public:
444     MOCK_METHOD2(OnResult, void(int32_t result, const AccountSA::Attributes &extraInfo));
445     MOCK_METHOD3(OnAcquireInfo, void(int32_t module, uint32_t acquireInfo, const AccountSA::Attributes &extraInfo));
446 };
447 
448 class TestIIDMCallback : public IDMCallbackStub {
449 public:
TestIIDMCallback(const std::shared_ptr<MockIIDMCallback> & callback)450     explicit TestIIDMCallback(const std::shared_ptr<MockIIDMCallback> &callback) : callback_(callback)
451     {}
OnResult(int32_t result,const AccountSA::Attributes & extraInfo)452     void OnResult(int32_t result, const AccountSA::Attributes &extraInfo)
453     {
454         callback_->OnResult(result, extraInfo);
455         std::unique_lock<std::mutex> lock(mutex);
456         isReady = true;
457         cv.notify_one();
458         return;
459     }
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const AccountSA::Attributes & extraInfo)460     void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const AccountSA::Attributes &extraInfo)
461     {
462         callback_->OnAcquireInfo(module, acquireInfo, extraInfo);
463     }
464     std::condition_variable cv;
465     bool isReady = false;
466     std::mutex mutex;
467 
468 private:
469     std::shared_ptr<MockIIDMCallback> callback_;
470 };
471 
472 class MockIIDMCallback2 : public IDMCallbackStub {
473 public:
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)474     void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override
475     {
476         module_ = module;
477         acquireInfo_ = acquireInfo;
478         return;
479     }
OnResult(int32_t result,const Attributes & extraInfo)480     void OnResult(int32_t result, const Attributes &extraInfo) override
481     {
482         result_ = result;
483         return;
484     }
485 
486 public:
487     int32_t result_ = -1;
488     int32_t module_ = 0;
489     uint32_t acquireInfo_ = 0;
490 };
491 
492 class MockPreRemoteAuthCallback : public PreRemoteAuthCallbackStub {
493 public:
494     MOCK_METHOD1(OnResult, void(int32_t result));
495 };
496 
497 class TestPreRemoteAuthCallback : public PreRemoteAuthCallbackStub {
498 public:
TestPreRemoteAuthCallback(const std::shared_ptr<MockPreRemoteAuthCallback> & callback)499     explicit TestPreRemoteAuthCallback(const std::shared_ptr<MockPreRemoteAuthCallback> &callback) : callback_(callback)
500     {}
OnResult(int32_t result)501     void OnResult(int32_t result)
502     {
503         callback_->OnResult(result);
504         return;
505     }
506 
507 private:
508     std::shared_ptr<MockPreRemoteAuthCallback> callback_;
509 };
510 
511 class AccountIamManagerTest : public testing::Test {
512 public:
513     static void SetUpTestCase();
514     static void TearDownTestCase();
515     void SetUp();
516     void TearDown();
517 };
518 
FscryptEnable()519 static bool FscryptEnable()
520 {
521     const int bufferLen = 128;
522     char fscryptValue[bufferLen] = {0};
523     int ret = GetParameter("fscrypt.policy.config", "", fscryptValue, bufferLen - 1);
524     if (ret <= 0) {
525         return false;
526     }
527     return true;
528 }
529 
SetUpTestCase()530 void AccountIamManagerTest::SetUpTestCase()
531 {
532     ASSERT_TRUE(MockTokenId("accountmgr"));
533     setuid(ACCOUNT_UID);
534     g_fscryptEnable = FscryptEnable();
535 }
536 
TearDownTestCase()537 void AccountIamManagerTest::TearDownTestCase()
538 {
539     std::cout << "AccountIamManagerTest::TearDownTestCase" << std::endl;
540 }
541 
SetUp(void)542 void AccountIamManagerTest::SetUp(void) __attribute__((no_sanitize("cfi")))
543 {
544     testing::UnitTest *test = testing::UnitTest::GetInstance();
545     ASSERT_NE(test, nullptr);
546     const testing::TestInfo *testinfo = test->current_test_info();
547     ASSERT_NE(testinfo, nullptr);
548     string testCaseName = string(testinfo->name());
549     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
550 }
551 
TearDown()552 void AccountIamManagerTest::TearDown()
553 {
554 }
555 
556 /**
557  * @tc.name: OpenSession001
558  * @tc.desc: Open Session.
559  * @tc.type: FUNC
560  * @tc.require:
561  */
562 HWTEST_F(AccountIamManagerTest, OpenSession001, TestSize.Level2)
563 {
564     std::vector<uint8_t> challenge;
565     InnerAccountIAMManager::GetInstance().OpenSession(TEST_USER_ID, challenge); // 1111: invalid userid
566     EXPECT_TRUE(challenge.size() != 0);
567 
568     InnerAccountIAMManager::GetInstance().CloseSession(0);
569     InnerAccountIAMManager::GetInstance().CloseSession(TEST_USER_ID);
570 }
571 
572 /**
573  * @tc.name: AddCredential001
574  * @tc.desc: Add credential.
575  * @tc.type: FUNC
576  * @tc.require:
577  */
578 HWTEST_F(AccountIamManagerTest, AddCredential001, TestSize.Level0)
579 {
580     CredentialParameters testPara = {};
581     std::shared_ptr<MockIIDMCallback> callback = std::make_shared<MockIIDMCallback>();
582     EXPECT_NE(callback, nullptr);
583     sptr<TestIIDMCallback> testCallback = new(std::nothrow) TestIIDMCallback(callback);
584     EXPECT_NE(testCallback, nullptr);
585     EXPECT_CALL(*callback, OnResult(_, _)).Times(Exactly(0));
586     InnerAccountIAMManager::GetInstance().AddCredential(TEST_USER_ID, testPara, nullptr);
587     InnerAccountIAMManager::GetInstance().AddCredential(TEST_USER_ID, testPara, testCallback);
588     std::unique_lock<std::mutex> lock(testCallback->mutex);
589     testCallback->cv.wait_for(
__anond546527e0202() 590         lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; });
591 }
592 
593 /**
594  * @tc.name: UpdateCredential001
595  * @tc.desc: Update credential.
596  * @tc.type: FUNC
597  * @tc.require:
598  */
599 HWTEST_F(AccountIamManagerTest, UpdateCredential001, TestSize.Level0)
600 {
601     CredentialParameters testPara = {};
602     std::shared_ptr<MockIIDMCallback> callback = std::make_shared<MockIIDMCallback>();
603     EXPECT_NE(callback, nullptr);
604     sptr<TestIIDMCallback> testCallback = new(std::nothrow) TestIIDMCallback(callback);
605     EXPECT_NE(testCallback, nullptr);
606     EXPECT_CALL(*callback, OnResult(_, _)).Times(Exactly(2));
607     InnerAccountIAMManager::GetInstance().UpdateCredential(TEST_USER_ID, testPara, nullptr);
608     InnerAccountIAMManager::GetInstance().UpdateCredential(TEST_USER_ID, testPara, testCallback);
609     {
610         std::unique_lock<std::mutex> lock(testCallback->mutex);
611         testCallback->cv.wait_for(
__anond546527e0302() 612             lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; });
613     }
614 
615     testPara.token = {1, 2, 3};
616     InnerAccountIAMManager::GetInstance().UpdateCredential(TEST_USER_ID, testPara, testCallback);
617     {
618         std::unique_lock<std::mutex> lock(testCallback->mutex);
619         testCallback->cv.wait_for(
__anond546527e0402() 620             lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; });
621     }
622 }
623 
624 /**
625  * @tc.name: Cancel001
626  * @tc.desc: Cancel with .
627  * @tc.type: FUNC
628  * @tc.require:
629  */
630 HWTEST_F(AccountIamManagerTest, Cancel001, TestSize.Level0)
631 {
632     InnerAccountIAMManager::GetInstance().SetState(TEST_USER_ID, AFTER_OPEN_SESSION);
633     int32_t ret = InnerAccountIAMManager::GetInstance().Cancel(TEST_USER_ID);
634     EXPECT_NE(ret, 0);
635 }
636 
637 /**
638  * @tc.name: Cancel002
639  * @tc.desc: Cancel after add credential.
640  * @tc.type: FUNC
641  * @tc.require:
642  */
643 HWTEST_F(AccountIamManagerTest, Cancel002, TestSize.Level0)
644 {
645     InnerAccountIAMManager::GetInstance().SetState(TEST_USER_ID, AFTER_ADD_CRED);
646     int32_t ret = InnerAccountIAMManager::GetInstance().Cancel(TEST_USER_ID);
647     EXPECT_EQ(ret, ResultCode::GENERAL_ERROR);
648 }
649 
650 /**
651  * @tc.name: Cancel003
652  * @tc.desc: Cancel with invalid user id.
653  * @tc.type: FUNC
654  * @tc.require:
655  */
656 HWTEST_F(AccountIamManagerTest, Cancel003, TestSize.Level0)
657 {
658     InnerAccountIAMManager::GetInstance().SetState(TEST_USER_ID, AFTER_ADD_CRED);
659     int32_t ret = InnerAccountIAMManager::GetInstance().Cancel(TEST_USER_ID);
660     EXPECT_EQ(ret, ResultCode::GENERAL_ERROR);
661 }
662 
663 /**
664  * @tc.name: DelCred001
665  * @tc.desc: Delete credential.
666  * @tc.type: FUNC
667  * @tc.require:
668  */
669 HWTEST_F(AccountIamManagerTest, DelCred001, TestSize.Level0)
670 {
671     uint64_t testCredentialId = 111;
672     std::vector<uint8_t> testAuthToken;
673     std::shared_ptr<MockIIDMCallback> callback = std::make_shared<MockIIDMCallback>();
674     EXPECT_NE(callback, nullptr);
675     sptr<TestIIDMCallback> testCallback = new(std::nothrow) TestIIDMCallback(callback);
676     EXPECT_NE(testCallback, nullptr);
677     EXPECT_CALL(*callback, OnResult(_, _)).Times(Exactly(2));
678     InnerAccountIAMManager::GetInstance().DelCred(TEST_USER_ID, testCredentialId, testAuthToken, nullptr);
679 
680     InnerAccountIAMManager::GetInstance().DelCred(TEST_USER_ID, testCredentialId, testAuthToken, testCallback);
681     {
682         std::unique_lock<std::mutex> lock(testCallback->mutex);
683         testCallback->cv.wait_for(
__anond546527e0502() 684             lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; });
685     }
686 
687     testAuthToken = {1, 2, 3, 4};
688     InnerAccountIAMManager::GetInstance().DelCred(TEST_USER_ID, testCredentialId, testAuthToken, testCallback);
689     {
690         std::unique_lock<std::mutex> lock(testCallback->mutex);
691         testCallback->cv.wait_for(
__anond546527e0602() 692             lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; });
693     }
694 }
695 
696 /**
697  * @tc.name: DelUser001
698  * @tc.desc: Delete user.
699  * @tc.type: FUNC
700  * @tc.require:
701  */
702 HWTEST_F(AccountIamManagerTest, DelUser001, TestSize.Level0)
703 {
704     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
705     std::shared_ptr<MockIIDMCallback> callback = std::make_shared<MockIIDMCallback>();
706     EXPECT_NE(callback, nullptr);
707     sptr<TestIIDMCallback> testCallback = new(std::nothrow) TestIIDMCallback(callback);
708     EXPECT_NE(testCallback, nullptr);
709     EXPECT_CALL(*callback, OnResult(_, _)).Times(Exactly(2));
710     InnerAccountIAMManager::GetInstance().DelUser(TEST_USER_ID, testAuthToken, nullptr);
711     InnerAccountIAMManager::GetInstance().DelUser(TEST_USER_ID, testAuthToken, testCallback);
712     {
713         std::unique_lock<std::mutex> lock(testCallback->mutex);
714         testCallback->cv.wait_for(
__anond546527e0702() 715             lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; });
716     }
717 
718     testAuthToken = {1, 2, 3, 4};
719     InnerAccountIAMManager::GetInstance().DelUser(TEST_USER_ID, testAuthToken, testCallback);
720     {
721         std::unique_lock<std::mutex> lock(testCallback->mutex);
722         testCallback->cv.wait_for(
__anond546527e0802() 723             lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; });
724     }
725 }
726 
727 /**
728  * @tc.name: AuthUser001
729  * @tc.desc: Auth user.
730  * @tc.type: FUNC
731  * @tc.require:
732  */
733 HWTEST_F(AccountIamManagerTest, AuthUser001, TestSize.Level0)
734 {
735     SetPropertyRequest testRequest = {};
736     std::shared_ptr<MockIIDMCallback> callback = std::make_shared<MockIIDMCallback>();
737     EXPECT_NE(callback, nullptr);
738     sptr<TestIIDMCallback> testCallback = new(std::nothrow) TestIIDMCallback(callback);
739     EXPECT_NE(testCallback, nullptr);
740     EXPECT_CALL(*callback, OnResult(_, _)).Times(0);
741     AccountSA::AuthParam authParam = {
742         .userId = TEST_EXIST_ID,
743         .challenge = TEST_CHALLENGE,
744         .authType = AuthType::PIN,
745         .authTrustLevel = AuthTrustLevel::ATL1
746     };
747     uint64_t contextId = 0;
748     ErrCode errCode = InnerAccountIAMManager::GetInstance().AuthUser(authParam, nullptr, contextId);
749     EXPECT_EQ(ERR_ACCOUNT_COMMON_NULL_PTR_ERROR, errCode);
750 
751     errCode = InnerAccountIAMManager::GetInstance().AuthUser(authParam, testCallback, contextId);
752     EXPECT_EQ(ERR_ACCOUNT_COMMON_ADD_DEATH_RECIPIENT, errCode);
753     InnerAccountIAMManager::GetInstance().CancelAuth(contextId);
754 }
755 
756 /**
757  * @tc.name: GetState001
758  * @tc.desc: Get state.
759  * @tc.type: FUNC
760  * @tc.require:
761  */
762 HWTEST_F(AccountIamManagerTest, GetState001, TestSize.Level0)
763 {
764     int32_t userId = 4444; // 1111: invalid userId
765     EXPECT_EQ(IDLE, InnerAccountIAMManager::GetInstance().GetState(userId));
766 
767     EXPECT_NE(IDLE, InnerAccountIAMManager::GetInstance().GetState(TEST_USER_ID));
768 }
769 
770 /**
771  * @tc.name: ActivateUserKey001
772  * @tc.desc: ActivateUserKey.
773  * @tc.type: FUNC
774  * @tc.require:
775  */
776 HWTEST_F(AccountIamManagerTest, ActivateUserKey001, TestSize.Level2)
777 {
778     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
779     std::vector<uint8_t> testSecret = {1, 2, 3, 4};
780 
781     auto &innerIamMgr_ = InnerAccountIAMManager::GetInstance();
782 
783     EXPECT_NE(ERR_OK, innerIamMgr_.ActivateUserKey(TEST_USER_ID, testAuthToken, testSecret));
784 
785     int32_t userId = 112;
786     EXPECT_NE(ERR_OK, innerIamMgr_.ActivateUserKey(userId, testAuthToken, testSecret));
787 
788     // userid is out of range
789     userId = 11112;
790     EXPECT_NE(ERR_OK, innerIamMgr_.ActivateUserKey(userId, testAuthToken, testSecret));
791 }
792 
793 /**
794  * @tc.name: UpdateCredCallback_OnResult_0001
795  * @tc.desc: OnResult with not PIN.
796  * @tc.type: FUNC
797  * @tc.require:
798  */
799 HWTEST_F(AccountIamManagerTest, UpdateCredCallback_OnResult_0001, TestSize.Level0)
800 {
801     sptr<MockIIDMCallback2> callback = new (std::nothrow) MockIIDMCallback2();
802     CredentialParameters credInfo = {};
803     credInfo.authType = AuthType::PIN;
804     Attributes extraInfo;
805     auto updateCredCallback = std::make_shared<UpdateCredCallback>(UPDATE_USER_ID, credInfo, callback);
806 
807     EXPECT_TRUE(updateCredCallback->innerCallback_ != nullptr);
808     int32_t errCode = 10;
809     updateCredCallback->OnResult(errCode, extraInfo);
810     EXPECT_EQ(errCode, callback->result_);
811     errCode = 0;
812     updateCredCallback->OnResult(errCode, extraInfo);
813     EXPECT_NE(errCode, callback->result_);
814 }
815 
816 /**
817  * @tc.name: UpdateCredCallback_OnResult_0002
818  * @tc.desc: OnResult with not PIN.
819  * @tc.type: FUNC
820  * @tc.require:
821  */
822 HWTEST_F(AccountIamManagerTest, UpdateCredCallback_OnResult_0002, TestSize.Level0)
823 {
824     sptr<MockIIDMCallback2> callback = new (std::nothrow) MockIIDMCallback2();
825     CredentialParameters credInfo = {};
826     credInfo.authType = AuthType::PIN;
827     Attributes extraInfo;
828     auto updateCredCallback = std::make_shared<UpdateCredCallback>(UPDATE_FAIL_USER_ID, credInfo, callback);
829     EXPECT_TRUE(updateCredCallback->innerCallback_ != nullptr);
830     int32_t errCode = 0;
831     updateCredCallback->OnResult(errCode, extraInfo);
832     EXPECT_NE(errCode, callback->result_);
833 
834     UpdateCredInfo extraUpdateInfo;
835     auto commitUpdateCredCallback = std::make_shared<CommitCredUpdateCallback>(UPDATE_FAIL_USER_ID,
836         extraUpdateInfo, callback);
837     commitUpdateCredCallback->OnResult(errCode, extraInfo);
838     commitUpdateCredCallback->OnResult(1, extraInfo);
839 }
840 
841 /**
842  * @tc.name: PrepareRemoteAuth001
843  * @tc.desc: PrepareRemoteAuth.
844  * @tc.type: FUNC
845  * @tc.require:
846  */
847 HWTEST_F(AccountIamManagerTest, PrepareRemoteAuth001, TestSize.Level0)
848 {
849     ErrCode errCode = InnerAccountIAMManager::GetInstance().PrepareRemoteAuth("testString", nullptr);
850     EXPECT_EQ(ERR_ACCOUNT_COMMON_NULL_PTR_ERROR, errCode);
851 
852     std::shared_ptr<MockPreRemoteAuthCallback> callback = std::make_shared<MockPreRemoteAuthCallback>();
853     EXPECT_NE(callback, nullptr);
854     sptr<TestPreRemoteAuthCallback> testCallback = new(std::nothrow) TestPreRemoteAuthCallback(callback);
855     EXPECT_NE(testCallback, nullptr);
856 
857     InnerAccountIAMManager::GetInstance().PrepareRemoteAuth("testString", testCallback);
858 }
859 }  // namespace AccountTest
860 }  // namespace OHOS
861