1 /*
2  * Copyright (c) 2022-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 "mock_ipc_common.h"
17 
18 #include "iam_logger.h"
19 #ifdef HAS_OS_ACCOUNT_PART
20 #include "os_account_manager.h"
21 #endif // HAS_OS_ACCOUNT_PART
22 #define LOG_TAG "USER_AUTH_SA"
23 
24 namespace {
25     const uint32_t TEST_USER_ID = 548781;
26     const std::string TEST_CALLER_BUNDLE_NAME = "com.ohos.useriam.authwidgettest";
27 }
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 std::set<Permission> IpcCommon::permSet_;
33 bool IpcCommon::isSetTokenId_ = false;
34 uint32_t IpcCommon::tokenId_ = 0;
35 bool IpcCommon::skipFlag_ = false;
36 
GetCallingUserId(IPCObjectStub & stub,int32_t & userId)37 int32_t IpcCommon::GetCallingUserId(IPCObjectStub &stub, int32_t &userId)
38 {
39     if (userId != INVALID_USER_ID || skipFlag_) {
40         return FAIL;
41     }
42     userId = TEST_USER_ID;
43     return SUCCESS;
44 }
45 
GetActiveUserId(std::optional<int32_t> & userId)46 int32_t IpcCommon::GetActiveUserId(std::optional<int32_t> &userId)
47 {
48     if (userId.has_value() && userId.value() != 0) {
49         return SUCCESS;
50     }
51     std::vector<int32_t> ids;
52 #ifdef HAS_OS_ACCOUNT_PART
53     ErrCode queryRet = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
54     if (queryRet != ERR_OK || ids.empty()) {
55         IAM_LOGE("failed to query active account id");
56         return GENERAL_ERROR;
57     }
58 #else  // HAS_OS_ACCOUNT_PART
59     const int32_t DEFAULT_OS_ACCOUNT_ID = 0;
60     ids.push_back(DEFAULT_OS_ACCOUNT_ID);
61     IAM_LOGI("there is no os account part, use default id");
62 #endif // HAS_OS_ACCOUNT_PART
63     userId = ids.front();
64     return SUCCESS;
65 }
66 
GetAllUserId(std::vector<int32_t> & userIds)67 int32_t IpcCommon::GetAllUserId(std::vector<int32_t> &userIds)
68 {
69 #ifdef HAS_OS_ACCOUNT_PART
70     std::vector<OHOS::AccountSA::OsAccountInfo> accountInfos = {};
71     ErrCode ret = AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accountInfos);
72     if (ret != ERR_OK) {
73         IAM_LOGE("failed to query all account id ret %{public}d ", ret);
74         return GENERAL_ERROR;
75     }
76 
77     if (accountInfos.empty()) {
78         IAM_LOGE("accountInfos count %{public}zu", accountInfos.size());
79         return SUCCESS;
80     }
81 
82     std::transform(accountInfos.begin(), accountInfos.end(), std::back_inserter(userIds),
83         [](auto &iter) { return iter.GetLocalId(); });
84 #else
85     const int32_t DEFAULT_OS_ACCOUNT_ID = 0;
86     userIds.push_back(DEFAULT_OS_ACCOUNT_ID);
87 #endif
88     return SUCCESS;
89 }
90 
GetUserTypeByUserId(int32_t userId,int32_t & userType)91 int32_t IpcCommon::GetUserTypeByUserId(int32_t userId, int32_t &userType)
92 {
93     userType = 0;
94     return SUCCESS;
95 }
96 
97 // for unittest only
CheckPermission(IPCObjectStub & stub,Permission permission)98 bool IpcCommon::CheckPermission(IPCObjectStub &stub, Permission permission)
99 {
100     return permSet_.find(permission) != permSet_.end();
101 }
102 
GetAccessTokenId(IPCObjectStub & stub)103 uint32_t IpcCommon::GetAccessTokenId(IPCObjectStub &stub)
104 {
105     if (isSetTokenId_) {
106         return tokenId_;
107     }
108     tokenId_ = stub.GetFirstTokenID();
109     if (tokenId_ == 0) {
110         tokenId_ = stub.GetCallingTokenID();
111     }
112     return tokenId_;
113 }
114 
SetAccessTokenId(uint32_t tokenId,bool isSetTokenId)115 void IpcCommon::SetAccessTokenId(uint32_t tokenId, bool isSetTokenId)
116 {
117     tokenId_ =  tokenId;
118     isSetTokenId_ = isSetTokenId;
119 }
120 
AddPermission(Permission perm)121 void IpcCommon::AddPermission(Permission perm)
122 {
123     permSet_.insert(perm);
124 }
125 
DeleteAllPermission()126 void IpcCommon::DeleteAllPermission()
127 {
128     permSet_.clear();
129 }
130 
GetTokenId(IPCObjectStub & stub)131 uint32_t IpcCommon::GetTokenId(IPCObjectStub &stub)
132 {
133     uint32_t tokenId = stub.GetCallingTokenID();
134     IAM_LOGI("get tokenId: %{public}d", tokenId);
135     return tokenId;
136 }
137 
SetSkipUserFlag(bool isSkip)138 void IpcCommon::SetSkipUserFlag(bool isSkip)
139 {
140     skipFlag_ = isSkip;
141 }
142 
GetCallerName(IPCObjectStub & stub,std::string & callerName,int32_t & callerType)143 bool IpcCommon::GetCallerName(IPCObjectStub &stub, std::string &callerName, int32_t &callerType)
144 {
145     callerName = "";
146     callerType = 0;
147     return true;
148 }
149 
GetCallingAppID(IPCObjectStub & stub,std::string & callingAppID)150 bool IpcCommon::GetCallingAppID(IPCObjectStub &stub, std::string &callingAppID)
151 {
152     callingAppID = "";
153     return true;
154 }
155 
CheckForegroundApplication(const std::string & bundleName)156 bool IpcCommon::CheckForegroundApplication(const std::string &bundleName)
157 {
158     return true;
159 }
160 
IsOsAccountVerified(int32_t userId)161 bool IpcCommon::IsOsAccountVerified(int32_t userId)
162 {
163     return true;
164 }
165 } // namespace UserAuth
166 } // namespace UserIam
167 } // namespace OHOS