• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "accesstoken_kit.h"
17 #include "token_setproc.h"
18 #include <gtest/gtest.h>
19 
20 using namespace OHOS::Security::AccessToken;
21 PermissionDef connectPerm = {
22     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
23     .bundleName = "net_client_pac_file_url_test",
24     .grantMode = 1,
25     .label = "label",
26     .labelId = 1,
27     .description = "Test web connect maneger",
28     .descriptionId = 1,
29     .availableLevel = APL_SYSTEM_BASIC,
30 };
31 
32 PermissionStateFull connectState = {
33     .grantFlags = {2},
34     .grantStatus = {PermissionState::PERMISSION_GRANTED},
35     .isGeneral = true,
36     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
37     .resDeviceID = {"local"},
38 };
39 
40 PermissionDef pacPerm = {
41     .permissionName = "ohos.permission.SET_PAC_URL",
42     .bundleName = "net_client_pac_file_url_test",
43     .grantMode = 1,
44     .label = "label",
45     .labelId = 1,
46     .description = "Test web connect maneger",
47     .descriptionId = 1,
48     .availableLevel = APL_SYSTEM_BASIC,
49 };
50 
51 PermissionStateFull pacState = {
52     .grantFlags = {2},
53     .grantStatus = {PermissionState::PERMISSION_GRANTED},
54     .isGeneral = true,
55     .permissionName = "ohos.permission.SET_PAC_URL",
56     .resDeviceID = {"local"},
57 };
58 
59 HapPolicyParams testPolicyPrams = {
60     .apl = APL_SYSTEM_BASIC,
61     .domain = "test.domain",
62     .permList = {pacPerm, connectPerm},
63     .permStateList = {pacState, connectState},
64 };
65 
66 HapInfoParams testInfoParms = {
67     .userID = 1,
68     .bundleName = "net_client_pac_file_url_test",
69     .instIndex = 0,
70     .appIDDesc = "test",
71     .isSystemApp = true,
72 };
73 static AccessTokenID accessID_;
74 static uint64_t currentID_;
75 
SetUpPermission()76 void SetUpPermission()
77 {
78     currentID_ = GetSelfTokenID();
79     AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
80     accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
81     SetSelfTokenID(tokenIdEx.tokenIDEx);
82 }
83 
UnsetUpPermission()84 void UnsetUpPermission()
85 {
86     AccessTokenKit::DeleteToken(accessID_);
87     SetSelfTokenID(currentID_);
88 }
89