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 "distributed_sched_util.h"
17
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "token_setproc.h"
21
22 namespace OHOS {
23 namespace DistributedSchedule {
24 namespace {
25 const char* DISTSCHED_PROCESS_NAME = "distributedsched";
26 }
MockPermission()27 void DistributedSchedUtil::MockPermission()
28 {
29 static const char *PERMS[] = {
30 "ohos.permission.DISTRIBUTED_DATASYNC"
31 };
32 MockProcessAndPermission(DISTSCHED_PROCESS_NAME, PERMS, 1);
33 }
34
MockProcess(const char * processName)35 void DistributedSchedUtil::MockProcess(const char* processName)
36 {
37 MockProcessAndPermission(processName);
38 }
39
MockProcessAndPermission(const char * processName,const char * perms[],int32_t permsNum)40 void DistributedSchedUtil::MockProcessAndPermission(const char* processName, const char *perms[], int32_t permsNum)
41 {
42 uint64_t tokenId;
43 NativeTokenInfoParams infoInstance = {
44 .dcapsNum = 0,
45 .permsNum = permsNum,
46 .aclsNum = 0,
47 .dcaps = nullptr,
48 .perms = perms,
49 .acls = nullptr,
50 .processName = processName,
51 .aplStr = "system_core",
52 };
53 tokenId = GetAccessTokenId(&infoInstance);
54 SetSelfTokenID(tokenId);
55 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
56 }
57 }
58 }