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