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