1 /*
2 * Copyright (C) 2024 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 <iostream>
17 #include <memory>
18 #include <securec.h>
19 #include <stack>
20 #include <string_ex.h>
21 #include <vector>
22
23 #define private public
24
25 #include "accesstoken_kit.h"
26 #include "cellular_data_client.h"
27 #include "core_service_client.h"
28 #include "core_service_test_code.h"
29 #include "ims_reg_info_callback_gtest.h"
30 #include "state_registry_observer.h"
31 #include "system_ability_definition.h"
32 #include "telephony_errors.h"
33 #include "telephony_observer_client.h"
34 #include "token_setproc.h"
35
36 namespace OHOS {
37 namespace Telephony {
38 using namespace OHOS::Security::AccessToken;
39 using OHOS::Security::AccessToken::AccessTokenID;
40 constexpr int16_t SIM1_SLOTID = 0;
41 constexpr int16_t DEFAULT_VALUE = 0;
42
43 enum class CallManagerInterfaceType {
44 REGISTER_CALLBACK_TYPE = 1,
45 INTERFACE_BLUETOOTH_CALL_TYPE,
46 };
47
48 CoreServiceClient &g_coreServiceClientPtr = CoreServiceClient::GetInstance();
49 CellularDataClient &g_cellularDataClient = CellularDataClient::GetInstance();
50 TelephonyObserverClient &g_observerDataClient = TelephonyObserverClient::GetInstance();
51 using CoreServiceFunc = void (*)();
52 std::map<uint32_t, CoreServiceFunc> g_memberFuncMap;
53 std::vector<AccessTokenIDEx> simAccountCallbackTokenIDVec_;
54 std::vector<AccessTokenIDEx> imsCallbackTokenIDVec_;
55 std::vector<AccessTokenIDEx> stateObserverTokenIDVec_;
56 AccessTokenID currentThreadTokenId_ = 0;
57 AccessTokenIDEx currentTokenIDEx_;
58
59 HapInfoParams testInfoParams = {
60 .userID = 1,
61 .bundleName = "tel_core_service_ui_test",
62 .instIndex = 0,
63 .appIDDesc = "test",
64 .isSystemApp = true,
65 };
66
67 PermissionDef testPermSetTelephonyStateDef = {
68 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
69 .bundleName = "tel_core_service_ui_test",
70 .grantMode = 1, // SYSTEM_GRANT
71 .availableLevel = APL_SYSTEM_BASIC,
72 .label = "label",
73 .labelId = 1,
74 .description = "Test call maneger",
75 .descriptionId = 1,
76 };
77
78 PermissionStateFull testSetTelephonyState = {
79 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
80 .isGeneral = true,
81 .resDeviceID = { "local" },
82 .grantStatus = { PermissionState::PERMISSION_GRANTED },
83 .grantFlags = { 2 }, // PERMISSION_USER_SET
84 };
85
86 PermissionDef testPermGetTelephonyStateDef = {
87 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
88 .bundleName = "tel_core_service_ui_test",
89 .grantMode = 1, // SYSTEM_GRANT
90 .availableLevel = APL_SYSTEM_BASIC,
91 .label = "label",
92 .labelId = 1,
93 .description = "Test call maneger",
94 .descriptionId = 1,
95 };
96
97 PermissionStateFull testGetTelephonyState = {
98 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
99 .isGeneral = true,
100 .resDeviceID = { "local" },
101 .grantStatus = { PermissionState::PERMISSION_GRANTED },
102 .grantFlags = { 2 }, // PERMISSION_USER_SET
103 };
104
105 PermissionDef testNetPermGetNetworkInfoDef = {
106 .permissionName = "ohos.permission.GET_NETWORK_INFO",
107 .bundleName = "tel_state_registry_test",
108 .grantMode = 1, // SYSTEM_GRANT
109 .availableLevel = APL_SYSTEM_BASIC,
110 .label = "label",
111 .labelId = 1,
112 .description = "Test state registry",
113 .descriptionId = 1,
114 };
115
116 PermissionStateFull testNetPermGetNetworkInfo = {
117 .permissionName = "ohos.permission.GET_NETWORK_INFO",
118 .isGeneral = true,
119 .resDeviceID = { "local" },
120 .grantStatus = { PermissionState::PERMISSION_GRANTED },
121 .grantFlags = { 2 }, // PERMISSION_USER_SET
122 };
123
124 HapPolicyParams testPolicyParams = {
125 .apl = APL_SYSTEM_BASIC,
126 .domain = "test.domain",
127 .permList = { testPermSetTelephonyStateDef, testPermGetTelephonyStateDef, testNetPermGetNetworkInfoDef },
128 .permStateList = { testSetTelephonyState, testGetTelephonyState, testNetPermGetNetworkInfo },
129 };
130
131 class AccessToken {
132 public:
AccessToken()133 AccessToken()
134 {
135 currentID_ = GetSelfTokenID();
136 currentThreadTokenId_ = currentID_;
137 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
138 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
139 SetSelfTokenID(tokenIdEx.tokenIDEx);
140 std::cout << "Current tokenId is: " << accessID_ << std::endl;
141 }
~AccessToken()142 ~AccessToken()
143 {
144 AccessTokenKit::DeleteToken(accessID_);
145 SetSelfTokenID(currentID_);
146 std::cout << "currentID_ tokenId is: " << currentID_ << std::endl;
147 }
148
149 private:
150 AccessTokenID currentID_ = 0;
151 AccessTokenID accessID_ = 0;
152 };
153
RegisterSimAccountCallback()154 void RegisterSimAccountCallback()
155 {
156 g_cellularDataClient.RegisterSimAccountCallback();
157 std::cout << "RegisterSimAccountCallback success!" << std::endl;
158 g_cellularDataClient.registerStatus_ = false;
159 simAccountCallbackTokenIDVec_.push_back(currentTokenIDEx_);
160 }
161
UnRegisterSimAccountCallback()162 void UnRegisterSimAccountCallback()
163 {
164 if (simAccountCallbackTokenIDVec_.empty()) {
165 std::cout << "no callback need to unregister!";
166 return;
167 }
168 std::cout << "Callback list: \n";
169 for (int32_t i = 0; i < simAccountCallbackTokenIDVec_.size(); i++) {
170 SetSelfTokenID(simAccountCallbackTokenIDVec_[i].tokenIDEx);
171 std::cout << i << ": tokenId is " << GetSelfTokenID() << "\n";
172 }
173 std::cout << "Please select tokeId which you need unregister...\n";
174 int32_t index = 0;
175 std::cin >> index;
176 AccessTokenIDEx currentIDEx = simAccountCallbackTokenIDVec_[index];
177 SetSelfTokenID(currentIDEx.tokenIDEx);
178 std::cout << "Selected tokenId is " << GetSelfTokenID() << std::endl;
179 g_cellularDataClient.UnregisterSimAccountCallback();
180 simAccountCallbackTokenIDVec_.erase(simAccountCallbackTokenIDVec_.begin() + index);
181 AccessTokenKit::DeleteToken(currentIDEx.tokenIdExStruct.tokenID);
182 SetSelfTokenID(currentThreadTokenId_);
183 std::cout << "UnRegisterSimAccountCallback success!" << std::endl;
184 }
185
RegisterImsRegInfoCallback()186 void RegisterImsRegInfoCallback()
187 {
188 int32_t ret = TELEPHONY_SUCCESS;
189 sptr<ImsRegInfoCallback> imsRegInfoCallback = new ImsRegInfoCallbackGtest();
190 ret = g_coreServiceClientPtr.RegisterImsRegInfoCallback(
191 SIM1_SLOTID, ImsServiceType::TYPE_VOICE, imsRegInfoCallback);
192 if (ret == TELEPHONY_SUCCESS) {
193 std::cout << "RegisterImsRegInfoCallback success!" << std::endl;
194 imsCallbackTokenIDVec_.push_back(currentTokenIDEx_);
195 return;
196 }
197 std::cout << "RegisterImsRegInfoCallback fail!" << std::endl;
198 }
199
UnRegisterImsRegInfoCallback()200 void UnRegisterImsRegInfoCallback()
201 {
202 if (imsCallbackTokenIDVec_.empty()) {
203 std::cout << "no callback need to unregister!";
204 return;
205 }
206 int32_t ret = TELEPHONY_SUCCESS;
207 std::cout << "Callback list: \n";
208 for (int32_t i = 0; i < imsCallbackTokenIDVec_.size(); i++) {
209 SetSelfTokenID(imsCallbackTokenIDVec_[i].tokenIDEx);
210 std::cout << i << ": tokenId is " << GetSelfTokenID() << "\n";
211 }
212 std::cout << "Please select tokeId which you need unregister...\n";
213 int32_t index = 0;
214 std::cin >> index;
215 AccessTokenIDEx currentIDEx = imsCallbackTokenIDVec_[index];
216 SetSelfTokenID(currentIDEx.tokenIDEx);
217 std::cout << "Selected tokenId is " << GetSelfTokenID() << std::endl;
218 ret = g_coreServiceClientPtr.UnregisterImsRegInfoCallback(SIM1_SLOTID, ImsServiceType::TYPE_VOICE);
219 if (ret == TELEPHONY_SUCCESS) {
220 imsCallbackTokenIDVec_.erase(imsCallbackTokenIDVec_.begin() + index);
221 AccessTokenKit::DeleteToken(currentIDEx.tokenIdExStruct.tokenID);
222 SetSelfTokenID(currentThreadTokenId_);
223 std::cout << "UnRegisterImsRegInfoCallback success!" << std::endl;
224 return;
225 }
226 std::cout << "UnRegisterImsRegInfoCallback fail!" << std::endl;
227 }
228
AddStateObserver()229 void AddStateObserver()
230 {
231 int32_t ret = TELEPHONY_SUCCESS;
232 sptr<StateRegistryObserver> telephonyObserver = std::make_unique<StateRegistryObserver>().release();
233 ret = g_observerDataClient.AddStateObserver(
234 telephonyObserver, SIM1_SLOTID, Telephony::TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE, true);
235 if (ret == TELEPHONY_SUCCESS) {
236 std::cout << "AddStateObserver success!" << std::endl;
237 stateObserverTokenIDVec_.push_back(currentTokenIDEx_);
238 return;
239 }
240 std::cout << "AddStateObserver fail!" << std::endl;
241 }
242
RemoveStateObserver()243 void RemoveStateObserver()
244 {
245 if (stateObserverTokenIDVec_.empty()) {
246 std::cout << "no callback need to unregister!";
247 return;
248 }
249 int32_t ret = TELEPHONY_SUCCESS;
250 std::cout << "Callback list: \n";
251 for (int32_t i = 0; i < stateObserverTokenIDVec_.size(); i++) {
252 SetSelfTokenID(stateObserverTokenIDVec_[i].tokenIDEx);
253 std::cout << i << ": tokenId is " << GetSelfTokenID() << "\n";
254 }
255 std::cout << "Please select tokeId which you need unregister...\n";
256 int32_t index = 0;
257 std::cin >> index;
258 AccessTokenIDEx currentIDEx = stateObserverTokenIDVec_[index];
259 SetSelfTokenID(currentIDEx.tokenIDEx);
260 std::cout << "Selected tokenId is " << GetSelfTokenID() << std::endl;
261 ret = g_observerDataClient.RemoveStateObserver(
262 SIM1_SLOTID, Telephony::TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE);
263 if (ret == TELEPHONY_SUCCESS) {
264 stateObserverTokenIDVec_.erase(stateObserverTokenIDVec_.begin() + index);
265 AccessTokenKit::DeleteToken(currentIDEx.tokenIdExStruct.tokenID);
266 SetSelfTokenID(currentThreadTokenId_);
267 std::cout << "RemoveStateObserver success!" << std::endl;
268 return;
269 }
270 std::cout << "RemoveStateObserver fail!" << std::endl;
271 }
272
InitCallBasicPower()273 void InitCallBasicPower()
274 {
275 g_memberFuncMap[static_cast<int32_t>(CoreServiceTestCode::REGISTER_SIM_ACCOUNT_CODE)] =
276 &OHOS::Telephony::RegisterSimAccountCallback;
277 g_memberFuncMap[static_cast<int32_t>(CoreServiceTestCode::UNREGISTER_SIM_ACCOUNT_CODE)] =
278 &OHOS::Telephony::UnRegisterSimAccountCallback;
279 g_memberFuncMap[static_cast<int32_t>(CoreServiceTestCode::REGISTER_IMS_REG_CODE)] =
280 &OHOS::Telephony::RegisterImsRegInfoCallback;
281 g_memberFuncMap[static_cast<int32_t>(CoreServiceTestCode::UNREGISTER_IMS_REG_CODE)] =
282 &OHOS::Telephony::UnRegisterImsRegInfoCallback;
283 g_memberFuncMap[static_cast<int32_t>(CoreServiceTestCode::ADD_STATE_OBSERVER)] = &OHOS::Telephony::AddStateObserver;
284 g_memberFuncMap[static_cast<int32_t>(CoreServiceTestCode::REMOVE_STATE_OBSERVER)] =
285 &OHOS::Telephony::RemoveStateObserver;
286 }
287
Init()288 int32_t Init()
289 {
290 if (g_coreServiceClientPtr.GetProxy() == nullptr) {
291 std::cout << "\n--- telephonyService == nullptr\n" << std::endl;
292 return 0;
293 }
294 if (g_cellularDataClient.GetProxy() == nullptr) {
295 std::cout << "\n--- telephonyService == nullptr\n" << std::endl;
296 return 0;
297 }
298 if (g_observerDataClient.GetProxy() == nullptr) {
299 std::cout << "\n--- telephonyService == nullptr\n" << std::endl;
300 return 0;
301 }
302 InitCallBasicPower();
303 return TELEPHONY_SUCCESS;
304 }
305
PrintfCallBasisInterface()306 void PrintfCallBasisInterface()
307 {
308 std::cout << "\n\n-----------start--------------\n"
309 << "usage:please input a cmd num:\n"
310 << "1:register sim account callback\n"
311 << "2:unregister sim account callback\n"
312 << "3:register ims reg callback\n"
313 << "4:unregister ims reg callback\n"
314 << "5:add state observer\n"
315 << "6:remove state observer\n";
316 }
317
PrintfUsage()318 void PrintfUsage()
319 {
320 PrintfCallBasisInterface();
321 std::cout << "1000:exit\n";
322 }
323
MainExit()324 int32_t MainExit()
325 {
326 if (!simAccountCallbackTokenIDVec_.empty()) {
327 std::vector<AccessTokenIDEx>::iterator it = simAccountCallbackTokenIDVec_.begin();
328 while (it != simAccountCallbackTokenIDVec_.end()) {
329 simAccountCallbackTokenIDVec_.erase(it++);
330 }
331 }
332 if (!imsCallbackTokenIDVec_.empty()) {
333 std::vector<AccessTokenIDEx>::iterator it = imsCallbackTokenIDVec_.begin();
334 while (it != imsCallbackTokenIDVec_.end()) {
335 imsCallbackTokenIDVec_.erase(it++);
336 }
337 }
338 if (!stateObserverTokenIDVec_.empty()) {
339 std::vector<AccessTokenIDEx>::iterator it = stateObserverTokenIDVec_.begin();
340 while (it != stateObserverTokenIDVec_.end()) {
341 stateObserverTokenIDVec_.erase(it++);
342 }
343 }
344 OHOS::Telephony::g_memberFuncMap.clear();
345 std::cout << "exit success" << std::endl;
346 return OHOS::Telephony::TELEPHONY_SUCCESS;
347 }
348
RunTest()349 int32_t RunTest()
350 {
351 std::cout << "coreService test start...." << std::endl;
352 int32_t interfaceNum = DEFAULT_VALUE;
353 const int32_t exitNumber = 1000;
354 if (Init() != TELEPHONY_SUCCESS) {
355 std::cout << "coreService test init failed!" << std::endl;
356 return TELEPHONY_SUCCESS;
357 }
358 AccessToken token;
359 while (true) {
360 PrintfUsage();
361 std::cin >> interfaceNum;
362 if (interfaceNum == static_cast<int32_t>(CoreServiceTestCode::REGISTER_SIM_ACCOUNT_CODE) ||
363 interfaceNum == static_cast<int32_t>(CoreServiceTestCode::REGISTER_IMS_REG_CODE) ||
364 interfaceNum == static_cast<int32_t>(CoreServiceTestCode::ADD_STATE_OBSERVER)) {
365 std::cout << "Please input instIndex..." << std::endl;
366 int32_t index = DEFAULT_VALUE;
367 std::cin >> index;
368 testInfoParams.instIndex = index;
369 std::cout << "Please input userId..." << std::endl;
370 int32_t userId = DEFAULT_VALUE;
371 std::cin >> userId;
372 testInfoParams.userID = userId;
373 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
374 currentTokenIDEx_ = tokenIdEx;
375 SetSelfTokenID(tokenIdEx.tokenIDEx);
376 std::cout << "Current userId is: " << testInfoParams.userID << std::endl
377 << "bundleName is: " << testInfoParams.bundleName << std::endl
378 << "instIndex is: " << testInfoParams.instIndex << std::endl
379 << "tokenId is: " << GetSelfTokenID() << std::endl;
380 }
381 if (interfaceNum == exitNumber) {
382 std::cout << "start to exit now...." << std::endl;
383 break;
384 }
385 auto itFunc = g_memberFuncMap.find(interfaceNum);
386 if (itFunc != g_memberFuncMap.end() && itFunc->second != nullptr) {
387 auto memberFunc = itFunc->second;
388 (*memberFunc)();
389 continue;
390 }
391 std::cout << "err: invalid input!" << std::endl;
392 break;
393 }
394 return MainExit();
395 }
396 } // namespace Telephony
397 } // namespace OHOS
398
main()399 int32_t main()
400 {
401 int32_t code = OHOS::Telephony::DEFAULT_VALUE;
402 const int32_t exitCode = 1000;
403 std::cout << "Please select test type...." << std::endl;
404 while (true) {
405 std::cout << "1: Register callback\n"
406 << "1000:exit\n";
407 std::cin >> code;
408 if (code == static_cast<int32_t>(OHOS::Telephony::CallManagerInterfaceType::REGISTER_CALLBACK_TYPE)) {
409 OHOS::Telephony::RunTest();
410 OHOS::Telephony::MainExit();
411 } else if (code == exitCode) {
412 break;
413 }
414 std::cout << "err: invalid input!" << std::endl;
415 }
416 return OHOS::Telephony::TELEPHONY_SUCCESS;
417 }
418