1 /* 2 * Copyright (C) 2021 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 STATE_REGISTER_TEST_H 17 #define STATE_REGISTER_TEST_H 18 19 #include <iostream> 20 #include <list> 21 #include <securec.h> 22 23 #include "accesstoken_kit.h" 24 #include "gtest/gtest.h" 25 #include "i_telephony_state_notify.h" 26 #include "if_system_ability_manager.h" 27 #include "iservice_registry.h" 28 #include "state_registry_errors.h" 29 #include "state_registry_observer.h" 30 #include "string_ex.h" 31 #include "system_ability_definition.h" 32 #include "telephony_types.h" 33 #include "token_setproc.h" 34 35 namespace OHOS { 36 namespace Telephony { 37 using namespace Security::AccessToken; 38 using namespace testing::ext; 39 using Security::AccessToken::AccessTokenID; 40 41 HapInfoParams testStateRegistryParams = { 42 .bundleName = "tel_state_registry_test", 43 .userID = 1, 44 .instIndex = 0, 45 .appIDDesc = "test", 46 .isSystemApp = true, 47 }; 48 49 PermissionDef testNetPermGetNetworkInfoDef = { 50 .permissionName = "ohos.permission.GET_NETWORK_INFO", 51 .bundleName = "tel_state_registry_test", 52 .grantMode = 1, // SYSTEM_GRANT 53 .label = "label", 54 .labelId = 1, 55 .description = "Test state registry", 56 .descriptionId = 1, 57 .availableLevel = APL_SYSTEM_BASIC, 58 }; 59 60 PermissionStateFull testNetPermGetNetworkInfo = { 61 .grantFlags = { 2 }, // PERMISSION_USER_SET 62 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 63 .isGeneral = true, 64 .permissionName = "ohos.permission.GET_NETWORK_INFO", 65 .resDeviceID = { "local" }, 66 }; 67 68 PermissionDef testNetPermSetTelephonyStateDef = { 69 .permissionName = "ohos.permission.SET_TELEPHONY_STATE", 70 .bundleName = "tel_state_registry_test", 71 .grantMode = 1, // SYSTEM_GRANT 72 .label = "label", 73 .labelId = 1, 74 .description = "Test state registry", 75 .descriptionId = 1, 76 .availableLevel = APL_SYSTEM_BASIC, 77 }; 78 79 PermissionStateFull testNetSetTelephonyState = { 80 .grantFlags = { 2 }, // PERMISSION_USER_SET 81 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 82 .isGeneral = true, 83 .permissionName = "ohos.permission.SET_TELEPHONY_STATE", 84 .resDeviceID = { "local" }, 85 }; 86 87 PermissionDef testNetPermGetTelephonyStateDef = { 88 .permissionName = "ohos.permission.GET_TELEPHONY_STATE", 89 .bundleName = "tel_state_registry_test", 90 .grantMode = 1, // SYSTEM_GRANT 91 .label = "label", 92 .labelId = 1, 93 .description = "Test state registry", 94 .descriptionId = 1, 95 .availableLevel = APL_SYSTEM_BASIC, 96 }; 97 98 PermissionStateFull testNetGetTelephonyState = { 99 .grantFlags = { 2 }, // PERMISSION_USER_SET 100 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 101 .isGeneral = true, 102 .permissionName = "ohos.permission.GET_TELEPHONY_STATE", 103 .resDeviceID = { "local" }, 104 }; 105 106 PermissionDef testNetPermLocationDef = { 107 .permissionName = "ohos.permission.LOCATION", 108 .bundleName = "tel_state_registry_test", 109 .grantMode = 1, // SYSTEM_GRANT 110 .label = "label", 111 .labelId = 1, 112 .description = "Test state registry", 113 .descriptionId = 1, 114 .availableLevel = APL_SYSTEM_BASIC, 115 }; 116 117 PermissionStateFull testNetPermLocation = { 118 .grantFlags = { 2 }, // PERMISSION_USER_SET 119 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 120 .isGeneral = true, 121 .permissionName = "ohos.permission.LOCATION", 122 .resDeviceID = { "local" }, 123 }; 124 125 PermissionDef testNetPermReadCallLogDef = { 126 .permissionName = "ohos.permission.READ_CALL_LOG", 127 .bundleName = "tel_state_registry_test", 128 .grantMode = 1, // SYSTEM_GRANT 129 .label = "label", 130 .labelId = 1, 131 .description = "Test state registry", 132 .descriptionId = 1, 133 .availableLevel = APL_SYSTEM_BASIC, 134 }; 135 136 PermissionStateFull testNetPermReadCallLog = { 137 .grantFlags = { 2 }, // PERMISSION_USER_SET 138 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 139 .isGeneral = true, 140 .permissionName = "ohos.permission.READ_CALL_LOG", 141 .resDeviceID = { "local" }, 142 }; 143 144 HapPolicyParams testPolicyParams = { 145 .apl = APL_SYSTEM_BASIC, 146 .domain = "test.domain", 147 .permList = { testNetPermGetNetworkInfoDef, testNetPermSetTelephonyStateDef, testNetPermGetTelephonyStateDef, 148 testNetPermLocationDef, testNetPermReadCallLogDef }, 149 .permStateList = { testNetPermGetNetworkInfo, testNetSetTelephonyState, testNetGetTelephonyState, 150 testNetPermLocation, testNetPermReadCallLog }, 151 }; 152 153 class AccessToken { 154 public: AccessToken()155 AccessToken() 156 { 157 currentID_ = GetSelfTokenID(); 158 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testStateRegistryParams, testPolicyParams); 159 accessID_ = tokenIdEx.tokenIdExStruct.tokenID; 160 SetSelfTokenID(tokenIdEx.tokenIDEx); 161 } ~AccessToken()162 ~AccessToken() 163 { 164 AccessTokenKit::DeleteToken(accessID_); 165 SetSelfTokenID(currentID_); 166 } 167 168 private: 169 AccessTokenID currentID_ = 0; 170 AccessTokenID accessID_ = 0; 171 }; 172 173 class StateRegistryTest : public testing::Test { 174 public: 175 static void SetUpTestCase(); 176 static void TearDownTestCase(); 177 void SetUp(); 178 void TearDown(); 179 void CreateProxy(); 180 static bool HasSimCard(int32_t slotId); 181 182 void UpdateCallState(int32_t slotId); 183 void UpdateCallStateForSlotId(int32_t slotId); 184 void UpdateSignalInfo(int32_t slotId); 185 void UpdateCellularDataConnectState(int32_t slotId); 186 void UpdateCellularDataFlow(int32_t slotId); 187 void UpdateSimState(int32_t slotId); 188 void UpdateNetworkState(int32_t slotId); 189 void UpdateCfuIndicator(int32_t slotId); 190 void UpdateVoiceMailMsgIndicator(int32_t slotId); 191 void UpdateIccAccount(); 192 193 public: 194 using RequestFuncType = void (StateRegistryTest::*)(int32_t slotId); 195 std::map<char, RequestFuncType> requestFuncMap_; 196 197 private: 198 static void InitTelephonyObserver(); 199 static void DisableTelephonyObserver(); 200 201 private: 202 static sptr<StateRegistryObserver> telephonyObserver0_; 203 static sptr<StateRegistryObserver> telephonyObserver1_; 204 TelephonyObserver telephonyObserver; 205 }; 206 } // namespace Telephony 207 } // namespace OHOS 208 #endif // STATE_REGISTER_TEST_H