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 #ifndef SMS_MMS_GTEST_H 16 #define SMS_MMS_GTEST_H 17 18 #include "accesstoken_kit.h" 19 #include "telephony_log_wrapper.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 class AccessMmsToken { 28 HapInfoParams testMmsInfoParams = { 29 .userID = 1, 30 .bundleName = "tel_sms_mms_gtest", 31 .instIndex = 0, 32 .appIDDesc = "test", 33 .isSystemApp = true, 34 }; 35 PermissionDef testPermReceiveSmsDef = { 36 .permissionName = "ohos.permission.RECEIVE_SMS", 37 .bundleName = "tel_sms_mms_gtest", 38 .grantMode = 1, // SYSTEM_GRANT 39 .availableLevel = APL_SYSTEM_BASIC, 40 .label = "label", 41 .labelId = 1, 42 .description = "Test sms manager", 43 .descriptionId = 1, 44 }; 45 PermissionStateFull testReceiveSmsState = { 46 .permissionName = "ohos.permission.RECEIVE_SMS", 47 .isGeneral = true, 48 .resDeviceID = { "local" }, 49 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 50 .grantFlags = { 2 }, // PERMISSION_USER_SET 51 }; 52 PermissionDef testPermSendSmsDef = { 53 .permissionName = "ohos.permission.SEND_MESSAGES", 54 .bundleName = "tel_sms_mms_gtest", 55 .grantMode = 1, // SYSTEM_GRANT 56 .availableLevel = APL_SYSTEM_BASIC, 57 .label = "label", 58 .labelId = 1, 59 .description = "Test sms manager", 60 .descriptionId = 1, 61 }; 62 PermissionStateFull testSendSmsState = { 63 .permissionName = "ohos.permission.SEND_MESSAGES", 64 .isGeneral = true, 65 .resDeviceID = { "local" }, 66 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 67 .grantFlags = { 2 }, // PERMISSION_USER_SET 68 }; 69 PermissionDef testPermSetTelephonyDef = { 70 .permissionName = "ohos.permission.SET_TELEPHONY_STATE", 71 .bundleName = "tel_sms_mms_gtest", 72 .grantMode = 1, // SYSTEM_GRANT 73 .availableLevel = APL_SYSTEM_BASIC, 74 .label = "label", 75 .labelId = 1, 76 .description = "Test sms manager", 77 .descriptionId = 1, 78 }; 79 PermissionStateFull testSetTelephonyState = { 80 .permissionName = "ohos.permission.SET_TELEPHONY_STATE", 81 .isGeneral = true, 82 .resDeviceID = { "local" }, 83 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 84 .grantFlags = { 2 }, // PERMISSION_USER_SET 85 }; 86 PermissionDef testPermGetTelephonyDef = { 87 .permissionName = "ohos.permission.GET_TELEPHONY_STATE", 88 .bundleName = "tel_sms_mms_gtest", 89 .grantMode = 1, // SYSTEM_GRANT 90 .availableLevel = APL_SYSTEM_BASIC, 91 .label = "label", 92 .labelId = 1, 93 .description = "Test sms manager", 94 .descriptionId = 1, 95 }; 96 PermissionStateFull testGetTelephonyState = { 97 .permissionName = "ohos.permission.GET_TELEPHONY_STATE", 98 .isGeneral = true, 99 .resDeviceID = { "local" }, 100 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 101 .grantFlags = { 2 }, // PERMISSION_USER_SET 102 }; 103 HapPolicyParams testMmsPolicyParams = { 104 .apl = APL_SYSTEM_BASIC, 105 .domain = "test.domain", 106 .permList = { testPermReceiveSmsDef, testPermSendSmsDef, testPermSetTelephonyDef, testPermGetTelephonyDef }, 107 .permStateList = { testReceiveSmsState, testSendSmsState, testSetTelephonyState, testGetTelephonyState }, 108 }; 109 110 public: AccessMmsToken()111 AccessMmsToken() 112 { 113 currentID_ = GetSelfTokenID(); 114 TELEPHONY_LOGI("AccessMmsToken currentID_%{public}d", currentID_); 115 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testMmsInfoParams, testMmsPolicyParams); 116 accessID_ = tokenIdEx.tokenIdExStruct.tokenID; 117 SetSelfTokenID(tokenIdEx.tokenIDEx); 118 } ~AccessMmsToken()119 ~AccessMmsToken() 120 { 121 TELEPHONY_LOGI("AccessMmsToken ~AccessMmsToken"); 122 AccessTokenKit::DeleteToken(accessID_); 123 SetSelfTokenID(currentID_); 124 } 125 126 private: 127 AccessTokenID currentID_ = 0; 128 AccessTokenID accessID_ = 0; 129 }; 130 } // namespace Telephony 131 } // namespace OHOS 132 #endif