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 #include <iostream>
17 #include <map>
18 #include <string>
19 #include <unistd.h>
20
21 #include "ability_context.h"
22 #include "accesstoken_kit.h"
23 #include "cellular_data_client.h"
24 #include "core_service_client.h"
25 #include "net_conn_callback_stub.h"
26 #include "net_conn_client.h"
27 #include "net_handle.h"
28 #include "net_specifier.h"
29 #include "telephony_types.h"
30 #include "token_setproc.h"
31
32 namespace OHOS {
33 namespace Telephony {
34 using namespace OHOS::Security::AccessToken;
35 using OHOS::Security::AccessToken::AccessTokenID;
36 using namespace OHOS::NetManagerStandard;
37
38 HapInfoParams testInfoParams = {
39 .bundleName = "tel_cellular_data_ui_test",
40 .userID = 1,
41 .instIndex = 0,
42 .appIDDesc = "test",
43 .isSystemApp = true,
44 };
45
46 PermissionDef testPermGetTelephonyStateDef = {
47 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
48 .bundleName = "tel_cellular_data_ui_test",
49 .grantMode = 1, // SYSTEM_GRANT
50 .label = "label",
51 .labelId = 1,
52 .description = "Test cellular data",
53 .descriptionId = 1,
54 .availableLevel = APL_SYSTEM_BASIC,
55 };
56
57 PermissionStateFull testGetTelephonyState = {
58 .grantFlags = { 2 }, // PERMISSION_USER_SET
59 .grantStatus = { PermissionState::PERMISSION_GRANTED },
60 .isGeneral = true,
61 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
62 .resDeviceID = { "local" },
63 };
64
65 PermissionDef testPermSetTelephonyStateDef = {
66 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
67 .bundleName = "tel_cellular_data_ui_test",
68 .grantMode = 1, // SYSTEM_GRANT
69 .label = "label",
70 .labelId = 1,
71 .description = "Test cellular data",
72 .descriptionId = 1,
73 .availableLevel = APL_SYSTEM_BASIC,
74 };
75
76 PermissionStateFull testSetTelephonyState = {
77 .grantFlags = { 2 }, // PERMISSION_USER_SET
78 .grantStatus = { PermissionState::PERMISSION_GRANTED },
79 .isGeneral = true,
80 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
81 .resDeviceID = { "local" },
82 };
83
84 PermissionDef testPermGetNetworkInfoDef = {
85 .permissionName = "ohos.permission.GET_NETWORK_INFO",
86 .bundleName = "tel_core_service_gtest",
87 .grantMode = 1, // SYSTEM_GRANT
88 .label = "label",
89 .labelId = 1,
90 .description = "Test cellular data",
91 .descriptionId = 1,
92 .availableLevel = APL_SYSTEM_BASIC,
93 };
94
95 PermissionStateFull testPermGetNetworkInfo = {
96 .grantFlags = { 2 }, // PERMISSION_USER_SET
97 .grantStatus = { PermissionState::PERMISSION_GRANTED },
98 .isGeneral = true,
99 .permissionName = "ohos.permission.GET_NETWORK_INFO",
100 .resDeviceID = { "local" },
101 };
102
103 HapPolicyParams testPolicyParams = {
104 .apl = APL_SYSTEM_BASIC,
105 .domain = "test.domain",
106 .permList = { testPermGetTelephonyStateDef, testPermSetTelephonyStateDef, testPermGetNetworkInfoDef },
107 .permStateList = { testGetTelephonyState, testSetTelephonyState, testPermGetNetworkInfo },
108 };
109
110 class AccessToken {
111 public:
AccessToken()112 AccessToken()
113 {
114 currentID_ = GetSelfTokenID();
115 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
116 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
117 SetSelfTokenID(tokenIdEx.tokenIDEx);
118 }
~AccessToken()119 ~AccessToken()
120 {
121 AccessTokenKit::DeleteToken(accessID_);
122 SetSelfTokenID(currentID_);
123 }
124
125 private:
126 AccessTokenID currentID_ = 0;
127 AccessTokenID accessID_ = 0;
128 };
129
130 class TestCallback : public NetManagerStandard::NetConnCallbackStub {
NetAvailable(sptr<NetManagerStandard::NetHandle> & netHandle)131 int32_t NetAvailable(sptr<NetManagerStandard::NetHandle> &netHandle) override
132 {
133 std::cout << "TestCallback::NetAvailable" << std::endl;
134 return 0;
135 }
136
NetCapabilitiesChange(sptr<NetManagerStandard::NetHandle> & netHandle,const sptr<NetManagerStandard::NetAllCapabilities> & netAllCap)137 int32_t NetCapabilitiesChange(sptr<NetManagerStandard::NetHandle> &netHandle,
138 const sptr<NetManagerStandard::NetAllCapabilities> &netAllCap) override
139 {
140 std::cout << "TestCallback::NetCapabilitiesChange" << std::endl;
141 return 0;
142 }
143
NetConnectionPropertiesChange(sptr<NetManagerStandard::NetHandle> & netHandle,const sptr<NetManagerStandard::NetLinkInfo> & info)144 int32_t NetConnectionPropertiesChange(
145 sptr<NetManagerStandard::NetHandle> &netHandle, const sptr<NetManagerStandard::NetLinkInfo> &info) override
146 {
147 std::cout << "TestCallback::NetConnectionPropertiesChange" << std::endl;
148 return 0;
149 }
150
NetLost(sptr<NetManagerStandard::NetHandle> & netHandle)151 int32_t NetLost(sptr<NetManagerStandard::NetHandle> &netHandle) override
152 {
153 std::cout << "TestCallback::NetLost" << std::endl;
154 return 0;
155 }
156
NetUnavailable()157 int32_t NetUnavailable() override
158 {
159 std::cout << "TestCallback::NetUnavailable" << std::endl;
160 return 0;
161 }
162
NetBlockStatusChange(sptr<NetManagerStandard::NetHandle> & netHandle,bool blocked)163 int32_t NetBlockStatusChange(sptr<NetManagerStandard::NetHandle> &netHandle, bool blocked) override
164 {
165 std::cout << "TestCallback::NetBlockStatusChange" << std::endl;
166 return 0;
167 }
168 };
169
170 static const int32_t IS_CELLULAR_DATA_ENABLED_TEST = 0;
171 static const int32_t ENABLE_CELLULAR_DATA_TEST = 1;
172 static const int32_t GET_CELLULAR_DATA_STATE_TEST = 2;
173 static const int32_t IS_DATA_ROAMING_ENABLED_TEST = 3;
174 static const int32_t ENABLE_DATA_ROAMING_TEST = 4;
175 static const int32_t APN_CHANGED_TEST = 5;
176 static const int32_t GET_DEFAULT_SLOT_ID = 6;
177 static const int32_t SET_DEFAULT_SLOT_ID = 7;
178 static const int32_t GET_DATA_FLOW_TYPE = 8;
179 static const int32_t TOUCH_OR_RM_DATA_PACKAGE = 9;
180 static const int32_t ACQUIRE_MMS_NETWORK = 10;
181 static const int32_t RELEASE_MMS_NETWORK = 11;
182 static const int32_t EXIT_CELLULAR_DATA_TEST = 1000;
183 static const int32_t TEST_DATA_PACKAGE_UP = 0;
184 static const int32_t TEST_DATA_PACKAGE_DOWN = 1;
185 static const int32_t TEST_DATA_PACKAGE_UP_DOWN = 2;
186 static const int32_t TEST_DATA_PACKAGE_EXIT = 100;
187 static const int32_t NET_REGISTER_TIMEOUT_MS = 20000;
188 static sptr<INetConnCallback> g_callback;
189
190 class CellularDataCodeTest {
191 using Fun = void (*)();
192
193 public:
194 CellularDataCodeTest() = default;
195
196 ~CellularDataCodeTest() = default;
197
IsCellularDataEnabledTest()198 static void IsCellularDataEnabledTest()
199 {
200 AccessToken token;
201 bool enabled = false;
202 int32_t result = CellularDataClient::GetInstance().IsCellularDataEnabled(enabled);
203 std::cout << "TelephonyTestService Remote IsCellularDataEnabled result [" << result << "]"
204 << " enabled [" << enabled << "]" << std::endl;
205 }
206
EnableCellularDataTest()207 static void EnableCellularDataTest()
208 {
209 AccessToken token;
210 const int32_t maxTestCount = 61;
211 const uint32_t spaceTime = 1000 * 100 * 2.6;
212 const int32_t useMaxType = 1;
213 const int32_t evenNumber = 2;
214 int32_t type = 0;
215 std::cout << "please input whether enable: enable/1,disable/0 " << std::endl;
216 std::cin >> type;
217 int32_t result = 0;
218 if (type > useMaxType) {
219 for (int32_t i = 1; i < maxTestCount; i++) {
220 bool dataEnable = i % evenNumber;
221 if (dataEnable) {
222 std::cout << "enable====: " << dataEnable << std::endl;
223 CellularDataClient::GetInstance().EnableCellularData(dataEnable);
224 usleep(spaceTime);
225 result = CellularDataClient::GetInstance().GetCellularDataState();
226 std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
227 } else {
228 std::cout << "enable false====: " << dataEnable << std::endl;
229 CellularDataClient::GetInstance().EnableCellularData(dataEnable);
230 usleep(spaceTime);
231 result = CellularDataClient::GetInstance().GetCellularDataState();
232 std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
233 }
234 std::cout << "i := " << i << std::endl;
235 }
236 return;
237 }
238 bool enable = (type > 0);
239 result = CellularDataClient::GetInstance().EnableCellularData(enable);
240 std::cout << "Remote EnableCellularData " << enable <<" result [" << result << "]" << std::endl;
241 }
242
GetCellularDataStateTest()243 static void GetCellularDataStateTest()
244 {
245 int32_t result = CellularDataClient::GetInstance().GetCellularDataState();
246 std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
247 }
248
IsCellularDataRoamingEnabledTest()249 static void IsCellularDataRoamingEnabledTest()
250 {
251 AccessToken token;
252 int32_t slotId = DEFAULT_SIM_SLOT_ID;
253 std::cout << "please input parameter int slotId" << std::endl;
254 std::cin >> slotId;
255 bool isDataRoaming = false;
256 int32_t result = CellularDataClient::GetInstance().IsCellularDataRoamingEnabled(slotId, isDataRoaming);
257 std::cout << "Remote IsCellularDataRoamingEnabled result [" << result << "]"
258 << " isDataRoaming [" << isDataRoaming << "]" << std::endl;
259 }
260
EnableCellularDataRoamingTest()261 static void EnableCellularDataRoamingTest()
262 {
263 AccessToken token;
264 int32_t slotId = DEFAULT_SIM_SLOT_ID;
265 int32_t type = 0;
266 std::cout << "please input parameter int slotId " << std::endl;
267 std::cin >> slotId;
268 std::cout << "please input parameter roaming enable: enable/1,disable/0" << std::endl;
269 std::cin >> type;
270 bool enable = (type > 0);
271 int32_t result = CellularDataClient::GetInstance().EnableCellularDataRoaming(slotId, enable);
272 std::cout << "Remote GetOperatorName " << enable << " result [" << result << "]" << std::endl;
273 }
274
HandleApnChangedTest()275 static void HandleApnChangedTest()
276 {
277 AccessToken token;
278 int32_t slotId = DEFAULT_SIM_SLOT_ID;
279 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
280 if (proxy != nullptr) {
281 int32_t result = proxy->HandleApnChanged(slotId);
282 std::cout << "TelephonyTestService Remote result [" << result << "]" << std::endl;
283 } else {
284 std::cout << "TelephonyTestService Remote is null" << std::endl;
285 }
286 }
287
GetDefaultSlotId()288 static void GetDefaultSlotId()
289 {
290 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
291 int32_t slotId;
292 if (proxy != nullptr) {
293 int32_t result = proxy->GetDefaultCellularDataSlotId(slotId);
294 std::cout << "get default slot id is := " << slotId << std::endl;
295 } else {
296 std::cout << "get default slot id fail" << std::endl;
297 }
298 }
299
SetDefaultSlotId()300 static void SetDefaultSlotId()
301 {
302 AccessToken token;
303 int32_t slotId = DEFAULT_SIM_SLOT_ID;
304 std::cout << "please input parameter int slot" << std::endl;
305 std::cin >> slotId;
306 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
307 if (proxy != nullptr) {
308 int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
309 std::cout << "TelephonyTestService SetDefaultSlotId is " << result << std::endl;
310 } else {
311 std::cout << "TelephonyTestService SetDefaultSlotId is null" << std::endl;
312 }
313 }
314
GetDataFlowType()315 static void GetDataFlowType()
316 {
317 int32_t type;
318 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
319 if (proxy != nullptr) {
320 int32_t result = proxy->GetCellularDataFlowType(type);
321 std::cout << "get CellDataFlowType is := " << type << std::endl;
322 } else {
323 std::cout << "get CellDataFlowType is fail" << std::endl;
324 }
325 }
326
TestDataPackageUp()327 static void TestDataPackageUp()
328 {
329 system("rm /data/iface_tx_p");
330 system("touch /data/iface_rx_p");
331 if (access("/data/iface_tx_p", F_OK) == 0) {
332 std::cout << "Please manually delete iface_tx_p file " << std::endl;
333 return;
334 }
335 if (access("/data/iface_tx_p", F_OK) != 0) {
336 std::cout << "Please manually touch iface_tx_p file" << std::endl;
337 }
338 }
339
TestDataPackageDown()340 static void TestDataPackageDown()
341 {
342 system("rm /data/iface_rx_p");
343 system("touch /data/iface_tx_p");
344 if (access("/data/iface_rx_p", F_OK) == 0) {
345 std::cout << "Please manually delete iface_rx_p file " << std::endl;
346 return;
347 }
348 if (access("/data/iface_tx_p", F_OK) != 0) {
349 std::cout << "Please manually touch iface_tx_p file" << std::endl;
350 }
351 }
352
TestDataPackageUpDown()353 static void TestDataPackageUpDown()
354 {
355 system("rm /data/iface_tx_p");
356 system("rm /data/iface_rx_p");
357 if (access("/data/iface_tx_p", F_OK) == 0 || access("/data/iface_rx_p", F_OK) == 0) {
358 std::cout << "Please manually delete iface_rx_p and iface_tx_p file " << std::endl;
359 return;
360 }
361 }
362
DataPackageTest(const int32_t testId)363 static bool DataPackageTest(const int32_t testId)
364 {
365 std::map<int32_t, Fun> testFunMap {
366 { TEST_DATA_PACKAGE_UP, &CellularDataCodeTest::TestDataPackageUp },
367 { TEST_DATA_PACKAGE_DOWN, &CellularDataCodeTest::TestDataPackageDown },
368 { TEST_DATA_PACKAGE_UP_DOWN, &CellularDataCodeTest::TestDataPackageUpDown },
369 };
370 std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
371 if (it != testFunMap.end()) {
372 (*(it->second))();
373 } else if (testId == TEST_DATA_PACKAGE_EXIT) {
374 std::cout << "exit..." << std::endl;
375 return false;
376 } else {
377 std::cout << "please input correct number..." << std::endl;
378 }
379 return true;
380 }
381
TouchOrRmDataPackage()382 static void TouchOrRmDataPackage()
383 {
384 int32_t input = 0;
385 while (true) {
386 std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
387 "0:testDataPackageUp\n"
388 "1:testDataPackageDown\n"
389 "2:testDataPackageUpDown\n"
390 "100:exit\n"
391 << std::endl;
392 std::cin >> input;
393 std::cout << "inputCMD is [" << input << "]" << std::endl;
394 if (!DataPackageTest(input)) {
395 break;
396 }
397 }
398 }
399
AcquireMmsNetwork()400 static void AcquireMmsNetwork()
401 {
402 AccessToken token;
403 int32_t slotId = DEFAULT_SIM_SLOT_ID;
404 std::cout << "please input parameter int slotId" << std::endl;
405 std::cin >> slotId;
406 if (g_callback == nullptr) {
407 g_callback = new (std::nothrow) TestCallback();
408 }
409 if (g_callback == nullptr) {
410 std::cout << "g_callback is null" << std::endl;
411 return;
412 }
413
414 NetSpecifier netSpecifier;
415 NetAllCapabilities netAllCapabilities;
416 netAllCapabilities.netCaps_.insert(NetCap::NET_CAPABILITY_MMS);
417 netAllCapabilities.bearerTypes_.insert(NetBearType::BEARER_CELLULAR);
418 int32_t simId = CoreServiceClient::GetInstance().GetSimId(slotId);
419 netSpecifier.ident_ = "simId" + std::to_string(simId);
420 netSpecifier.netCapabilities_ = netAllCapabilities;
421 sptr<NetSpecifier> specifier = new (std::nothrow) NetSpecifier(netSpecifier);
422 if (specifier == nullptr) {
423 std::cout << "specifier is null" << std::endl;
424 return;
425 }
426
427 int32_t result = NetConnClient::GetInstance().RegisterNetConnCallback(
428 specifier, g_callback, NET_REGISTER_TIMEOUT_MS);
429 std::cout << "RegisterNetConnCallback result [" << result << "]" << std::endl;
430 }
431
ReleaseMmsNetwork()432 static void ReleaseMmsNetwork()
433 {
434 AccessToken token;
435 if (g_callback == nullptr) {
436 std::cout << "g_callback is null" << std::endl;
437 return;
438 }
439 int32_t result =
440 NetConnClient::GetInstance().UnregisterNetConnCallback(g_callback);
441 std::cout << "UnregisterNetConnCallback result [" << result << "]" << std::endl;
442 g_callback = nullptr;
443 }
444
UnitTest(const int32_t testId) const445 bool UnitTest(const int32_t testId) const
446 {
447 std::map<int32_t, Fun> testFunMap {
448 { IS_CELLULAR_DATA_ENABLED_TEST, &IsCellularDataEnabledTest },
449 { ENABLE_CELLULAR_DATA_TEST, &EnableCellularDataTest },
450 { GET_CELLULAR_DATA_STATE_TEST, &GetCellularDataStateTest },
451 { IS_DATA_ROAMING_ENABLED_TEST, &IsCellularDataRoamingEnabledTest },
452 { ENABLE_DATA_ROAMING_TEST, &EnableCellularDataRoamingTest },
453 { APN_CHANGED_TEST, &HandleApnChangedTest },
454 { GET_DEFAULT_SLOT_ID, &GetDefaultSlotId },
455 { SET_DEFAULT_SLOT_ID, &SetDefaultSlotId },
456 { GET_DATA_FLOW_TYPE, &GetDataFlowType },
457 { TOUCH_OR_RM_DATA_PACKAGE, &TouchOrRmDataPackage },
458 { ACQUIRE_MMS_NETWORK, &AcquireMmsNetwork },
459 { RELEASE_MMS_NETWORK, &ReleaseMmsNetwork },
460 };
461 std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
462 if (it != testFunMap.end()) {
463 (*(it->second))();
464 } else if (testId == EXIT_CELLULAR_DATA_TEST) {
465 std::cout << "exit..." << std::endl;
466 return false;
467 } else {
468 std::cout << "please input correct number..." << std::endl;
469 }
470 return true;
471 }
472 };
473 } // namespace Telephony
474 } // namespace OHOS
475
main()476 int main()
477 {
478 OHOS::Telephony::CellularDataClient &cellularDataClient = OHOS::Telephony::CellularDataClient::GetInstance();
479 if (cellularDataClient.GetProxy() == nullptr) {
480 std::cout << "\n--- telephonyService == nullptr\n" << std::endl;
481 return 0;
482 }
483 int32_t inputCMD = 0;
484 OHOS::Telephony::CellularDataCodeTest cellularDataCodeTest;
485 while (true) {
486 std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
487 "0:IsCellularDataEnabledTest\n"
488 "1:EnableCellularDataTest\n"
489 "2:GetCellularDataStateTest\n"
490 "3:IsCellularDataRoamingEnabledTest\n"
491 "4:EnableCellularDataRoamingTest\n"
492 "5:ApnChangedTest\n"
493 "6:GetDefaultSlotId\n"
494 "7:SetDefaultSlotId\n"
495 "8:GetCellularDataFlowType\n"
496 "9:TouchOrRmDataPackage\n"
497 "10:AcquireMmsNetwork\n"
498 "11:ReleaseMmsNetwork\n"
499 "1000:exit\n"
500 << std::endl;
501 std::cin >> inputCMD;
502 std::cout << "inputCMD is [" << inputCMD << "]" << std::endl;
503 if (!cellularDataCodeTest.UnitTest(inputCMD)) {
504 break;
505 }
506 }
507 return 0;
508 }