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 }
25 
MockQueryForgroundOsAccountId(bool mockRet,uint8_t mockCase)26 void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase)
27 {
28     g_mockQueryForgroundOsAccountRet = mockRet;
29     switch (mockCase) {
30         case 1: {
31             g_mockId = 101; // 101 mockcase1
32             break;
33         }
34         default: {
35             g_mockId = 100; // 100 mockdefault
36             break;
37         }
38     }
39 }
40 
MockIsOsAccountExists(bool mockRet)41 void MockIsOsAccountExists(bool mockRet)
42 {
43     g_mockOsAccountExists = mockRet;
44 }
45 
ResetAccountMock()46 void ResetAccountMock()
47 {
48     g_mockId = 100; // 100 mockId
49     g_mockQueryForgroundOsAccountRet = true;
50     g_mockGetOsAccountLocalIdFromUidRet = true;
51     g_mockIdForGetOsAccountLocalIdFromUid = 100;
52     g_mockOsAccountExists = true;
53 }
54 
MockGetOsAccountLocalIdFromUid(bool mockRet,uint8_t mockCase=0)55 void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase = 0)
56 {
57     g_mockGetOsAccountLocalIdFromUidRet = mockRet;
58     switch (mockCase) {
59         case 1: { // mock for invalid id
60             g_mockIdForGetOsAccountLocalIdFromUid = -2; // -2 mock for invalid id
61             break;
62         }
63         case 2: { // mock for system id
64             g_mockIdForGetOsAccountLocalIdFromUid = 88; // 88 mock for system id
65             break;
66         }
67         default: {
68             g_mockIdForGetOsAccountLocalIdFromUid = 100; // 100 mock for system id
69             break;
70         }
71     }
72 }
73 
74 namespace OHOS {
75 namespace AccountSA {
GetForegroundOsAccountLocalId(int32_t & id)76 ErrCode OsAccountManager::GetForegroundOsAccountLocalId(int32_t &id)
77 {
78     if (!g_mockQueryForgroundOsAccountRet) {
79         return ERR_INVALID_OPERATION;
80     }
81     id = g_mockId;
82     return ERR_OK;
83 }
84 
GetOsAccountLocalIdFromUid(const int32_t uid,int32_t & id)85 ErrCode OsAccountManager::GetOsAccountLocalIdFromUid(const int32_t uid, int32_t &id)
86 {
87     id = g_mockIdForGetOsAccountLocalIdFromUid;
88     return g_mockGetOsAccountLocalIdFromUidRet ? ERR_OK : ERR_INVALID_OPERATION;
89 }
90 
IsOsAccountExists(const int id,bool & isOsAccountExists)91 ErrCode OsAccountManager::IsOsAccountExists(const int id, bool &isOsAccountExists)
92 {
93     isOsAccountExists = g_mockOsAccountExists;
94     return ERR_OK;
95 }
96 }  // namespace EventFwk
97 }  // namespace OHOS