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 HapPolicyParams testPolicyParams = {
76     .apl = APL_SYSTEM_BASIC,
77     .domain = "test.domain",
78     .permList = { testPermGetNetworkInfoDef, testPermSetTelephonyStateDef },
79     .permStateList = { testGetNetworkInfoState, testSetTelephonyState },
80 };
81 } // namespace
82 
DataAccessToken()83 DataAccessToken::DataAccessToken()
84 {
85     currentID_ = GetSelfTokenID();
86     AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
87     accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
88     SetSelfTokenID(tokenIdEx.tokenIDEx);
89 }
~DataAccessToken()90 DataAccessToken::~DataAccessToken()
91 {
92     AccessTokenKit::DeleteToken(accessID_);
93     SetSelfTokenID(currentID_);
94 }
95 } // namespace Telephony
96 } // namespace OHOS