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