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 <cstdint>
17 #include <string>
18
19 #define private public
20 #include <stdio.h>
21
22 #include "access_token.h"
23 #include "accesstoken_kit.h"
24 #include "cellular_data_client.h"
25 #include "cellular_data_error.h"
26 #include "cellular_data_service.h"
27 #include "cellular_data_types.h"
28 #include "core_service_client.h"
29 #include "gtest/gtest-message.h"
30 #include "gtest/gtest-test-part.h"
31 #include "gtest/gtest_pred_impl.h"
32 #include "gtest/hwext/gtest-tag.h"
33 #include "hap_token_info.h"
34 #include "i_cellular_data_manager.h"
35 #include "iosfwd"
36 #include "iostream"
37 #include "ostream"
38 #include "permission_def.h"
39 #include "permission_state_full.h"
40 #include "refbase.h"
41 #include "stdio.h"
42 #include "telephony_types.h"
43 #include "token_setproc.h"
44 #include "unistd.h"
45
46 namespace OHOS {
47 namespace Telephony {
48 using namespace testing::ext;
49 using namespace Security::AccessToken;
50 using Security::AccessToken::AccessTokenID;
51
52 static const int32_t SLEEP_TIME = 1;
53 static const int32_t SIM_SLOT_ID_1 = DEFAULT_SIM_SLOT_ID + 1;
54 static const int32_t DATA_SLOT_ID_INVALID = DEFAULT_SIM_SLOT_ID + 10;
55 static const int32_t PING_CHECK_SUCCESS = 0;
56 static const int32_t PING_CHECK_FAIL = 1;
57 static const int32_t MAX_TIMES = 35;
58 static const int32_t CMD_BUF_SIZE = 10240;
59
60 HapInfoParams testInfoParams = {
61 .bundleName = "tel_cellular_data_test",
62 .userID = 1,
63 .instIndex = 0,
64 .appIDDesc = "test",
65 .isSystemApp = true,
66 };
67
68 PermissionDef testPermGetNetworkInfoDef = {
69 .permissionName = "ohos.permission.GET_NETWORK_INFO",
70 .bundleName = "tel_cellular_data_test",
71 .grantMode = 1, // SYSTEM_GRANT
72 .label = "label",
73 .labelId = 1,
74 .description = "Test cellular data",
75 .descriptionId = 1,
76 .availableLevel = APL_SYSTEM_BASIC,
77 };
78
79 PermissionStateFull testGetNetworkInfoState = {
80 .grantFlags = { 2 }, // PERMISSION_USER_SET
81 .grantStatus = { PermissionState::PERMISSION_GRANTED },
82 .isGeneral = true,
83 .permissionName = "ohos.permission.GET_NETWORK_INFO",
84 .resDeviceID = { "local" },
85 };
86
87 PermissionDef testPermSetTelephonyStateDef = {
88 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
89 .bundleName = "tel_cellular_data_test",
90 .grantMode = 1, // SYSTEM_GRANT
91 .label = "label",
92 .labelId = 1,
93 .description = "Test cellular data",
94 .descriptionId = 1,
95 .availableLevel = APL_SYSTEM_BASIC,
96 };
97
98 PermissionStateFull testSetTelephonyState = {
99 .grantFlags = { 2 }, // PERMISSION_USER_SET
100 .grantStatus = { PermissionState::PERMISSION_GRANTED },
101 .isGeneral = true,
102 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
103 .resDeviceID = { "local" },
104 };
105
106 HapPolicyParams testPolicyParams = {
107 .apl = APL_SYSTEM_BASIC,
108 .domain = "test.domain",
109 .permList = { testPermGetNetworkInfoDef, testPermSetTelephonyStateDef },
110 .permStateList = { testGetNetworkInfoState, testSetTelephonyState },
111 };
112
113 class AccessToken {
114 public:
AccessToken()115 AccessToken()
116 {
117 currentID_ = GetSelfTokenID();
118 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
119 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
120 SetSelfTokenID(tokenIdEx.tokenIDEx);
121 }
~AccessToken()122 ~AccessToken()
123 {
124 AccessTokenKit::DeleteToken(accessID_);
125 SetSelfTokenID(currentID_);
126 }
127
128 private:
129 AccessTokenID currentID_ = 0;
130 AccessTokenID accessID_ = 0;
131 };
132
133 class CellularDataTest : public testing::Test {
134 public:
135 static void SetUpTestCase();
136 static void TearDownTestCase();
137 virtual void SetUp();
138 virtual void TearDown();
139 static bool HasSimCard(const int32_t slotId);
140 static int32_t IsCellularDataEnabledTest(bool &dataEnabled);
141 static int32_t EnableCellularDataTest(bool enable);
142 static int32_t GetCellularDataStateTest();
143 static int32_t IsCellularDataRoamingEnabledTest(int32_t slotId, bool &dataRoamingEnabled);
144 static int32_t EnableCellularDataRoamingTest(int32_t slotId, bool enable);
145 static int32_t GetDefaultCellularDataSlotIdTest();
146 static int32_t GetDefaultCellularDataSimIdTest();
147 static int32_t SetDefaultCellularDataSlotIdTest(int32_t slotId);
148 static int32_t GetCellularDataFlowTypeTest();
149 static void WaitTestTimeout(const int32_t status);
150 static sptr<ICellularDataManager> GetProxy();
151 static string GetCmdResult();
152 static int32_t PingTest();
153 static int32_t HasInternetCapability(int32_t slotId, int32_t cid);
154 static int32_t ClearCellularDataConnections(int32_t slotId);
155 };
156
HasSimCard(const int32_t slotId)157 bool CellularDataTest::HasSimCard(const int32_t slotId)
158 {
159 bool hasSimCard = false;
160 DelayedRefSingleton<CoreServiceClient>::GetInstance().HasSimCard(slotId, hasSimCard);
161 return hasSimCard;
162 }
163
TearDownTestCase()164 void CellularDataTest::TearDownTestCase() {}
165
SetUp()166 void CellularDataTest::SetUp() {}
167
TearDown()168 void CellularDataTest::TearDown() {}
169
SetUpTestCase()170 void CellularDataTest::SetUpTestCase()
171 {
172 if (CoreServiceClient::GetInstance().GetProxy() == nullptr) {
173 std::cout << "connect coreService server failed!" << std::endl;
174 return;
175 }
176
177 AccessToken token;
178 int32_t slotId = DATA_SLOT_ID_INVALID;
179 if (HasSimCard(DEFAULT_SIM_SLOT_ID)) {
180 slotId = DEFAULT_SIM_SLOT_ID;
181 } else if (HasSimCard(SIM_SLOT_ID_1)) {
182 slotId = SIM_SLOT_ID_1;
183 }
184 if (slotId == DATA_SLOT_ID_INVALID) {
185 return;
186 }
187 // Set the default slot
188 int32_t result = CellularDataClient::GetInstance().SetDefaultCellularDataSlotId(slotId);
189 if (result != TELEPHONY_ERR_SUCCESS) {
190 return;
191 }
192 int32_t enable = CellularDataClient::GetInstance().EnableCellularData(true);
193 ASSERT_TRUE(enable == TELEPHONY_ERR_SUCCESS);
194 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
195 }
196
WaitTestTimeout(const int32_t status)197 void CellularDataTest::WaitTestTimeout(const int32_t status)
198 {
199 int32_t count = 0;
200 while (count < MAX_TIMES) {
201 sleep(SLEEP_TIME);
202 if (CellularDataClient::GetInstance().GetCellularDataState() == status) {
203 return;
204 }
205 count++;
206 }
207 }
208
GetCmdResult()209 string CellularDataTest::GetCmdResult()
210 {
211 string strCmd = "ping -c3 www.openharmony.cn";
212 char buf[CMD_BUF_SIZE] = { 0 };
213 FILE *pf;
214
215 if ((pf = popen(strCmd.c_str(), "r")) == nullptr) {
216 return "";
217 }
218 string strResult;
219 while (fgets(buf, sizeof(buf), pf) != nullptr) {
220 strResult += buf;
221 }
222 pclose(pf);
223 unsigned int iSize = strResult.size();
224 if (iSize > 0 && strResult[iSize - 1] == '\n') {
225 strResult = strResult.substr(0, iSize - 1);
226 }
227 return strResult;
228 }
229
PingTest()230 int32_t CellularDataTest::PingTest()
231 {
232 string strRe = GetCmdResult();
233 std::cout << strRe << std::endl;
234
235 // if ping succeed, the result should contains something like:
236 // 3 packets transmitted, 3 received, 0% packet loss, time 5440ms
237 if (strRe.find("3 received") != string::npos) {
238 return PING_CHECK_SUCCESS;
239 } else {
240 return PING_CHECK_FAIL;
241 }
242 }
243
IsCellularDataRoamingEnabledTest(int32_t slotId,bool & dataRoamingEnabled)244 int32_t CellularDataTest::IsCellularDataRoamingEnabledTest(int32_t slotId, bool &dataRoamingEnabled)
245 {
246 return CellularDataClient::GetInstance().IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
247 }
248
IsCellularDataEnabledTest(bool & dataEnabled)249 int32_t CellularDataTest::IsCellularDataEnabledTest(bool &dataEnabled)
250 {
251 return CellularDataClient::GetInstance().IsCellularDataEnabled(dataEnabled);
252 }
253
EnableCellularDataTest(bool enable)254 int32_t CellularDataTest::EnableCellularDataTest(bool enable)
255 {
256 return CellularDataClient::GetInstance().EnableCellularData(enable);
257 }
258
GetCellularDataStateTest()259 int32_t CellularDataTest::GetCellularDataStateTest()
260 {
261 return CellularDataClient::GetInstance().GetCellularDataState();
262 }
263
EnableCellularDataRoamingTest(int32_t slotId,bool enable)264 int32_t CellularDataTest::EnableCellularDataRoamingTest(int32_t slotId, bool enable)
265 {
266 return CellularDataClient::GetInstance().EnableCellularDataRoaming(slotId, enable);
267 }
268
GetDefaultCellularDataSlotIdTest()269 int32_t CellularDataTest::GetDefaultCellularDataSlotIdTest()
270 {
271 return CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
272 }
273
GetDefaultCellularDataSimIdTest()274 int32_t CellularDataTest::GetDefaultCellularDataSimIdTest()
275 {
276 int32_t simId = 0;
277 return CellularDataClient::GetInstance().GetDefaultCellularDataSimId(simId);
278 }
279
SetDefaultCellularDataSlotIdTest(int32_t slotId)280 int32_t CellularDataTest::SetDefaultCellularDataSlotIdTest(int32_t slotId)
281 {
282 return CellularDataClient::GetInstance().SetDefaultCellularDataSlotId(slotId);
283 }
284
GetCellularDataFlowTypeTest()285 int32_t CellularDataTest::GetCellularDataFlowTypeTest()
286 {
287 return CellularDataClient::GetInstance().GetCellularDataFlowType();
288 }
289
HasInternetCapability(int32_t slotId,int32_t cid)290 int32_t CellularDataTest::HasInternetCapability(int32_t slotId, int32_t cid)
291 {
292 CellularDataClient::GetInstance().IsConnect();
293 return CellularDataClient::GetInstance().HasInternetCapability(slotId, cid);
294 }
295
ClearCellularDataConnections(int32_t slotId)296 int32_t CellularDataTest::ClearCellularDataConnections(int32_t slotId)
297 {
298 return CellularDataClient::GetInstance().ClearCellularDataConnections(slotId);
299 }
300 #ifndef TEL_TEST_UNSUPPORT
301 /**
302 * @tc.number IsCellularDataEnabled_Test
303 * @tc.name Test cellular data switch status(enabled or disabled)
304 * @tc.desc Function test
305 */
306 HWTEST_F(CellularDataTest, IsCellularDataEnabled_Test, TestSize.Level1)
307 {
308 AccessToken token;
309 bool dataEnabled = false;
310 CellularDataTest::IsCellularDataEnabledTest(dataEnabled);
311 ASSERT_TRUE(dataEnabled >= static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_DISABLED));
312 }
313
314 /**
315 * @tc.number DefaultCellularDataSlotId_Test
316 * @tc.name Test set default data card slot
317 * @tc.desc Function test
318 */
319 HWTEST_F(CellularDataTest, DefaultCellularDataSlotId_Test, TestSize.Level2)
320 {
321 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
322 return;
323 }
324 AccessToken token;
325 int32_t result = CellularDataTest::GetDefaultCellularDataSlotIdTest();
326 if (result < DEFAULT_SIM_SLOT_ID_REMOVE) {
327 return;
328 }
329 result = CellularDataTest::SetDefaultCellularDataSlotIdTest(DEFAULT_SIM_SLOT_ID);
330 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
331 // Multiple cards will need to be optimized again
332 result = CellularDataTest::SetDefaultCellularDataSlotIdTest(DEFAULT_SIM_SLOT_ID - 1);
333 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
334 result = CellularDataTest::SetDefaultCellularDataSlotIdTest(DATA_SLOT_ID_INVALID);
335 ASSERT_TRUE(result != TELEPHONY_ERR_SUCCESS);
336 }
337
338 /**
339 * @tc.number GetDefaultCellularDataSimId
340 * @tc.name Test get default data sim id
341 * @tc.desc Function test
342 */
343 HWTEST_F(CellularDataTest, DefaultCellularDataSimId_Test, TestSize.Level2)
344 {
345 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
346 return;
347 }
348 int32_t result = CellularDataTest::GetDefaultCellularDataSimIdTest();
349 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
350 }
351
352 /**
353 * @tc.number DefaultCellularDataSlotId_Test_01
354 * @tc.name Test set default data card slot
355 * @tc.desc Function test
356 */
357 HWTEST_F(CellularDataTest, DefaultCellularDataSlotId_Test_01, TestSize.Level2)
358 {
359 if (!HasSimCard(SIM_SLOT_ID_1)) {
360 return;
361 }
362 AccessToken token;
363 int32_t result = CellularDataTest::GetDefaultCellularDataSlotIdTest();
364 if (result < DEFAULT_SIM_SLOT_ID_REMOVE) {
365 return;
366 }
367 result = CellularDataTest::SetDefaultCellularDataSlotIdTest(SIM_SLOT_ID_1);
368 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
369 result = CellularDataTest::SetDefaultCellularDataSlotIdTest(DATA_SLOT_ID_INVALID);
370 ASSERT_TRUE(result != TELEPHONY_ERR_SUCCESS);
371 }
372
373 /**
374 * @tc.number EnableCellularData_Test_01
375 * @tc.name Test cellular data switch
376 * @tc.desc Function test
377 */
378 HWTEST_F(CellularDataTest, EnableCellularData_Test_01, TestSize.Level2)
379 {
380 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
381 return;
382 }
383 AccessToken token;
384 CellularDataTest::SetDefaultCellularDataSlotIdTest(DEFAULT_SIM_SLOT_ID);
385 CellularDataTest::EnableCellularDataTest(false);
386 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
387 sleep(SLEEP_TIME);
388 int32_t result = CellularDataTest::EnableCellularDataTest(true);
389 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
390 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
391 sleep(SLEEP_TIME);
392 std::cout << "Cellular Data Connected Ping..." << std::endl;
393 int32_t pingResult = CellularDataTest::PingTest();
394 ASSERT_TRUE(pingResult == PING_CHECK_SUCCESS);
395 CellularDataTest::EnableCellularDataTest(false);
396 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
397 sleep(SLEEP_TIME);
398 std::cout << "Cellular Data Disconnected Ping..." << std::endl;
399 pingResult = CellularDataTest::PingTest();
400 ASSERT_TRUE(pingResult == PING_CHECK_FAIL);
401 }
402
403 /**
404 * @tc.number EnableCellularData_Test_02
405 * @tc.name Test cellular data switch
406 * @tc.desc Function test
407 */
408 HWTEST_F(CellularDataTest, EnableCellularData_Test_02, TestSize.Level2)
409 {
410 if (!HasSimCard(SIM_SLOT_ID_1)) {
411 return;
412 }
413 AccessToken token;
414 CellularDataTest::SetDefaultCellularDataSlotIdTest(SIM_SLOT_ID_1);
415 CellularDataTest::EnableCellularDataTest(false);
416 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
417 sleep(SLEEP_TIME);
418 int32_t result = CellularDataTest::EnableCellularDataTest(true);
419 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
420 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
421 sleep(SLEEP_TIME);
422 std::cout << "Cellular Data Connected Ping..." << std::endl;
423 int32_t pingResult = CellularDataTest::PingTest();
424 ASSERT_TRUE(pingResult == PING_CHECK_SUCCESS);
425 CellularDataTest::EnableCellularDataTest(false);
426 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
427 sleep(SLEEP_TIME);
428 std::cout << "Cellular Data Disconnected Ping..." << std::endl;
429 pingResult = CellularDataTest::PingTest();
430 ASSERT_TRUE(pingResult == PING_CHECK_FAIL);
431 }
432
433 /**
434 * @tc.number DataRoamingState_ValidSlot_Test_01
435 * @tc.name Test the cellular data roaming switch with a slot id
436 * @tc.desc Function test
437 */
438 HWTEST_F(CellularDataTest, DataRoamingState_ValidSlot_Test_01, TestSize.Level3)
439 {
440 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
441 return;
442 }
443 AccessToken token;
444 CellularDataTest::SetDefaultCellularDataSlotIdTest(DEFAULT_SIM_SLOT_ID);
445 int32_t disabled = CellularDataTest::EnableCellularDataTest(false);
446 ASSERT_TRUE(disabled == TELEPHONY_ERR_SUCCESS);
447 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
448
449 // slot0 enable data roaming
450 int32_t enabled = CellularDataTest::EnableCellularDataRoamingTest(DEFAULT_SIM_SLOT_ID, true);
451 ASSERT_TRUE(enabled == TELEPHONY_ERR_SUCCESS);
452 bool dataRoamingEnabled = false;
453 CellularDataTest::IsCellularDataRoamingEnabledTest(DEFAULT_SIM_SLOT_ID, dataRoamingEnabled);
454 ASSERT_TRUE(dataRoamingEnabled);
455 // slot0 close
456 int32_t enable = CellularDataTest::EnableCellularDataRoamingTest(DEFAULT_SIM_SLOT_ID, false);
457 ASSERT_TRUE(enable == TELEPHONY_ERR_SUCCESS);
458 CellularDataTest::IsCellularDataRoamingEnabledTest(DEFAULT_SIM_SLOT_ID, dataRoamingEnabled);
459 ASSERT_TRUE(!dataRoamingEnabled);
460
461 // At present, multiple card problems, the subsequent need to continue to deal with
462 enable = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, true);
463 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
464 int32_t result = CellularDataTest::IsCellularDataRoamingEnabledTest(DATA_SLOT_ID_INVALID, dataRoamingEnabled);
465 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
466 enable = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, false);
467 // At present, multiple card problems, the subsequent need to continue to deal with
468 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
469 result = CellularDataTest::IsCellularDataRoamingEnabledTest(DATA_SLOT_ID_INVALID, dataRoamingEnabled);
470 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
471 }
472
473 /**
474 * @tc.number DataRoamingState_ValidSlot_Test_02
475 * @tc.name Test the cellular data roaming switch with a slot id
476 * @tc.desc Function test
477 */
478 HWTEST_F(CellularDataTest, DataRoamingState_ValidSlot_Test_02, TestSize.Level3)
479 {
480 if (!HasSimCard(SIM_SLOT_ID_1)) {
481 return;
482 }
483 AccessToken token;
484 CellularDataTest::SetDefaultCellularDataSlotIdTest(SIM_SLOT_ID_1);
485 int32_t disabled = CellularDataTest::EnableCellularDataTest(false);
486 ASSERT_TRUE(disabled == TELEPHONY_ERR_SUCCESS);
487 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
488
489 // slot1 enable data roaming
490 int32_t enabled = CellularDataTest::EnableCellularDataRoamingTest(SIM_SLOT_ID_1, true);
491 ASSERT_TRUE(enabled == TELEPHONY_ERR_SUCCESS);
492 bool dataRoamingEnabled = false;
493 CellularDataTest::IsCellularDataRoamingEnabledTest(SIM_SLOT_ID_1, dataRoamingEnabled);
494 ASSERT_TRUE(dataRoamingEnabled);
495 // slot1 close
496 int32_t enable = CellularDataTest::EnableCellularDataRoamingTest(SIM_SLOT_ID_1, false);
497 ASSERT_TRUE(enable == TELEPHONY_ERR_SUCCESS);
498 CellularDataTest::IsCellularDataRoamingEnabledTest(SIM_SLOT_ID_1, dataRoamingEnabled);
499 ASSERT_TRUE(!dataRoamingEnabled);
500
501 // At present, multiple card problems, the subsequent need to continue to deal with
502 enable = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, true);
503 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
504 int32_t result = CellularDataTest::IsCellularDataRoamingEnabledTest(DATA_SLOT_ID_INVALID, dataRoamingEnabled);
505 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
506 enable = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, false);
507 // At present, multiple card problems, the subsequent need to continue to deal with
508 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
509 result = CellularDataTest::IsCellularDataRoamingEnabledTest(DATA_SLOT_ID_INVALID, dataRoamingEnabled);
510 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
511 }
512
513 /**
514 * @tc.number EnableCellularDataRoaming_ValidSlot_Test_01
515 * @tc.name Test the cellular data roaming switch with a slot id
516 * @tc.desc Function test
517 */
518 HWTEST_F(CellularDataTest, EnableCellularDataRoaming_ValidSlot_Test_01, TestSize.Level3)
519 {
520 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
521 return;
522 }
523 AccessToken token;
524 CellularDataTest::SetDefaultCellularDataSlotIdTest(DEFAULT_SIM_SLOT_ID);
525 int32_t disabled = CellularDataTest::EnableCellularDataTest(false);
526 ASSERT_TRUE(disabled == TELEPHONY_ERR_SUCCESS);
527 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
528
529 bool dataRoamingEnabled = false;
530 CellularDataTest::IsCellularDataRoamingEnabledTest(DEFAULT_SIM_SLOT_ID, dataRoamingEnabled);
531 if (dataRoamingEnabled) {
532 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(DEFAULT_SIM_SLOT_ID, false);
533 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
534 } else {
535 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(DEFAULT_SIM_SLOT_ID, true);
536 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
537 }
538 // At present, multiple card problems, the subsequent need to continue to deal with
539 CellularDataTest::IsCellularDataRoamingEnabledTest(DEFAULT_SIM_SLOT_ID, dataRoamingEnabled);
540 if (dataRoamingEnabled) {
541 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, false);
542 ASSERT_TRUE(result != TELEPHONY_ERR_SUCCESS);
543 } else {
544 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, true);
545 ASSERT_TRUE(result != TELEPHONY_ERR_SUCCESS);
546 }
547 }
548
549 /**
550 * @tc.number EnableCellularDataRoaming_ValidSlot_Test_02
551 * @tc.name Test the cellular data roaming switch with a slot id
552 * @tc.desc Function test
553 */
554 HWTEST_F(CellularDataTest, EnableCellularDataRoaming_ValidSlot_Test_02, TestSize.Level3)
555 {
556 if (!HasSimCard(SIM_SLOT_ID_1)) {
557 return;
558 }
559 AccessToken token;
560 CellularDataTest::SetDefaultCellularDataSlotIdTest(SIM_SLOT_ID_1);
561 int32_t disabled = CellularDataTest::EnableCellularDataTest(false);
562 ASSERT_TRUE(disabled == TELEPHONY_ERR_SUCCESS);
563 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
564
565 bool dataRoamingEnabled = false;
566 CellularDataTest::IsCellularDataRoamingEnabledTest(SIM_SLOT_ID_1, dataRoamingEnabled);
567 if (dataRoamingEnabled) {
568 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(SIM_SLOT_ID_1, false);
569 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
570 } else {
571 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(SIM_SLOT_ID_1, true);
572 ASSERT_TRUE(result == TELEPHONY_ERR_SUCCESS);
573 }
574 // At present, multiple card problems, the subsequent need to continue to deal with
575 CellularDataTest::IsCellularDataRoamingEnabledTest(SIM_SLOT_ID_1, dataRoamingEnabled);
576 if (dataRoamingEnabled) {
577 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, false);
578 ASSERT_TRUE(result != TELEPHONY_ERR_SUCCESS);
579 } else {
580 int32_t result = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, true);
581 ASSERT_TRUE(result != TELEPHONY_ERR_SUCCESS);
582 }
583 }
584
585 /**
586 * @tc.number GetCellularDataState_ValidityTest_01
587 * @tc.name Test the GetCellularDataState function
588 * @tc.desc Function test
589 */
590 HWTEST_F(CellularDataTest, GetCellularDataState_ValidityTest_01, TestSize.Level3)
591 {
592 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
593 return;
594 }
595 AccessToken token;
596 CellularDataTest::SetDefaultCellularDataSlotIdTest(DEFAULT_SIM_SLOT_ID);
597 bool dataEnabled = false;
598 CellularDataTest::IsCellularDataEnabledTest(dataEnabled);
599 if (dataEnabled) {
600 CellularDataTest::EnableCellularDataTest(false);
601 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
602 sleep(SLEEP_TIME);
603 CellularDataTest::EnableCellularDataTest(true);
604 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
605 int32_t result = CellularDataTest::GetCellularDataStateTest();
606 ASSERT_TRUE(result == static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
607 } else {
608 CellularDataTest::EnableCellularDataTest(true);
609 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
610 sleep(SLEEP_TIME);
611 CellularDataTest::EnableCellularDataTest(false);
612 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
613 int32_t result = CellularDataTest::GetCellularDataStateTest();
614 ASSERT_TRUE(result == static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
615 }
616 CellularDataTest::EnableCellularDataTest(false);
617 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
618 }
619
620 /**
621 * @tc.number GetCellularDataState_ValidityTest_02
622 * @tc.name Test the GetCellularDataState function
623 * @tc.desc Function test
624 */
625 HWTEST_F(CellularDataTest, GetCellularDataState_ValidityTest_02, TestSize.Level3)
626 {
627 if (!HasSimCard(SIM_SLOT_ID_1)) {
628 return;
629 }
630 AccessToken token;
631 CellularDataTest::SetDefaultCellularDataSlotIdTest(SIM_SLOT_ID_1);
632 bool dataEnabled = false;
633 CellularDataTest::IsCellularDataEnabledTest(dataEnabled);
634 if (dataEnabled) {
635 CellularDataTest::EnableCellularDataTest(false);
636 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
637 sleep(SLEEP_TIME);
638 CellularDataTest::EnableCellularDataTest(true);
639 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
640 int32_t result = CellularDataTest::GetCellularDataStateTest();
641 ASSERT_TRUE(result == static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
642 } else {
643 CellularDataTest::EnableCellularDataTest(true);
644 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
645 sleep(SLEEP_TIME);
646 CellularDataTest::EnableCellularDataTest(false);
647 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
648 int32_t result = CellularDataTest::GetCellularDataStateTest();
649 ASSERT_TRUE(result == static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
650 }
651 CellularDataTest::EnableCellularDataTest(false);
652 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
653 }
654
655 /**
656 * @tc.number DataRoamingState_InValidSlot_Test_01
657 * @tc.name Test the EnableCellularDataRoaming function with a invalid slot id
658 * @tc.desc Function test
659 */
660 HWTEST_F(CellularDataTest, DataRoamingState_InValidSlot_Test_01, TestSize.Level3)
661 {
662 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
663 return;
664 }
665 AccessToken token;
666 // invalid slot turn on data roaming
667 int32_t enable = CellularDataTest::EnableCellularDataRoamingTest(DEFAULT_SIM_SLOT_ID - 1, true);
668 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
669 bool dataRoamingEnabled = false;
670 int32_t result = CellularDataTest::IsCellularDataRoamingEnabledTest(DEFAULT_SIM_SLOT_ID - 1, dataRoamingEnabled);
671 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
672 enable = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, true);
673 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
674 result = CellularDataTest::IsCellularDataRoamingEnabledTest(DATA_SLOT_ID_INVALID, dataRoamingEnabled);
675 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
676 // invalid slot disable roaming
677 enable = CellularDataTest::EnableCellularDataRoamingTest(DEFAULT_SIM_SLOT_ID - 1, false);
678 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
679 result = CellularDataTest::IsCellularDataRoamingEnabledTest(DEFAULT_SIM_SLOT_ID - 1, dataRoamingEnabled);
680 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
681 enable = CellularDataTest::EnableCellularDataRoamingTest(DATA_SLOT_ID_INVALID, false);
682 ASSERT_TRUE(enable != TELEPHONY_ERR_SUCCESS);
683 result = CellularDataTest::IsCellularDataRoamingEnabledTest(DATA_SLOT_ID_INVALID, dataRoamingEnabled);
684 ASSERT_TRUE(result == CELLULAR_DATA_INVALID_PARAM);
685 }
686
687 /**
688 * @tc.number DataFlowType_Test_01
689 * @tc.name Test the GetCellularDataFlowType function
690 * @tc.desc Function test
691 */
692 HWTEST_F(CellularDataTest, DataFlowType_Test_01, TestSize.Level3)
693 {
694 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
695 return;
696 }
697 AccessToken token;
698 CellularDataTest::SetDefaultCellularDataSlotIdTest(DEFAULT_SIM_SLOT_ID);
699 CellularDataTest::EnableCellularDataTest(false);
700 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
701 sleep(SLEEP_TIME);
702
703 CellularDataTest::EnableCellularDataTest(true);
704 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
705 sleep(SLEEP_TIME);
706 std::cout << "Cellular Data Connected Ping..." << std::endl;
707 int32_t pingResult = CellularDataTest::PingTest();
708 ASSERT_TRUE(pingResult == PING_CHECK_SUCCESS);
709 int32_t dataFlowType = CellularDataTest::GetCellularDataFlowTypeTest();
710 ASSERT_TRUE(dataFlowType >= 0);
711
712 CellularDataTest::EnableCellularDataTest(false);
713 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
714 sleep(SLEEP_TIME);
715 std::cout << "Cellular Data Disconnected Ping..." << std::endl;
716 pingResult = CellularDataTest::PingTest();
717 ASSERT_TRUE(pingResult == PING_CHECK_FAIL);
718 dataFlowType = CellularDataTest::GetCellularDataFlowTypeTest();
719 ASSERT_TRUE(dataFlowType == 0);
720 }
721
722 /**
723 * @tc.number DataFlowType_Test_02
724 * @tc.name Test the GetCellularDataFlowType function
725 * @tc.desc Function test
726 */
727 HWTEST_F(CellularDataTest, DataFlowType_Test_02, TestSize.Level3)
728 {
729 if (!HasSimCard(SIM_SLOT_ID_1)) {
730 return;
731 }
732 AccessToken token;
733 CellularDataTest::SetDefaultCellularDataSlotIdTest(SIM_SLOT_ID_1);
734 CellularDataTest::EnableCellularDataTest(false);
735 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
736 sleep(SLEEP_TIME);
737
738 CellularDataTest::EnableCellularDataTest(true);
739 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED));
740 sleep(SLEEP_TIME);
741 std::cout << "Cellular Data Connected Ping..." << std::endl;
742 int32_t pingResult = CellularDataTest::PingTest();
743 ASSERT_TRUE(pingResult == PING_CHECK_SUCCESS);
744 int32_t dataFlowType = CellularDataTest::GetCellularDataFlowTypeTest();
745 ASSERT_TRUE(dataFlowType >= 0);
746
747 CellularDataTest::EnableCellularDataTest(false);
748 WaitTestTimeout(static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED));
749 sleep(SLEEP_TIME);
750 std::cout << "Cellular Data Disconnected Ping..." << std::endl;
751 pingResult = CellularDataTest::PingTest();
752 ASSERT_TRUE(pingResult == PING_CHECK_FAIL);
753 dataFlowType = CellularDataTest::GetCellularDataFlowTypeTest();
754 ASSERT_TRUE(dataFlowType == 0);
755 }
756
757 /**
758 * @tc.number HasInternetCapability_Test_01
759 * @tc.name Test the HasInternetCapability function
760 * @tc.desc Function test
761 */
762 HWTEST_F(CellularDataTest, HasInternetCapability_Test_01, TestSize.Level3)
763 {
764 if (!HasSimCard(SIM_SLOT_ID_1)) {
765 return;
766 }
767
768 int32_t cid = 1;
769 int32_t result = CellularDataTest::HasInternetCapability(SIM_SLOT_ID_1, cid);
770 ASSERT_TRUE(result == static_cast<int32_t>(RequestNetCode::REQUEST_FAILED));
771 }
772
773 /**
774 * @tc.number HasInternetCapability_Test_02
775 * @tc.name Test the HasInternetCapability function
776 * @tc.desc Function test
777 */
778 HWTEST_F(CellularDataTest, HasInternetCapability_Test_02, TestSize.Level3)
779 {
780 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
781 return;
782 }
783
784 int32_t cid = 1;
785 int32_t result = CellularDataTest::HasInternetCapability(DEFAULT_SIM_SLOT_ID, cid);
786 ASSERT_TRUE(result == static_cast<int32_t>(RequestNetCode::REQUEST_FAILED));
787 }
788
789 /**
790 * @tc.number ClearCellularDataConnections_Test_01
791 * @tc.name Test the ClearCellularDataConnections function
792 * @tc.desc Function test
793 */
794 HWTEST_F(CellularDataTest, ClearCellularDataConnections_Test_01, TestSize.Level3)
795 {
796 if (!HasSimCard(SIM_SLOT_ID_1)) {
797 return;
798 }
799 AccessToken token;
800 int32_t result = CellularDataTest::ClearCellularDataConnections(SIM_SLOT_ID_1);
801 ASSERT_TRUE(result == static_cast<int32_t>(RequestNetCode::REQUEST_SUCCESS));
802 }
803
804 /**
805 * @tc.number ClearCellularDataConnections_Test_02
806 * @tc.name Test the ClearCellularDataConnections function
807 * @tc.desc Function test
808 */
809 HWTEST_F(CellularDataTest, ClearCellularDataConnections_Test_02, TestSize.Level3)
810 {
811 if (!HasSimCard(DEFAULT_SIM_SLOT_ID)) {
812 return;
813 }
814 AccessToken token;
815 int32_t result = CellularDataTest::ClearCellularDataConnections(DEFAULT_SIM_SLOT_ID);
816 ASSERT_TRUE(result == static_cast<int32_t>(RequestNetCode::REQUEST_SUCCESS));
817 }
818
819 /**
820 * @tc.number CellularDataDump_Test_01
821 * @tc.name TestDump
822 * @tc.desc Function test
823 */
824 HWTEST_F(CellularDataTest, CellularDataDump_Test_01, Function | MediumTest | Level3)
825 {
826 std::vector<std::u16string> emptyArgs = {};
827 std::vector<std::u16string> args = { u"test", u"test1" };
828 EXPECT_GE(DelayedSingleton<CellularDataService>::GetInstance()->Dump(-1, args), 0);
829 EXPECT_GE(DelayedSingleton<CellularDataService>::GetInstance()->Dump(0, emptyArgs), 0);
830 EXPECT_GE(DelayedSingleton<CellularDataService>::GetInstance()->Dump(0, args), 0);
831 }
832
833 #else // TEL_TEST_UNSUPPORT
834 /**
835 * @tc.number DataMock_Test_01
836 * @tc.name Test for unsupport platform
837 * @tc.desc Function test
838 */
839 HWTEST_F(CellularDataTest, DataMock_Test_01, TestSize.Level3)
840 {
841 EXPECT_TRUE(true);
842 }
843 #endif // TEL_TEST_UNSUPPORT
844 } // namespace Telephony
845 } // namespace OHOS
846