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