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