• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #define private public
16 #define protected public
17 
18 #include "cellular_data_error.h"
19 #include "cellular_data_service.h"
20 #include "data_access_token.h"
21 #include "data_connection_monitor.h"
22 #include "gtest/gtest.h"
23 #include "tel_ril_network_parcel.h"
24 #include "traffic_management.h"
25 
26 namespace OHOS {
27 namespace Telephony {
28 using namespace testing::ext;
29 
30 static const int32_t SLEEP_TIME = 3;
31 
32 class CellularDataServiceTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38     std::shared_ptr<CellularDataService> service = DelayedSingleton<CellularDataService>::GetInstance();
39 };
SetUpTestCase()40 void CellularDataServiceTest::SetUpTestCase() {}
41 
TearDownTestCase()42 void CellularDataServiceTest::TearDownTestCase()
43 {
44     sleep(SLEEP_TIME);
45 }
46 
SetUp()47 void CellularDataServiceTest::SetUp() {}
48 
TearDown()49 void CellularDataServiceTest::TearDown() {}
50 
51 /**
52  * @tc.number   CellularDataService_001
53  * @tc.name     test function branch
54  * @tc.desc     Function test
55  */
56 HWTEST_F(CellularDataServiceTest, CellularDataService_001, TestSize.Level0)
57 {
58     DataAccessToken token;
59     service->OnStart();
60     service->isInitSuccess_ = true;
61     bool dataEnabled = false;
62     bool dataRoamingEnabled = false;
63     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->IsCellularDataEnabled(dataEnabled));
64     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableCellularData(false));
65     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableCellularData(true));
66     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableIntelligenceSwitch(false));
67     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableIntelligenceSwitch(true));
68     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->GetCellularDataState());
69     ASSERT_EQ(static_cast<int32_t>(DisConnectionReason::REASON_CHANGE_CONNECTION),
70         service->GetApnState(DEFAULT_SIM_SLOT_ID, std::string()));
71     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->IsCellularDataRoamingEnabled(DEFAULT_SIM_SLOT_ID, dataRoamingEnabled));
72     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableCellularDataRoaming(DEFAULT_SIM_SLOT_ID, true));
73     NetRequest request;
74     request.ident = "simId12";
75     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->RequestNet(request));
76     request.ident = "simId2";
77     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->ReleaseNet(request));
78     auto event = AppExecFwk::InnerEvent::Get(0);
79     service->DispatchEvent(DEFAULT_SIM_SLOT_ID, event);
80     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->HandleApnChanged(DEFAULT_SIM_SLOT_ID));
81 }
82 
83 /**
84  * @tc.number   CellularDataService_002
85  * @tc.name     test function branch
86  * @tc.desc     Function test
87  */
88 HWTEST_F(CellularDataServiceTest, CellularDataService_002, TestSize.Level0)
89 {
90     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->GetCellularDataFlowType());
91     ASSERT_EQ("default slotId: -1", service->GetStateMachineCurrentStatusDump());
92     service->GetFlowDataInfoDump();
93     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->StrategySwitch(DEFAULT_SIM_SLOT_ID, false));
94     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->HasInternetCapability(DEFAULT_SIM_SLOT_ID, 0));
95     DisConnectionReason reason = DisConnectionReason::REASON_NORMAL;
96     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->ClearAllConnections(DEFAULT_SIM_SLOT_ID, reason));
97     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->ChangeConnectionForDsds(DEFAULT_SIM_SLOT_ID, false));
98     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->ChangeConnectionForDsds(DEFAULT_SIM_SLOT_ID, true));
99     ApnItem::Attribute apnAttr;
100     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->GetDataConnApnAttr(DEFAULT_SIM_SLOT_ID, apnAttr));
101     std::string ipType;
102     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->GetDataConnIpType(DEFAULT_SIM_SLOT_ID, ipType));
103     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->IsNeedDoRecovery(DEFAULT_SIM_SLOT_ID, true));
104     uint32_t supplierId = 0;
105     ASSERT_EQ(TELEPHONY_ERR_PERMISSION_ERR,
106         service->GetCellularDataSupplierId(DEFAULT_SIM_SLOT_ID, NetCap::NET_CAPABILITY_INTERNET, supplierId));
107     ASSERT_EQ(TELEPHONY_ERR_PERMISSION_ERR,
108         service->GetCellularDataSupplierId(DEFAULT_SIM_SLOT_ID, NetCap::NET_CAPABILITY_END, supplierId));
109     service->CorrectNetSupplierNoAvailable(DEFAULT_SIM_SLOT_ID);
110     ASSERT_EQ(TELEPHONY_ERR_PERMISSION_ERR, service->EstablishAllApnsIfConnectable(DEFAULT_SIM_SLOT_ID));
111     int32_t regState = -1;
112     service->GetSupplierRegisterState(supplierId, regState);
113     bool isSupportDun = false;
114     EXPECT_EQ(TELEPHONY_ERR_PERMISSION_ERR, service->GetIfSupportDunApn(isSupportDun));
115     service->OnStop();
116 }
117 
118 /**
119  * @tc.number   DataConnectionMonitor_HandleScreenStateChanged_001
120  * @tc.name     test function branch
121  * @tc.desc     Function test
122  */
123 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_HandleScreenStateChanged_001, TestSize.Level0)
124 {
125     std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
126     dataConnectionMonitor->isScreenOn_ = false;
127     dataConnectionMonitor->HandleScreenStateChanged(true);
128     ASSERT_EQ(dataConnectionMonitor->isScreenOn_, true);
129     dataConnectionMonitor->isScreenOn_ = false;
130     dataConnectionMonitor->HandleScreenStateChanged(false);
131     ASSERT_EQ(dataConnectionMonitor->isScreenOn_, false);
132 }
133 
134 /**
135  * @tc.number   DataConnectionMonitor_OnStallDetectionTimer_001
136  * @tc.name     test function branch
137  * @tc.desc     Function test
138  */
139 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_OnStallDetectionTimer_001, TestSize.Level0)
140 {
141     std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
142     dataConnectionMonitor->noRecvPackets_ = 20;
143     dataConnectionMonitor->stallDetectionEnabled_ = true;
144     dataConnectionMonitor->OnStallDetectionTimer();
145     ASSERT_NE(dataConnectionMonitor->noRecvPackets_, 20);
146 }
147 
148 /**
149  * @tc.number   DataConnectionMonitor_HandleRecovery_001
150  * @tc.name     test function branch
151  * @tc.desc     Function test
152  */
153 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_HandleRecovery_001, TestSize.Level0)
154 {
155     std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
156     dataConnectionMonitor->dataRecoveryState_ = RecoveryState::STATE_REQUEST_CONTEXT_LIST;
157     dataConnectionMonitor->HandleRecovery();
158     ASSERT_EQ(dataConnectionMonitor->dataRecoveryState_, RecoveryState::STATE_CLEANUP_CONNECTIONS);
159     dataConnectionMonitor->dataRecoveryState_ = RecoveryState::STATE_REREGISTER_NETWORK;
160     dataConnectionMonitor->HandleRecovery();
161     ASSERT_EQ(dataConnectionMonitor->dataRecoveryState_, RecoveryState::STATE_RADIO_STATUS_RESTART);
162     dataConnectionMonitor->dataRecoveryState_ = RecoveryState::STATE_RADIO_STATUS_RESTART;
163     dataConnectionMonitor->HandleRecovery();
164     ASSERT_EQ(dataConnectionMonitor->dataRecoveryState_, RecoveryState::STATE_REQUEST_CONTEXT_LIST);
165 }
166 
167 /**
168  * @tc.number   DataConnectionMonitor_EndNetStatistics_001
169  * @tc.name     test function branch
170  * @tc.desc     Function test
171  */
172 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_EndNetStatistics_001, TestSize.Level0)
173 {
174     std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
175     dataConnectionMonitor->dataFlowType_ = CellDataFlowType::DATA_FLOW_TYPE_DOWN;
176     dataConnectionMonitor->EndNetStatistics();
177     ASSERT_EQ(dataConnectionMonitor->dataFlowType_, CellDataFlowType::DATA_FLOW_TYPE_NONE);
178 }
179 
180 /**
181  * @tc.number   DataConnectionMonitor_UpdateNetTrafficState_001
182  * @tc.name     test function branch
183  * @tc.desc     Function test
184  */
185 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_UpdateNetTrafficState_001, TestSize.Level0)
186 {
187     std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
188     dataConnectionMonitor->updateNetStat_ = true;
189     dataConnectionMonitor->UpdateNetTrafficState();
190     std::shared_ptr<PreferredNetworkTypeInfo> preferredTypeInfo = std::make_shared<PreferredNetworkTypeInfo>();
191     auto event = AppExecFwk::InnerEvent::Get(0, preferredTypeInfo);
192     dataConnectionMonitor->SetPreferredNetworkPara(event);
193     ASSERT_EQ(dataConnectionMonitor->dataFlowType_, CellDataFlowType::DATA_FLOW_TYPE_NONE);
194 }
195 
196 /**
197  * @tc.number   DataConnectionMonitor_UpdateDataFlowType_001
198  * @tc.name     test function branch
199  * @tc.desc     Function test
200  */
201 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_UpdateDataFlowType_001, TestSize.Level0)
202 {
203     std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
204     dataConnectionMonitor->dataFlowType_ = CellDataFlowType::DATA_FLOW_TYPE_DOWN;
205     dataConnectionMonitor->trafficManager_->sendPackets_ = 200;
206     dataConnectionMonitor->trafficManager_->recvPackets_ = 100;
207     dataConnectionMonitor->UpdateDataFlowType();
208     ASSERT_EQ(static_cast<int32_t>(dataConnectionMonitor->dataFlowType_),
209         static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE));
210 }
211 
212 /**
213  * @tc.number   DataConnectionMonitor_SetDataFlowType_001
214  * @tc.name     test function branch
215  * @tc.desc     Function test
216  */
217 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_SetDataFlowType_001, TestSize.Level0)
218 {
219     std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
220     auto event = AppExecFwk::InnerEvent::Get(CellularDataEventCode::MSG_RUN_MONITOR_TASK);
221     dataConnectionMonitor->ProcessEvent(event);
222     event = AppExecFwk::InnerEvent::Get(CellularDataEventCode::MSG_STALL_DETECTION_EVENT_ID);
223     dataConnectionMonitor->ProcessEvent(event);
224     event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_DATA_CALL_LIST_CHANGED);
225     dataConnectionMonitor->ProcessEvent(event);
226     event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_GET_PREFERRED_NETWORK_MODE);
227     dataConnectionMonitor->ProcessEvent(event);
228     event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_PREFERRED_NETWORK_MODE);
229     dataConnectionMonitor->ProcessEvent(event);
230     event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_OFF);
231     dataConnectionMonitor->ProcessEvent(event);
232     event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_ON);
233     dataConnectionMonitor->ProcessEvent(event);
234     dataConnectionMonitor->dataFlowType_ = CellDataFlowType::DATA_FLOW_TYPE_NONE;
235     dataConnectionMonitor->SetDataFlowType(CellDataFlowType::DATA_FLOW_TYPE_DOWN);
236     ASSERT_EQ(static_cast<int32_t>(dataConnectionMonitor->dataFlowType_),
237         static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DOWN));
238 }
239 
240 /**
241  * @tc.number   CellularDataController_SetIntelligenceSwitchEnable_001
242  * @tc.name     test function branch
243  * @tc.desc     Function test
244  */
245 HWTEST_F(CellularDataServiceTest, CellularDataController_SetIntelligenceSwitchEnable_001, TestSize.Level0)
246 {
247     std::shared_ptr<CellularDataController> cellularDataController = std::make_shared<CellularDataController>(0);
248     cellularDataController->cellularDataHandler_ = nullptr;
249     int32_t result = cellularDataController->SetIntelligenceSwitchEnable(true);
250     ASSERT_EQ(result, TELEPHONY_ERR_LOCAL_PTR_NULL);
251 }
252 
253 /**
254  * @tc.number   CellularDataController_SetIntelligenceSwitchEnable_002
255  * @tc.name     test function branch
256  * @tc.desc     Function test
257  */
258 HWTEST_F(CellularDataServiceTest, CellularDataController_SetIntelligenceSwitchEnable_002, TestSize.Level0)
259 {
260     std::shared_ptr<CellularDataController> cellularDataController = std::make_shared<CellularDataController>(0);
261     cellularDataController->Init();
262     int32_t result = cellularDataController->SetIntelligenceSwitchEnable(false);
263     ASSERT_EQ(result, 0);
264 }
265 
266 /**
267  * @tc.number   CellularDataController_OnAddSystemAbility_001
268  * @tc.name     test function branch
269  * @tc.desc     Function test
270  */
271 HWTEST_F(CellularDataServiceTest, CellularDataController_OnAddSystemAbility_001, TestSize.Level0)
272 {
273     std::shared_ptr<CellularDataController> cellularDataController = std::make_shared<CellularDataController>(0);
274     cellularDataController->systemAbilityListener_ =
275         new (std::nothrow) CellularDataController::SystemAbilityStatusChangeListener(0, nullptr);
276     if (cellularDataController->systemAbilityListener_ != nullptr) {
277         cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(
278             COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, "");
279         cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(
280             COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID, "");
281         cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(
282             DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, "");
283         cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(-1, "");
284     }
285     ASSERT_EQ(cellularDataController->cellularDataHandler_, nullptr);
286 }
287 
288 /**
289  * @tc.number   RemoveUid_ShouldReturnInvalidParam_WhenIdentLengthLessThanIdentPreLen
290  * @tc.name     test function branch
291  * @tc.desc     Function test
292  */
293 HWTEST_F(CellularDataServiceTest, RemoveUid_WhenIdentLengthLessThanIdentPreLen, TestSize.Level1)
294 {
295     NetRequest request;
296     request.ident = "";
297     int32_t result = service->RemoveUid(request);
298     EXPECT_EQ(result, CELLULAR_DATA_INVALID_PARAM);
299 }
300 
301 /**
302  * @tc.number   RemoveUid_ShouldReturnInvalidParam_WhenIdentIsNotValidDecValue
303  * @tc.name     test function branch
304  * @tc.desc     Function test
305  */
306 HWTEST_F(CellularDataServiceTest, RemoveUid_WhenIdentIsNotValidDecValue, TestSize.Level1)
307 {
308     NetRequest request;
309     request.ident = "simIdABC";
310     int32_t result = service->RemoveUid(request);
311     EXPECT_EQ(result, CELLULAR_DATA_INVALID_PARAM);
312 }
313 
314 /**
315  * @tc.number   RemoveUid_ShouldReturnInvalidParam_WhenCellularDataControllerIsNull
316  * @tc.name     test function branch
317  * @tc.desc     Function test
318  */
319 HWTEST_F(CellularDataServiceTest, RemoveUid_WhenCellularDataControllerIsNull, TestSize.Level1)
320 {
321     NetRequest request;
322     request.ident = "simId1";
323     int32_t result = service->RemoveUid(request);
324     EXPECT_EQ(result, CELLULAR_DATA_INVALID_PARAM);
325 }
326 
327 /**
328  * @tc.number   AddUid_ShouldReturnInvalidParam_WhenIdentLengthLessThanIdentPreLen
329  * @tc.name     test function branch
330  * @tc.desc     Function test
331  */
332 HWTEST_F(CellularDataServiceTest, AddUid_WhenIdentLengthLessThanIdentPreLen, TestSize.Level1)
333 {
334     NetRequest request;
335     request.ident = "";
336     int32_t result = service->AddUid(request);
337     EXPECT_EQ(result, CELLULAR_DATA_INVALID_PARAM);
338 }
339 
340 /**
341  * @tc.number   AddUid_ShouldReturnInvalidParam_WhenIdentIsNotValidDecValue
342  * @tc.name     test function branch
343  * @tc.desc     Function test
344  */
345 HWTEST_F(CellularDataServiceTest, AddUid_WhenIdentIsNotValidDecValue, TestSize.Level1)
346 {
347     NetRequest request;
348     request.ident = "simIdABC";
349     int32_t result = service->AddUid(request);
350     EXPECT_EQ(result, CELLULAR_DATA_INVALID_PARAM);
351 }
352 
353 /**
354  * @tc.number   AddUid_ShouldReturnInvalidParam_WhenCellularDataControllerIsNull
355  * @tc.name     test function branch
356  * @tc.desc     Function test
357  */
358 HWTEST_F(CellularDataServiceTest, AddUid_WhenCellularDataControllerIsNull, TestSize.Level1)
359 {
360     NetRequest request;
361     request.ident = "simId1";
362     int32_t result = service->AddUid(request);
363     EXPECT_EQ(result, CELLULAR_DATA_INVALID_PARAM);
364 }
365 
366 /**
367  * @tc.number   RemoveOrAddUidTest001
368  * @tc.name     test function branch
369  * @tc.desc     Function test
370  */
371 HWTEST_F(CellularDataServiceTest, RemoveOrAddUidTest001, TestSize.Level0)
372 {
373     NetRequest request;
374     request.ident = "simId123456789123";
375     EXPECT_EQ(service->AddUid(request), CELLULAR_DATA_INVALID_PARAM);
376     EXPECT_EQ(service->RemoveUid(request), CELLULAR_DATA_INVALID_PARAM);
377     request.ident = "simId12";
378     EXPECT_EQ(service->AddUid(request), CELLULAR_DATA_INVALID_PARAM);
379     EXPECT_EQ(service->RemoveUid(request), CELLULAR_DATA_INVALID_PARAM);
380 }
381 
382 /**
383  * @tc.number   EstablishAllApnsIfConnectableTest001
384  * @tc.name     test function branch
385  * @tc.desc     Function test
386  */
387 HWTEST_F(CellularDataServiceTest, EstablishAllApnsIfConnectableTest001, TestSize.Level0)
388 {
389     int32_t slotId = -1;
390     EXPECT_EQ(service->EstablishAllApnsIfConnectable(slotId), TELEPHONY_ERR_PERMISSION_ERR);
391     EXPECT_EQ(service->ReleaseCellularDataConnection(slotId), TELEPHONY_ERR_PERMISSION_ERR);
392 }
393 
394 } // namespace Telephony
395 } // namespace OHOS