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 */
HWTEST_F(CellularDataServiceTest, CellularDataService_001, TestSize.Level0)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 */
HWTEST_F(CellularDataServiceTest, CellularDataService_002, TestSize.Level0)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_SUCCESS,
106 service->GetCellularDataSupplierId(DEFAULT_SIM_SLOT_ID, NetCap::NET_CAPABILITY_INTERNET, supplierId));
107 ASSERT_EQ(CELLULAR_DATA_INVALID_PARAM,
108 service->GetCellularDataSupplierId(DEFAULT_SIM_SLOT_ID, NetCap::NET_CAPABILITY_END, supplierId));
109 service->CorrectNetSupplierNoAvailable(DEFAULT_SIM_SLOT_ID);
110 int32_t regState = -1;
111 service->GetSupplierRegisterState(supplierId, regState);
112 service->OnStop();
113 }
114
115 /**
116 * @tc.number DataConnectionMonitor_HandleScreenStateChanged_001
117 * @tc.name test function branch
118 * @tc.desc Function test
119 */
HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_HandleScreenStateChanged_001, TestSize.Level0)120 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_HandleScreenStateChanged_001, TestSize.Level0)
121 {
122 std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
123 dataConnectionMonitor->isScreenOn_ = false;
124 dataConnectionMonitor->HandleScreenStateChanged(true);
125 ASSERT_EQ(dataConnectionMonitor->isScreenOn_, true);
126 dataConnectionMonitor->isScreenOn_ = false;
127 dataConnectionMonitor->HandleScreenStateChanged(false);
128 ASSERT_EQ(dataConnectionMonitor->isScreenOn_, false);
129 }
130
131 /**
132 * @tc.number DataConnectionMonitor_OnStallDetectionTimer_001
133 * @tc.name test function branch
134 * @tc.desc Function test
135 */
HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_OnStallDetectionTimer_001, TestSize.Level0)136 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_OnStallDetectionTimer_001, TestSize.Level0)
137 {
138 std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
139 dataConnectionMonitor->noRecvPackets_ = 20;
140 dataConnectionMonitor->stallDetectionEnabled_ = true;
141 dataConnectionMonitor->OnStallDetectionTimer();
142 ASSERT_EQ(dataConnectionMonitor->noRecvPackets_, 20);
143 }
144
145 /**
146 * @tc.number DataConnectionMonitor_HandleRecovery_001
147 * @tc.name test function branch
148 * @tc.desc Function test
149 */
HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_HandleRecovery_001, TestSize.Level0)150 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_HandleRecovery_001, TestSize.Level0)
151 {
152 std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
153 dataConnectionMonitor->dataRecoveryState_ = RecoveryState::STATE_REQUEST_CONTEXT_LIST;
154 dataConnectionMonitor->HandleRecovery();
155 ASSERT_EQ(dataConnectionMonitor->dataRecoveryState_, RecoveryState::STATE_CLEANUP_CONNECTIONS);
156 dataConnectionMonitor->dataRecoveryState_ = RecoveryState::STATE_REREGISTER_NETWORK;
157 dataConnectionMonitor->HandleRecovery();
158 ASSERT_EQ(dataConnectionMonitor->dataRecoveryState_, RecoveryState::STATE_RADIO_STATUS_RESTART);
159 dataConnectionMonitor->dataRecoveryState_ = RecoveryState::STATE_RADIO_STATUS_RESTART;
160 dataConnectionMonitor->HandleRecovery();
161 ASSERT_EQ(dataConnectionMonitor->dataRecoveryState_, RecoveryState::STATE_REQUEST_CONTEXT_LIST);
162 }
163
164 /**
165 * @tc.number DataConnectionMonitor_EndNetStatistics_001
166 * @tc.name test function branch
167 * @tc.desc Function test
168 */
HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_EndNetStatistics_001, TestSize.Level0)169 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_EndNetStatistics_001, TestSize.Level0)
170 {
171 std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
172 dataConnectionMonitor->dataFlowType_ = CellDataFlowType::DATA_FLOW_TYPE_DOWN;
173 dataConnectionMonitor->EndNetStatistics();
174 ASSERT_EQ(dataConnectionMonitor->dataFlowType_, CellDataFlowType::DATA_FLOW_TYPE_NONE);
175 }
176
177 /**
178 * @tc.number DataConnectionMonitor_UpdateNetTrafficState_001
179 * @tc.name test function branch
180 * @tc.desc Function test
181 */
HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_UpdateNetTrafficState_001, TestSize.Level0)182 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_UpdateNetTrafficState_001, TestSize.Level0)
183 {
184 std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
185 dataConnectionMonitor->updateNetStat_ = true;
186 dataConnectionMonitor->UpdateNetTrafficState();
187 std::shared_ptr<PreferredNetworkTypeInfo> preferredTypeInfo = std::make_shared<PreferredNetworkTypeInfo>();
188 auto event = AppExecFwk::InnerEvent::Get(0, preferredTypeInfo);
189 dataConnectionMonitor->SetPreferredNetworkPara(event);
190 ASSERT_EQ(dataConnectionMonitor->dataFlowType_, CellDataFlowType::DATA_FLOW_TYPE_NONE);
191 }
192
193 /**
194 * @tc.number DataConnectionMonitor_UpdateDataFlowType_001
195 * @tc.name test function branch
196 * @tc.desc Function test
197 */
HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_UpdateDataFlowType_001, TestSize.Level0)198 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_UpdateDataFlowType_001, TestSize.Level0)
199 {
200 std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
201 dataConnectionMonitor->dataFlowType_ = CellDataFlowType::DATA_FLOW_TYPE_DOWN;
202 dataConnectionMonitor->trafficManager_->sendPackets_ = 200;
203 dataConnectionMonitor->trafficManager_->recvPackets_ = 100;
204 dataConnectionMonitor->UpdateDataFlowType();
205 ASSERT_EQ(static_cast<int32_t>(dataConnectionMonitor->dataFlowType_),
206 static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE));
207 }
208
209 /**
210 * @tc.number DataConnectionMonitor_SetDataFlowType_001
211 * @tc.name test function branch
212 * @tc.desc Function test
213 */
HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_SetDataFlowType_001, TestSize.Level0)214 HWTEST_F(CellularDataServiceTest, DataConnectionMonitor_SetDataFlowType_001, TestSize.Level0)
215 {
216 std::shared_ptr<DataConnectionMonitor> dataConnectionMonitor = std::make_shared<DataConnectionMonitor>(0);
217 auto event = AppExecFwk::InnerEvent::Get(CellularDataEventCode::MSG_RUN_MONITOR_TASK);
218 dataConnectionMonitor->ProcessEvent(event);
219 event = AppExecFwk::InnerEvent::Get(CellularDataEventCode::MSG_STALL_DETECTION_EVENT_ID);
220 dataConnectionMonitor->ProcessEvent(event);
221 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_DATA_CALL_LIST_CHANGED);
222 dataConnectionMonitor->ProcessEvent(event);
223 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_GET_PREFERRED_NETWORK_MODE);
224 dataConnectionMonitor->ProcessEvent(event);
225 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_PREFERRED_NETWORK_MODE);
226 dataConnectionMonitor->ProcessEvent(event);
227 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_OFF);
228 dataConnectionMonitor->ProcessEvent(event);
229 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_ON);
230 dataConnectionMonitor->ProcessEvent(event);
231 dataConnectionMonitor->dataFlowType_ = CellDataFlowType::DATA_FLOW_TYPE_NONE;
232 dataConnectionMonitor->SetDataFlowType(CellDataFlowType::DATA_FLOW_TYPE_DOWN);
233 ASSERT_EQ(static_cast<int32_t>(dataConnectionMonitor->dataFlowType_),
234 static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DOWN));
235 }
236
237 /**
238 * @tc.number CellularDataController_SetIntelligenceSwitchEnable_001
239 * @tc.name test function branch
240 * @tc.desc Function test
241 */
HWTEST_F(CellularDataServiceTest, CellularDataController_SetIntelligenceSwitchEnable_001, TestSize.Level0)242 HWTEST_F(CellularDataServiceTest, CellularDataController_SetIntelligenceSwitchEnable_001, TestSize.Level0)
243 {
244 std::shared_ptr<CellularDataController> cellularDataController = std::make_shared<CellularDataController>(0);
245 cellularDataController->cellularDataHandler_ = nullptr;
246 int32_t result = cellularDataController->SetIntelligenceSwitchEnable(true);
247 ASSERT_EQ(result, TELEPHONY_ERR_LOCAL_PTR_NULL);
248 }
249
250 /**
251 * @tc.number CellularDataController_SetIntelligenceSwitchEnable_002
252 * @tc.name test function branch
253 * @tc.desc Function test
254 */
HWTEST_F(CellularDataServiceTest, CellularDataController_SetIntelligenceSwitchEnable_002, TestSize.Level0)255 HWTEST_F(CellularDataServiceTest, CellularDataController_SetIntelligenceSwitchEnable_002, TestSize.Level0)
256 {
257 std::shared_ptr<CellularDataController> cellularDataController = std::make_shared<CellularDataController>(0);
258 cellularDataController->Init();
259 int32_t result = cellularDataController->SetIntelligenceSwitchEnable(false);
260 ASSERT_EQ(result, 0);
261 }
262
263 /**
264 * @tc.number CellularDataController_OnAddSystemAbility_001
265 * @tc.name test function branch
266 * @tc.desc Function test
267 */
HWTEST_F(CellularDataServiceTest, CellularDataController_OnAddSystemAbility_001, TestSize.Level0)268 HWTEST_F(CellularDataServiceTest, CellularDataController_OnAddSystemAbility_001, TestSize.Level0)
269 {
270 std::shared_ptr<CellularDataController> cellularDataController = std::make_shared<CellularDataController>(0);
271 cellularDataController->systemAbilityListener_ =
272 new (std::nothrow) CellularDataController::SystemAbilityStatusChangeListener(0, nullptr);
273 if (cellularDataController->systemAbilityListener_ != nullptr) {
274 cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(
275 COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, "");
276 cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(
277 COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID, "");
278 cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(
279 DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, "");
280 cellularDataController->systemAbilityListener_->OnRemoveSystemAbility(-1, "");
281 }
282 ASSERT_EQ(cellularDataController->cellularDataHandler_, nullptr);
283 }
284
285 /**
286 * @tc.number RemoveOrAddUidTest001
287 * @tc.name test function branch
288 * @tc.desc Function test
289 */
HWTEST_F(CellularDataServiceTest, RemoveOrAddUidTest001, TestSize.Level0)290 HWTEST_F(CellularDataServiceTest, RemoveOrAddUidTest001, TestSize.Level0)
291 {
292 NetRequest request;
293 request.ident = "simId123456789123";
294 EXPECT_EQ(service->AddUid(request), CELLULAR_DATA_INVALID_PARAM);
295 EXPECT_EQ(service->RemoveUid(request), CELLULAR_DATA_INVALID_PARAM);
296 request.ident = "simId12";
297 EXPECT_EQ(service->AddUid(request), CELLULAR_DATA_INVALID_PARAM);
298 EXPECT_EQ(service->RemoveUid(request), CELLULAR_DATA_INVALID_PARAM);
299 }
300
301 /**
302 * @tc.number EstablishAllApnsIfConnectableTest001
303 * @tc.name test function branch
304 * @tc.desc Function test
305 */
HWTEST_F(CellularDataServiceTest, EstablishAllApnsIfConnectableTest001, TestSize.Level0)306 HWTEST_F(CellularDataServiceTest, EstablishAllApnsIfConnectableTest001, TestSize.Level0)
307 {
308 int32_t slotId = -1;
309 EXPECT_EQ(service->EstablishAllApnsIfConnectable(slotId), TELEPHONY_ERR_PERMISSION_ERR);
310 EXPECT_EQ(service->ReleaseCellularDataConnection(slotId), TELEPHONY_ERR_PERMISSION_ERR);
311 }
312
313 } // namespace Telephony
314 } // namespace OHOS