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 "telephony_types.h"
25 #include "token_setproc.h"
26
27 using namespace OHOS::Security::AccessToken;
28 using OHOS::Security::AccessToken::AccessTokenID;
29
30 HapInfoParams testInfoParams = {
31 .bundleName = "tel_cellular_data_ui_test",
32 .userID = 1,
33 .instIndex = 0,
34 .appIDDesc = "test",
35 };
36
37 PermissionDef testPermGetTelephonyStateDef = {
38 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
39 .bundleName = "tel_cellular_data_ui_test",
40 .grantMode = 1, // SYSTEM_GRANT
41 .label = "label",
42 .labelId = 1,
43 .description = "Test cellular data",
44 .descriptionId = 1,
45 .availableLevel = APL_SYSTEM_BASIC,
46 };
47
48 PermissionStateFull testGetTelephonyState = {
49 .grantFlags = { 2 }, // PERMISSION_USER_SET
50 .grantStatus = { PermissionState::PERMISSION_GRANTED },
51 .isGeneral = true,
52 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
53 .resDeviceID = { "local" },
54 };
55
56 PermissionDef testPermSetTelephonyStateDef = {
57 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
58 .bundleName = "tel_cellular_data_ui_test",
59 .grantMode = 1, // SYSTEM_GRANT
60 .label = "label",
61 .labelId = 1,
62 .description = "Test cellular data",
63 .descriptionId = 1,
64 .availableLevel = APL_SYSTEM_BASIC,
65 };
66
67 PermissionStateFull testSetTelephonyState = {
68 .grantFlags = { 2 }, // PERMISSION_USER_SET
69 .grantStatus = { PermissionState::PERMISSION_GRANTED },
70 .isGeneral = true,
71 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
72 .resDeviceID = { "local" },
73 };
74
75 PermissionDef testPermGetNetworkInfoDef = {
76 .permissionName = "ohos.permission.GET_NETWORK_INFO",
77 .bundleName = "tel_core_service_gtest",
78 .grantMode = 1, // SYSTEM_GRANT
79 .label = "label",
80 .labelId = 1,
81 .description = "Test cellular data",
82 .descriptionId = 1,
83 .availableLevel = APL_SYSTEM_BASIC,
84 };
85
86 PermissionStateFull testPermGetNetworkInfo = {
87 .grantFlags = { 2 }, // PERMISSION_USER_SET
88 .grantStatus = { PermissionState::PERMISSION_GRANTED },
89 .isGeneral = true,
90 .permissionName = "ohos.permission.GET_NETWORK_INFO",
91 .resDeviceID = { "local" },
92 };
93
94 HapPolicyParams testPolicyParams = {
95 .apl = APL_SYSTEM_BASIC,
96 .domain = "test.domain",
97 .permList = { testPermGetTelephonyStateDef, testPermSetTelephonyStateDef, testPermGetNetworkInfoDef },
98 .permStateList = { testGetTelephonyState, testSetTelephonyState, testPermGetNetworkInfo },
99 };
100
101 class AccessToken {
102 public:
AccessToken()103 AccessToken()
104 {
105 currentID_ = GetSelfTokenID();
106 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
107 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
108 SetSelfTokenID(accessID_);
109 }
~AccessToken()110 ~AccessToken()
111 {
112 AccessTokenKit::DeleteToken(accessID_);
113 SetSelfTokenID(currentID_);
114 }
115
116 private:
117 AccessTokenID currentID_ = 0;
118 AccessTokenID accessID_ = 0;
119 };
120
121 namespace OHOS {
122 namespace Telephony {
123 class CellularDataCodeTest {
124 using Fun = void (*)();
125 public:
126 CellularDataCodeTest() = default;
127
128 ~CellularDataCodeTest() = default;
129
IsCellularDataEnabledTest()130 static void IsCellularDataEnabledTest()
131 {
132 AccessToken token;
133 bool enabled = false;
134 int32_t result = CellularDataClient::GetInstance().IsCellularDataEnabled(enabled);
135 std::cout << "TelephonyTestService Remote IsCellularDataEnabled result [" << result << "]"
136 << " enabled [" << enabled << "]" << std::endl;
137 }
138
EnableCellularDataTest()139 static void EnableCellularDataTest()
140 {
141 AccessToken token;
142 const int32_t maxTestCount = 61;
143 const uint32_t spaceTime = 1000 * 100 * 2.6;
144 const int32_t useMaxType = 1;
145 const int32_t evenNumber = 2;
146 int32_t type = 0;
147 std::cout << "please input whether enable: enable/1,disable/0 " << std::endl;
148 std::cin >> type;
149 int32_t result = 0;
150 if (type > useMaxType) {
151 for (int32_t i = 1; i < maxTestCount; i++) {
152 bool dataEnable = i % evenNumber;
153 if (dataEnable) {
154 std::cout << "enable====: " << dataEnable << std::endl;
155 CellularDataClient::GetInstance().EnableCellularData(dataEnable);
156 usleep(spaceTime);
157 result = CellularDataClient::GetInstance().GetCellularDataState();
158 std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
159 } else {
160 std::cout << "enable false====: " << dataEnable << std::endl;
161 CellularDataClient::GetInstance().EnableCellularData(dataEnable);
162 usleep(spaceTime);
163 result = CellularDataClient::GetInstance().GetCellularDataState();
164 std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
165 }
166 std::cout << "i := " << i << std::endl;
167 }
168 return;
169 }
170 bool enable = (type > 0);
171 result = CellularDataClient::GetInstance().EnableCellularData(enable);
172 std::cout << "Remote EnableCellularData " << enable <<" result [" << result << "]" << std::endl;
173 }
174
GetCellularDataStateTest()175 static void GetCellularDataStateTest()
176 {
177 int32_t result = CellularDataClient::GetInstance().GetCellularDataState();
178 std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
179 }
180
IsCellularDataRoamingEnabledTest()181 static void IsCellularDataRoamingEnabledTest()
182 {
183 AccessToken token;
184 int32_t slotId = DEFAULT_SIM_SLOT_ID;
185 std::cout << "please input parameter int slotId" << std::endl;
186 std::cin >> slotId;
187 bool isDataRoaming = false;
188 int32_t result = CellularDataClient::GetInstance().IsCellularDataRoamingEnabled(slotId, isDataRoaming);
189 std::cout << "Remote IsCellularDataRoamingEnabled result [" << result << "]"
190 << " isDataRoaming [" << isDataRoaming << "]" << std::endl;
191 }
192
EnableCellularDataRoamingTest()193 static void EnableCellularDataRoamingTest()
194 {
195 AccessToken token;
196 int32_t slotId = DEFAULT_SIM_SLOT_ID;
197 int32_t type = 0;
198 std::cout << "please input parameter int slotId " << std::endl;
199 std::cin >> slotId;
200 std::cout << "please input parameter roaming enable: enable/1,disable/0" << std::endl;
201 std::cin >> type;
202 bool enable = (type > 0);
203 int32_t result = CellularDataClient::GetInstance().EnableCellularDataRoaming(slotId, enable);
204 std::cout << "Remote GetOperatorName " << enable << " result [" << result << "]" << std::endl;
205 }
206
HandleApnChangedTest()207 static void HandleApnChangedTest()
208 {
209 AccessToken token;
210 int32_t slotId = DEFAULT_SIM_SLOT_ID;
211 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
212 if (proxy != nullptr) {
213 int32_t result = proxy->HandleApnChanged(slotId);
214 std::cout << "TelephonyTestService Remote result [" << result << "]" << std::endl;
215 } else {
216 std::cout << "TelephonyTestService Remote is null" << std::endl;
217 }
218 }
219
GetDefaultSlotId()220 static void GetDefaultSlotId()
221 {
222 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
223 if (proxy != nullptr) {
224 int32_t result = proxy->GetDefaultCellularDataSlotId();
225 std::cout << "get default slot id is := " << result << std::endl;
226 } else {
227 std::cout << "get default slot id fail" << std::endl;
228 }
229 }
230
SetDefaultSlotId()231 static void SetDefaultSlotId()
232 {
233 AccessToken token;
234 int32_t slotId = DEFAULT_SIM_SLOT_ID;
235 std::cout << "please input parameter int slot" << std::endl;
236 std::cin >> slotId;
237 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
238 if (proxy != nullptr) {
239 int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
240 std::cout << "TelephonyTestService SetDefaultSlotId is " << result << std::endl;
241 } else {
242 std::cout << "TelephonyTestService SetDefaultSlotId is null" << std::endl;
243 }
244 }
245
GetDataFlowType()246 static void GetDataFlowType()
247 {
248 sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
249 if (proxy != nullptr) {
250 int32_t result = proxy->GetCellularDataFlowType();
251 std::cout << "get CellDataFlowType is := " << result << std::endl;
252 } else {
253 std::cout << "get CellDataFlowType is fail" << std::endl;
254 }
255 }
256
TestDataPackageUp()257 static void TestDataPackageUp()
258 {
259 system("rm /data/iface_tx_p");
260 system("touch /data/iface_rx_p");
261 if (access("/data/iface_tx_p", F_OK) == 0) {
262 std::cout << "Please manually delete iface_tx_p file " << std::endl;
263 return;
264 }
265 if (access("/data/iface_tx_p", F_OK) != 0) {
266 std::cout << "Please manually touch iface_tx_p file" << std::endl;
267 }
268 }
269
TestDataPackageDown()270 static void TestDataPackageDown()
271 {
272 system("rm /data/iface_rx_p");
273 system("touch /data/iface_tx_p");
274 if (access("/data/iface_rx_p", F_OK) == 0) {
275 std::cout << "Please manually delete iface_rx_p file " << std::endl;
276 return;
277 }
278 if (access("/data/iface_tx_p", F_OK) != 0) {
279 std::cout << "Please manually touch iface_tx_p file" << std::endl;
280 }
281 }
282
TestDataPackageUpDown()283 static void TestDataPackageUpDown()
284 {
285 system("rm /data/iface_tx_p");
286 system("rm /data/iface_rx_p");
287 if (access("/data/iface_tx_p", F_OK) == 0 || access("/data/iface_rx_p", F_OK) == 0) {
288 std::cout << "Please manually delete iface_rx_p and iface_tx_p file " << std::endl;
289 return;
290 }
291 }
292
DataPackageTest(const int32_t testId)293 static bool DataPackageTest(const int32_t testId)
294 {
295 std::map<int32_t, Fun> testFunMap {
296 {TEST_DATA_PACKAGE_UP, &CellularDataCodeTest::TestDataPackageUp},
297 {TEST_DATA_PACKAGE_DOWN, &CellularDataCodeTest::TestDataPackageDown},
298 {TEST_DATA_PACKAGE_UP_DOWN, &CellularDataCodeTest::TestDataPackageUpDown},
299
300 };
301 std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
302 if (it != testFunMap.end()) {
303 (*(it->second))();
304 } else if (TEST_DATA_PACKAGE_EXIT == testId) {
305 std::cout << "exit..." << std::endl;
306 return false;
307 } else {
308 std::cout << "please input correct number..." << std::endl;
309 }
310 return true;
311 }
312
TouchOrRmDataPackage()313 static void TouchOrRmDataPackage()
314 {
315 int32_t input = 0;
316 while (true) {
317 std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
318 "0:testDataPackageUp\n"
319 "1:testDataPackageDown\n"
320 "2:testDataPackageUpDown\n"
321 "100:exit\n"
322 << std::endl;
323 std::cin >> input;
324 std::cout << "inputCMD is [" << input << "]" << std::endl;
325 if (!DataPackageTest(input)) {
326 break;
327 }
328 }
329 }
330
UnitTest(const int32_t testId) const331 bool UnitTest(const int32_t testId) const
332 {
333 std::map<int32_t, Fun> testFunMap {
334 {IS_CELLULAR_DATA_ENABLED_TEST, &IsCellularDataEnabledTest},
335 {ENABLE_CELLULAR_DATA_TEST, &EnableCellularDataTest},
336 {GET_CELLULAR_DATA_STATE_TEST, &GetCellularDataStateTest},
337 {IS_DATA_ROAMING_ENABLED_TEST, &IsCellularDataRoamingEnabledTest},
338 {ENABLE_DATA_ROAMING_TEST, &EnableCellularDataRoamingTest},
339 {APN_CHANGED_TEST, &HandleApnChangedTest},
340 {GET_DEFAULT_SLOT_ID, &GetDefaultSlotId},
341 {SET_DEFAULT_SLOT_ID, &SetDefaultSlotId},
342 {GET_DATA_FLOW_TYPE, &GetDataFlowType},
343 {TOUCH_OR_RM_DATA_PACKAGE, &TouchOrRmDataPackage},
344 };
345 std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
346 if (it != testFunMap.end()) {
347 (*(it->second))();
348 } else if (EXIT_CELLULAR_DATA_TEST == testId) {
349 std::cout << "exit..." << std::endl;
350 return false;
351 } else {
352 std::cout << "please input correct number..." << std::endl;
353 }
354 return true;
355 }
356
357 private:
358 const int32_t IS_CELLULAR_DATA_ENABLED_TEST = 0;
359 const int32_t ENABLE_CELLULAR_DATA_TEST = 1;
360 const int32_t GET_CELLULAR_DATA_STATE_TEST = 2;
361 const int32_t IS_DATA_ROAMING_ENABLED_TEST = 3;
362 const int32_t ENABLE_DATA_ROAMING_TEST = 4;
363 const int32_t APN_CHANGED_TEST = 5;
364 const int32_t GET_DEFAULT_SLOT_ID = 6;
365 const int32_t SET_DEFAULT_SLOT_ID = 7;
366 const int32_t GET_DATA_FLOW_TYPE = 8;
367 const int32_t TOUCH_OR_RM_DATA_PACKAGE = 9;
368 const int32_t EXIT_CELLULAR_DATA_TEST = 1000;
369 static const int32_t TEST_DATA_PACKAGE_UP = 0;
370 static const int32_t TEST_DATA_PACKAGE_DOWN = 1;
371 static const int32_t TEST_DATA_PACKAGE_UP_DOWN = 2;
372 static const int32_t TEST_DATA_PACKAGE_EXIT = 100;
373 };
374 } // namespace Telephony
375 } // namespace OHOS
376
main()377 int main()
378 {
379 OHOS::Telephony::CellularDataClient &cellularDataClient = OHOS::Telephony::CellularDataClient::GetInstance();
380 if (cellularDataClient.GetProxy() == nullptr) {
381 std::cout << "\n--- telephonyService == nullptr\n" << std::endl;
382 return 0;
383 }
384 int32_t inputCMD = 0;
385 OHOS::Telephony::CellularDataCodeTest cellularDataCodeTest;
386 while (true) {
387 std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
388 "0:IsCellularDataEnabledTest\n"
389 "1:EnableCellularDataTest\n"
390 "2:GetCellularDataStateTest\n"
391 "3:IsCellularDataRoamingEnabledTest\n"
392 "4:EnableCellularDataRoamingTest\n"
393 "5:ApnChangedTest\n"
394 "6:GetDefaultSlotId\n"
395 "7:SetDefaultSlotId\n"
396 "8:GetCellularDataFlowType\n"
397 "9:TouchOrRmDataPackage\n"
398 "1000:exit\n"
399 << std::endl;
400 std::cin >> inputCMD;
401 std::cout << "inputCMD is [" << inputCMD << "]" << std::endl;
402 if (!cellularDataCodeTest.UnitTest(inputCMD)) {
403 break;
404 }
405 }
406 return 0;
407 }
408