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 HapPolicyParams testPolicyParams = {
95 .apl = APL_SYSTEM_BASIC,
96 .domain = "test.domain",
97 .permList = { testPermGetNetworkInfoDef, testPermSetTelephonyStateDef, testPermGetTelephonyStateDef },
98 .permStateList = { testGetNetworkInfoState, testSetTelephonyState, testGetTelephonyState },
99 };
100 } // namespace
101
DataAccessToken()102 DataAccessToken::DataAccessToken()
103 {
104 currentID_ = GetSelfTokenID();
105 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
106 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
107 SetSelfTokenID(tokenIdEx.tokenIDEx);
108 }
~DataAccessToken()109 DataAccessToken::~DataAccessToken()
110 {
111 AccessTokenKit::DeleteToken(accessID_);
112 SetSelfTokenID(currentID_);
113 }
114 } // namespace Telephony
115 } // namespace OHOS