• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (proxy != nullptr) {
292             int32_t result = proxy->GetDefaultCellularDataSlotId();
293             std::cout << "get default slot id is := " << result << std::endl;
294         } else {
295             std::cout << "get default slot id fail" << std::endl;
296         }
297     }
298 
SetDefaultSlotId()299     static void SetDefaultSlotId()
300     {
301         AccessToken token;
302         int32_t slotId = DEFAULT_SIM_SLOT_ID;
303         std::cout << "please input parameter int slot" << std::endl;
304         std::cin >> slotId;
305         sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
306         if (proxy != nullptr) {
307             int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
308             std::cout << "TelephonyTestService SetDefaultSlotId is " << result << std::endl;
309         } else {
310             std::cout << "TelephonyTestService SetDefaultSlotId is null" << std::endl;
311         }
312     }
313 
GetDataFlowType()314     static void GetDataFlowType()
315     {
316         sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
317         if (proxy != nullptr) {
318             int32_t result = proxy->GetCellularDataFlowType();
319             std::cout << "get CellDataFlowType is := " << result << std::endl;
320         } else {
321             std::cout << "get CellDataFlowType is fail" << std::endl;
322         }
323     }
324 
TestDataPackageUp()325     static void TestDataPackageUp()
326     {
327         system("rm /data/iface_tx_p");
328         system("touch /data/iface_rx_p");
329         if (access("/data/iface_tx_p", F_OK) == 0) {
330             std::cout << "Please manually delete iface_tx_p file " << std::endl;
331             return;
332         }
333         if (access("/data/iface_tx_p", F_OK) != 0) {
334             std::cout << "Please manually touch iface_tx_p file" << std::endl;
335         }
336     }
337 
TestDataPackageDown()338     static void TestDataPackageDown()
339     {
340         system("rm /data/iface_rx_p");
341         system("touch /data/iface_tx_p");
342         if (access("/data/iface_rx_p", F_OK) == 0) {
343             std::cout << "Please manually delete iface_rx_p file " << std::endl;
344             return;
345         }
346         if (access("/data/iface_tx_p", F_OK) != 0) {
347             std::cout << "Please manually touch iface_tx_p file" << std::endl;
348         }
349     }
350 
TestDataPackageUpDown()351     static void TestDataPackageUpDown()
352     {
353         system("rm /data/iface_tx_p");
354         system("rm /data/iface_rx_p");
355         if (access("/data/iface_tx_p", F_OK) == 0 || access("/data/iface_rx_p", F_OK) == 0) {
356             std::cout << "Please manually delete iface_rx_p and iface_tx_p file " << std::endl;
357             return;
358         }
359     }
360 
DataPackageTest(const int32_t testId)361     static bool DataPackageTest(const int32_t testId)
362     {
363         std::map<int32_t, Fun> testFunMap {
364             { TEST_DATA_PACKAGE_UP, &CellularDataCodeTest::TestDataPackageUp },
365             { TEST_DATA_PACKAGE_DOWN, &CellularDataCodeTest::TestDataPackageDown },
366             { TEST_DATA_PACKAGE_UP_DOWN, &CellularDataCodeTest::TestDataPackageUpDown },
367         };
368         std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
369         if (it != testFunMap.end()) {
370             (*(it->second))();
371         } else if (TEST_DATA_PACKAGE_EXIT == testId) {
372             std::cout << "exit..." << std::endl;
373             return false;
374         } else {
375             std::cout << "please input correct number..." << std::endl;
376         }
377         return true;
378     }
379 
TouchOrRmDataPackage()380     static void TouchOrRmDataPackage()
381     {
382         int32_t input = 0;
383         while (true) {
384             std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
385                 "0:testDataPackageUp\n"
386                 "1:testDataPackageDown\n"
387                 "2:testDataPackageUpDown\n"
388                 "100:exit\n"
389             << std::endl;
390             std::cin >> input;
391             std::cout << "inputCMD is [" << input << "]" << std::endl;
392             if (!DataPackageTest(input)) {
393                 break;
394             }
395         }
396     }
397 
AcquireMmsNetwork()398     static void AcquireMmsNetwork()
399     {
400         AccessToken token;
401         int32_t slotId = DEFAULT_SIM_SLOT_ID;
402         std::cout << "please input parameter int slotId" << std::endl;
403         std::cin >> slotId;
404         if (g_callback == nullptr) {
405             g_callback = new (std::nothrow) TestCallback();
406         }
407         if (g_callback == nullptr) {
408             std::cout << "g_callback is null" << std::endl;
409             return;
410         }
411 
412         NetSpecifier netSpecifier;
413         NetAllCapabilities netAllCapabilities;
414         netAllCapabilities.netCaps_.insert(NetCap::NET_CAPABILITY_MMS);
415         netAllCapabilities.bearerTypes_.insert(NetBearType::BEARER_CELLULAR);
416         int32_t simId = CoreServiceClient::GetInstance().GetSimId(slotId);
417         netSpecifier.ident_ = "simId" + std::to_string(simId);
418         netSpecifier.netCapabilities_ = netAllCapabilities;
419         sptr<NetSpecifier> specifier = new (std::nothrow) NetSpecifier(netSpecifier);
420         if (specifier == nullptr) {
421             std::cout << "specifier is null" << std::endl;
422             return;
423         }
424 
425         int32_t result = NetConnClient::GetInstance().RegisterNetConnCallback(
426             specifier, g_callback, NET_REGISTER_TIMEOUT_MS);
427         std::cout << "RegisterNetConnCallback result [" << result << "]" << std::endl;
428     }
429 
ReleaseMmsNetwork()430     static void ReleaseMmsNetwork()
431     {
432         AccessToken token;
433         if (g_callback == nullptr) {
434             std::cout << "g_callback is null" << std::endl;
435             return;
436         }
437         int32_t result =
438             NetConnClient::GetInstance().UnregisterNetConnCallback(g_callback);
439         std::cout << "UnregisterNetConnCallback result [" << result << "]" << std::endl;
440         g_callback = nullptr;
441     }
442 
UnitTest(const int32_t testId) const443     bool UnitTest(const int32_t testId) const
444     {
445         std::map<int32_t, Fun> testFunMap {
446             { IS_CELLULAR_DATA_ENABLED_TEST, &IsCellularDataEnabledTest },
447             { ENABLE_CELLULAR_DATA_TEST, &EnableCellularDataTest },
448             { GET_CELLULAR_DATA_STATE_TEST, &GetCellularDataStateTest },
449             { IS_DATA_ROAMING_ENABLED_TEST, &IsCellularDataRoamingEnabledTest },
450             { ENABLE_DATA_ROAMING_TEST, &EnableCellularDataRoamingTest },
451             { APN_CHANGED_TEST, &HandleApnChangedTest },
452             { GET_DEFAULT_SLOT_ID, &GetDefaultSlotId },
453             { SET_DEFAULT_SLOT_ID, &SetDefaultSlotId },
454             { GET_DATA_FLOW_TYPE, &GetDataFlowType },
455             { TOUCH_OR_RM_DATA_PACKAGE, &TouchOrRmDataPackage },
456             { ACQUIRE_MMS_NETWORK, &AcquireMmsNetwork },
457             { RELEASE_MMS_NETWORK, &ReleaseMmsNetwork },
458         };
459         std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
460         if (it != testFunMap.end()) {
461             (*(it->second))();
462         } else if (testId == EXIT_CELLULAR_DATA_TEST) {
463             std::cout << "exit..." << std::endl;
464             return false;
465         } else {
466             std::cout << "please input correct number..." << std::endl;
467         }
468         return true;
469     }
470 };
471 } // namespace Telephony
472 } // namespace OHOS
473 
main()474 int main()
475 {
476     OHOS::Telephony::CellularDataClient &cellularDataClient = OHOS::Telephony::CellularDataClient::GetInstance();
477     if (cellularDataClient.GetProxy() == nullptr) {
478         std::cout << "\n--- telephonyService == nullptr\n" << std::endl;
479         return 0;
480     }
481     int32_t inputCMD = 0;
482     OHOS::Telephony::CellularDataCodeTest cellularDataCodeTest;
483     while (true) {
484         std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
485                      "0:IsCellularDataEnabledTest\n"
486                      "1:EnableCellularDataTest\n"
487                      "2:GetCellularDataStateTest\n"
488                      "3:IsCellularDataRoamingEnabledTest\n"
489                      "4:EnableCellularDataRoamingTest\n"
490                      "5:ApnChangedTest\n"
491                      "6:GetDefaultSlotId\n"
492                      "7:SetDefaultSlotId\n"
493                      "8:GetCellularDataFlowType\n"
494                      "9:TouchOrRmDataPackage\n"
495                      "10:AcquireMmsNetwork\n"
496                      "11:ReleaseMmsNetwork\n"
497                      "1000:exit\n"
498                   << std::endl;
499         std::cin >> inputCMD;
500         std::cout << "inputCMD is [" << inputCMD << "]" << std::endl;
501         if (!cellularDataCodeTest.UnitTest(inputCMD)) {
502             break;
503         }
504     }
505     return 0;
506 }