1 /*
2 * Copyright (c) 2022 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_common_defines.h"
19 #include "iam_logger.h"
20 #ifdef HAS_OS_ACCOUNT_PART
21 #include "os_account_manager.h"
22 #endif // HAS_OS_ACCOUNT_PART
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24
25 namespace {
26 const uint32_t TEST_USER_ID = 548781;
27 }
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 std::set<Permission> IpcCommon::permSet_;
33
GetCallingUserId(IPCObjectStub & stub,int32_t & userId)34 int32_t IpcCommon::GetCallingUserId(IPCObjectStub &stub, int32_t &userId)
35 {
36 if (userId != 0) {
37 return SUCCESS;
38 }
39 userId = TEST_USER_ID;
40 return SUCCESS;
41 }
42
GetActiveUserId(std::optional<int32_t> & userId)43 int32_t IpcCommon::GetActiveUserId(std::optional<int32_t> &userId)
44 {
45 if (userId.has_value() && userId.value() != 0) {
46 return SUCCESS;
47 }
48 std::vector<int32_t> ids;
49 #ifdef HAS_OS_ACCOUNT_PART
50 ErrCode queryRet = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
51 if (queryRet != ERR_OK || ids.empty()) {
52 IAM_LOGE("failed to query active account id");
53 return GENERAL_ERROR;
54 }
55 #else // HAS_OS_ACCOUNT_PART
56 const int32_t DEFAULT_OS_ACCOUNT_ID = 0;
57 ids.push_back(DEFAULT_OS_ACCOUNT_ID);
58 IAM_LOGI("there is no os account part, use default id");
59 #endif // HAS_OS_ACCOUNT_PART
60 userId = ids.front();
61 return SUCCESS;
62 }
63
64 // for unittest only
CheckPermission(IPCObjectStub & stub,Permission permission)65 bool IpcCommon::CheckPermission(IPCObjectStub &stub, Permission permission)
66 {
67 return permSet_.find(permission) != permSet_.end();
68 }
69
GetAccessTokenId(IPCObjectStub & stub)70 uint32_t IpcCommon::GetAccessTokenId(IPCObjectStub &stub)
71 {
72 uint32_t tokenId = stub.GetFirstTokenID();
73 if (tokenId == 0) {
74 tokenId = stub.GetCallingTokenID();
75 }
76 return tokenId;
77 }
78
AddPermission(Permission perm)79 void IpcCommon::AddPermission(Permission perm)
80 {
81 permSet_.insert(perm);
82 }
83
DeleteAllPermission()84 void IpcCommon::DeleteAllPermission()
85 {
86 permSet_.clear();
87 }
88 } // namespace UserAuth
89 } // namespace UserIam
90 } // namespace OHOS