1 /* 2 * Copyright (C) 2021-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 #ifndef TELEPHONY_TOKEN_H 17 #define TELEPHONY_TOKEN_H 18 19 #include "accesstoken_kit.h" 20 #include "token_setproc.h" 21 22 namespace OHOS { 23 namespace Telephony { 24 using namespace Security::AccessToken; 25 using Security::AccessToken::AccessTokenID; 26 27 inline HapInfoParams testInfoParams = { 28 .bundleName = "tel_cellular_call_ims_gtest", 29 .userID = 1, 30 .instIndex = 0, 31 .appIDDesc = "test", 32 .isSystemApp = true, 33 }; 34 35 inline PermissionDef testConnectImsServiceDef = { 36 .permissionName = "ohos.permission.CONNECT_IMS_SERVICE", 37 .bundleName = "tel_cellular_call_ims_gtest", 38 .grantMode = 1, // SYSTEM_GRANT 39 .label = "label", 40 .labelId = 1, 41 .description = "Test cellular call", 42 .descriptionId = 1, 43 .availableLevel = APL_SYSTEM_BASIC, 44 }; 45 46 inline PermissionStateFull testConnectImsServiceState = { 47 .grantFlags = { 2 }, // PERMISSION_USER_SET 48 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 49 .isGeneral = true, 50 .permissionName = "ohos.permission.CONNECT_IMS_SERVICE", 51 .resDeviceID = { "local" }, 52 }; 53 54 inline PermissionDef testPermPlaceCallDef = { 55 .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE", 56 .bundleName = "tel_cellular_call_ims_gtest", 57 .grantMode = 1, // SYSTEM_GRANT 58 .label = "label", 59 .labelId = 1, 60 .description = "Test cellular call", 61 .descriptionId = 1, 62 .availableLevel = APL_SYSTEM_BASIC, 63 }; 64 65 inline PermissionStateFull testPlaceCallState = { 66 .grantFlags = { 2 }, // PERMISSION_USER_SET 67 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 68 .isGeneral = true, 69 .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE", 70 .resDeviceID = { "local" }, 71 }; 72 73 inline PermissionDef testGetTelephonyStateDef = { 74 .permissionName = "ohos.permission.GET_TELEPHONY_STATE", 75 .bundleName = "tel_cellular_call_ims_gtest", 76 .grantMode = 1, // SYSTEM_GRANT 77 .label = "label", 78 .labelId = 1, 79 .description = "Test cellular call", 80 .descriptionId = 1, 81 .availableLevel = APL_SYSTEM_BASIC, 82 }; 83 84 inline PermissionStateFull testGetTelephonyState = { 85 .grantFlags = { 2 }, // PERMISSION_USER_SET 86 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 87 .isGeneral = true, 88 .permissionName = "ohos.permission.GET_TELEPHONY_STATE", 89 .resDeviceID = { "local" }, 90 }; 91 92 inline HapPolicyParams testPolicyParams = { 93 .apl = APL_SYSTEM_BASIC, 94 .domain = "test.domain", 95 .permList = { testPermPlaceCallDef, testConnectImsServiceDef, testGetTelephonyStateDef }, 96 .permStateList = { testPlaceCallState, testConnectImsServiceState, testGetTelephonyState }, 97 }; 98 99 class AccessToken { 100 public: AccessToken()101 AccessToken() 102 { 103 currentID_ = GetSelfTokenID(); 104 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams); 105 accessID_ = tokenIdEx.tokenIdExStruct.tokenID; 106 SetSelfTokenID(tokenIdEx.tokenIDEx); 107 } ~AccessToken()108 ~AccessToken() 109 { 110 AccessTokenKit::DeleteToken(accessID_); 111 SetSelfTokenID(currentID_); 112 } 113 114 private: 115 AccessTokenID currentID_ = 0; 116 AccessTokenID accessID_ = 0; 117 }; 118 } // namespace Telephony 119 } // namespace OHOS 120 121 #endif // TELEPHONY_TOKEN_H 122