1 /*
2 * Copyright (c) 2023 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 "security_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_core_service_gtest",
31 .userID = 1,
32 .instIndex = 0,
33 .appIDDesc = "test",
34 .isSystemApp = true,
35 };
36
37 PermissionDef testPermGetTelephonyStateDef = {
38 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
39 .bundleName = "tel_core_service_gtest",
40 .grantMode = 1, // SYSTEM_GRANT
41 .label = "label",
42 .labelId = 1,
43 .description = "Test core service",
44 .descriptionId = 1,
45 .availableLevel = APL_SYSTEM_BASIC,
46 };
47
48 PermissionStateFull testGetTelephonyState = {
49 .grantFlags = { 2 }, // PERMISSION_USER_SET
50 .grantStatus = { PermissionState::PERMISSION_GRANTED },
51 .isGeneral = true,
52 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
53 .resDeviceID = { "local" },
54 };
55
56 PermissionDef testPermSetTelephonyStateDef = {
57 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
58 .bundleName = "tel_core_service_gtest",
59 .grantMode = 1, // SYSTEM_GRANT
60 .label = "label",
61 .labelId = 1,
62 .description = "Test core service",
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 testPermGetNetworkInfoDef = {
76 .permissionName = "ohos.permission.GET_NETWORK_INFO",
77 .bundleName = "tel_core_service_gtest",
78 .grantMode = 1, // SYSTEM_GRANT
79 .label = "label",
80 .labelId = 1,
81 .description = "Test core service",
82 .descriptionId = 1,
83 .availableLevel = APL_SYSTEM_BASIC,
84 };
85
86 PermissionStateFull testPermGetNetworkInfo = {
87 .grantFlags = { 2 }, // PERMISSION_USER_SET
88 .grantStatus = { PermissionState::PERMISSION_GRANTED },
89 .isGeneral = true,
90 .permissionName = "ohos.permission.GET_NETWORK_INFO",
91 .resDeviceID = { "local" },
92 };
93
94 PermissionDef testSimPermWriteContactsDef = {
95 .permissionName = "ohos.permission.WRITE_CONTACTS",
96 .bundleName = "tel_core_service_gtest",
97 .grantMode = 1, // SYSTEM_GRANT
98 .label = "label",
99 .labelId = 1,
100 .description = "Test core service",
101 .descriptionId = 1,
102 .availableLevel = APL_SYSTEM_BASIC,
103 };
104
105 PermissionStateFull testSimPermWriteContacts = {
106 .grantFlags = { 2 }, // PERMISSION_USER_SET
107 .grantStatus = { PermissionState::PERMISSION_GRANTED },
108 .isGeneral = true,
109 .permissionName = "ohos.permission.WRITE_CONTACTS",
110 .resDeviceID = { "local" },
111 };
112
113 PermissionDef testSimPermReadContactsDef = {
114 .permissionName = "ohos.permission.READ_CONTACTS",
115 .bundleName = "tel_core_service_gtest",
116 .grantMode = 1, // SYSTEM_GRANT
117 .label = "label",
118 .labelId = 1,
119 .description = "Test core service",
120 .descriptionId = 1,
121 .availableLevel = APL_SYSTEM_BASIC,
122 };
123
124 PermissionStateFull testSimPermReadContacts = {
125 .grantFlags = { 2 }, // PERMISSION_USER_SET
126 .grantStatus = { PermissionState::PERMISSION_GRANTED },
127 .isGeneral = true,
128 .permissionName = "ohos.permission.READ_CONTACTS",
129 .resDeviceID = { "local" },
130 };
131
132 HapPolicyParams testPolicyParams = {
133 .apl = APL_SYSTEM_BASIC,
134 .domain = "test.domain",
135 .permList = { testPermGetTelephonyStateDef, testPermSetTelephonyStateDef, testPermGetNetworkInfoDef,
136 testSimPermWriteContactsDef, testSimPermReadContactsDef },
137 .permStateList = { testGetTelephonyState, testSetTelephonyState, testPermGetNetworkInfo, testSimPermWriteContacts,
138 testSimPermReadContacts },
139 };
140 } // namespace
141
SecurityToken()142 SecurityToken::SecurityToken()
143 {
144 currentID_ = GetSelfTokenID();
145 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
146 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
147 SetSelfTokenID(tokenIdEx.tokenIDEx);
148 }
149
~SecurityToken()150 SecurityToken::~SecurityToken()
151 {
152 AccessTokenKit::DeleteToken(accessID_);
153 SetSelfTokenID(currentID_);
154 }
155 } // namespace Telephony
156 } // namespace OHOS