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 }; 47 48 PermissionDef testNetPermGetNetworkInfoDef = { 49 .permissionName = "ohos.permission.GET_NETWORK_INFO", 50 .bundleName = "tel_state_registry_test", 51 .grantMode = 1, // SYSTEM_GRANT 52 .label = "label", 53 .labelId = 1, 54 .description = "Test state registry", 55 .descriptionId = 1, 56 .availableLevel = APL_SYSTEM_BASIC, 57 }; 58 59 PermissionStateFull testNetPermGetNetworkInfo = { 60 .grantFlags = { 2 }, // PERMISSION_USER_SET 61 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 62 .isGeneral = true, 63 .permissionName = "ohos.permission.GET_NETWORK_INFO", 64 .resDeviceID = { "local" }, 65 }; 66 67 PermissionDef testNetPermSetTelephonyStateDef = { 68 .permissionName = "ohos.permission.SET_TELEPHONY_STATE", 69 .bundleName = "tel_state_registry_test", 70 .grantMode = 1, // SYSTEM_GRANT 71 .label = "label", 72 .labelId = 1, 73 .description = "Test state registry", 74 .descriptionId = 1, 75 .availableLevel = APL_SYSTEM_BASIC, 76 }; 77 78 PermissionStateFull testNetSetTelephonyState = { 79 .grantFlags = { 2 }, // PERMISSION_USER_SET 80 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 81 .isGeneral = true, 82 .permissionName = "ohos.permission.SET_TELEPHONY_STATE", 83 .resDeviceID = { "local" }, 84 }; 85 86 PermissionDef testNetPermLocationDef = { 87 .permissionName = "ohos.permission.LOCATION", 88 .bundleName = "tel_state_registry_test", 89 .grantMode = 1, // SYSTEM_GRANT 90 .label = "label", 91 .labelId = 1, 92 .description = "Test state registry", 93 .descriptionId = 1, 94 .availableLevel = APL_SYSTEM_BASIC, 95 }; 96 97 PermissionStateFull testNetPermLocation = { 98 .grantFlags = { 2 }, // PERMISSION_USER_SET 99 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 100 .isGeneral = true, 101 .permissionName = "ohos.permission.LOCATION", 102 .resDeviceID = { "local" }, 103 }; 104 105 PermissionDef testNetPermReadCallLogDef = { 106 .permissionName = "ohos.permission.READ_CALL_LOG", 107 .bundleName = "tel_state_registry_test", 108 .grantMode = 1, // SYSTEM_GRANT 109 .label = "label", 110 .labelId = 1, 111 .description = "Test state registry", 112 .descriptionId = 1, 113 .availableLevel = APL_SYSTEM_BASIC, 114 }; 115 116 PermissionStateFull testNetPermReadCallLog = { 117 .grantFlags = { 2 }, // PERMISSION_USER_SET 118 .grantStatus = { PermissionState::PERMISSION_GRANTED }, 119 .isGeneral = true, 120 .permissionName = "ohos.permission.READ_CALL_LOG", 121 .resDeviceID = { "local" }, 122 }; 123 124 HapPolicyParams testPolicyParams = { 125 .apl = APL_SYSTEM_BASIC, 126 .domain = "test.domain", 127 .permList = { testNetPermGetNetworkInfoDef, testNetPermSetTelephonyStateDef, testNetPermLocationDef, 128 testNetPermReadCallLogDef }, 129 .permStateList = { testNetPermGetNetworkInfo, testNetSetTelephonyState, testNetPermLocation, 130 testNetPermReadCallLog }, 131 }; 132 133 class AccessToken { 134 public: AccessToken()135 AccessToken() 136 { 137 currentID_ = GetSelfTokenID(); 138 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testStateRegistryParams, testPolicyParams); 139 accessID_ = tokenIdEx.tokenIdExStruct.tokenID; 140 SetSelfTokenID(accessID_); 141 } ~AccessToken()142 ~AccessToken() 143 { 144 AccessTokenKit::DeleteToken(accessID_); 145 SetSelfTokenID(currentID_); 146 } 147 148 private: 149 AccessTokenID currentID_ = 0; 150 AccessTokenID accessID_ = 0; 151 }; 152 153 class StateRegistryTest : public testing::Test { 154 public: 155 static void SetUpTestCase(); 156 static void TearDownTestCase(); 157 void SetUp(); 158 void TearDown(); 159 void CreateProxy(); 160 static bool HasSimCard(int32_t slotId); 161 162 void UpdateCallState(int32_t slotId); 163 void UpdateCallStateForSlotId(int32_t slotId); 164 void UpdateSignalInfo(int32_t slotId); 165 void UpdateCellularDataConnectState(int32_t slotId); 166 void UpdateCellularDataFlow(int32_t slotId); 167 void UpdateSimState(int32_t slotId); 168 void UpdateNetworkState(int32_t slotId); 169 170 public: 171 using RequestFuncType = void (StateRegistryTest::*)(int32_t slotId); 172 std::map<char, RequestFuncType> requestFuncMap_; 173 174 private: 175 static void InitTelephonyObserver(); 176 static void DisableTelephonyObserver(); 177 178 private: 179 static sptr<StateRegistryObserver> telephonyObserver0_; 180 static sptr<StateRegistryObserver> telephonyObserver1_; 181 TelephonyObserver telephonyObserver; 182 }; 183 } // namespace Telephony 184 } // namespace OHOS 185 #endif // STATE_REGISTER_TEST_H