1 /*
2 * Copyright (c) 2024 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 "data_access_token.h"
17
18 #include <iostream>
19
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 using namespace Security::AccessToken;
26 using Security::AccessToken::AccessTokenID;
27 namespace {
28
29 HapInfoParams testInfoParams = {
30 .bundleName = "tel_cellular_data_test",
31 .userID = 1,
32 .instIndex = 0,
33 .appIDDesc = "test",
34 .isSystemApp = true,
35 };
36
37 PermissionDef testPermGetNetworkInfoDef = {
38 .permissionName = "ohos.permission.GET_NETWORK_INFO",
39 .bundleName = "tel_cellular_data_test",
40 .grantMode = 1, // SYSTEM_GRANT
41 .label = "label",
42 .labelId = 1,
43 .description = "Test cellular data",
44 .descriptionId = 1,
45 .availableLevel = APL_SYSTEM_BASIC,
46 };
47
48 PermissionStateFull testGetNetworkInfoState = {
49 .grantFlags = { 2 }, // PERMISSION_USER_SET
50 .grantStatus = { PermissionState::PERMISSION_GRANTED },
51 .isGeneral = true,
52 .permissionName = "ohos.permission.GET_NETWORK_INFO",
53 .resDeviceID = { "local" },
54 };
55
56 PermissionDef testPermSetTelephonyStateDef = {
57 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
58 .bundleName = "tel_cellular_data_test",
59 .grantMode = 1, // SYSTEM_GRANT
60 .label = "label",
61 .labelId = 1,
62 .description = "Test cellular data",
63 .descriptionId = 1,
64 .availableLevel = APL_SYSTEM_BASIC,
65 };
66
67 PermissionStateFull testSetTelephonyState = {
68 .grantFlags = { 2 }, // PERMISSION_USER_SET
69 .grantStatus = { PermissionState::PERMISSION_GRANTED },
70 .isGeneral = true,
71 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
72 .resDeviceID = { "local" },
73 };
74
75 PermissionDef testPermGetTelephonyStateDef = {
76 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
77 .bundleName = "tel_cellular_data_test",
78 .grantMode = 1, // SYSTEM_GRANT
79 .label = "label",
80 .labelId = 1,
81 .description = "Test cellular data",
82 .descriptionId = 1,
83 .availableLevel = APL_SYSTEM_BASIC,
84 };
85
86 PermissionStateFull testGetTelephonyState = {
87 .grantFlags = { 2 }, // PERMISSION_USER_SET
88 .grantStatus = { PermissionState::PERMISSION_GRANTED },
89 .isGeneral = true,
90 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
91 .resDeviceID = { "local" },
92 };
93
94 PermissionDef testPermManagerApnSettingDef = {
95 .permissionName = "ohos.permission.MANAGE_APN_SETTING",
96 .bundleName = "tel_cellular_data_test",
97 .grantMode = 1, // SYSTEM_GRANT
98 .label = "label",
99 .labelId = 1,
100 .description = "Test cellular data",
101 .descriptionId = 1,
102 .availableLevel = APL_SYSTEM_BASIC,
103 };
104
105 PermissionStateFull testManagerApnSetting = {
106 .grantFlags = { 2 }, // PERMISSION_USER_SET
107 .grantStatus = { PermissionState::PERMISSION_GRANTED },
108 .isGeneral = true,
109 .permissionName = "ohos.permission.MANAGE_APN_SETTING",
110 .resDeviceID = { "local" },
111 };
112
113 HapPolicyParams testPolicyParams = {
114 .apl = APL_SYSTEM_BASIC,
115 .domain = "test.domain",
116 .permList = { testPermGetNetworkInfoDef, testPermSetTelephonyStateDef,
117 testPermGetTelephonyStateDef, testPermManagerApnSettingDef },
118 .permStateList = { testGetNetworkInfoState, testSetTelephonyState, testGetTelephonyState, testManagerApnSetting },
119 };
120 } // namespace
121
DataAccessToken()122 DataAccessToken::DataAccessToken()
123 {
124 currentID_ = GetSelfTokenID();
125 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
126 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
127 SetSelfTokenID(tokenIdEx.tokenIDEx);
128 }
~DataAccessToken()129 DataAccessToken::~DataAccessToken()
130 {
131 AccessTokenKit::DeleteToken(accessID_);
132 SetSelfTokenID(currentID_);
133 }
134 } // namespace Telephony
135 } // namespace OHOS