1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cerrno>
17 #include <filesystem>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <thread>
21 #include <unistd.h>
22 #include "access_token.h"
23 #include "accesstoken_kit.h"
24 #include "account_info.h"
25 #include "account_log_wrapper.h"
26 #include "account_proxy.h"
27 #ifdef HAS_CES_PART
28 #include "common_event_manager.h"
29 #include "common_event_subscriber.h"
30 #include "common_event_support.h"
31 #include "common_event_subscribe_info.h"
32 #include "matching_skills.h"
33 #endif // HAS_CES_PART
34 #include "if_system_ability_manager.h"
35 #include "ipc_skeleton.h"
36 #include "iservice_registry.h"
37 #include "os_account_manager.h"
38 #define private public
39 #include "account_file_operator.h"
40 #undef private
41 #include "os_account_constants.h"
42 #define private public
43 #include "os_account.h"
44 #undef private
45 #ifdef BUNDLE_ADAPTER_MOCK
46 #define private public
47 #include "os_account_manager_service.h"
48 #include "os_account_proxy.h"
49 #include "iinner_os_account_manager.h"
50 #undef private
51 #endif
52 #include "parameter.h"
53 #include "system_ability.h"
54 #include "system_ability_definition.h"
55 #include "token_setproc.h"
56
57 using namespace testing;
58 using namespace testing::ext;
59 using namespace OHOS;
60 using namespace OHOS::AccountSA;
61 using namespace OHOS::Security::AccessToken;
62 using namespace OHOS::AccountSA::Constants;
63 using namespace OHOS::EventFwk;
64
65 namespace {
66 static uint64_t g_selfTokenID;
67 const std::string STRING_EMPTY = "";
68 const std::string STRING_NAME = "name";
69 const std::string STRING_TEST_NAME_TWO = "test_account_name_2";
70 const std::uint32_t INVALID_TOKEN_ID = 0;
71 #ifdef DOMAIN_ACCOUNT_TEST_CASE
72 const std::uint32_t INVALID_BUNDLE_ID = -1;
73 #endif
74 const std::int32_t ERROR_LOCAL_ID = -1;
75 const std::int32_t LOCAL_ID = 105;
76 const std::int64_t INVALID_SERIAL_NUM = 123;
77 const std::int32_t WAIT_A_MOMENT = 3000;
78 const std::int32_t MAIN_ACCOUNT_ID = 100;
79 const std::int32_t INVALID_ID = 200;
80 const std::uint32_t MAX_WAIT_FOR_READY_CNT = 10;
81 const std::int32_t DEFAULT_API_VERSION = 8;
82 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
83 const int32_t WAIT_TIME = 20;
84 #ifdef BUNDLE_ADAPTER_MOCK
85 const uid_t ACCOUNT_UID = 3058;
86 const gid_t ACCOUNT_GID = 3058;
87 #endif
88 const uid_t ROOT_UID = 0;
89 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
90
91 const std::vector<std::string> CONSTANTS_VECTOR {
92 "constraint.print",
93 "constraint.screen.timeout.set",
94 "constraint.share.into.profile"
95 };
96
97 const std::vector<std::string> CONSTANTS_VECTOR_TEST {
98 "constraint.private.dns.set",
99 };
100
101 const std::vector<std::string> PERMISSION_LIST {
102 "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS",
103 "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION"
104 };
105
106 const std::string CONSTRAINT_PRIVATE_DNS_SET = "constraint.private.dns.set";
107 const std::string CONSTANT_WIFI = "constraint.wifi";
108
109 const std::string CONSTANT_PRINT = "constraint.print";
110 const std::string STRING_NAME_OUT_OF_RANGE(1200, '1'); // length 1200
111 const std::string STRING_PHOTO_OUT_OF_RANGE(1024 * 1024 + 1, '1'); // length 1024*1024*10+1
112 const std::string STRING_PHOTO_MAX(1024 * 1024, '1'); // length 1024*1024*10+1
113 const std::string PHOTO_IMG =
114 "data:image/"
115 "png;base64,"
116 "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AAA"
117 "FBSURBVDiN7ZQ/S8NQFMVPxU/QCx06GBzrkqUZ42rBbHWUBDqYxSnUoTxXydCSycVsgltfBiFDR8HNdHGxY4nQQAPvMzwHsWn+KMWsPdN7h/"
118 "vj3He5vIaUEjV0UAfe85X83KMBT7N75JEXVdSlfEAVfPRyZ5yfIrBoUkVlMU82Hkp8wu9ddt1vFew4sIiIiKwgzcXIvN7GTZOvpZRrbja3tDG/"
119 "D3I1NZvmdCXz+XOv5wJANKHOVYjRTAghxIyh0FHKb+0QQH5+kXf2zkYGAG0oFr5RfnK8DAGkwY19wliRT2L448vjv0YGQFVa8VKdDXUU+"
120 "faFUxpblhxYRNRzmd6FNnS0H3/X/VH6j0IIIRxMLJ5k/j/2L/"
121 "zchW8pKj7iFAA0R2wajl5d46idlR3+GtPV2XOvQ3bBNvyFs8U39v9PLX0Bp0CN+yY0OAEAAAAASUVORK5CYII=";
122 const std::string PHOTO_IMG_ERROR =
123 "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AAA"
124 "FBSURBVDiN7ZQ/S8NQFMVPxU/QCx06GBzrkqUZ42rBbHWUBDqYxSnUoTxXydCSycVsgltfBiFDR8HNdHGxY4nQQAPvMzwHsWn+KMWsPdN7h/"
125 "vj3He5vIaUEjV0UAfe85X83KMBT7N75JEXVdSlfEAVfPRyZ5yfIrBoUkVlMU82Hkp8wu9ddt1vFew4sIiIiKwgzcXIvN7GTZOvpZRrbja3tDG/"
126 "D3I1NZvmdCXz+XOv5wJANKHOVYjRTAghxIyh0FHKb+0QQH5+kXf2zkYGAG0oFr5RfnK8DAGkwY19wliRT2L448vjv0YGQFVa8VKdDXUU+"
127 "faFUxpblhxYRNRzmd6FNnS0H3/X/VH6j0IIIRxMLJ5k/j/2L/"
128 "zchW8pKj7iFAA0R2wajl5d46idlR3+GtPV2XOvQ3bBNvyFs8U39v9PLX0Bp0CN+yY0OAEAAAAASUVORK5CYII=";
129 const std::string STRING_DOMAIN_NAME_OUT_OF_RANGE(200, '1'); // length 200
130 const std::string STRING_DOMAIN_ACCOUNT_NAME_OUT_OF_RANGE(600, '1'); // length 600
131 const std::string STRING_DOMAIN_VALID = "TestDomainMT";
132 const std::string STRING_DOMAIN_ACCOUNT_NAME_VALID = "TestDomainAccountNameMT";
133 const std::string TEST_ACCOUNT_NAME = "TestAccountNameOS";
134 const std::string TEST_ACCOUNT_UID = "123456789os";
135 const std::string TEST_EXPECTED_UID = "4E7FA9CA2E8760692F2ADBA7AE59B37E02E650670E5FA5F3D01232DCD52D3893";
136 std::shared_ptr<AccountFileOperator> g_accountFileOperator = std::make_shared<AccountFileOperator>();
137
138 static PermissionDef INFO_MANAGER_TEST_PERM_DEF1 = {
139 .permissionName = "open the door",
140 .bundleName = "osaccount_test",
141 .grantMode = 1,
142 .availableLevel = APL_NORMAL,
143 .provisionEnable = false,
144 .distributedSceneEnable = false,
145 .label = "label",
146 .labelId = 1,
147 .description = "open the door",
148 .descriptionId = 1
149 };
150
151 static PermissionDef INFO_MANAGER_TEST_PERM_DEF2 = {
152 .permissionName = "break the door",
153 .bundleName = "osaccount_test",
154 .grantMode = 1,
155 .availableLevel = APL_NORMAL,
156 .provisionEnable = false,
157 .distributedSceneEnable = false,
158 .label = "label",
159 .labelId = 1,
160 .description = "break the door",
161 .descriptionId = 1
162 };
163
164 static PermissionStateFull INFO_MANAGER_TEST_STATE1 = {
165 .permissionName = "open the door",
166 .isGeneral = true,
167 .resDeviceID = {"local"},
168 .grantStatus = {1},
169 .grantFlags = {1}
170 };
171
172 static PermissionStateFull INFO_MANAGER_TEST_STATE2 = {
173 .permissionName = "break the door",
174 .isGeneral = false,
175 .resDeviceID = {"device 1", "device 2"},
176 .grantStatus = {1, 3},
177 .grantFlags = {1, 2}
178 };
179
180 static HapPolicyParams INFO_MANAGER_TEST_POLICY_PRAMS = {
181 .apl = APL_NORMAL,
182 .domain = "test.domain",
183 .permList = {INFO_MANAGER_TEST_PERM_DEF1, INFO_MANAGER_TEST_PERM_DEF2},
184 .permStateList = {INFO_MANAGER_TEST_STATE1, INFO_MANAGER_TEST_STATE2}
185 };
186
187 HapInfoParams infoManagerTestNormalInfoParms = {
188 .userID = 1,
189 .bundleName = "osaccount_test",
190 .instIndex = 0,
191 .appIDDesc = "testtesttesttest",
192 .apiVersion = DEFAULT_API_VERSION,
193 .isSystemApp = false
194 };
195
196 HapInfoParams infoManagerTestSystemInfoParms = {
197 .userID = 1,
198 .bundleName = "osaccount_test",
199 .instIndex = 0,
200 .appIDDesc = "testtesttesttest",
201 .apiVersion = DEFAULT_API_VERSION,
202 .isSystemApp = true
203 };
204 } // namespace
205
206 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
AllocPermission(std::vector<std::string> permissions,AccessTokenID & tokenID,bool isSystemApp=true)207 static bool AllocPermission(std::vector<std::string> permissions, AccessTokenID &tokenID, bool isSystemApp = true)
208 {
209 std::vector<PermissionStateFull> permissionStates;
210 for (const auto& permission : permissions) {
211 PermissionStateFull permissionState = {
212 .permissionName = permission,
213 .isGeneral = true,
214 .resDeviceID = {"local"},
215 .grantStatus = {PermissionState::PERMISSION_GRANTED},
216 .grantFlags = {PERMISSION_SYSTEM_FIXED}
217 };
218 permissionStates.emplace_back(permissionState);
219 }
220 HapPolicyParams hapPolicyParams = {
221 .apl = APL_NORMAL,
222 .domain = "test.domain",
223 .permList = {},
224 .permStateList = permissionStates
225 };
226
227 HapInfoParams hapInfoParams = {
228 .userID = 100,
229 .bundleName = "account_test",
230 .instIndex = 0,
231 .appIDDesc = "account_test",
232 .apiVersion = DEFAULT_API_VERSION,
233 .isSystemApp = isSystemApp
234 };
235
236 AccessTokenIDEx tokenIdEx = {0};
237 tokenIdEx = AccessTokenKit::AllocHapToken(hapInfoParams, hapPolicyParams);
238 tokenID = tokenIdEx.tokenIdExStruct.tokenID;
239 return (INVALID_TOKENID != tokenIdEx.tokenIDEx) && (0 == SetSelfTokenID(tokenIdEx.tokenIDEx));
240 }
241
RecoveryPermission(AccessTokenID tokenID)242 static bool RecoveryPermission(AccessTokenID tokenID)
243 {
244 return (ERR_OK == AccessTokenKit::DeleteToken(tokenID)) && (ERR_OK == SetSelfTokenID(g_selfTokenID));
245 }
246 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
247
248 class OsAccountManagerModuleTest : public testing::Test {
249 public:
250 static void SetUpTestCase(void);
251 static void TearDownTestCase(void);
252 void SetUp();
253 void TearDown();
254 };
255
SetUpTestCase(void)256 void OsAccountManagerModuleTest::SetUpTestCase(void)
257 {
258 GTEST_LOG_(INFO) << "SetUpTestCase enter";
259 #ifdef ACCOUNT_TEST
260 AccountFileOperator osAccountFileOperator;
261 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
262 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
263 #endif // ACCOUNT_TEST
264 bool isOsAccountActived = false;
265 ErrCode ret = OsAccountManager::IsOsAccountActived(MAIN_ACCOUNT_ID, isOsAccountActived);
266 std::uint32_t waitCnt = 0;
267 while (ret != ERR_OK || !isOsAccountActived) {
268 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_A_MOMENT));
269 waitCnt++;
270 GTEST_LOG_(INFO) << "SetUpTestCase waitCnt " << waitCnt << " ret = " << ret;
271 ret = OsAccountManager::IsOsAccountActived(MAIN_ACCOUNT_ID, isOsAccountActived);
272 if (waitCnt >= MAX_WAIT_FOR_READY_CNT) {
273 GTEST_LOG_(INFO) << "SetUpTestCase waitCnt " << waitCnt;
274 GTEST_LOG_(INFO) << "SetUpTestCase wait for ready failed!";
275 break;
276 }
277 }
278 GTEST_LOG_(INFO) << "SetUpTestCase finished, waitCnt " << waitCnt;
279 g_selfTokenID = IPCSkeleton::GetSelfTokenID();
280 #ifdef BUNDLE_ADAPTER_MOCK
281 auto osAccountService = new (std::nothrow) OsAccountManagerService();
282 ASSERT_NE(osAccountService, nullptr);
283 IInnerOsAccountManager::GetInstance().Init();
284 IInnerOsAccountManager::GetInstance().ActivateDefaultOsAccount();
285 OsAccount::GetInstance().proxy_ = new (std::nothrow) OsAccountProxy(osAccountService->AsObject());
286 ASSERT_NE(OsAccount::GetInstance().proxy_, nullptr);
287 #endif
288 }
289
TearDownTestCase(void)290 void OsAccountManagerModuleTest::TearDownTestCase(void)
291 {
292 GTEST_LOG_(INFO) << "TearDownTestCase";
293 #ifdef ACCOUNT_TEST
294 AccountFileOperator osAccountFileOperator;
295 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
296 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
297 #endif // ACCOUNT_TEST
298 }
299
SetUp(void)300 void OsAccountManagerModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
301 {
302 testing::UnitTest *test = testing::UnitTest::GetInstance();
303 ASSERT_NE(test, nullptr);
304 const testing::TestInfo *testinfo = test->current_test_info();
305 ASSERT_NE(testinfo, nullptr);
306 string testCaseName = string(testinfo->name());
307 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
308
309 std::vector<OsAccountInfo> osAccountInfos;
310 EXPECT_EQ(OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos), ERR_OK);
311 for (const auto &info : osAccountInfos) {
312 if (info.GetLocalId() == START_USER_ID) {
313 continue;
314 }
315 ACCOUNT_LOGI("[SetUp] remove account %{public}d", info.GetLocalId());
316 OsAccountManager::RemoveOsAccount(info.GetLocalId());
317 }
318 }
319
TearDown(void)320 void OsAccountManagerModuleTest::TearDown(void)
321 {}
322
323 class MockSubscriberListener {
324 public:
325 MOCK_METHOD1(OnReceiveEvent, void(const std::string &action));
326 };
327
328 class AccountTestEventSubscriber final : public EventFwk::CommonEventSubscriber {
329 public:
AccountTestEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,const std::shared_ptr<MockSubscriberListener> & listener)330 AccountTestEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
331 const std::shared_ptr<MockSubscriberListener> &listener)
332 : CommonEventSubscriber(subscribeInfo), listener_(listener)
333 {}
334
OnReceiveEvent(const EventFwk::CommonEventData & data)335 void OnReceiveEvent(const EventFwk::CommonEventData &data)
336 {
337 if (listener_ == nullptr) {
338 return;
339 }
340 auto want = data.GetWant();
341 listener_->OnReceiveEvent(want.GetAction());
342 std::unique_lock<std::mutex> lock(mutex);
343 if (want.GetAction() == EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING) {
344 stoppingEventReady = true;
345 } else {
346 stoppedEventReady = true;
347 }
348 cv.notify_one();
349 return;
350 }
351 std::condition_variable cv;
352 bool stoppingEventReady = false;
353 bool stoppedEventReady = false;
354 std::mutex mutex;
355
356 private:
357 const std::shared_ptr<MockSubscriberListener> listener_;
358 };
359
360 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
361 #ifndef BUNDLE_ADAPTER_MOCK
Wait(const std::shared_ptr<AccountTestEventSubscriber> & ptr)362 static void Wait(const std::shared_ptr<AccountTestEventSubscriber> &ptr)
363 {
364 std::unique_lock<std::mutex> lock(ptr->mutex);
365 ptr->cv.wait_for(lock, std::chrono::seconds(WAIT_TIME),
366 [lockPtr = ptr]() { return lockPtr->stoppingEventReady && lockPtr->stoppedEventReady; });
367 }
368 #endif
369 #endif
370
371 class MockOsAccountSubscriber {
372 public:
373 MOCK_METHOD1(OnAccountsChanged, void(const int &id));
374 };
375
376 class DeactivateOsAccountSubscriber final : public OsAccountSubscriber {
377 public:
DeactivateOsAccountSubscriber(const OsAccountSubscribeInfo & subscribeInfo,const std::shared_ptr<MockOsAccountSubscriber> & callback)378 explicit DeactivateOsAccountSubscriber(
379 const OsAccountSubscribeInfo &subscribeInfo, const std::shared_ptr<MockOsAccountSubscriber> &callback)
380 : OsAccountSubscriber(subscribeInfo), callback_(callback) {}
381
OnAccountsChanged(const int & id)382 void OnAccountsChanged(const int &id)
383 {
384 callback_->OnAccountsChanged(id);
385 std::unique_lock<std::mutex> lock(mutex);
386 isReady = true;
387 cv.notify_one();
388 return;
389 }
390 std::condition_variable cv;
391 bool isReady = false;
392 std::mutex mutex;
393
394 private:
395 std::shared_ptr<MockOsAccountSubscriber> callback_;
396 };
397
398 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
Wait(const std::shared_ptr<DeactivateOsAccountSubscriber> & ptr)399 static void Wait(const std::shared_ptr<DeactivateOsAccountSubscriber> &ptr)
400 {
401 std::unique_lock<std::mutex> lock(ptr->mutex);
402 ptr->cv.wait_for(lock, std::chrono::seconds(WAIT_TIME),
403 [lockPtr = ptr]() { return lockPtr->isReady; });
404 }
405 #endif
406
407 class ActiveOsAccountSubscriber final : public OsAccountSubscriber {
408 public:
ActiveOsAccountSubscriber(const OsAccountSubscribeInfo & subscribeInfo)409 explicit ActiveOsAccountSubscriber(const OsAccountSubscribeInfo &subscribeInfo)
410 : OsAccountSubscriber(subscribeInfo) {}
411
412 MOCK_METHOD1(OnAccountsChanged, void(const int &id));
413 MOCK_METHOD2(OnAccountsSwitch, void(const int &newId, const int &oldId));
414 };
415
416 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
417 /**
418 * @tc.name: CreateOsAccountWithFullInfo001
419 * @tc.desc: Test next id.
420 * @tc.type: FUNC
421 * @tc.require:
422 */
423 HWTEST_F(OsAccountManagerModuleTest, CreateOsAccountWithFullInfo001, TestSize.Level1)
424 {
425 OsAccountInfo osAccountInfo;
426 osAccountInfo.SetLocalName("testNextID_001");
427 int expectUid = 1000; // test random uid, next account should start from 1000
428 osAccountInfo.SetLocalId(expectUid);
429 osAccountInfo.SetSerialNumber(2023023100000033); // test random input
430 osAccountInfo.SetCreateTime(1695883215000); // test random input
431 osAccountInfo.SetLastLoginTime(1695863215000); // test random input
432 EXPECT_EQ(ERR_OK, OsAccountManager::CreateOsAccountWithFullInfo(osAccountInfo));
433 EXPECT_EQ(osAccountInfo.GetLocalId(), expectUid);
434 OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId());
435
436 OsAccountInfo osAccountInfoOne;
437 ASSERT_EQ(OsAccountManager::CreateOsAccount("testNextID_002", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
438 EXPECT_EQ(osAccountInfoOne.GetLocalId(), expectUid + 1);
439 OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId());
440 }
441
442 /**
443 * @tc.name: OsAccountManagerModuleTest001
444 * @tc.desc: Test create guest account.
445 * @tc.type: FUNC
446 * @tc.require: issueI4IU74
447 */
448 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest001, TestSize.Level0)
449 {
450 OsAccountInfo osAccountInfoOne;
451 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest001", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
452 OsAccountInfo osAccountInfoTwo;
453 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfoOne.GetLocalId(), osAccountInfoTwo), ERR_OK);
454 DomainAccountInfo domainInfo;
455 osAccountInfoTwo.GetDomainInfo(domainInfo);
456 domainInfo.status_ = DomainAccountStatus::LOG_END;
457 osAccountInfoTwo.SetDomainInfo(domainInfo);
458 EXPECT_EQ(osAccountInfoOne.ToString(), osAccountInfoTwo.ToString());
459 EXPECT_EQ(osAccountInfoOne.GetType(), OsAccountType::GUEST);
460 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
461 }
462
463 /**
464 * @tc.name: OsAccountManagerModuleTest002
465 * @tc.desc: Test CreateOsAccount with too long name.
466 * @tc.type: FUNC
467 * @tc.require: issueI4IU51
468 */
469 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest002, TestSize.Level1)
470 {
471 OsAccountInfo osAccountInfoOne;
472 EXPECT_NE(OsAccountManager::CreateOsAccount(STRING_NAME_OUT_OF_RANGE, OsAccountType::GUEST, osAccountInfoOne),
473 ERR_OK);
474 }
475
476 /**
477 * @tc.name: OsAccountManagerModuleTest003
478 * @tc.desc: Test CreateOsAccount with empty name.
479 * @tc.type: FUNC
480 * @tc.require: issueI4IU51
481 */
482 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest003, TestSize.Level1)
483 {
484 OsAccountInfo osAccountInfoOne;
485 EXPECT_NE(OsAccountManager::CreateOsAccount(STRING_EMPTY, OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
486 }
487
488 /**
489 * @tc.name: OsAccountManagerModuleTest004
490 * @tc.desc: Test create admin account.
491 * @tc.type: FUNC
492 * @tc.require: issueI4IU51
493 */
494 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest004, TestSize.Level1)
495 {
496 OsAccountInfo osAccountInfoOne;
497 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest004", OsAccountType::ADMIN, osAccountInfoOne), ERR_OK);
498 OsAccountInfo osAccountInfoTwo;
499 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfoOne.GetLocalId(), osAccountInfoTwo), ERR_OK);
500 DomainAccountInfo domainInfo;
501 osAccountInfoTwo.GetDomainInfo(domainInfo);
502 domainInfo.status_ = DomainAccountStatus::LOG_END;
503 osAccountInfoTwo.SetDomainInfo(domainInfo);
504 EXPECT_EQ(osAccountInfoOne.ToString(), osAccountInfoTwo.ToString());
505 EXPECT_EQ(osAccountInfoOne.GetType(), OsAccountType::ADMIN);
506 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
507 }
508
509 /**
510 * @tc.name: OsAccountManagerModuleTest005
511 * @tc.desc: Test create normal account.
512 * @tc.type: FUNC
513 * @tc.require: issueI4IU51
514 */
515 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest005, TestSize.Level1)
516 {
517 OsAccountInfo osAccountInfoOne;
518 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest005", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
519 OsAccountInfo osAccountInfoTwo;
520 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfoOne.GetLocalId(), osAccountInfoTwo), ERR_OK);
521 DomainAccountInfo domainInfo;
522 osAccountInfoTwo.GetDomainInfo(domainInfo);
523 domainInfo.status_ = DomainAccountStatus::LOG_END;
524 osAccountInfoTwo.SetDomainInfo(domainInfo);
525 EXPECT_EQ(osAccountInfoOne.ToString(), osAccountInfoTwo.ToString());
526 EXPECT_EQ(osAccountInfoOne.GetType(), OsAccountType::NORMAL);
527 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
528 }
529
530 #ifdef BUNDLE_ADAPTER_MOCK
531 /**
532 * @tc.name: OsAccountManagerModuleTest006
533 * @tc.desc: Test CreateOsAccount when cannot find account_list.json.
534 * @tc.type: FUNC
535 * @tc.require: issueI4IU51
536 */
537 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest006, TestSize.Level1)
538 {
539 // save file content to ram
540 std::string fileContext;
541 g_accountFileOperator->GetFileContentByPath(Constants::ACCOUNT_LIST_FILE_JSON_PATH, fileContext);
542
543 // remove file
544 ASSERT_EQ(g_accountFileOperator->DeleteDirOrFile(Constants::ACCOUNT_LIST_FILE_JSON_PATH), ERR_OK);
545
546 // create account
547 OsAccountInfo osAccountInfoOne;
548 EXPECT_NE(OsAccountManager::CreateOsAccount("ModuleTest006", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
549
550 // rewrite file content
551 g_accountFileOperator->InputFileByPathAndContent(Constants::ACCOUNT_LIST_FILE_JSON_PATH, fileContext);
552
553 // recover permission
554 if (chmod(Constants::ACCOUNT_LIST_FILE_JSON_PATH.c_str(), S_IRUSR | S_IWUSR) != 0) {
555 ACCOUNT_LOGE("OsAccountManagerModuleTest006, chmod failed! errno %{public}d.", errno);
556 }
557 if (chown(Constants::ACCOUNT_LIST_FILE_JSON_PATH.c_str(), ACCOUNT_UID, ACCOUNT_GID) != 0) {
558 ACCOUNT_LOGE("OsAccountManagerModuleTest006, chown failed! errno %{public}d.", errno);
559 }
560 }
561 #endif
562
563 /**
564 * @tc.name: OsAccountManagerModuleTest007
565 * @tc.desc: Test RemoveOsAccount with valid data.
566 * @tc.type: FUNC
567 * @tc.require: issueI4IU74
568 */
569 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest007, TestSize.Level0)
570 {
571 OsAccountInfo osAccountInfoOne;
572 EXPECT_NE(OsAccountManager::CreateOsAccount(STRING_EMPTY, OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
573 EXPECT_NE(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
574 EXPECT_EQ(g_accountFileOperator->IsExistDir(
575 Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + osAccountInfoOne.GetPrimeKey()), false);
576 }
577 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
578
579 /**
580 * @tc.name: OsAccountManagerModuleTest008
581 * @tc.desc: Test RemoveOsAccount with cannot remove id.
582 * @tc.type: FUNC
583 * @tc.require: issueI4IU74
584 */
585 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest008, TestSize.Level1)
586 {
587 EXPECT_NE(OsAccountManager::RemoveOsAccount(Constants::START_USER_ID), ERR_OK);
588 }
589
590 /**
591 * @tc.name: OsAccountManagerModuleTest009
592 * @tc.desc: Test RemoveOsAccount with does not exists id.
593 * @tc.type: FUNC
594 * @tc.require: issueI4IU51
595 */
596 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest009, TestSize.Level1)
597 {
598 EXPECT_NE(OsAccountManager::RemoveOsAccount(Constants::MAX_USER_ID + 1), ERR_OK);
599 }
600
601 /**
602 * @tc.name: OsAccountManagerModuleTest010
603 * @tc.desc: Test IsOsAccountExists with valid data.
604 * @tc.type: FUNC
605 * @tc.require: issueI4IU51
606 */
607 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest010, TestSize.Level1)
608 {
609 bool isOsAccountExists = false;
610 EXPECT_EQ(OsAccountManager::IsOsAccountExists(Constants::START_USER_ID, isOsAccountExists), ERR_OK);
611 EXPECT_EQ(isOsAccountExists, true);
612 }
613
614 /**
615 * @tc.name: OsAccountManagerModuleTest011
616 * @tc.desc: Test IsOsAccountExists with not exists data.
617 * @tc.type: FUNC
618 * @tc.require: issueI4IU51
619 */
620 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest011, TestSize.Level1)
621 {
622 bool isOsAccountExists = true;
623 EXPECT_EQ(OsAccountManager::IsOsAccountExists(Constants::MAX_USER_ID + 1, isOsAccountExists), ERR_OK);
624 EXPECT_EQ(isOsAccountExists, false);
625 }
626
627 /**
628 * @tc.name: OsAccountManagerModuleTest012
629 * @tc.desc: Test IsOsAccountActived with valid data.
630 * @tc.type: FUNC
631 * @tc.require: issueI4JBFF
632 */
633 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest012, TestSize.Level0)
634 {
635 bool isOsAccountActived = false;
636 EXPECT_EQ(OsAccountManager::IsOsAccountActived(Constants::ADMIN_LOCAL_ID, isOsAccountActived), ERR_OK);
637 EXPECT_EQ(isOsAccountActived, true);
638 }
639
640 /**
641 * @tc.name: OsAccountManagerModuleTest013
642 * @tc.desc: Test IsOsAccountActived with not active account id.
643 * @tc.type: FUNC
644 * @tc.require: issueI4JBFF
645 */
646 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
647 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest013, TestSize.Level1)
648 {
649 OsAccountInfo osAccountInfoOne;
650 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest013", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
651 bool isOsAccountActived = false;
652 EXPECT_EQ(OsAccountManager::IsOsAccountActived(osAccountInfoOne.GetLocalId(), isOsAccountActived), ERR_OK);
653 EXPECT_EQ(isOsAccountActived, false);
654 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
655 }
656
657 /**
658 * @tc.name: OsAccountManagerModuleTest014
659 * @tc.desc: Test SetOsAccountConstraints with valid data.
660 * @tc.type: FUNC
661 * @tc.require: issueI4IU6A
662 */
663 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest014, TestSize.Level1)
664 {
665 OsAccountInfo osAccountInfoOne;
666 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest014", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
667 bool enable = false;
668 EXPECT_EQ(OsAccountManager::SetOsAccountConstraints(osAccountInfoOne.GetLocalId(), CONSTANTS_VECTOR, enable),
669 ERR_OK);
670 OsAccountInfo osAccountInfoTwo;
671 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfoOne.GetLocalId(), osAccountInfoTwo), ERR_OK);
672 std::vector<std::string> constraints = osAccountInfoTwo.GetConstraints();
673 EXPECT_TRUE(std::includes(constraints.begin(), constraints.end(), CONSTANTS_VECTOR.begin(), CONSTANTS_VECTOR.end(),
__anon2e9c97090402(const std::string& s1, const std::string& s2) 674 [](const std::string& s1, const std::string& s2) { return s1 == s2; }));
675 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
676 }
677
678 /**
679 * @tc.name: OsAccountManagerModuleTest015
680 * @tc.desc: Test SetOsAccountConstraints with valid data.
681 * @tc.type: FUNC
682 * @tc.require: issueI4IU6A
683 */
684 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest015, TestSize.Level1)
685 {
686 OsAccountInfo osAccountInfoOne;
687 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest015", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
688 bool enable = true;
689 EXPECT_EQ(OsAccountManager::SetOsAccountConstraints(osAccountInfoOne.GetLocalId(), CONSTANTS_VECTOR, enable),
690 ERR_OK);
691 OsAccountInfo osAccountInfoTwo;
692 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfoOne.GetLocalId(), osAccountInfoTwo), ERR_OK);
693 std::vector<std::string> constraints = osAccountInfoTwo.GetConstraints();
694 EXPECT_TRUE(std::includes(constraints.begin(), constraints.end(), CONSTANTS_VECTOR.begin(), CONSTANTS_VECTOR.end(),
__anon2e9c97090502(const std::string& s1, const std::string& s2) 695 [](const std::string& s1, const std::string& s2) { return s1 == s2; }));
696 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
697 }
698
699 /**
700 * @tc.name: OsAccountManagerModuleTest016
701 * @tc.desc: Test IsOsAccountConstraintEnable with valid data.
702 * @tc.type: FUNC
703 * @tc.require: issueI4IU6A issueI4RCGG
704 */
705 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest016, TestSize.Level1)
706 {
707 OsAccountInfo osAccountInfoOne;
708 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest016", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
709 bool enable = true;
710 EXPECT_EQ(OsAccountManager::SetOsAccountConstraints(osAccountInfoOne.GetLocalId(), CONSTANTS_VECTOR, enable),
711 ERR_OK);
712 bool isEnable = false;
713 EXPECT_EQ(
714 OsAccountManager::IsOsAccountConstraintEnable(osAccountInfoOne.GetLocalId(), CONSTANT_PRINT, isEnable),
715 ERR_OK);
716 EXPECT_EQ(isEnable, true);
717 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
718 }
719
720 /**
721 * @tc.name: OsAccountManagerModuleTest017
722 * @tc.desc: Test IsOsAccountConstraintEnable with valid data.
723 * @tc.type: FUNC
724 * @tc.require: issueI4IU6A issueI4RCGG
725 */
726 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest017, TestSize.Level1)
727 {
728 OsAccountInfo osAccountInfoOne;
729 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest017", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
730 bool isEnable = true;
731 EXPECT_EQ(
732 OsAccountManager::IsOsAccountConstraintEnable(osAccountInfoOne.GetLocalId(), CONSTANT_PRINT, isEnable),
733 ERR_OK);
734 EXPECT_EQ(isEnable, false);
735 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
736 }
737
738 /**
739 * @tc.name: OsAccountManagerModuleTest018
740 * @tc.desc: Test IsMultiOsAccountEnable
741 * @tc.type: FUNC
742 * @tc.require: issueI4JBFF
743 */
744 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest018, TestSize.Level1)
745 {
746 bool isMultiOsAccountEnable = false;
747 EXPECT_EQ(OsAccountManager::IsMultiOsAccountEnable(isMultiOsAccountEnable), ERR_OK);
748 EXPECT_EQ(isMultiOsAccountEnable, true);
749 }
750 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
751
752 /**
753 * @tc.name: OsAccountManagerModuleTest019
754 * @tc.desc: Test IsOsAccountVerified with not verified os account id.
755 * @tc.type: FUNC
756 * @tc.require: issueI4JBFF
757 */
758 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest019, TestSize.Level1)
759 {
760 bool isVerified = false;
761 EXPECT_EQ(OsAccountManager::IsOsAccountVerified(Constants::START_USER_ID, isVerified), ERR_OK);
762 EXPECT_EQ(isVerified, true);
763 EXPECT_EQ(OsAccountManager::SetOsAccountIsVerified(Constants::START_USER_ID, false), ERR_OK);
764 EXPECT_EQ(OsAccountManager::IsOsAccountVerified(Constants::START_USER_ID, isVerified), ERR_OK);
765 EXPECT_EQ(isVerified, false);
766 EXPECT_EQ(OsAccountManager::SetOsAccountIsVerified(Constants::START_USER_ID, true), ERR_OK);
767 EXPECT_EQ(OsAccountManager::IsOsAccountVerified(Constants::START_USER_ID, isVerified), ERR_OK);
768 EXPECT_EQ(isVerified, true);
769 }
770
771 /**
772 * @tc.name: OsAccountManagerModuleTest020
773 * @tc.desc: Test IsOsAccountVerified with does not exists os account id.
774 * @tc.type: FUNC
775 * @tc.require: issueI4JBFF
776 */
777 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest020, TestSize.Level1)
778 {
779 bool isVerified = true;
780 EXPECT_NE(OsAccountManager::IsOsAccountVerified(Constants::MAX_USER_ID + 1, isVerified), ERR_OK);
781 }
782
783 /**
784 * @tc.name: OsAccountManagerModuleTest021
785 * @tc.desc: Test IsOsAccountVerified with does not exists os account id.
786 * @tc.type: FUNC
787 * @tc.require: issueI4JBFF
788 */
789 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest021, TestSize.Level1)
790 {
791 bool isVerified = true;
792 EXPECT_NE(OsAccountManager::IsOsAccountVerified(Constants::MAX_USER_ID + 1, isVerified), ERR_OK);
793 }
794
795 /**
796 * @tc.name: OsAccountManagerModuleTest022
797 * @tc.desc: Test GetCreatedOsAccountsCount.
798 * @tc.type: FUNC
799 * @tc.require: issueI4JBFF
800 */
801 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest022, TestSize.Level1)
802 {
803 unsigned int osAccountsCount = 0;
804 EXPECT_EQ(OsAccountManager::GetCreatedOsAccountsCount(osAccountsCount), ERR_OK);
805 }
806
807 /**
808 * @tc.name: OsAccountManagerModuleTest023
809 * @tc.desc: Test GetOsAccountLocalIdFromProcess.
810 * @tc.type: FUNC
811 * @tc.require: issueI4IU6N
812 */
813 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest023, TestSize.Level1)
814 {
815 int id = -1;
816 EXPECT_EQ(OsAccountManager::GetOsAccountLocalIdFromProcess(id), ERR_OK);
817 }
818
819 /**
820 * @tc.name: OsAccountManagerModuleTest024
821 * @tc.desc: Test GetOsAccountLocalIdFromUid.
822 * @tc.type: FUNC
823 * @tc.require: issueI4IU6N
824 */
825 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest024, TestSize.Level1)
826 {
827 int id = -1;
828 int testUid = 1000000; // uid for test
829 int expectedUserID = 5; // the expected result user ID
830 EXPECT_EQ(OsAccountManager::GetOsAccountLocalIdFromUid(testUid, id), ERR_OK);
831 EXPECT_EQ(expectedUserID, id);
832 }
833
834 /**
835 * @tc.name: OsAccountManagerModuleTest025
836 * @tc.desc: Test QueryMaxOsAccountNumber.
837 * @tc.type: FUNC
838 * @tc.require: issueI4JBFF
839 */
840 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest025, TestSize.Level1)
841 {
842 uint32_t maxOsAccountNumber = 0;
843 EXPECT_EQ(OsAccountManager::QueryMaxOsAccountNumber(maxOsAccountNumber), ERR_OK);
844 }
845
846 /**
847 * @tc.name: QueryMaxLoggedInOsAccountNumberModuleTest001
848 * @tc.desc: Test QueryMaxLoggedInOsAccountNumber.
849 * @tc.type: FUNC
850 * @tc.require:
851 */
852 HWTEST_F(OsAccountManagerModuleTest, QueryMaxLoggedInOsAccountNumberModuleTest001, TestSize.Level1)
853 {
854 uint32_t maxNum = 0;
855 EXPECT_EQ(OsAccountManager::QueryMaxLoggedInOsAccountNumber(maxNum), ERR_OK);
856 }
857
858 /**
859 * @tc.name: OsAccountManagerModuleTest026
860 * @tc.desc: Test GetOsAccountAllConstraints with exisit os account id.
861 * @tc.type: FUNC
862 * @tc.require: issueI4IU6A issueI4RCGG
863 */
864 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest026, TestSize.Level1)
865 {
866 std::vector<std::string> constraints;
867 EXPECT_EQ(OsAccountManager::GetOsAccountAllConstraints(Constants::START_USER_ID, constraints), ERR_OK);
868 const unsigned int size = 0;
869 EXPECT_NE(size, constraints.size());
870 constraints.clear();
871 EXPECT_NE(OsAccountManager::GetOsAccountAllConstraints(199, constraints), ERR_OK);
872 }
873
874 /**
875 * @tc.name: OsAccountManagerModuleTest027
876 * @tc.desc: Test GetOsAccountAllConstraints with does not exisit os account id.
877 * @tc.type: FUNC
878 * @tc.require: issueI4IU6A issueI4RCGG
879 */
880 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest027, TestSize.Level1)
881 {
882 std::vector<std::string> constraints;
883 EXPECT_NE(OsAccountManager::GetOsAccountAllConstraints(Constants::MAX_USER_ID + 1, constraints), ERR_OK);
884 }
885
886 /**
887 * @tc.name: OsAccountManagerModuleTest028
888 * @tc.desc: Test QueryAllCreatedOsAccounts.
889 * @tc.type: FUNC
890 * @tc.require: issueI4RCGG
891 */
892 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest028, TestSize.Level1)
893 {
894 std::vector<OsAccountInfo> osAccountInfos;
895 EXPECT_EQ(OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos), ERR_OK);
896 EXPECT_EQ(1, osAccountInfos.size());
897 OsAccountInfo osAccountInfoOne;
898 EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest028_1", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
899 osAccountInfos.clear();
900 EXPECT_EQ(OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos), ERR_OK);
901 EXPECT_EQ(2, osAccountInfos.size());
902 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
903
904 OsAccountInfo osAccountInfoTwo;
905 EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest028_2", OsAccountType::GUEST, osAccountInfoTwo), ERR_OK);
906 osAccountInfos.clear();
907 EXPECT_EQ(OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos), ERR_OK);
908 EXPECT_EQ(2, osAccountInfos.size());
909
910 EXPECT_EQ(OsAccountManager::SetOsAccountToBeRemoved(osAccountInfoTwo.GetLocalId(), true), ERR_OK);
911 osAccountInfos.clear();
912 EXPECT_EQ(OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos), ERR_OK);
913 EXPECT_EQ(1, osAccountInfos.size());
914
915 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoTwo.GetLocalId()), ERR_OK);
916 }
917
918 /**
919 * @tc.name: OsAccountManagerModuleTest029
920 * @tc.desc: Test QueryCurrentOsAccount.
921 * @tc.type: FUNC
922 * @tc.require: issueI4JBFF
923 */
924 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest029, TestSize.Level1)
925 {
926 OsAccountInfo osAccountInfo;
927 EXPECT_EQ(OsAccountManager::QueryCurrentOsAccount(osAccountInfo), ERR_OK);
928 }
929
930 /**
931 * @tc.name: OsAccountManagerModuleTest030
932 * @tc.desc: Test QueryOsAccountById with valid data.
933 * @tc.type: FUNC
934 * @tc.require: issueI4IU6N
935 */
936 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest030, TestSize.Level0)
937 {
938 OsAccountInfo osAccountInfo;
939 EXPECT_EQ(OsAccountManager::QueryOsAccountById(Constants::START_USER_ID, osAccountInfo), ERR_OK);
940 EXPECT_EQ(Constants::START_USER_ID, osAccountInfo.GetLocalId());
941 }
942
943 /**
944 * @tc.name: OsAccountManagerModuleTest031
945 * @tc.desc: Test QueryOsAccountById with invalid data.
946 * @tc.type: FUNC
947 * @tc.require: issueI4IU6N
948 */
949 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest031, TestSize.Level1)
950 {
951 OsAccountInfo osAccountInfo;
952 EXPECT_NE(OsAccountManager::QueryOsAccountById(Constants::MAX_USER_ID + 1, osAccountInfo), ERR_OK);
953 }
954
955 /**
956 * @tc.name: OsAccountManagerModuleTest032
957 * @tc.desc: Test GetOsAccountTypeFromProcess.
958 * @tc.type: FUNC
959 * @tc.require: issueI4JBFF
960 */
961 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest032, TestSize.Level1)
962 {
963 OsAccountType type = OsAccountType::ADMIN;
964 EXPECT_EQ(OsAccountManager::GetOsAccountTypeFromProcess(type), ERR_OK);
965 }
966
967 /**
968 * @tc.name: OsAccountManagerModuleTest033
969 * @tc.desc: Test SetOsAccountName with valid data.
970 * @tc.type: FUNC
971 * @tc.require: issueI4IU6N
972 */
973 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
974 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest033, TestSize.Level1)
975 {
976 OsAccountInfo osAccountInfoOne;
977 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest033", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
978 EXPECT_EQ(OsAccountManager::SetOsAccountName(osAccountInfoOne.GetLocalId(), STRING_NAME), ERR_OK);
979 OsAccountInfo osAccountInfoTwo;
980 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfoOne.GetLocalId(), osAccountInfoTwo), ERR_OK);
981 EXPECT_EQ(STRING_NAME, osAccountInfoTwo.GetLocalName());
982 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
983 }
984
985 /**
986 * @tc.name: OsAccountManagerModuleTest034
987 * @tc.desc: Test SetOsAccountName with invalid data.
988 * @tc.type: FUNC
989 * @tc.require: issueI4IU6N
990 */
991 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest034, TestSize.Level1)
992 {
993 OsAccountInfo osAccountInfoOne;
994 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest034", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
995 EXPECT_NE(OsAccountManager::SetOsAccountName(osAccountInfoOne.GetLocalId(), STRING_EMPTY), ERR_OK);
996 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
997 }
998
999 /**
1000 * @tc.name: OsAccountManagerModuleTest035
1001 * @tc.desc: Test SetOsAccountName with invalid data.
1002 * @tc.type: FUNC
1003 * @tc.require: issueI4IU6N
1004 */
1005 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest035, TestSize.Level1)
1006 {
1007 OsAccountInfo osAccountInfoOne;
1008 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest035", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1009 EXPECT_NE(OsAccountManager::SetOsAccountName(osAccountInfoOne.GetLocalId(), STRING_NAME_OUT_OF_RANGE), ERR_OK);
1010 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1011 }
1012 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1013
1014 /**
1015 * @tc.name: OsAccountManagerModuleTest036
1016 * @tc.desc: Test GetDistributedVirtualDeviceId.
1017 * @tc.type: FUNC
1018 * @tc.require: issueI4JBFF
1019 */
1020 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest036, TestSize.Level1)
1021 {
1022 // before ohos account login
1023 std::string deviceId;
1024 ErrCode ret = OsAccountManager::GetDistributedVirtualDeviceId(deviceId);
1025 EXPECT_EQ(ret, ERR_OK);
1026
1027 // ohos account login
1028 sptr<ISystemAbilityManager> systemMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1029 ASSERT_NE(systemMgr, nullptr);
1030 sptr<IRemoteObject> accountObj = systemMgr->GetSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
1031 ASSERT_NE(accountObj, nullptr);
1032 sptr<IAccount> ohosMgr = iface_cast<AccountProxy>(accountObj);
1033 EXPECT_NE(ohosMgr, nullptr);
1034 ohosMgr->UpdateOhosAccountInfo(TEST_ACCOUNT_NAME, TEST_ACCOUNT_UID, OHOS_ACCOUNT_EVENT_LOGIN);
1035
1036 // after ohos account login
1037 ret = OsAccountManager::GetDistributedVirtualDeviceId(deviceId);
1038 EXPECT_EQ(ret, ERR_OK);
1039
1040 // ohos account logout
1041 ohosMgr->UpdateOhosAccountInfo(TEST_ACCOUNT_NAME, TEST_ACCOUNT_UID, OHOS_ACCOUNT_EVENT_LOGOUT);
1042
1043 // after ohos account logout
1044 ret = OsAccountManager::GetDistributedVirtualDeviceId(deviceId);
1045 EXPECT_EQ(ret, ERR_OK);
1046 }
1047
1048 /**
1049 * @tc.name: OsAccountManagerModuleTest037
1050 * @tc.desc: Test GetOsAccountLocalIdBySerialNumber with valid data.
1051 * @tc.type: FUNC
1052 * @tc.require: issueI4IU6N
1053 */
1054 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest037, TestSize.Level1)
1055 {
1056 int id = 0;
1057 EXPECT_EQ(OsAccountManager::GetOsAccountLocalIdBySerialNumber(
1058 Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + 1, id), ERR_OK);
1059 EXPECT_EQ(id, Constants::START_USER_ID);
1060 }
1061
1062 /**
1063 * @tc.name: OsAccountManagerModuleTest038
1064 * @tc.desc: Test GetOsAccountLocalIdBySerialNumber with invalid data.
1065 * @tc.type: FUNC
1066 * @tc.require: issueI4IU6N
1067 */
1068 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest038, TestSize.Level1)
1069 {
1070 int32_t id = 0;
1071 EXPECT_NE(OsAccountManager::GetOsAccountLocalIdBySerialNumber(INVALID_SERIAL_NUM, id), ERR_OK);
1072 }
1073
1074 /**
1075 * @tc.name: OsAccountManagerModuleTest039
1076 * @tc.desc: Test GetSerialNumberByOsAccountLocalId with valid data.
1077 * @tc.type: FUNC
1078 * @tc.require: issueI4IU6N
1079 */
1080 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest039, TestSize.Level1)
1081 {
1082 int64_t serialNumber;
1083 EXPECT_EQ(OsAccountManager::GetSerialNumberByOsAccountLocalId(Constants::START_USER_ID, serialNumber), ERR_OK);
1084 EXPECT_EQ(serialNumber, Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + 1);
1085 }
1086
1087 /**
1088 * @tc.name: OsAccountManagerModuleTest040
1089 * @tc.desc: Test GetSerialNumberByOsAccountLocalId with invalid data.
1090 * @tc.type: FUNC
1091 * @tc.require: issueI4IU6N
1092 */
1093 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest040, TestSize.Level1)
1094 {
1095 int64_t serialNumber;
1096 EXPECT_NE(OsAccountManager::GetSerialNumberByOsAccountLocalId(Constants::MAX_USER_ID + 1, serialNumber), ERR_OK);
1097 }
1098
1099 /**
1100 * @tc.name: OsAccountManagerModuleTest041
1101 * @tc.desc: Test SetOsAccountProfilePhoto with valid data.
1102 * @tc.type: FUNC
1103 * @tc.require: issueI4IU33
1104 */
1105 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1106 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest041, TestSize.Level1)
1107 {
1108 OsAccountInfo osAccountInfoOne;
1109 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest041", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1110 EXPECT_EQ(OsAccountManager::SetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), PHOTO_IMG), ERR_OK);
1111 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1112 }
1113
1114 /**
1115 * @tc.name: OsAccountManagerModuleTest042
1116 * @tc.desc: Test SetOsAccountProfilePhoto with invalid data.
1117 * @tc.type: FUNC
1118 * @tc.require: issueI4IU33
1119 */
1120 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest042, TestSize.Level1)
1121 {
1122 OsAccountInfo osAccountInfoOne;
1123 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest042", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1124 EXPECT_NE(
1125 OsAccountManager::SetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), STRING_PHOTO_OUT_OF_RANGE), ERR_OK);
1126 EXPECT_EQ(
1127 OsAccountManager::SetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), STRING_PHOTO_MAX), ERR_OK);
1128 std::string photo;
1129 EXPECT_EQ(
1130 OsAccountManager::GetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), photo), ERR_OK);
1131 OsAccountInfo osAccountInfoTwo;
1132 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfoOne.GetLocalId(), osAccountInfoTwo), ERR_OK);
1133 EXPECT_EQ(osAccountInfoTwo.GetLocalId(), osAccountInfoOne.GetLocalId());
1134 OsAccountInfo osAccountInfoThree;
1135 ASSERT_EQ(OsAccountManager::CreateOsAccount("Modulelist042", OsAccountType::GUEST, osAccountInfoThree), ERR_OK);
1136 std::vector<OsAccountInfo> osAccountInfos;
1137 ASSERT_EQ(OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos), ERR_OK);
1138 ASSERT_EQ(osAccountInfos.size(), 3);
1139 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1140 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoThree.GetLocalId()), ERR_OK);
1141 }
1142
1143 /**
1144 * @tc.name: OsAccountManagerModuleTest043
1145 * @tc.desc: Test SetOsAccountProfilePhoto with invalid data.
1146 * @tc.type: FUNC
1147 * @tc.require: issueI4IU33
1148 */
1149 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest043, TestSize.Level1)
1150 {
1151 OsAccountInfo osAccountInfoOne;
1152 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest043", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1153 EXPECT_EQ(OsAccountManager::SetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), PHOTO_IMG_ERROR), ERR_OK);
1154 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1155 }
1156
1157 /**
1158 * @tc.name: OsAccountManagerModuleTest044
1159 * @tc.desc: Test GetOsAccountProfilePhoto with valid data.
1160 * @tc.type: FUNC
1161 * @tc.require: issueI4IU6N
1162 */
1163 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest044, TestSize.Level1)
1164 {
1165 OsAccountInfo osAccountInfoOne;
1166 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest044", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1167 EXPECT_EQ(OsAccountManager::SetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), PHOTO_IMG), ERR_OK);
1168 std::string photo;
1169 EXPECT_EQ(OsAccountManager::GetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), photo), ERR_OK);
1170 EXPECT_EQ(photo, PHOTO_IMG);
1171 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1172 }
1173
1174 /**
1175 * @tc.name: OsAccountManagerModuleTest045
1176 * @tc.desc: test get default photo.
1177 * @tc.type: FUNC
1178 * @tc.require: issueI4IU6N
1179 */
1180 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest045, TestSize.Level1)
1181 {
1182 OsAccountInfo osAccountInfoOne;
1183 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest045", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1184 std::string photo;
1185 EXPECT_EQ(OsAccountManager::GetOsAccountProfilePhoto(osAccountInfoOne.GetLocalId(), photo), ERR_OK);
1186 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1187 }
1188 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1189
1190 /**
1191 * @tc.name: OsAccountManagerModuleTest046
1192 * @tc.desc: Test GetOsAccountProfilePhoto with invalid id.
1193 * @tc.type: FUNC
1194 * @tc.require: issueI4IU6N
1195 */
1196 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest046, TestSize.Level1)
1197 {
1198 std::string photo;
1199 EXPECT_NE(OsAccountManager::GetOsAccountProfilePhoto(Constants::MAX_USER_ID + 1, photo), ERR_OK);
1200 }
1201
1202 /**
1203 * @tc.name: OsAccountManagerModuleTest047
1204 * @tc.desc: Test StartOsAccount with valid id.
1205 * @tc.type: FUNC
1206 * @tc.require: issueI4IU3B
1207 */
1208 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1209 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest047, TestSize.Level1)
1210 {
1211 OsAccountInfo osAccountInfoOne;
1212 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest047", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1213 EXPECT_EQ(OsAccountManager::StartOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1214 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1215 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1216 }
1217 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1218
1219 /**
1220 * @tc.name: OsAccountManagerModuleTest048
1221 * @tc.desc: Test StartOsAccount with invalid id.
1222 * @tc.type: FUNC
1223 * @tc.require: issueI4IU3B
1224 */
1225 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest048, TestSize.Level1)
1226 {
1227 EXPECT_EQ(OsAccountManager::StartOsAccount(Constants::MAX_USER_ID + 1), ERR_OK);
1228 }
1229
1230 /**
1231 * @tc.name: OsAccountManagerModuleTest049
1232 * @tc.desc: Test StartOsAccount with started id.
1233 * @tc.type: FUNC
1234 * @tc.require: issueI4IU3B
1235 */
1236 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest049, TestSize.Level1)
1237 {
1238 EXPECT_EQ(OsAccountManager::StartOsAccount(Constants::START_USER_ID), ERR_OK);
1239 }
1240
1241 /**
1242 * @tc.name: OsAccountManagerModuleTest052
1243 * @tc.desc: Test IsOsAccountVerified with invalid data.
1244 * @tc.type: FUNC
1245 * @tc.require: issueI4IU3B
1246 */
1247 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest052, TestSize.Level1)
1248 {
1249 bool isVerified = false;
1250 EXPECT_NE(OsAccountManager::IsOsAccountVerified(ERROR_LOCAL_ID, isVerified), ERR_OK);
1251 }
1252
1253 /**
1254 * @tc.name: OsAccountManagerModuleTest053
1255 * @tc.desc: Create os account for domain, and remove it
1256 * @tc.type: FUNC
1257 * @tc.require: issueI4IU3V
1258 */
1259 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1260 #ifdef DOMAIN_ACCOUNT_TEST_CASE
1261 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest053, TestSize.Level1)
1262 {
1263 DomainAccountInfo domainInfo(STRING_DOMAIN_VALID, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1264 OsAccountType type = NORMAL;
1265 OsAccountInfo osAccountInfo;
1266 EXPECT_EQ(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo), ERR_OK);
1267
1268 bool checkValid = (osAccountInfo.GetLocalId() > Constants::START_USER_ID);
1269 EXPECT_EQ(checkValid, true);
1270
1271 DomainAccountInfo resDomainInfo;
1272 osAccountInfo.GetDomainInfo(resDomainInfo);
1273 checkValid = (resDomainInfo.accountName_ == domainInfo.accountName_);
1274 EXPECT_EQ(checkValid, true);
1275 checkValid = (resDomainInfo.domain_ == domainInfo.domain_);
1276 EXPECT_EQ(checkValid, true);
1277
1278 std::string osAccountName = domainInfo.domain_ + "/" + domainInfo.accountName_;
1279 checkValid = (osAccountInfo.GetLocalName() == osAccountName);
1280 EXPECT_EQ(checkValid, true);
1281
1282 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
1283 }
1284
1285 /**
1286 * @tc.name: OsAccountManagerModuleTest054
1287 * @tc.desc: Create os account for domain, and activate it.
1288 * @tc.type: FUNC
1289 * @tc.require: issueI4IU3V
1290 */
1291 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest054, TestSize.Level1)
1292 {
1293 // create
1294 DomainAccountInfo domainInfo(STRING_DOMAIN_VALID, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1295 OsAccountType type = NORMAL;
1296 OsAccountInfo osAccountInfo;
1297 EXPECT_EQ(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo), ERR_OK);
1298
1299 bool checkValid = (osAccountInfo.GetLocalId() > Constants::START_USER_ID);
1300 EXPECT_EQ(checkValid, true);
1301
1302 DomainAccountInfo resDomainInfo;
1303 osAccountInfo.GetDomainInfo(resDomainInfo);
1304 checkValid = (resDomainInfo.accountName_ == domainInfo.accountName_);
1305 EXPECT_EQ(checkValid, true);
1306 checkValid = (resDomainInfo.domain_ == domainInfo.domain_);
1307 EXPECT_EQ(checkValid, true);
1308
1309 std::string osAccountName = domainInfo.domain_ + "/" + domainInfo.accountName_;
1310 checkValid = (osAccountInfo.GetLocalName() == osAccountName);
1311 EXPECT_EQ(checkValid, true);
1312
1313 // check
1314 OsAccountInfo queryAccountInfo;
1315 EXPECT_EQ(OsAccountManager::QueryOsAccountById(osAccountInfo.GetLocalId(), queryAccountInfo), ERR_OK);
1316 DomainAccountInfo queryDomainInfo;
1317 queryAccountInfo.GetDomainInfo(queryDomainInfo);
1318
1319 EXPECT_EQ(queryAccountInfo.GetLocalId(), osAccountInfo.GetLocalId());
1320 checkValid = (queryAccountInfo.GetLocalName() == osAccountName);
1321 EXPECT_EQ(checkValid, true);
1322 checkValid = (queryDomainInfo.accountName_ == domainInfo.accountName_);
1323 EXPECT_EQ(checkValid, true);
1324 checkValid = (queryDomainInfo.domain_ == domainInfo.domain_);
1325 EXPECT_EQ(checkValid, true);
1326
1327 // remove
1328 OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId());
1329 }
1330
1331 /**
1332 * @tc.name: OsAccountManagerModuleTest055
1333 * @tc.desc: Create os account for domain use invalid data.
1334 * @tc.type: FUNC
1335 * @tc.require: issueI4IU3V
1336 */
1337 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest055, TestSize.Level1)
1338 {
1339 DomainAccountInfo domainNameInvalid(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1340 OsAccountType type = NORMAL;
1341 OsAccountInfo osAccountInfo;
1342 ErrCode ret = OsAccountManager::CreateOsAccountForDomain(type, domainNameInvalid, osAccountInfo);
1343 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1344
1345 DomainAccountInfo domainAccountNameInvalid(STRING_DOMAIN_VALID, STRING_DOMAIN_ACCOUNT_NAME_OUT_OF_RANGE);
1346 ret = OsAccountManager::CreateOsAccountForDomain(type, domainAccountNameInvalid, osAccountInfo);
1347 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1348
1349 DomainAccountInfo domainEmpty("", STRING_DOMAIN_ACCOUNT_NAME_VALID);
1350 ret = OsAccountManager::CreateOsAccountForDomain(type, domainEmpty, osAccountInfo);
1351 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1352
1353 DomainAccountInfo domainAccountEmpty(STRING_DOMAIN_VALID, "");
1354 ret = OsAccountManager::CreateOsAccountForDomain(type, domainAccountEmpty, osAccountInfo);
1355 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1356 }
1357
1358 /**
1359 * @tc.name: OsAccountManagerModuleTest056
1360 * @tc.desc: repeat create os account for domain by module
1361 * @tc.type: FUNC
1362 * @tc.require: issueI4IU3V
1363 */
1364 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest056, TestSize.Level1)
1365 {
1366 // create
1367 DomainAccountInfo domainInfo(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1368 OsAccountType type = NORMAL;
1369 OsAccountInfo osAccountInfo;
1370 EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo), ERR_OK);
1371
1372 // create again
1373 EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo),
1374 ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR);
1375 }
1376
1377 /**
1378 * @tc.name: OsAccountManagerModuleTest057
1379 * @tc.desc: repeat create os account for domain after remove by module
1380 * @tc.type: FUNC
1381 * @tc.require: issueI4IU3V
1382 */
1383 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest057, TestSize.Level1)
1384 {
1385 // create
1386 DomainAccountInfo domainInfo(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1387 OsAccountType type = NORMAL;
1388 OsAccountInfo osAccountInfo;
1389 EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo), ERR_OK);
1390
1391 // create again
1392 EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo),
1393 ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR);
1394
1395 // remove
1396 EXPECT_NE(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
1397
1398 // create again
1399 EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo), ERR_OK);
1400
1401 // remove
1402 EXPECT_NE(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
1403 }
1404
1405 /**
1406 * @tc.name: OsAccountManagerModuleTest058
1407 * @tc.desc: query os account by domain info
1408 * @tc.type: FUNC
1409 * @tc.require: issueI4IU3V
1410 */
1411 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest058, TestSize.Level1)
1412 {
1413 // create
1414 DomainAccountInfo domainInfo(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1415 OsAccountType type = NORMAL;
1416 OsAccountInfo osAccountInfo;
1417 EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo), ERR_OK);
1418
1419 // remove
1420 EXPECT_NE(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
1421 }
1422
1423 /**
1424 * @tc.name: OsAccountManagerModuleTest059
1425 * @tc.desc: query os account by domain info
1426 * @tc.type: FUNC
1427 * @tc.require: issueI4IU3V
1428 */
1429 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest059, TestSize.Level1)
1430 {
1431 // create
1432 DomainAccountInfo domainInfo(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1433 OsAccountType type = NORMAL;
1434 OsAccountInfo osAccountInfo;
1435 ErrCode ret = OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo);
1436 EXPECT_NE(ret, ERR_OK);
1437
1438 // get os account local id by domain
1439 int resID = -1;
1440 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainInfo, resID);
1441 EXPECT_NE(ret, ERR_OK);
1442
1443 // remove
1444 ret = OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId());
1445 EXPECT_NE(ret, ERR_OK);
1446
1447 // cannot query
1448 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainInfo, resID);
1449 EXPECT_NE(ret, ERR_OK);
1450 }
1451 #endif // DOMAIN_ACCOUNT_TEST_CASE
1452 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1453
1454 /**
1455 * @tc.name: OsAccountManagerModuleTest060
1456 * @tc.desc: query os account by domain info
1457 * @tc.type: FUNC
1458 * @tc.require: issueI4IU3V
1459 */
1460 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest060, TestSize.Level1)
1461 {
1462 DomainAccountInfo domainAllEmpty("", "");
1463 int resID = -1;
1464 ErrCode ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainAllEmpty, resID);
1465 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1466
1467 DomainAccountInfo domainNameEmpty("", STRING_DOMAIN_ACCOUNT_NAME_VALID);
1468 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainNameEmpty, resID);
1469 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1470
1471 DomainAccountInfo domainAccountEmpty(STRING_DOMAIN_VALID, "");
1472 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainAccountEmpty, resID);
1473 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1474
1475 DomainAccountInfo domainAllTooLong(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_OUT_OF_RANGE);
1476 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainAllTooLong, resID);
1477 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1478
1479 DomainAccountInfo domainNameTooLong(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_VALID);
1480 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainNameTooLong, resID);
1481 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1482
1483 DomainAccountInfo domainAccountTooLong(STRING_DOMAIN_VALID, STRING_DOMAIN_ACCOUNT_NAME_OUT_OF_RANGE);
1484 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainAccountTooLong, resID);
1485 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
1486 }
1487
1488 /**
1489 * @tc.name: OsAccountManagerModuleTest061
1490 * @tc.desc: Test get os account info from database
1491 * @tc.type: FUNC
1492 * @tc.require: issueI4JBFI
1493 */
1494 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest061, TestSize.Level1)
1495 {
1496 char udid[Constants::DEVICE_UUID_LENGTH] = {0};
1497 int ret = GetDevUdid(udid, Constants::DEVICE_UUID_LENGTH);
1498 EXPECT_EQ(ret, 0);
1499 }
1500
1501 /**
1502 * @tc.name: OsAccountManagerModuleTest062
1503 * @tc.desc: Test get os account info from database
1504 * @tc.type: FUNC
1505 * @tc.require: issueI4JBFI
1506 */
1507 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest062, TestSize.Level1)
1508 {
1509 char udid[Constants::DEVICE_UUID_LENGTH] = {0};
1510 int ret = GetDevUdid(udid, Constants::DEVICE_UUID_LENGTH);
1511 EXPECT_EQ(ret, 0);
1512 }
1513
1514 /**
1515 * @tc.name: OsAccountManagerModuleTest063
1516 * @tc.desc: Test get os account info from database
1517 * @tc.type: FUNC
1518 * @tc.require: issueI4JBFI
1519 */
1520 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1521 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest063, TestSize.Level1)
1522 {
1523 char udid[Constants::DEVICE_UUID_LENGTH] = {0};
1524 int ret = GetDevUdid(udid, Constants::DEVICE_UUID_LENGTH);
1525 ASSERT_EQ(ret, 0);
1526
1527 // create a new account
1528 OsAccountInfo osAccountInfoOne;
1529 EXPECT_NE(OsAccountManager::CreateOsAccount("", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1530
1531 // get created account info
1532 OsAccountInfo osAccountInfo;
1533 ret = OsAccountManager::GetOsAccountFromDatabase("", osAccountInfoOne.GetLocalId(), osAccountInfo);
1534 EXPECT_NE(ret, ERR_OK);
1535 EXPECT_NE(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1536 }
1537
1538 /**
1539 * @tc.name: OsAccountManagerModuleTest065
1540 * @tc.desc: Test get os account info from database
1541 * @tc.type: FUNC
1542 * @tc.require: issueI4JBFI
1543 */
1544 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest065, TestSize.Level1)
1545 {
1546 char udid[Constants::DEVICE_UUID_LENGTH] = {0};
1547 int ret = GetDevUdid(udid, Constants::DEVICE_UUID_LENGTH);
1548 ASSERT_EQ(ret, 0);
1549 std::string storeID = std::string(udid);
1550
1551 // create a new os account
1552 OsAccountInfo osAccountInfoOne;
1553 EXPECT_NE(OsAccountManager::CreateOsAccount("", OsAccountType::GUEST, osAccountInfoOne), ERR_OK);
1554
1555 // get created account info
1556 OsAccountInfo osAccountInfo;
1557 ret = OsAccountManager::GetOsAccountFromDatabase(storeID, osAccountInfoOne.GetLocalId(), osAccountInfo);
1558 EXPECT_NE(ret, ERR_OK);
1559
1560 // remove the new os account
1561 ret = OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId());
1562 EXPECT_NE(ret, ERR_OK);
1563
1564 // cannot find the account in database
1565 OsAccountInfo osAccountInfoAfterRm;
1566 ret = OsAccountManager::GetOsAccountFromDatabase(storeID, osAccountInfoOne.GetLocalId(), osAccountInfoAfterRm);
1567 EXPECT_NE(ret, ERR_OK);
1568 }
1569 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1570
1571 /**
1572 * @tc.name: OsAccountManagerModuleTest066
1573 * @tc.desc: Test query active os account ids.
1574 * @tc.type: FUNC
1575 * @tc.require: issueI4M8FW
1576 */
1577 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest066, TestSize.Level1)
1578 {
1579 std::vector<int32_t> ids;
1580 EXPECT_EQ(OsAccountManager::QueryActiveOsAccountIds(ids), ERR_OK);
1581 }
1582
1583 /**
1584 * @tc.name: OsAccountManagerModuleTest067
1585 * @tc.desc: Test GetBundleIdFromUid.
1586 * @tc.type: FUNC
1587 * @tc.require:
1588 */
1589 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest067, TestSize.Level1)
1590 {
1591 int id = -1;
1592 int expectedBundleID = 5; // the expected result user ID
1593 int testUid = 1000000 + expectedBundleID; // uid for test
1594 EXPECT_EQ(OsAccountManager::GetBundleIdFromUid(testUid, id), ERR_OK);
1595 EXPECT_EQ(expectedBundleID, id);
1596 }
1597
1598 /**
1599 * @tc.name: OsAccountManagerModuleTest068
1600 * @tc.desc: Test IsMainOsAccount.
1601 * @tc.type: FUNC
1602 * @tc.require:
1603 */
1604 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest068, TestSize.Level1)
1605 {
1606 bool isMainOsAccount = false;
1607 EXPECT_EQ(OsAccountManager::IsMainOsAccount(isMainOsAccount), ERR_OK);
1608 int id = -1;
1609 EXPECT_EQ(OsAccountManager::GetOsAccountLocalIdFromProcess(id), ERR_OK);
1610 if (id == MAIN_ACCOUNT_ID) {
1611 EXPECT_EQ(isMainOsAccount, true);
1612 } else {
1613 EXPECT_EQ(isMainOsAccount, false);
1614 }
1615 }
1616
1617 /**
1618 * @tc.name: OsAccountManagerModuleTest069
1619 * @tc.desc: Test SetGlobalOsAccountConstraints.
1620 * @tc.type: FUNC
1621 * @tc.require:
1622 */
1623 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1624 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest069, TestSize.Level1)
1625 {
1626 OsAccountInfo osAccountInfoOne;
1627 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest069", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
1628
1629 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1630 CONSTANTS_VECTOR, true, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1631 bool isEnable = false;
1632 EXPECT_EQ(
1633 OsAccountManager::IsOsAccountConstraintEnable(osAccountInfoOne.GetLocalId(), CONSTANT_PRINT, isEnable),
1634 ERR_OK);
1635 EXPECT_EQ(isEnable, true);
1636 isEnable = false;
1637 EXPECT_EQ(
1638 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1639 ERR_OK);
1640 EXPECT_EQ(isEnable, true);
1641
1642 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1643 CONSTANTS_VECTOR, false, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1644 EXPECT_EQ(
1645 OsAccountManager::IsOsAccountConstraintEnable(osAccountInfoOne.GetLocalId(), CONSTANT_PRINT, isEnable),
1646 ERR_OK);
1647 EXPECT_EQ(isEnable, false);
1648 EXPECT_EQ(
1649 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1650 ERR_OK);
1651 EXPECT_EQ(isEnable, false);
1652
1653 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1654 }
1655
1656 /**
1657 * @tc.name: OsAccountManagerModuleTest070
1658 * @tc.desc: Test SetGlobalOsAccountConstraints.
1659 * @tc.type: FUNC
1660 * @tc.require:
1661 */
1662 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest070, TestSize.Level1)
1663 {
1664 OsAccountInfo osAccountInfoOne;
1665 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest070", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
1666 OsAccountInfo osAccountInfoTwo;
1667 ASSERT_EQ(OsAccountManager::CreateOsAccount(STRING_TEST_NAME_TWO, OsAccountType::NORMAL, osAccountInfoTwo), ERR_OK);
1668
1669 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1670 CONSTANTS_VECTOR, true, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1671 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1672 CONSTANTS_VECTOR, true, osAccountInfoTwo.GetLocalId(), true), ERR_OK);
1673 bool isEnable = false;
1674 EXPECT_EQ(
1675 OsAccountManager::IsOsAccountConstraintEnable(osAccountInfoOne.GetLocalId(), CONSTANT_PRINT, isEnable),
1676 ERR_OK);
1677 EXPECT_EQ(isEnable, true);
1678 isEnable = false;
1679 EXPECT_EQ(
1680 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1681 ERR_OK);
1682 EXPECT_EQ(isEnable, true);
1683
1684 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1685 CONSTANTS_VECTOR, false, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1686 EXPECT_EQ(
1687 OsAccountManager::IsOsAccountConstraintEnable(osAccountInfoOne.GetLocalId(), CONSTANT_PRINT, isEnable),
1688 ERR_OK);
1689 EXPECT_EQ(isEnable, true);
1690 EXPECT_EQ(
1691 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1692 ERR_OK);
1693 EXPECT_EQ(isEnable, true);
1694
1695 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1696 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoTwo.GetLocalId()), ERR_OK);
1697 EXPECT_EQ(
1698 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1699 ERR_OK);
1700 EXPECT_EQ(isEnable, false);
1701 }
1702
1703 /**
1704 * @tc.name: OsAccountManagerModuleTest071
1705 * @tc.desc: Test SetSpecificOsAccountConstraints.
1706 * @tc.type: FUNC
1707 * @tc.require:
1708 */
1709 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest071, TestSize.Level1)
1710 {
1711 OsAccountInfo osAccountInfoOne;
1712 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest071", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
1713
1714 EXPECT_EQ(OsAccountManager::SetSpecificOsAccountConstraints(
1715 CONSTANTS_VECTOR, true, MAIN_ACCOUNT_ID, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1716 bool isEnable = false;
1717 EXPECT_EQ(
1718 OsAccountManager::IsOsAccountConstraintEnable(osAccountInfoOne.GetLocalId(), CONSTANT_PRINT, isEnable),
1719 ERR_OK);
1720 EXPECT_EQ(isEnable, false);
1721 EXPECT_EQ(
1722 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1723 ERR_OK);
1724 EXPECT_EQ(isEnable, true);
1725
1726 EXPECT_EQ(OsAccountManager::SetSpecificOsAccountConstraints(
1727 CONSTANTS_VECTOR, false, MAIN_ACCOUNT_ID, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1728 EXPECT_EQ(
1729 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1730 ERR_OK);
1731 EXPECT_EQ(isEnable, false);
1732 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1733 }
1734
1735 /**
1736 * @tc.name: OsAccountManagerModuleTest072
1737 * @tc.desc: Test QueryOsAccountConstraintSourceTypes.
1738 * @tc.type: FUNC
1739 * @tc.require:
1740 */
1741 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest072, TestSize.Level1)
1742 {
1743 OsAccountInfo osAccountInfoOne;
1744 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest072", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
1745 EXPECT_EQ(OsAccountManager::SetSpecificOsAccountConstraints(
1746 CONSTANTS_VECTOR, true, MAIN_ACCOUNT_ID, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1747 bool isEnable = false;
1748 EXPECT_EQ(
1749 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1750 ERR_OK);
1751 EXPECT_EQ(isEnable, true);
1752
1753 std::vector<ConstraintSourceTypeInfo> constraintSourceTypeInfos;
1754 EXPECT_EQ(OsAccountManager::QueryOsAccountConstraintSourceTypes(
1755 MAIN_ACCOUNT_ID, CONSTANT_PRINT, constraintSourceTypeInfos), ERR_OK);
1756
1757 EXPECT_EQ(constraintSourceTypeInfos[0].typeInfo, 2);
1758 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1759 constraintSourceTypeInfos.clear();
1760 EXPECT_NE(OsAccountManager::QueryOsAccountConstraintSourceTypes(
1761 999, CONSTANT_PRINT, constraintSourceTypeInfos), ERR_OK);
1762 EXPECT_EQ(
1763 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTANT_PRINT, isEnable),
1764 ERR_OK);
1765 EXPECT_EQ(isEnable, false);
1766 }
1767
1768 /**
1769 * @tc.name: OsAccountManagerModuleTest073
1770 * @tc.desc: Test QueryOsAccountConstraintSourceTypes.
1771 * @tc.type: FUNC
1772 * @tc.require:
1773 */
1774 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest073, TestSize.Level1)
1775 {
1776 OsAccountInfo osAccountInfoOne;
1777 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest073", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
1778 OsAccountInfo osAccountInfoTwo;
1779 ASSERT_EQ(OsAccountManager::CreateOsAccount(STRING_TEST_NAME_TWO, OsAccountType::NORMAL, osAccountInfoTwo), ERR_OK);
1780
1781 std::vector<ConstraintSourceTypeInfo> constraintSourceTypeInfos;
1782 EXPECT_EQ(OsAccountManager::QueryOsAccountConstraintSourceTypes(
1783 MAIN_ACCOUNT_ID, CONSTANT_PRINT, constraintSourceTypeInfos), ERR_OK);
1784 EXPECT_EQ(constraintSourceTypeInfos[0].typeInfo, 0);
1785
1786 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1787 CONSTANTS_VECTOR_TEST, true, osAccountInfoOne.GetLocalId(), false), ERR_OK);
1788 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1789 CONSTANTS_VECTOR_TEST, true, osAccountInfoTwo.GetLocalId(), false), ERR_OK);
1790
1791 bool isEnable = false;
1792 EXPECT_EQ(
1793 OsAccountManager::IsOsAccountConstraintEnable(MAIN_ACCOUNT_ID, CONSTRAINT_PRIVATE_DNS_SET, isEnable),
1794 ERR_OK);
1795 EXPECT_EQ(isEnable, true);
1796
1797 EXPECT_EQ(OsAccountManager::QueryOsAccountConstraintSourceTypes(
1798 MAIN_ACCOUNT_ID, CONSTRAINT_PRIVATE_DNS_SET, constraintSourceTypeInfos), ERR_OK);
1799 EXPECT_EQ(constraintSourceTypeInfos.size(), 2);
1800 ACCOUNT_LOGD("OsAccountManagerModuleTest073 constraintSourceTypeInfos %{public}d %{public}d",
1801 constraintSourceTypeInfos[0].localId, constraintSourceTypeInfos[0].typeInfo);
1802 ACCOUNT_LOGD("OsAccountManagerModuleTest073 constraintSourceTypeInfos %{public}d %{public}d",
1803 constraintSourceTypeInfos[1].localId, constraintSourceTypeInfos[1].typeInfo);
1804 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoTwo.GetLocalId()), ERR_OK);
1805
1806 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1807
1808 EXPECT_EQ(OsAccountManager::QueryOsAccountConstraintSourceTypes(
1809 MAIN_ACCOUNT_ID, CONSTRAINT_PRIVATE_DNS_SET, constraintSourceTypeInfos), ERR_OK);
1810 EXPECT_EQ(constraintSourceTypeInfos.size(), 1);
1811 EXPECT_EQ(constraintSourceTypeInfos[0].typeInfo, 0);
1812 }
1813 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1814
1815 /**
1816 * @tc.name: OsAccountManagerModuleTest074
1817 * @tc.desc: Test QueryOsAccountConstraintSourceTypes.
1818 * @tc.type: FUNC
1819 * @tc.require:
1820 */
1821 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest074, TestSize.Level1)
1822 {
1823 std::vector<ConstraintSourceTypeInfo> constraintSourceTypeInfos;
1824 EXPECT_EQ(OsAccountManager::QueryOsAccountConstraintSourceTypes(
1825 MAIN_ACCOUNT_ID, CONSTANT_WIFI, constraintSourceTypeInfos), ERR_OK);
1826 EXPECT_EQ(constraintSourceTypeInfos[0].typeInfo, 1);
1827
1828 EXPECT_EQ(OsAccountManager::QueryOsAccountConstraintSourceTypes(
1829 MAIN_ACCOUNT_ID, CONSTRAINT_PRIVATE_DNS_SET, constraintSourceTypeInfos), ERR_OK);
1830 EXPECT_EQ(constraintSourceTypeInfos.size(), 1);
1831 EXPECT_EQ(constraintSourceTypeInfos[0].typeInfo, 0);
1832 }
1833
1834 /**
1835 * @tc.name: OsAccountManagerModuleTest075
1836 * @tc.desc: Test GetOsAccountAllConstraints.
1837 * @tc.type: FUNC
1838 * @tc.require:
1839 */
1840 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1841 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest075, TestSize.Level1)
1842 {
1843 OsAccountInfo osAccountInfoOne;
1844 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest075", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
1845
1846 std::vector<std::string> constraints;
1847 EXPECT_EQ(OsAccountManager::GetOsAccountAllConstraints(osAccountInfoOne.GetLocalId(), constraints), ERR_OK);
1848 int counts = constraints.size();
1849
1850 EXPECT_EQ(OsAccountManager::SetGlobalOsAccountConstraints(
1851 CONSTANTS_VECTOR_TEST, true, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1852
1853 EXPECT_EQ(OsAccountManager::SetSpecificOsAccountConstraints(
1854 CONSTANTS_VECTOR_TEST, true, MAIN_ACCOUNT_ID, osAccountInfoOne.GetLocalId(), true), ERR_OK);
1855
1856 constraints.clear();
1857 EXPECT_EQ(OsAccountManager::GetOsAccountAllConstraints(osAccountInfoOne.GetLocalId(), constraints), ERR_OK);
1858 EXPECT_NE(constraints.size(), counts + 2); // test number
1859 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
1860 }
1861 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1862
1863 /**
1864 * @tc.name: OsAccountManagerModuleTest076
1865 * @tc.desc: Test IsCurrentOsAccountVerified.
1866 * @tc.type: FUNC
1867 * @tc.require: issueI4JBFI
1868 */
1869 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest076, TestSize.Level1)
1870 {
1871 bool isVerified = false;
1872 EXPECT_EQ(OsAccountManager::IsCurrentOsAccountVerified(isVerified), ERR_OK);
1873 EXPECT_EQ(isVerified, false);
1874 }
1875
1876 /**
1877 * @tc.name: OsAccountManagerModuleTest077
1878 * @tc.desc: Test IsOsAccountCompleted.
1879 * @tc.type: FUNC
1880 * @tc.require: issueI4JBFI
1881 */
1882 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest077, TestSize.Level1)
1883 {
1884 bool isOsAccountCompleted = false;
1885 EXPECT_EQ(OsAccountManager::IsOsAccountCompleted(MAIN_ACCOUNT_ID, isOsAccountCompleted), ERR_OK);
1886 EXPECT_EQ(isOsAccountCompleted, true);
1887 }
1888
1889 /**
1890 * @tc.name: OsAccountManagerModuleTest078
1891 * @tc.desc: Test IsOsAccountCompleted.
1892 * @tc.type: FUNC
1893 * @tc.require: issueI4JBFI
1894 */
1895 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest078, TestSize.Level1)
1896 {
1897 bool isOsAccountCompleted = false;
1898 EXPECT_NE(OsAccountManager::IsOsAccountCompleted(ERROR_LOCAL_ID, isOsAccountCompleted), ERR_OK);
1899 EXPECT_EQ(isOsAccountCompleted, false);
1900 }
1901
1902 /**
1903 * @tc.name: OsAccountManagerModuleTest079
1904 * @tc.desc: Test SetCurrentOsAccountIsVerified.
1905 * @tc.type: FUNC
1906 * @tc.require: issueI4JBFI
1907 */
1908 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest079, TestSize.Level1)
1909 {
1910 bool isVerified = false;
1911 EXPECT_NE(OsAccountManager::SetCurrentOsAccountIsVerified(true), ERR_OK);
1912 EXPECT_EQ(OsAccountManager::IsCurrentOsAccountVerified(isVerified), ERR_OK);
1913 EXPECT_EQ(isVerified, false);
1914 }
1915
1916 /**
1917 * @tc.name: OsAccountManagerModuleTest080
1918 * @tc.desc: Test GetCreatedOsAccountNumFromDatabase.
1919 * @tc.type: FUNC
1920 * @tc.require: issueI4JBFI
1921 */
1922 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest080, TestSize.Level1)
1923 {
1924 int createdOsAccountNum = -1;
1925 #ifdef BUNDLE_ADAPTER_MOCK
1926 EXPECT_NE(OsAccountManager::GetCreatedOsAccountNumFromDatabase("", createdOsAccountNum), ERR_OK);
1927 #else // BUNDLE_ADAPTER_MOCK
1928 EXPECT_EQ(OsAccountManager::GetCreatedOsAccountNumFromDatabase("", createdOsAccountNum), ERR_OK);
1929 EXPECT_NE(createdOsAccountNum, -1);
1930 #endif
1931 }
1932
1933 /**
1934 * @tc.name: OsAccountManagerModuleTest081
1935 * @tc.desc: Test GetCreatedOsAccountNumFromDatabase.
1936 * @tc.type: FUNC
1937 * @tc.require: issueI4JBFI
1938 */
1939 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest081, TestSize.Level1)
1940 {
1941 int createdOsAccountNum = -1;
1942 EXPECT_NE(OsAccountManager::GetCreatedOsAccountNumFromDatabase("ERROR_STORE_ID", createdOsAccountNum), ERR_OK);
1943 }
1944
1945 /**
1946 * @tc.name: OsAccountManagerModuleTest082
1947 * @tc.desc: Test GetSerialNumberFromDatabase.
1948 * @tc.type: FUNC
1949 * @tc.require: issueI4JBFI
1950 */
1951 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest082, TestSize.Level1)
1952 {
1953 int64_t serialNumber = -1;
1954 #ifdef BUNDLE_ADAPTER_MOCK
1955 EXPECT_NE(OsAccountManager::GetSerialNumberFromDatabase("", serialNumber), ERR_OK);
1956 #else // BUNDLE_ADAPTER_MOCK
1957 EXPECT_EQ(OsAccountManager::GetSerialNumberFromDatabase("", serialNumber), ERR_OK);
1958 EXPECT_NE(serialNumber, -1);
1959 #endif
1960 }
1961
1962 /**
1963 * @tc.name: OsAccountManagerModuleTest083
1964 * @tc.desc: Test GetSerialNumberFromDatabase.
1965 * @tc.type: FUNC
1966 * @tc.require: issueI4JBFI
1967 */
1968 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest083, TestSize.Level1)
1969 {
1970 int64_t serialNumber = -1;
1971 EXPECT_NE(OsAccountManager::GetSerialNumberFromDatabase("ERROR_STORE_ID", serialNumber), ERR_OK);
1972 }
1973
1974 /**
1975 * @tc.name: OsAccountManagerModuleTest084
1976 * @tc.desc: Test GetMaxAllowCreateIdFromDatabase.
1977 * @tc.type: FUNC
1978 * @tc.require: issueI4JBFI
1979 */
1980 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1981 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest084, TestSize.Level1)
1982 {
1983 int maxAllowCreateId = -1;
1984 #ifdef BUNDLE_ADAPTER_MOCK
1985 EXPECT_NE(OsAccountManager::GetMaxAllowCreateIdFromDatabase("", maxAllowCreateId), ERR_OK);
1986 #else // BUNDLE_ADAPTER_MOCK
1987 EXPECT_EQ(OsAccountManager::GetMaxAllowCreateIdFromDatabase("", maxAllowCreateId), ERR_OK);
1988 EXPECT_NE(maxAllowCreateId, -1);
1989 #endif
1990 }
1991
1992 /**
1993 * @tc.name: OsAccountManagerModuleTest085
1994 * @tc.desc: Test GetMaxAllowCreateIdFromDatabase.
1995 * @tc.type: FUNC
1996 * @tc.require: issueI4JBFI
1997 */
1998 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest085, TestSize.Level1)
1999 {
2000 int maxAllowCreateId = -1;
2001 EXPECT_NE(OsAccountManager::GetMaxAllowCreateIdFromDatabase("ERROR_STORE_ID", maxAllowCreateId), ERR_OK);
2002 }
2003 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
2004
2005 /**
2006 * @tc.name: OsAccountManagerModuleTest086
2007 * @tc.desc: Test GetOsAccountListFromDatabase.
2008 * @tc.type: FUNC
2009 * @tc.require: issueI4JBFI
2010 */
2011 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest086, TestSize.Level1)
2012 {
2013 std::vector<OsAccountInfo> osAccountList;
2014 #ifdef BUNDLE_ADAPTER_MOCK
2015 EXPECT_NE(OsAccountManager::GetOsAccountListFromDatabase("", osAccountList), ERR_OK);
2016 #else // BUNDLE_ADAPTER_MOCK
2017 EXPECT_EQ(OsAccountManager::GetOsAccountListFromDatabase("", osAccountList), ERR_OK);
2018 EXPECT_NE(osAccountList.size(), 0);
2019 #endif
2020 }
2021
2022 /**
2023 * @tc.name: OsAccountManagerModuleTest087
2024 * @tc.desc: Test GetOsAccountListFromDatabase.
2025 * @tc.type: FUNC
2026 * @tc.require: issueI4JBFI
2027 */
2028 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest087, TestSize.Level1)
2029 {
2030 std::vector<OsAccountInfo> osAccountList;
2031 #ifdef BUNDLE_ADAPTER_MOCK
2032 EXPECT_NE(OsAccountManager::GetOsAccountListFromDatabase("ERROR_STORE_ID", osAccountList), ERR_OK);
2033 #else // BUNDLE_ADAPTER_MOCK
2034 EXPECT_EQ(OsAccountManager::GetOsAccountListFromDatabase("ERROR_STORE_ID", osAccountList), ERR_OK);
2035 EXPECT_EQ(osAccountList.size(), 0);
2036 #endif
2037 }
2038
2039 /**
2040 * @tc.name: OsAccountManagerModuleTest088
2041 * @tc.desc: Test ActivateOsAccount.
2042 * @tc.type: FUNC
2043 * @tc.require: issueI4JBFI
2044 */
2045 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest088, TestSize.Level1)
2046 {
2047 EXPECT_NE(OsAccountManager::ActivateOsAccount(INVALID_ID), ERR_OK);
2048 }
2049
2050 /**
2051 * @tc.name: OsAccountManagerModuleTest089
2052 * @tc.desc: Test ActivateOsAccount.
2053 * @tc.type: FUNC
2054 * @tc.require: issueI4JBFI
2055 */
2056 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest089, TestSize.Level1)
2057 {
2058 EXPECT_NE(OsAccountManager::ActivateOsAccount(ERROR_LOCAL_ID), ERR_OK);
2059 }
2060
2061 /**
2062 * @tc.name: OsAccountManagerModuleTest090
2063 * @tc.desc: Test remove activating os account.
2064 * @tc.type: FUNC
2065 * @tc.require: issueI4JBFI
2066 */
2067 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
2068 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest090, TestSize.Level1)
2069 {
2070 OsAccountInfo osAccountInfoOne;
2071 EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest090", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
2072 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
2073 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
2074 }
2075 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
2076
2077 /**
2078 * @tc.name: OsAccountManagerModuleTest091
2079 * @tc.desc: Test SubscribeOsAccount.
2080 * @tc.type: FUNC
2081 * @tc.require: issueI4JBFI
2082 */
2083 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest091, TestSize.Level1)
2084 {
2085 std::shared_ptr<OsAccountSubscriber> subscriber = nullptr;
2086 EXPECT_NE(OsAccountManager::SubscribeOsAccount(subscriber), ERR_OK);
2087 }
2088
2089 /**
2090 * @tc.name: OsAccountManagerModuleTest092
2091 * @tc.desc: Test UnsubscribeOsAccount.
2092 * @tc.type: FUNC
2093 * @tc.require: issueI4JBFI
2094 */
2095 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest092, TestSize.Level1)
2096 {
2097 std::shared_ptr<OsAccountSubscriber> subscriber = nullptr;
2098 EXPECT_NE(OsAccountManager::UnsubscribeOsAccount(subscriber), ERR_OK);
2099 }
2100
2101 /**
2102 * @tc.name: OsAccountManagerModuleTest093
2103 * @tc.desc: Test GetOsAccountSwitchMod.
2104 * @tc.type: FUNC
2105 * @tc.require: issueI4JBFI
2106 */
2107 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest093, TestSize.Level1)
2108 {
2109 int modResult = 1;
2110 EXPECT_EQ(OsAccountManager::GetOsAccountSwitchMod(), modResult);
2111 }
2112
2113 /**
2114 * @tc.name: OsAccountManagerModuleTest094
2115 * @tc.desc: test create domain account and query its osaccount info.
2116 * @tc.type: FUNC
2117 * @tc.require: issueI4IU3V
2118 */
2119 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
2120 #ifdef DOMAIN_ACCOUNT_TEST_CASE
2121 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest094, TestSize.Level1)
2122 {
2123 // create
2124 std::string testDomainName = "test_domain_name";
2125 std::string testDomain = "test_domain";
2126 DomainAccountInfo domainInfo(testDomainName, testDomain);
2127 OsAccountType type = NORMAL;
2128 OsAccountInfo osAccountInfo;
2129 ErrCode ret = OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo);
2130 EXPECT_EQ(ret, ERR_OK);
2131
2132 // get os account local id by domain
2133 int resID = -1;
2134 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainInfo, resID);
2135 EXPECT_EQ(ret, ERR_OK);
2136 EXPECT_EQ(resID, osAccountInfo.GetLocalId());
2137
2138 // remove
2139 ret = OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId());
2140 EXPECT_EQ(ret, ERR_OK);
2141
2142 // cannot query
2143 ret = OsAccountManager::GetOsAccountLocalIdFromDomain(domainInfo, resID);
2144 EXPECT_NE(ret, ERR_OK);
2145 }
2146 #endif // DOMAIN_ACCOUNT_TEST_CASE
2147 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
2148
2149 class TestOsAccountSubscriber : public OsAccountSubscriber {
2150 public:
OnAccountsChanged(const int & id)2151 void OnAccountsChanged(const int& id) {}
2152 };
2153
2154 /**
2155 * @tc.name: OsAccountManagerModuleTest095
2156 * @tc.desc: test SubscribeOsAccount/UnSubscribeOsAccount nomal branch.
2157 * @tc.type: FUNC
2158 * @tc.require:
2159 */
2160 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest095, TestSize.Level1)
2161 {
2162 auto subscriber = std::make_shared<TestOsAccountSubscriber>();
2163 EXPECT_NE(nullptr, subscriber);
2164 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(subscriber));
2165 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(subscriber));
2166 }
2167
2168 /**
2169 * @tc.name: OsAccountManagerModuleTest096
2170 * @tc.desc: test SubscribeOsAccount subscribed twice.
2171 * @tc.type: FUNC
2172 * @tc.require:
2173 */
2174 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest096, TestSize.Level1)
2175 {
2176 auto subscriber = std::make_shared<TestOsAccountSubscriber>();
2177 EXPECT_NE(nullptr, subscriber);
2178 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(subscriber));
2179 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(subscriber));
2180 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(subscriber));
2181 }
2182
2183 /**
2184 * @tc.name: OsAccountManagerModuleTest097
2185 * @tc.desc: test SubscribeOsAccount subscribed exceed limitation.
2186 * @tc.type: FUNC
2187 * @tc.require:
2188 */
2189 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest097, TestSize.Level1)
2190 {
2191 std::vector<std::shared_ptr<OsAccountSubscriber>> subscribers;
2192 for (int i = 0; i < Constants::SUBSCRIBER_MAX_SIZE; i++) {
2193 auto subscriber = std::make_shared<TestOsAccountSubscriber>();
2194 EXPECT_NE(nullptr, subscriber);
2195 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(subscriber));
2196 subscribers.emplace_back(subscriber);
2197 }
2198 auto subscriber = std::make_shared<TestOsAccountSubscriber>();
2199 EXPECT_NE(nullptr, subscriber);
2200 EXPECT_EQ(ERR_OSACCOUNT_KIT_SUBSCRIBE_ERROR, OsAccountManager::SubscribeOsAccount(subscriber));
2201
2202 for (int i = 0; i < Constants::SUBSCRIBER_MAX_SIZE; i++) {
2203 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(subscribers[i]));
2204 }
2205 }
2206
2207 /**
2208 * @tc.name: OsAccountManagerModuleTest098
2209 * @tc.desc: test UnsubscribeOsAccount not subscribed .
2210 * @tc.type: FUNC
2211 * @tc.require:
2212 */
2213 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest098, TestSize.Level1)
2214 {
2215 auto subscriber = std::make_shared<TestOsAccountSubscriber>();
2216 EXPECT_NE(nullptr, subscriber);
2217 EXPECT_EQ(ERR_OSACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED,
2218 OsAccountManager::UnsubscribeOsAccount(subscriber));
2219 }
2220
2221 /**
2222 * @tc.name: OsAccountManagerModuleTest099
2223 * @tc.desc: test GetOsAccountFromDatabase normal branch.
2224 * @tc.type: FUNC
2225 * @tc.require:
2226 */
2227 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest099, TestSize.Level1)
2228 {
2229 OsAccountInfo osAccountInfo;
2230 #ifdef BUNDLE_ADAPTER_MOCK
2231 EXPECT_NE(ERR_OK, OsAccountManager::GetOsAccountFromDatabase("", MAIN_ACCOUNT_ID, osAccountInfo));
2232 #else // BUNDLE_ADAPTER_MOCK
2233 EXPECT_EQ(ERR_OK, OsAccountManager::GetOsAccountFromDatabase("", MAIN_ACCOUNT_ID, osAccountInfo));
2234 #endif
2235 }
2236
2237 /**
2238 * @tc.name: OsAccountManagerModuleTest100
2239 * @tc.desc: test cGetOsAccountLocalIdFromUid with invalid uid.
2240 * @tc.type: FUNC
2241 * @tc.require:
2242 */
2243 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest100, TestSize.Level1)
2244 {
2245 int uid = -1;
2246 int id;
2247 EXPECT_EQ(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, OsAccountManager::GetOsAccountLocalIdFromUid(uid, id));
2248 }
2249
2250 /**
2251 * @tc.name: OsAccountManagerModuleTest101
2252 * @tc.desc: test cGetOsAccountLocalIdFromUid with invalid uid.
2253 * @tc.type: FUNC
2254 * @tc.require:
2255 */
2256 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest101, TestSize.Level1)
2257 {
2258 int uid = -1;
2259 int bundleId;
2260 EXPECT_EQ(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, OsAccountManager::GetBundleIdFromUid(uid, bundleId));
2261 }
2262
2263 /**
2264 * @tc.name: OsAccountManagerModuleTest102
2265 * @tc.desc: test IsOsAccountActived with invalid data.
2266 * @tc.type: FUNC
2267 * @tc.require:
2268 */
2269 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest102, TestSize.Level1)
2270 {
2271 bool isOsAccountActived = true;
2272 EXPECT_EQ(OsAccountManager::IsOsAccountActived(Constants::MAX_USER_ID + 1, isOsAccountActived),
2273 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2274 EXPECT_EQ(isOsAccountActived, false);
2275 }
2276
2277 /**
2278 * @tc.name: OsAccountManagerModuleTest103
2279 * @tc.desc: test IsOsAccountConstraintEnable/CheckOsAccountConstraintEnabled with invalid data.
2280 * @tc.type: FUNC
2281 * @tc.require:
2282 */
2283 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest103, TestSize.Level1)
2284 {
2285 bool isConstraintEnable = true;
2286 EXPECT_EQ(ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR, OsAccountManager::IsOsAccountConstraintEnable(
2287 Constants::MAX_USER_ID + 1, CONSTANT_PRINT, isConstraintEnable));
2288 EXPECT_EQ(isConstraintEnable, false);
2289 EXPECT_EQ(ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR, OsAccountManager::CheckOsAccountConstraintEnabled(
2290 Constants::MAX_USER_ID + 1, CONSTANT_PRINT, isConstraintEnable));
2291 EXPECT_EQ(isConstraintEnable, false);
2292 }
2293
2294 /**
2295 * @tc.name: OsAccountManagerModuleTest104
2296 * @tc.desc: test SetOsAccountName with invalid data.
2297 * @tc.type: FUNC
2298 * @tc.require:
2299 */
2300 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest104, TestSize.Level1)
2301 {
2302 const std::string localName = "testname";
2303 EXPECT_EQ(OsAccountManager::SetOsAccountName(Constants::MAX_USER_ID + 1, localName),
2304 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2305 EXPECT_EQ(localName, "testname");
2306 }
2307
2308 /**
2309 * @tc.name: OsAccountManagerModuleTest105
2310 * @tc.desc: test SetOsAccountConstraints with invalid data.
2311 * @tc.type: FUNC
2312 * @tc.require:
2313 */
2314 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest105, TestSize.Level1)
2315 {
2316 bool enable = true;
2317 EXPECT_EQ(OsAccountManager::SetOsAccountConstraints(Constants::MAX_USER_ID + 1, CONSTANTS_VECTOR, enable),
2318 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2319 EXPECT_EQ(enable, true);
2320 }
2321
2322 /**
2323 * @tc.name: OsAccountManagerModuleTest106
2324 * @tc.desc: test SetOsAccountProfilePhoto with invalid data.
2325 * @tc.type: FUNC
2326 * @tc.require:
2327 */
2328 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest106, TestSize.Level1)
2329 {
2330 EXPECT_EQ(OsAccountManager::SetOsAccountProfilePhoto(Constants::MAX_USER_ID + 1, PHOTO_IMG),
2331 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2332 }
2333
2334 /**
2335 * @tc.name: OsAccountManagerModuleTest108
2336 * @tc.desc: test SetOsAccountIsVerified with invalid data.
2337 * @tc.type: FUNC
2338 * @tc.require:
2339 */
2340 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest108, TestSize.Level1)
2341 {
2342 EXPECT_EQ(OsAccountManager::SetOsAccountIsVerified(Constants::MAX_USER_ID + 1, false),
2343 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2344 }
2345
2346 /**
2347 * @tc.name: OsAccountManagerModuleTest109
2348 * @tc.desc: Test osaccount call service interface not pass system applicaiton verify.
2349 * @tc.type: FUNC
2350 * @tc.require: issueI66BG5
2351 */
2352 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
2353 #ifdef DOMAIN_ACCOUNT_TEST_CASE
2354 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest109, TestSize.Level0)
2355 {
2356 Security::AccessToken::AccessTokenIDEx tokenIdEx = {0};
2357 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestNormalInfoParms, INFO_MANAGER_TEST_POLICY_PRAMS);
2358 ASSERT_NE(INVALID_TOKEN_ID, tokenIdEx.tokenIDEx);
2359 SetSelfTokenID(tokenIdEx.tokenIDEx);
2360
2361 int bundleId = INVALID_BUNDLE_ID;
2362 ASSERT_EQ(OsAccountManager::GetBundleIdFromUid(ACCOUNT_UID, bundleId), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2363
2364 OsAccountInfo osAccountInfoOne;
2365 ASSERT_EQ(OsAccountManager::CreateOsAccount("ModuleTest109", OsAccountType::GUEST, osAccountInfoOne),
2366 ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2367
2368 ASSERT_EQ(OsAccountManager::ActivateOsAccount(LOCAL_ID), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2369
2370 ASSERT_EQ(OsAccountManager::RemoveOsAccount(LOCAL_ID), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2371
2372 bool enable = false;
2373 ASSERT_EQ(OsAccountManager::SetOsAccountConstraints(LOCAL_ID, CONSTANTS_VECTOR, enable),
2374 ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2375
2376 ASSERT_EQ(OsAccountManager::SetOsAccountName(LOCAL_ID, STRING_NAME), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2377
2378 int maxOsAccountNumber = 0;
2379 ASSERT_EQ(OsAccountManager::QueryMaxOsAccountNumber(maxOsAccountNumber), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2380
2381 DomainAccountInfo domainInfo(STRING_DOMAIN_VALID, STRING_DOMAIN_ACCOUNT_NAME_VALID);
2382 OsAccountType type = NORMAL;
2383 OsAccountInfo osAccountInfo;
2384 ASSERT_EQ(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo),
2385 ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2386
2387 AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(infoManagerTestNormalInfoParms.userID,
2388 infoManagerTestNormalInfoParms.bundleName, infoManagerTestNormalInfoParms.instIndex);
2389 AccessTokenKit::DeleteToken(tokenID);
2390 SetSelfTokenID(g_selfTokenID);
2391 }
2392 #endif // DOMAIN_ACCOUNT_TEST_CASE
2393 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
2394
2395 /**
2396 * @tc.name: OsAccountManagerModuleTest110
2397 * @tc.desc: Test osaccount call service interface not pass system applicaiton verify.
2398 * @tc.type: FUNC
2399 * @tc.require: issueI66BG5
2400 */
2401 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest110, TestSize.Level0)
2402 {
2403 Security::AccessToken::AccessTokenIDEx tokenIdEx = {0};
2404 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestNormalInfoParms, INFO_MANAGER_TEST_POLICY_PRAMS);
2405 ASSERT_NE(INVALID_TOKEN_ID, tokenIdEx.tokenIDEx);
2406 SetSelfTokenID(tokenIdEx.tokenIDEx);
2407
2408 OsAccountInfo osAccountInfoTwo;
2409 ASSERT_EQ(
2410 OsAccountManager::QueryOsAccountById(LOCAL_ID, osAccountInfoTwo), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2411
2412 std::string photo;
2413 ASSERT_EQ(OsAccountManager::GetOsAccountProfilePhoto(LOCAL_ID, photo), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2414
2415 ASSERT_EQ(OsAccountManager::SetOsAccountProfilePhoto(LOCAL_ID, PHOTO_IMG), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2416
2417 auto subscriber = std::make_shared<TestOsAccountSubscriber>();
2418 ASSERT_NE(nullptr, subscriber);
2419 ASSERT_EQ(OsAccountManager::SubscribeOsAccount(subscriber), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2420
2421 bool isMainOsAccount = false;
2422 ASSERT_EQ(OsAccountManager::IsMainOsAccount(isMainOsAccount), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2423
2424 std::vector<ConstraintSourceTypeInfo> constraintSourceTypeInfos;
2425 ASSERT_EQ(OsAccountManager::QueryOsAccountConstraintSourceTypes(
2426 MAIN_ACCOUNT_ID, CONSTANT_PRINT, constraintSourceTypeInfos), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2427
2428 AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(infoManagerTestNormalInfoParms.userID,
2429 infoManagerTestNormalInfoParms.bundleName, infoManagerTestNormalInfoParms.instIndex);
2430 AccessTokenKit::DeleteToken(tokenID);
2431 SetSelfTokenID(g_selfTokenID);
2432 }
2433
2434 /**
2435 * @tc.name: OsAccountManagerModuleTest111
2436 * @tc.desc: Test osaccount call service interface pass system app verify.
2437 * @tc.type: FUNC
2438 * @tc.require: issueI66BG5
2439 */
2440 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
2441 #ifdef DOMAIN_ACCOUNT_TEST_CASE
2442 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest111, TestSize.Level0)
2443 {
2444 Security::AccessToken::AccessTokenIDEx tokenIdEx = {0};
2445 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestSystemInfoParms, INFO_MANAGER_TEST_POLICY_PRAMS);
2446 ASSERT_NE(INVALID_TOKEN_ID, tokenIdEx.tokenIDEx);
2447 SetSelfTokenID(tokenIdEx.tokenIDEx);
2448 int bundleId = INVALID_BUNDLE_ID;
2449 ASSERT_EQ(OsAccountManager::GetBundleIdFromUid(ACCOUNT_UID, bundleId), ERR_OK);
2450 ASSERT_NE(bundleId, INVALID_BUNDLE_ID);
2451
2452 OsAccountInfo osAccountInfoOne;
2453 ASSERT_NE(OsAccountManager::CreateOsAccount("ModuleTest111", OsAccountType::GUEST, osAccountInfoOne),
2454 ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2455
2456 ASSERT_NE(OsAccountManager::ActivateOsAccount(LOCAL_ID), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2457
2458 ASSERT_NE(OsAccountManager::RemoveOsAccount(LOCAL_ID), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2459
2460 bool enable = false;
2461 ASSERT_NE(OsAccountManager::SetOsAccountConstraints(LOCAL_ID, CONSTANTS_VECTOR, enable),
2462 ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2463
2464 ASSERT_NE(OsAccountManager::SetOsAccountName(LOCAL_ID, STRING_NAME), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2465
2466 int maxOsAccountNumber = 0;
2467 ASSERT_NE(OsAccountManager::QueryMaxOsAccountNumber(maxOsAccountNumber), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2468
2469 DomainAccountInfo domainInfo(STRING_DOMAIN_VALID, STRING_DOMAIN_ACCOUNT_NAME_VALID);
2470 OsAccountType type = NORMAL;
2471 OsAccountInfo osAccountInfo;
2472 ASSERT_EQ(OsAccountManager::CreateOsAccountForDomain(type, domainInfo, osAccountInfo), ERR_OK);
2473 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2474
2475 AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(infoManagerTestSystemInfoParms.userID,
2476 infoManagerTestSystemInfoParms.bundleName, infoManagerTestSystemInfoParms.instIndex);
2477 AccessTokenKit::DeleteToken(tokenID);
2478 SetSelfTokenID(g_selfTokenID);
2479 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
2480 }
2481 #endif // DOMAIN_ACCOUNT_TEST_CASE
2482 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
2483
2484 /**
2485 * @tc.name: OsAccountManagerModuleTest112
2486 * @tc.desc: Test osaccount call service interface pass system app verify.
2487 * @tc.type: FUNC
2488 * @tc.require: issueI66BG5
2489 */
2490 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest112, TestSize.Level0)
2491 {
2492 Security::AccessToken::AccessTokenIDEx tokenIdEx = {0};
2493 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestSystemInfoParms, INFO_MANAGER_TEST_POLICY_PRAMS);
2494 ASSERT_NE(INVALID_TOKEN_ID, tokenIdEx.tokenIDEx);
2495 SetSelfTokenID(tokenIdEx.tokenIDEx);
2496
2497 OsAccountInfo osAccountInfoTwo;
2498 ASSERT_NE(
2499 OsAccountManager::QueryOsAccountById(LOCAL_ID, osAccountInfoTwo), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2500
2501 std::string photo;
2502 ASSERT_NE(OsAccountManager::GetOsAccountProfilePhoto(LOCAL_ID, photo), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2503
2504 ASSERT_NE(OsAccountManager::SetOsAccountProfilePhoto(LOCAL_ID, PHOTO_IMG), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2505
2506 auto subscriber = std::make_shared<TestOsAccountSubscriber>();
2507 ASSERT_NE(nullptr, subscriber);
2508 ASSERT_NE(OsAccountManager::SubscribeOsAccount(subscriber), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2509
2510 auto subscriberTwo = std::make_shared<TestOsAccountSubscriber>();
2511 ASSERT_NE(nullptr, subscriberTwo);
2512 ASSERT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(subscriberTwo));
2513 ASSERT_EQ(OsAccountManager::UnsubscribeOsAccount(subscriberTwo), ERR_OK);
2514
2515 bool isMainOsAccount = false;
2516 ASSERT_NE(OsAccountManager::IsMainOsAccount(isMainOsAccount), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2517
2518 std::vector<ConstraintSourceTypeInfo> constraintSourceTypeInfos;
2519 ASSERT_NE(OsAccountManager::QueryOsAccountConstraintSourceTypes(
2520 MAIN_ACCOUNT_ID, CONSTANT_PRINT, constraintSourceTypeInfos), ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR);
2521
2522 AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(infoManagerTestSystemInfoParms.userID,
2523 infoManagerTestSystemInfoParms.bundleName, infoManagerTestSystemInfoParms.instIndex);
2524 AccessTokenKit::DeleteToken(tokenID);
2525 SetSelfTokenID(g_selfTokenID);
2526 }
2527
2528 /**
2529 * @tc.name: OsAccountManagerModuleTest113
2530 * @tc.desc: Test set/get default activated os account id.
2531 * @tc.type: FUNC
2532 * @tc.require: issueI6AQUQ
2533 */
2534 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
2535 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest113, TestSize.Level1)
2536 {
2537 OsAccountInfo osAccountInfoOne;
2538 int id;
2539 EXPECT_EQ(OsAccountManager::GetDefaultActivatedOsAccount(id), ERR_OK);
2540 EXPECT_EQ(id, MAIN_ACCOUNT_ID);
2541 EXPECT_NE(OsAccountManager::SetDefaultActivatedOsAccount(MAIN_ACCOUNT_ID + 1), ERR_OK);
2542 EXPECT_NE(OsAccountManager::SetDefaultActivatedOsAccount(MAIN_ACCOUNT_ID - 1), ERR_OK);
2543 EXPECT_NE(OsAccountManager::SetDefaultActivatedOsAccount(Constants::MAX_USER_ID + 1), ERR_OK);
2544 EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest113", OsAccountType::NORMAL, osAccountInfoOne), ERR_OK);
2545 EXPECT_EQ(OsAccountManager::SetDefaultActivatedOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
2546 EXPECT_EQ(OsAccountManager::GetDefaultActivatedOsAccount(id), ERR_OK);
2547 EXPECT_EQ(id, osAccountInfoOne.GetLocalId());
2548 EXPECT_EQ(OsAccountManager::SetDefaultActivatedOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
2549 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId()), ERR_OK);
2550 EXPECT_EQ(OsAccountManager::GetDefaultActivatedOsAccount(id), ERR_OK);
2551 EXPECT_NE(id, osAccountInfoOne.GetLocalId());
2552 EXPECT_EQ(id, MAIN_ACCOUNT_ID);
2553 EXPECT_EQ(OsAccountManager::SetDefaultActivatedOsAccount(MAIN_ACCOUNT_ID), ERR_OK);
2554 EXPECT_EQ(OsAccountManager::GetDefaultActivatedOsAccount(id), ERR_OK);
2555 EXPECT_EQ(id, MAIN_ACCOUNT_ID);
2556 }
2557
2558 /**
2559 * @tc.name: OsAccountManagerModuleTest114
2560 * @tc.desc: Test DeactivateOsAccount success.
2561 * @tc.type: FUNC
2562 * @tc.require:
2563 */
2564 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest114, TestSize.Level1)
2565 {
2566 AccessTokenID tokenID;
2567 ASSERT_TRUE(AllocPermission(PERMISSION_LIST, tokenID));
2568
2569 OsAccountInfo osAccountInfo;
2570 EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest114", OsAccountType::NORMAL, osAccountInfo), ERR_OK);
2571 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2572
2573 #ifndef BUNDLE_ADAPTER_MOCK
2574 EventFwk::MatchingSkills matchingSkills;
2575 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING);
2576 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED);
2577 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2578 auto listener = std::make_shared<MockSubscriberListener>();
2579 std::shared_ptr<AccountTestEventSubscriber> subscriberPtr =
2580 std::make_shared<AccountTestEventSubscriber>(subscribeInfo, listener);
2581 ASSERT_EQ(EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2582 EXPECT_CALL(*listener, OnReceiveEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING)).Times(Exactly(1));
2583 EXPECT_CALL(*listener, OnReceiveEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED)).Times(Exactly(1));
2584 #endif
2585
2586 OsAccountSubscribeInfo subscribeStoppingInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPING, "subscribeStopping");
2587 auto stoppingPtr = std::make_shared<MockOsAccountSubscriber>();
2588 auto stoppingSubscriber = std::make_shared<DeactivateOsAccountSubscriber>(subscribeStoppingInfo, stoppingPtr);
2589 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppingSubscriber));
2590 EXPECT_CALL(*stoppingPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1));
2591
2592 OsAccountSubscribeInfo subscribeStoppedInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPED, "subscribeStopped");
2593 auto stoppedPtr = std::make_shared<MockOsAccountSubscriber>();
2594 auto stoppedSubscriber = std::make_shared<DeactivateOsAccountSubscriber>(subscribeStoppedInfo, stoppedPtr);
2595 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppedSubscriber));
2596 EXPECT_CALL(*stoppedPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1));
2597 OsAccount::GetInstance().RestoreListenerRecords();
2598
2599 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2600
2601 #ifndef BUNDLE_ADAPTER_MOCK
2602 Wait(subscriberPtr);
2603 #endif
2604 Wait(stoppingSubscriber);
2605 Wait(stoppedSubscriber);
2606
2607 #ifndef BUNDLE_ADAPTER_MOCK
2608 EXPECT_EQ(EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2609 #endif
2610 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppingSubscriber));
2611 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppedSubscriber));
2612 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2613 #ifndef BUNDLE_ADAPTER_MOCK
2614 testing::Mock::AllowLeak(listener.get());
2615 #endif
2616
2617 ASSERT_TRUE(RecoveryPermission(tokenID));
2618 }
2619
2620 /**
2621 * @tc.name: OsAccountManagerModuleTest115
2622 * @tc.desc: Test DeactivateOsAccount with invalid id.
2623 * @tc.type: FUNC
2624 * @tc.require:
2625 */
2626 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest115, TestSize.Level1)
2627 {
2628 #ifndef SUPPORT_STOP_MAIN_OS_ACCOUNT
2629 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(Constants::START_USER_ID),
2630 ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_STOP_ACTIVE_ERROR);
2631 #else
2632 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(Constants::START_USER_ID), ERR_OK);
2633 #endif
2634 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(Constants::START_USER_ID - 1),
2635 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2636 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(Constants::MAX_USER_ID + 1),
2637 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2638 }
2639
2640 /**
2641 * @tc.name: OsAccountManagerModuleTest116
2642 * @tc.desc: Test ActivateOsAccount.
2643 * @tc.type: FUNC
2644 * @tc.require:
2645 */
2646 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest116, TestSize.Level1)
2647 {
2648 AccessTokenID tokenID;
2649 ASSERT_TRUE(AllocPermission({"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}, tokenID));
2650 OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID);
2651 OsAccountInfo account;
2652 EXPECT_EQ(OsAccountManager::CreateOsAccount("AccountForegroundTest001", OsAccountType::NORMAL, account), ERR_OK);
2653
2654 // activing os account
2655 OsAccountSubscribeInfo subscribeActivingInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVATING, "subscribeActiving");
2656 auto activingSubscriber = std::make_shared<ActiveOsAccountSubscriber>(subscribeActivingInfo);
2657 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(activingSubscriber));
2658 EXPECT_CALL(*activingSubscriber, OnAccountsChanged(account.GetLocalId())).Times(Exactly(1));
2659 // activated os account
2660 OsAccountSubscribeInfo subscribeActivatedInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED, "subscribeActived");
2661 auto activedSubscriber = std::make_shared<ActiveOsAccountSubscriber>(subscribeActivatedInfo);
2662 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(activedSubscriber));
2663 EXPECT_CALL(*activedSubscriber, OnAccountsChanged(account.GetLocalId())).Times(Exactly(1));
2664
2665 // switched os account
2666 OsAccountSubscribeInfo subscribeSwitchedInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED, "subscribeSwitched");
2667 auto switchedSubscriber = std::make_shared<ActiveOsAccountSubscriber>(subscribeSwitchedInfo);
2668 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(switchedSubscriber));
2669 EXPECT_CALL(*switchedSubscriber, OnAccountsSwitch(account.GetLocalId(), MAIN_ACCOUNT_ID)).Times(Exactly(1));
2670
2671 // switching os account
2672 OsAccountSubscribeInfo subscribeSwitchingInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHING, "subscribeSwitching");
2673 auto switchingSubscriber = std::make_shared<ActiveOsAccountSubscriber>(subscribeSwitchingInfo);
2674 EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(switchingSubscriber));
2675 EXPECT_CALL(*switchingSubscriber, OnAccountsSwitch(account.GetLocalId(), MAIN_ACCOUNT_ID)).Times(Exactly(1));
2676 OsAccount::GetInstance().RestoreListenerRecords();
2677
2678 // common event: COMMON_EVENT_USER_FOREGROUND 、 COMMON_EVENT_USER_BACKGROUND
2679 #ifndef BUNDLE_ADAPTER_MOCK
2680 MatchingSkills matchingSkills;
2681 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND);
2682 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_BACKGROUND);
2683 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2684 auto listener = std::make_shared<MockSubscriberListener>();
2685 auto subscriberPtr = std::make_shared<AccountTestEventSubscriber>(subscribeInfo, listener);
2686 ASSERT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2687 EXPECT_CALL(*listener, OnReceiveEvent(CommonEventSupport::COMMON_EVENT_USER_BACKGROUND)).Times(Exactly(1));
2688 EXPECT_CALL(*listener, OnReceiveEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND)).Times(Exactly(1));
2689 #endif
2690
2691 EXPECT_EQ(OsAccountManager::ActivateOsAccount(account.GetLocalId()), ERR_OK);
2692
2693 sleep(1);
2694 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(activingSubscriber));
2695 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(activedSubscriber));
2696 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchedSubscriber));
2697 EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchingSubscriber));
2698 #ifndef BUNDLE_ADAPTER_MOCK
2699 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2700 #endif
2701
2702 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(account.GetLocalId()), ERR_OK);
2703 EXPECT_EQ(ERR_OK, OsAccountManager::RemoveOsAccount(account.GetLocalId()));
2704 #ifndef BUNDLE_ADAPTER_MOCK
2705 testing::Mock::AllowLeak(listener.get());
2706 #endif
2707 ASSERT_TRUE(RecoveryPermission(tokenID));
2708 }
2709
2710 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
2711 /**
2712 * @tc.name: OsAccountManagerModuleTest117
2713 * @tc.desc: Test ActivateOsAccount
2714 * @tc.type: FUNC
2715 * @tc.require:
2716 */
2717 HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest117, TestSize.Level1)
2718 {
2719 OsAccountInfo account;
2720 EXPECT_EQ(OsAccountManager::CreateOsAccount("AccountForegroundTest002", OsAccountType::NORMAL, account), ERR_OK);
2721 EXPECT_EQ(account.GetIsForeground(), false);
2722 EXPECT_NE(account.GetDisplayId(), 0);
2723 std::vector<int32_t> ids;
2724 OsAccountManager::QueryActiveOsAccountIds(ids);
2725 EXPECT_EQ(ids.size() > 0, true);
2726 EXPECT_NE(ids[0], account.GetLocalId());
2727
2728 EXPECT_EQ(OsAccountManager::ActivateOsAccount(account.GetLocalId()), ERR_OK);
2729 ids.clear();
2730 OsAccountManager::QueryActiveOsAccountIds(ids);
2731 EXPECT_EQ(ids.size() > 0, true);
2732 EXPECT_EQ(ids[0], account.GetLocalId());
2733 OsAccountInfo osAccountInfo;
2734 EXPECT_EQ(ERR_OK, OsAccountManager::QueryOsAccountById(account.GetLocalId(), osAccountInfo));
2735 EXPECT_EQ(osAccountInfo.GetIsActived(), true);
2736 EXPECT_EQ(osAccountInfo.GetIsForeground(), true);
2737 EXPECT_EQ(osAccountInfo.GetDisplayId(), 0);
2738
2739 EXPECT_EQ(ERR_OK, OsAccountManager::QueryOsAccountById(100, osAccountInfo));
2740 EXPECT_EQ(osAccountInfo.GetIsActived(), true);
2741 EXPECT_EQ(osAccountInfo.GetIsForeground(), false);
2742 EXPECT_NE(osAccountInfo.GetDisplayId(), 0);
2743
2744 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(account.GetLocalId()), ERR_OK);
2745 ids.clear();
2746 OsAccountManager::QueryActiveOsAccountIds(ids);
2747 EXPECT_EQ(ids.size() > 0, true);
2748 EXPECT_EQ(ids[0], 100);
2749
2750 EXPECT_EQ(ERR_OK, OsAccountManager::QueryOsAccountById(100, osAccountInfo));
2751 EXPECT_EQ(osAccountInfo.GetIsActived(), true);
2752 EXPECT_EQ(osAccountInfo.GetIsForeground(), true);
2753 EXPECT_EQ(osAccountInfo.GetDisplayId(), 0);
2754
2755 EXPECT_EQ(ERR_OK, OsAccountManager::QueryOsAccountById(account.GetLocalId(), osAccountInfo));
2756 EXPECT_EQ(osAccountInfo.GetIsActived(), false);
2757 EXPECT_EQ(osAccountInfo.GetIsForeground(), false);
2758 EXPECT_NE(osAccountInfo.GetDisplayId(), 0);
2759 EXPECT_EQ(ERR_OK, OsAccountManager::RemoveOsAccount(account.GetLocalId()));
2760 }
2761 #endif
2762
2763 /**
2764 * @tc.name: GetOsAccountType001
2765 * @tc.desc: Test GetOsAccountType.
2766 * @tc.type: FUNC
2767 * @tc.require:
2768 */
2769 HWTEST_F(OsAccountManagerModuleTest, GetOsAccountType001, TestSize.Level1)
2770 {
2771 OsAccountInfo osAccountInfoA;
2772 ASSERT_EQ(OsAccountManager::CreateOsAccount("GetTypeName001", OsAccountType::NORMAL, osAccountInfoA), ERR_OK);
2773 OsAccountType type = OsAccountType::ADMIN;
2774 EXPECT_EQ(OsAccountManager::GetOsAccountType(osAccountInfoA.GetLocalId(), type), ERR_OK);
2775 EXPECT_EQ(type, OsAccountType::NORMAL);
2776 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoA.GetLocalId()), ERR_OK);
2777 }
2778
2779 /**
2780 * @tc.name: PrivateTypeTest001
2781 * @tc.desc: Test PRIVATE type os account.
2782 * @tc.type: FUNC
2783 * @tc.require:
2784 */
2785 HWTEST_F(OsAccountManagerModuleTest, PrivateTypeTest001, TestSize.Level1)
2786 {
2787 std::string privateTestName = "PrivateTestName001";
2788 std::string privateTestNameTwo = "PrivateTestName002";
2789 // test create private os account with normal account duplicate name
2790 OsAccountInfo osAccountInfoA;
2791 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestName, OsAccountType::NORMAL, osAccountInfoA), ERR_OK);
2792 OsAccountInfo osAccountInfoB;
2793 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestName, OsAccountType::PRIVATE, osAccountInfoB), ERR_OK);
2794 OsAccountInfo osAccountInfoC;
2795 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestNameTwo, OsAccountType::PRIVATE, osAccountInfoC),
2796 ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR);
2797
2798 // test get os account type by id
2799 OsAccountType type;
2800 EXPECT_EQ(OsAccountManager::GetOsAccountType(osAccountInfoB.GetLocalId(), type), ERR_OK);
2801 EXPECT_EQ(type, OsAccountType::PRIVATE);
2802 EXPECT_EQ(OsAccountManager::GetOsAccountType(osAccountInfoA.GetLocalId(), type), ERR_OK);
2803 EXPECT_EQ(type, OsAccountType::NORMAL);
2804
2805 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoB.GetLocalId()), ERR_OK);
2806
2807 // test delete private os account
2808 OsAccountInfo osAccountInfoD;
2809 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestName, OsAccountType::PRIVATE, osAccountInfoD), ERR_OK);
2810 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoD.GetLocalId()), ERR_OK);
2811
2812 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoA.GetLocalId()), ERR_OK);
2813 }
2814
2815 /**
2816 * @tc.name: PrivateTypeTest002
2817 * @tc.desc: Test PRIVATE type os account.
2818 * @tc.type: FUNC
2819 * @tc.require:
2820 */
2821 HWTEST_F(OsAccountManagerModuleTest, PrivateTypeTest002, TestSize.Level1)
2822 {
2823 std::string privateTestName = "PrivateTestName001";
2824 // test create normal os account with private account duplicate name
2825 OsAccountInfo osAccountInfoA;
2826 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestName, OsAccountType::PRIVATE, osAccountInfoA), ERR_OK);
2827 OsAccountInfo osAccountInfoB;
2828 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestName, OsAccountType::NORMAL, osAccountInfoB), ERR_OK);
2829
2830 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoA.GetLocalId()), ERR_OK);
2831 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoB.GetLocalId()), ERR_OK);
2832 }
2833
2834 /**
2835 * @tc.name: PrivateTypeTest003
2836 * @tc.desc: Test PRIVATE type os account.
2837 * @tc.type: FUNC
2838 * @tc.require:
2839 */
2840 HWTEST_F(OsAccountManagerModuleTest, PrivateTypeTest003, TestSize.Level1)
2841 {
2842 std::string privateTestName = "PrivateTestName001";
2843 std::string privateTestNameTwo = "PrivateTestName002";
2844 std::string privateTestNameThree = "PrivateTestName003";
2845 // test set name with private account duplicate name
2846 OsAccountInfo osAccountInfoA;
2847 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestName, OsAccountType::PRIVATE, osAccountInfoA), ERR_OK);
2848 OsAccountInfo osAccountInfoB;
2849 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestNameTwo, OsAccountType::NORMAL, osAccountInfoB), ERR_OK);
2850 EXPECT_EQ(OsAccountManager::SetOsAccountName(osAccountInfoB.GetLocalId(), privateTestName), ERR_OK);
2851 OsAccountInfo osAccountInfoC;
2852 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateTestNameTwo, OsAccountType::NORMAL, osAccountInfoC), ERR_OK);
2853 EXPECT_EQ(OsAccountManager::SetOsAccountName(osAccountInfoA.GetLocalId(), privateTestNameTwo), ERR_OK);
2854
2855 EXPECT_EQ(OsAccountManager::SetOsAccountName(osAccountInfoA.GetLocalId(), privateTestNameThree), ERR_OK);
2856 EXPECT_EQ(OsAccountManager::SetOsAccountName(osAccountInfoC.GetLocalId(), privateTestNameThree), ERR_OK);
2857
2858 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoA.GetLocalId()), ERR_OK);
2859 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoB.GetLocalId()), ERR_OK);
2860 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfoC.GetLocalId()), ERR_OK);
2861 }
2862
2863 /**
2864 * @tc.name: IsOsAccountForeground001
2865 * @tc.desc: Test call IsOsAccountForeground(bool &isForeground) with singleton app.
2866 * @tc.type: FUNC
2867 * @tc.require:
2868 */
2869 HWTEST_F(OsAccountManagerModuleTest, IsOsAccountForeground001, TestSize.Level1)
2870 {
2871 bool isForeground = true;
2872 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(isForeground), ERR_OK);
2873 // uid = 0, localId = 0, not in foreground
2874 EXPECT_EQ(isForeground, false);
2875 }
2876
2877 /**
2878 * @tc.name: IsOsAccountForeground002
2879 * @tc.desc: Test call IsOsAccountForeground(bool &isForeground) with non singleton app.
2880 * @tc.type: FUNC
2881 * @tc.require:
2882 */
2883 HWTEST_F(OsAccountManagerModuleTest, IsOsAccountForeground002, TestSize.Level1)
2884 {
2885 OsAccountInfo osAccountInfo;
2886 ASSERT_EQ(OsAccountManager::CreateOsAccount("IsOsAccountForeground002", OsAccountType::NORMAL, osAccountInfo),
2887 ERR_OK);
2888
2889 bool isForeground = true;
2890
2891 // test not in foreground before switch
2892 EXPECT_EQ(setuid(osAccountInfo.GetLocalId() * UID_TRANSFORM_DIVISOR), 0);
2893 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(isForeground), ERR_OK);
2894 EXPECT_EQ(isForeground, false);
2895
2896 // switch to foreground
2897 EXPECT_EQ(setuid(ROOT_UID), 0);
2898 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2899
2900 // test in foreground after switch
2901 EXPECT_EQ(setuid(osAccountInfo.GetLocalId() * UID_TRANSFORM_DIVISOR), 0);
2902 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(isForeground), ERR_OK);
2903 EXPECT_EQ(isForeground, true);
2904
2905 EXPECT_EQ(setuid(ROOT_UID), 0);
2906 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2907
2908 // test not found after remove account
2909 EXPECT_EQ(setuid(osAccountInfo.GetLocalId() * UID_TRANSFORM_DIVISOR), 0);
2910 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(isForeground), ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2911 EXPECT_EQ(setuid(ROOT_UID), 0);
2912 }
2913
2914 /**
2915 * @tc.name: IsOsAccountForeground003
2916 * @tc.desc: Test call IsOsAccountForeground(int32_t localId, bool &isForeground) success.
2917 * @tc.type: FUNC
2918 * @tc.require:
2919 */
2920 HWTEST_F(OsAccountManagerModuleTest, IsOsAccountForeground003, TestSize.Level1)
2921 {
2922 OsAccountInfo osAccountInfo;
2923 ASSERT_EQ(OsAccountManager::CreateOsAccount("IsOsAccountForeground003", OsAccountType::NORMAL, osAccountInfo),
2924 ERR_OK);
2925
2926 bool isForeground = true;
2927
2928 // test not in foreground before switch
2929 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(osAccountInfo.GetLocalId(), isForeground), ERR_OK);
2930 EXPECT_EQ(isForeground, false);
2931
2932 // switch to foreground
2933 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2934
2935 // test in foreground
2936 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(osAccountInfo.GetLocalId(), isForeground), ERR_OK);
2937 EXPECT_EQ(isForeground, true);
2938
2939 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2940 }
2941
2942 /**
2943 * @tc.name: IsOsAccountForeground004
2944 * @tc.desc: Test call IsOsAccountForeground(int32_t localId, bool &isForeground) failed with invalid param.
2945 * @tc.type: FUNC
2946 * @tc.require:
2947 */
2948 HWTEST_F(OsAccountManagerModuleTest, IsOsAccountForeground004, TestSize.Level1)
2949 {
2950 bool isForeground = true;
2951
2952 // test localId < 0
2953 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(-1, isForeground), ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
2954
2955 // test localId = 0
2956 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(0, isForeground), ERR_OK);
2957 EXPECT_EQ(isForeground, false);
2958
2959 // test localId = 1
2960 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(1, isForeground), ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2961
2962 // test localId not exist
2963 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(1099, isForeground), ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
2964 }
2965
2966 /**
2967 * @tc.name: IsOsAccountForeground005
2968 * @tc.desc: Test call IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground)
2969 * success.
2970 * @tc.type: FUNC
2971 * @tc.require:
2972 */
2973 HWTEST_F(OsAccountManagerModuleTest, IsOsAccountForeground005, TestSize.Level1)
2974 {
2975 OsAccountInfo osAccountInfo;
2976 ASSERT_EQ(OsAccountManager::CreateOsAccount("IsOsAccountForeground005", OsAccountType::NORMAL, osAccountInfo),
2977 ERR_OK);
2978
2979 bool isForeground = true;
2980
2981 // test not in foreground before switch
2982 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(osAccountInfo.GetLocalId(), Constants::DEFAULT_DISPALY_ID,
2983 isForeground),
2984 ERR_OK);
2985 EXPECT_EQ(isForeground, false);
2986
2987 // switch to foreground
2988 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2989
2990 // test in foreground
2991 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(osAccountInfo.GetLocalId(), Constants::DEFAULT_DISPALY_ID,
2992 isForeground),
2993 ERR_OK);
2994 EXPECT_EQ(isForeground, true);
2995
2996 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
2997 }
2998
2999 /**
3000 * @tc.name: IsOsAccountForeground006
3001 * @tc.desc: Test call IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground) failed
3002 * with invalid param.
3003 * @tc.type: FUNC
3004 * @tc.require:
3005 */
3006 HWTEST_F(OsAccountManagerModuleTest, IsOsAccountForeground006, TestSize.Level1)
3007 {
3008 bool isForeground = true;
3009
3010 // test localId < 0
3011 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(-1, Constants::DEFAULT_DISPALY_ID, isForeground),
3012 ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
3013
3014 // test localId = 0
3015 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(0, Constants::DEFAULT_DISPALY_ID, isForeground), ERR_OK);
3016 EXPECT_EQ(isForeground, false);
3017
3018 // test localId = 1
3019 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(1, Constants::DEFAULT_DISPALY_ID, isForeground),
3020 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
3021
3022 // test localId not exist
3023 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(1099, Constants::DEFAULT_DISPALY_ID, isForeground),
3024 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
3025
3026 // test displayId not exist
3027 EXPECT_EQ(OsAccountManager::IsOsAccountForeground(MAIN_ACCOUNT_ID, 100000, isForeground),
3028 ERR_ACCOUNT_COMMON_DISPLAY_ID_NOT_EXIST_ERROR);
3029 }
3030
3031 /**
3032 * @tc.name: GetForegroundOsAccountLocalId001
3033 * @tc.desc: Test GetForegroundOsAccountLocalId(int32_t &localId) in main displayId success.
3034 * @tc.type: FUNC
3035 * @tc.require:
3036 */
3037 HWTEST_F(OsAccountManagerModuleTest, GetForegroundOsAccountLocalId001, TestSize.Level1)
3038 {
3039 OsAccountInfo osAccountInfo;
3040 ASSERT_EQ(
3041 OsAccountManager::CreateOsAccount("GetForegroundOsAccountLocalId001", OsAccountType::NORMAL, osAccountInfo),
3042 ERR_OK);
3043
3044 int32_t localId = 0;
3045
3046 // test in main account
3047 OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID);
3048 EXPECT_EQ(OsAccountManager::GetForegroundOsAccountLocalId(localId), ERR_OK);
3049 EXPECT_EQ(localId, MAIN_ACCOUNT_ID);
3050
3051 // test in account GetForegroundOsAccountLocalId001
3052 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
3053 EXPECT_EQ(OsAccountManager::GetForegroundOsAccountLocalId(localId), ERR_OK);
3054 EXPECT_EQ(localId, osAccountInfo.GetLocalId());
3055
3056 // test in main account after switch
3057 EXPECT_EQ(OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID), ERR_OK);
3058 EXPECT_EQ(OsAccountManager::GetForegroundOsAccountLocalId(localId), ERR_OK);
3059 EXPECT_EQ(localId, MAIN_ACCOUNT_ID);
3060
3061 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
3062 }
3063
3064 /**
3065 * @tc.name: GetForegroundOsAccountLocalId002
3066 * @tc.desc: Test GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) success.
3067 * @tc.type: FUNC
3068 * @tc.require:
3069 */
3070 HWTEST_F(OsAccountManagerModuleTest, GetForegroundOsAccountLocalId002, TestSize.Level1)
3071 {
3072 OsAccountInfo osAccountInfo;
3073 ASSERT_EQ(
3074 OsAccountManager::CreateOsAccount("GetForegroundOsAccountLocalId002", OsAccountType::NORMAL, osAccountInfo),
3075 ERR_OK);
3076
3077 int32_t localId = 0;
3078
3079 // test in main account
3080 OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID);
3081 EXPECT_EQ(OsAccountManager::GetForegroundOsAccountLocalId(Constants::DEFAULT_DISPALY_ID, localId), ERR_OK);
3082 EXPECT_EQ(localId, MAIN_ACCOUNT_ID);
3083
3084 // test in account GetForegroundOsAccountLocalId002
3085 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
3086 EXPECT_EQ(OsAccountManager::GetForegroundOsAccountLocalId(Constants::DEFAULT_DISPALY_ID, localId), ERR_OK);
3087 EXPECT_EQ(localId, osAccountInfo.GetLocalId());
3088
3089 ASSERT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
3090
3091 // test in main account
3092 EXPECT_EQ(OsAccountManager::GetForegroundOsAccountLocalId(Constants::DEFAULT_DISPALY_ID, localId), ERR_OK);
3093 EXPECT_EQ(localId, MAIN_ACCOUNT_ID);
3094 }
3095
3096 /**
3097 * @tc.name: GetForegroundOsAccountLocalId006
3098 * @tc.desc: Test call GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) failed with invalid
3099 * param.
3100 * @tc.type: FUNC
3101 * @tc.require:
3102 */
3103 HWTEST_F(OsAccountManagerModuleTest, GetForegroundOsAccountLocalId006, TestSize.Level1)
3104 {
3105 int32_t localId = 0;
3106
3107 // test displayId not exist
3108 EXPECT_EQ(OsAccountManager::GetForegroundOsAccountLocalId(100000, localId),
3109 ERR_ACCOUNT_COMMON_DISPLAY_ID_NOT_EXIST_ERROR);
3110 }
3111
3112 /**
3113 * @tc.name: GetForegroundOsAccounts001
3114 * @tc.desc: Test GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts) success.
3115 * @tc.type: FUNC
3116 * @tc.require:
3117 */
3118 HWTEST_F(OsAccountManagerModuleTest, GetForegroundOsAccounts001, TestSize.Level1)
3119 {
3120 OsAccountInfo account;
3121 ASSERT_EQ(OsAccountManager::CreateOsAccount("GetForegroundOsAccounts001", OsAccountType::NORMAL, account), ERR_OK);
3122
3123 // test account not in foregroud list after create
3124 std::vector<ForegroundOsAccount> accounts;
3125 EXPECT_EQ(OsAccountManager::GetForegroundOsAccounts(accounts), ERR_OK);
__anon2e9c97090602(const ForegroundOsAccount &foregroundAccounts) 3126 auto it = std::find_if(accounts.begin(), accounts.end(), [&](const ForegroundOsAccount &foregroundAccounts) {
3127 return foregroundAccounts.localId == account.GetLocalId();
3128 });
3129 EXPECT_TRUE(it == accounts.end());
3130
3131 // test account in foregroud list after active
3132 EXPECT_EQ(OsAccountManager::ActivateOsAccount(account.GetLocalId()), ERR_OK);
3133 EXPECT_EQ(OsAccountManager::GetForegroundOsAccounts(accounts), ERR_OK);
__anon2e9c97090702(const ForegroundOsAccount &foregroundAccounts) 3134 it = std::find_if(accounts.begin(), accounts.end(), [&](const ForegroundOsAccount &foregroundAccounts) {
3135 return foregroundAccounts.localId == account.GetLocalId();
3136 });
3137 EXPECT_TRUE(it != accounts.end());
3138 EXPECT_EQ(it->displayId, Constants::DEFAULT_DISPALY_ID);
3139
3140 // test account in foregroud list after switch
3141 EXPECT_EQ(OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID), ERR_OK);
3142 EXPECT_EQ(OsAccountManager::GetForegroundOsAccounts(accounts), ERR_OK);
__anon2e9c97090802(const ForegroundOsAccount &foregroundAccounts) 3143 it = std::find_if(accounts.begin(), accounts.end(), [&](const ForegroundOsAccount &foregroundAccounts) {
3144 return foregroundAccounts.localId == MAIN_ACCOUNT_ID;
3145 });
3146 EXPECT_TRUE(it != accounts.end());
3147 EXPECT_EQ(it->displayId, Constants::DEFAULT_DISPALY_ID);
3148
3149 // test account not in foregroud list after deactive
3150 EXPECT_EQ(OsAccountManager::DeactivateOsAccount(account.GetLocalId()), ERR_OK);
3151 EXPECT_EQ(OsAccountManager::GetForegroundOsAccounts(accounts), ERR_OK);
__anon2e9c97090902(const ForegroundOsAccount &foregroundAccounts) 3152 it = std::find_if(accounts.begin(), accounts.end(), [&](const ForegroundOsAccount &foregroundAccounts) {
3153 return foregroundAccounts.localId == account.GetLocalId();
3154 });
3155 EXPECT_TRUE(it == accounts.end());
3156
3157 ASSERT_EQ(OsAccountManager::RemoveOsAccount(account.GetLocalId()), ERR_OK);
3158 }
3159
3160
3161 /**
3162 * @tc.name: SetOsAccountToBeRemoved001
3163 * @tc.desc: Test SetOsAccountToBeRemoved.
3164 * @tc.type: FUNC
3165 * @tc.require:
3166 */
3167 HWTEST_F(OsAccountManagerModuleTest, SetOsAccountToBeRemoved001, TestSize.Level1)
3168 {
3169 OsAccountInfo account;
3170 ASSERT_EQ(OsAccountManager::CreateOsAccount("SetOsAccountToBeRemoved001", OsAccountType::NORMAL, account), ERR_OK);
3171 EXPECT_EQ(account.GetToBeRemoved(), false);
3172 EXPECT_EQ(OsAccountManager::SetOsAccountToBeRemoved(account.GetLocalId() + 1, true),
3173 ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
3174 EXPECT_EQ(OsAccountManager::SetOsAccountToBeRemoved(account.GetLocalId(), true), ERR_OK);
3175 EXPECT_EQ(OsAccountManager::QueryOsAccountById(account.GetLocalId(), account), ERR_OK);
3176 EXPECT_EQ(account.GetToBeRemoved(), true);
3177 ASSERT_EQ(OsAccountManager::RemoveOsAccount(account.GetLocalId()), ERR_OK);
3178 }
3179
3180 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
3181 /**
3182 * @tc.name: GetBackgroundOsAccountLocalIds001
3183 * @tc.desc: Test GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds) success.
3184 * @tc.type: FUNC
3185 * @tc.require:
3186 */
3187 HWTEST_F(OsAccountManagerModuleTest, GetBackgroundOsAccountLocalIds001, TestSize.Level1)
3188 {
3189 OsAccountInfo account;
3190 ASSERT_EQ(OsAccountManager::CreateOsAccount("GetBackgroundOsAccountLocalIds001", OsAccountType::NORMAL, account),
3191 ERR_OK);
3192
3193 // test account not in backgroud list after create
3194 std::vector<int32_t> localIds;
3195 EXPECT_EQ(OsAccountManager::GetBackgroundOsAccountLocalIds(localIds), ERR_OK);
3196 auto it = std::find_if(localIds.begin(), localIds.end(),
__anon2e9c97090a02(int32_t localId) 3197 [&](int32_t localId) { return localId == account.GetLocalId(); });
3198 EXPECT_TRUE(it == localIds.end());
3199
3200 // test account in backgroud list after switch
3201 OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID);
3202 EXPECT_EQ(OsAccountManager::ActivateOsAccount(account.GetLocalId()), ERR_OK);
3203 EXPECT_EQ(OsAccountManager::GetBackgroundOsAccountLocalIds(localIds), ERR_OK);
__anon2e9c97090b02(int32_t localId) 3204 it = std::find_if(localIds.begin(), localIds.end(), [&](int32_t localId) { return localId == MAIN_ACCOUNT_ID; });
3205 EXPECT_TRUE(it != localIds.end());
3206
3207 // test account in backgroud list after switch
3208 EXPECT_EQ(OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID), ERR_OK);
3209 EXPECT_EQ(OsAccountManager::GetBackgroundOsAccountLocalIds(localIds), ERR_OK);
3210 it = std::find_if(localIds.begin(), localIds.end(),
__anon2e9c97090c02(int32_t localId) 3211 [&](int32_t localId) { return localId == account.GetLocalId(); });
3212 EXPECT_TRUE(it != localIds.end());
3213
3214 ASSERT_EQ(OsAccountManager::RemoveOsAccount(account.GetLocalId()), ERR_OK);
3215
3216 // test account not in backgroud list after remove
3217 EXPECT_EQ(OsAccountManager::GetBackgroundOsAccountLocalIds(localIds), ERR_OK);
3218 it = std::find_if(localIds.begin(), localIds.end(),
__anon2e9c97090d02(int32_t localId) 3219 [&](int32_t localId) { return localId == account.GetLocalId(); });
3220 EXPECT_TRUE(it == localIds.end());
3221 }
3222 #endif
3223 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
3224
3225 /**
3226 * @tc.name: DeactivateAllOsAccountsModuleTest001
3227 * @tc.desc: Test DeactivateAllOsAccounts success.
3228 * @tc.type: FUNC
3229 * @tc.require:
3230 */
3231 HWTEST_F(OsAccountManagerModuleTest, DeactivateAllOsAccountsModuleTest001, TestSize.Level1)
3232 {
3233 std::string privateName = "DeactivateAllOsAccounts001";
3234 OsAccountInfo osAccountInfo;
3235 ASSERT_EQ(OsAccountManager::CreateOsAccount(privateName, OsAccountType::GUEST, osAccountInfo), ERR_OK);
3236 ASSERT_TRUE(osAccountInfo.GetLocalId() > Constants::START_USER_ID);
3237 EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
3238 EXPECT_EQ(OsAccountManager::DeactivateAllOsAccounts(), ERR_OK);
3239
3240 std::vector<int32_t> ids;
3241 EXPECT_EQ(OsAccountManager::QueryActiveOsAccountIds(ids), ERR_OK);
3242 EXPECT_EQ(ids.empty(), true);
3243
3244 EXPECT_EQ(OsAccountManager::ActivateOsAccount(MAIN_ACCOUNT_ID), ERR_OK);
3245 EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
3246 }
3247
3248 /**
3249 * @tc.name: IsOsAccountDeactivatingModuleTest001
3250 * @tc.desc: Test IsOsAccountDeactivating success.
3251 * @tc.type: FUNC
3252 * @tc.require:
3253 */
3254 HWTEST_F(OsAccountManagerModuleTest, IsOsAccountDeactivatingModuleTest001, TestSize.Level1)
3255 {
3256 bool isDeactivating;
3257 EXPECT_EQ(OsAccountManager::IsOsAccountDeactivating(MAIN_ACCOUNT_ID, isDeactivating), ERR_OK);
3258 }
3259