1 /*
2 * Copyright (c) 2023 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 "os_account_manager.h"
17
18 namespace {
19 int32_t g_mockId = 100; // default id when there is no os_account part
20 bool g_mockQueryForgroundOsAccountRet = true;
21 bool g_mockGetOsAccountLocalIdFromUidRet = true;
22 int32_t g_mockIdForGetOsAccountLocalIdFromUid = 100;
23 bool g_mockOsAccountExists = true;
24 std::vector<OHOS::AccountSA::OsAccountInfo> g_mockOsAccountInfos;
25 }
26
MockQueryForgroundOsAccountId(bool mockRet,uint8_t mockCase)27 void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase)
28 {
29 g_mockQueryForgroundOsAccountRet = mockRet;
30 switch (mockCase) {
31 case 1: {
32 g_mockId = 101; // 101 mockcase1
33 break;
34 }
35 case 2: {
36 g_mockId = 0; // 0 mockcase1
37 break;
38 }
39 default: {
40 g_mockId = 100; // 100 mockdefault
41 break;
42 }
43 }
44 }
45
MockIsOsAccountExists(bool mockRet)46 void MockIsOsAccountExists(bool mockRet)
47 {
48 g_mockOsAccountExists = mockRet;
49 }
50
ResetAccountMock()51 void ResetAccountMock()
52 {
53 g_mockId = 100; // 100 mockId
54 g_mockQueryForgroundOsAccountRet = true;
55 g_mockGetOsAccountLocalIdFromUidRet = true;
56 g_mockIdForGetOsAccountLocalIdFromUid = 100;
57 g_mockOsAccountExists = true;
58 }
59
MockGetOsAccountLocalIdFromUid(bool mockRet,uint8_t mockCase=0)60 void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase = 0)
61 {
62 g_mockGetOsAccountLocalIdFromUidRet = mockRet;
63 switch (mockCase) {
64 case 1: { // mock for invalid id
65 g_mockIdForGetOsAccountLocalIdFromUid = -2; // -2 mock for invalid id
66 break;
67 }
68 case 2: { // mock for system id
69 g_mockIdForGetOsAccountLocalIdFromUid = 88; // 88 mock for system id
70 break;
71 }
72 default: {
73 g_mockIdForGetOsAccountLocalIdFromUid = 100; // 100 mock for system id
74 break;
75 }
76 }
77 }
78
MockQueryAllCreatedOsAccounts(int32_t userId)79 void MockQueryAllCreatedOsAccounts(int32_t userId)
80 {
81 g_mockOsAccountInfos.clear();
82 OHOS::AccountSA::OsAccountInfo osAccountInfo;
83 osAccountInfo.SetLocalId(userId);
84 g_mockOsAccountInfos.push_back(osAccountInfo);
85 }
86
87 namespace OHOS {
88 namespace AccountSA {
GetForegroundOsAccountLocalId(int32_t & id)89 ErrCode OsAccountManager::GetForegroundOsAccountLocalId(int32_t &id)
90 {
91 if (!g_mockQueryForgroundOsAccountRet) {
92 return ERR_INVALID_OPERATION;
93 }
94 id = g_mockId;
95 return ERR_OK;
96 }
97
GetOsAccountLocalIdFromUid(const int32_t uid,int32_t & id)98 ErrCode OsAccountManager::GetOsAccountLocalIdFromUid(const int32_t uid, int32_t &id)
99 {
100 id = g_mockIdForGetOsAccountLocalIdFromUid;
101 return g_mockGetOsAccountLocalIdFromUidRet ? ERR_OK : ERR_INVALID_OPERATION;
102 }
103
IsOsAccountExists(const int id,bool & isOsAccountExists)104 ErrCode OsAccountManager::IsOsAccountExists(const int id, bool &isOsAccountExists)
105 {
106 isOsAccountExists = g_mockOsAccountExists;
107 return ERR_OK;
108 }
109
QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> & osAccountInfos)110 ErrCode OsAccountManager::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos)
111 {
112 osAccountInfos = g_mockOsAccountInfos;
113 return ERR_OK;
114 }
115 } // namespace EventFwk
116 } // namespace OHOS