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