1 /*
2 * Copyright (C) 2022 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 #include "apn_holder.h"
18 #include "apn_item.h"
19 #include "cellular_data_client.h"
20 #include "cellular_data_controller.h"
21 #include "cellular_data_dump_helper.h"
22 #include "cellular_data_handler.h"
23 #include "cellular_data_hisysevent.h"
24 #include "cellular_data_rdb_observer.h"
25 #include "cellular_data_roaming_observer.h"
26 #include "cellular_data_service.h"
27 #include "cellular_data_setting_observer.h"
28 #include "cellular_data_utils.h"
29 #include "common_event_manager.h"
30 #include "common_event_support.h"
31 #include "data_connection_manager.h"
32 #include "data_connection_monitor.h"
33 #include "gtest/gtest.h"
34 #include "net_manager_call_back.h"
35 #include "net_manager_tactics_call_back.h"
36 #include "network_search_callback.h"
37 #include "telephony_errors.h"
38 #include "telephony_hisysevent.h"
39 #include "telephony_log_wrapper.h"
40
41 namespace OHOS {
42 namespace Telephony {
43 using namespace testing::ext;
44
45 namespace {
46 const int32_t INVALID_SLOTID = -1;
47 const int32_t INVALID_SLOTID_TWO = 5;
48 const int32_t INVALID_CID = -1;
49 const int32_t INVALID_FD = -1;
50 } // namespace
51
52 class DemoHandler : public AppExecFwk::EventHandler {
53 public:
DemoHandler(std::shared_ptr<AppExecFwk::EventRunner> & runner)54 explicit DemoHandler(std::shared_ptr<AppExecFwk::EventRunner> &runner) : AppExecFwk::EventHandler(runner) {}
~DemoHandler()55 virtual ~DemoHandler() {}
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)56 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) {}
57 };
58
59 class BranchTest : public testing::Test {
60 public:
61 static void SetUpTestCase();
62 static void TearDownTestCase();
63 void SetUp();
64 void TearDown();
65 };
SetUpTestCase()66 void BranchTest::SetUpTestCase() {}
67
TearDownTestCase()68 void BranchTest::TearDownTestCase() {}
69
SetUp()70 void BranchTest::SetUp() {}
71
TearDown()72 void BranchTest::TearDown() {}
73
74 /**
75 * @tc.number Telephony_CellularDataHandler_001
76 * @tc.name test error branch
77 * @tc.desc Function test
78 */
79 HWTEST_F(BranchTest, Telephony_CellularDataHandler_001, Function | MediumTest | Level1)
80 {
81 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("test");
82 EventFwk::MatchingSkills matchingSkills;
83 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED);
84 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
85 CellularDataHandler cellularDataHandler { runner, subscriberInfo, 0 };
86 NetRequest request;
87 ASSERT_FALSE(cellularDataHandler.ReleaseNet(request));
88 ASSERT_FALSE(cellularDataHandler.RequestNet(request));
89 auto event = AppExecFwk::InnerEvent::Get(0);
90 event = nullptr;
91 cellularDataHandler.ProcessEvent(event);
92 EventFwk::CommonEventData data;
93 cellularDataHandler.OnReceiveEvent(data);
94 ASSERT_NE(cellularDataHandler.SetCellularDataEnable(true), TELEPHONY_ERR_SUCCESS);
95 bool dataEnaled = false;
96 cellularDataHandler.IsCellularDataEnabled(dataEnaled);
97 ASSERT_FALSE(dataEnaled);
98 bool dataRoamingEnabled = false;
99 cellularDataHandler.IsCellularDataRoamingEnabled(dataRoamingEnabled);
100 ASSERT_FALSE(dataRoamingEnabled);
101 ASSERT_NE(cellularDataHandler.SetCellularDataRoamingEnabled(true), TELEPHONY_ERR_SUCCESS);
102 ASSERT_EQ(ApnProfileState::PROFILE_STATE_IDLE, cellularDataHandler.GetCellularDataState());
103 ASSERT_EQ(ApnProfileState::PROFILE_STATE_IDLE, cellularDataHandler.GetCellularDataState(""));
104 sptr<ApnHolder> apnHolder;
105 DisConnectionReason reason = cellularDataHandler.GetDisConnectionReason();
106 cellularDataHandler.ClearConnection(apnHolder, reason);
107 cellularDataHandler.EstablishAllApnsIfConnectable();
108 cellularDataHandler.ClearAllConnections(reason);
109 cellularDataHandler.GetSlotId();
110 ASSERT_FALSE(cellularDataHandler.HandleApnChanged());
111 cellularDataHandler.HandleApnChanged(event);
112 cellularDataHandler.GetCellularDataFlowType();
113 cellularDataHandler.SetPolicyDataOn(true);
114 ASSERT_FALSE(cellularDataHandler.IsRestrictedMode());
115 cellularDataHandler.GetDisConnectionReason();
116 ASSERT_FALSE(cellularDataHandler.HasInternetCapability(0));
117 ASSERT_EQ(nullptr, cellularDataHandler.FindIdleCellularDataConnection());
118 cellularDataHandler.AttemptEstablishDataConnection(apnHolder);
119 ASSERT_FALSE(cellularDataHandler.CheckAttachAndSimState(apnHolder));
120 ASSERT_FALSE(cellularDataHandler.CheckRoamingState(apnHolder));
121 ASSERT_FALSE(cellularDataHandler.CheckApnState(apnHolder));
122 }
123
124 /**
125 * @tc.number Telephony_CellularDataHandler_002
126 * @tc.name test error branch
127 * @tc.desc Function test
128 */
129 HWTEST_F(BranchTest, Telephony_CellularDataHandler_002, Function | MediumTest | Level1)
130 {
131 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("test");
132 EventFwk::MatchingSkills matchingSkills;
133 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED);
134 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
135 CellularDataHandler cellularDataHandler { runner, subscriberInfo, 0 };
136 NetRequest request;
137 auto event = AppExecFwk::InnerEvent::Get(0);
138 event = nullptr;
139 sptr<ApnHolder> apnHolder;
140 cellularDataHandler.RadioPsConnectionAttached(event);
141 cellularDataHandler.RadioPsConnectionDetached(event);
142 cellularDataHandler.RoamingStateOn(event);
143 cellularDataHandler.RoamingStateOff(event);
144 cellularDataHandler.EstablishDataConnectionComplete(event);
145 cellularDataHandler.DisconnectDataComplete(event);
146 cellularDataHandler.MsgEstablishDataConnection(event);
147 cellularDataHandler.MsgRequestNetwork(event);
148 cellularDataHandler.HandleSettingSwitchChanged(event);
149 cellularDataHandler.HandleVoiceCallChanged(0);
150 cellularDataHandler.HandleSimStateOrRecordsChanged(event);
151 cellularDataHandler.HandleSimAccountLoaded(event);
152 cellularDataHandler.HandleRadioStateChanged(event);
153 cellularDataHandler.HandleDsdsModeChanged(event);
154 cellularDataHandler.SetRilAttachApnResponse(event);
155 cellularDataHandler.GetDefaultConfiguration();
156 cellularDataHandler.HandleRadioNrStateChanged(event);
157 cellularDataHandler.HandleRadioNrFrequencyChanged(event);
158 cellularDataHandler.HandleDBSettingEnableChanged(event);
159 cellularDataHandler.HandleDBSettingRoamingChanged(event);
160 cellularDataHandler.SetDataPermittedResponse(event);
161 cellularDataHandler.RegisterDataSettingObserver();
162 cellularDataHandler.UnRegisterDataSettingObserver();
163 ASSERT_FALSE(cellularDataHandler.HasAnyHigherPriorityConnection(apnHolder));
164 }
165
166 /**
167 * @tc.number Telephony_CellularDataHandler_003
168 * @tc.name test error branch
169 * @tc.desc Function test
170 */
171 HWTEST_F(BranchTest, Telephony_CellularDataHandler_003, Function | MediumTest | Level3)
172 {
173 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("test");
174 EventFwk::MatchingSkills matchingSkills;
175 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED);
176 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
177 CellularDataHandler cellularDataHandler { runner, subscriberInfo, INVALID_SLOTID };
178 cellularDataHandler.apnManager_ = std::make_unique<ApnManager>().release();
179 NetRequest request;
180 ASSERT_FALSE(cellularDataHandler.ReleaseNet(request));
181 sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
182 DisConnectionReason reason = DisConnectionReason::REASON_NORMAL;
183 cellularDataHandler.ClearConnection(apnHolder, reason);
184 cellularDataHandler.EstablishAllApnsIfConnectable();
185 cellularDataHandler.ClearAllConnections(reason);
186 cellularDataHandler.connectionManager_ = std::make_unique<DataConnectionManager>(runner, INVALID_SLOTID).release();
187 cellularDataHandler.ClearAllConnections(reason);
188 cellularDataHandler.EstablishAllApnsIfConnectable();
189 ASSERT_FALSE(cellularDataHandler.CheckApnState(apnHolder));
190 cellularDataHandler.AttemptEstablishDataConnection(apnHolder);
191 cellularDataHandler.connectionManager_ = nullptr;
192 auto event = AppExecFwk::InnerEvent::Get(0);
193 event = nullptr;
194 cellularDataHandler.MsgEstablishDataConnection(event);
195 ASSERT_FALSE(cellularDataHandler.HasAnyHigherPriorityConnection(apnHolder));
196 cellularDataHandler.connectionManager_ = std::make_unique<DataConnectionManager>(runner, INVALID_SLOTID).release();
197 ASSERT_FALSE(cellularDataHandler.HasInternetCapability(INVALID_CID));
198 }
199
200 /**
201 * @tc.number Telephony_CellularDataService_001
202 * @tc.name test error branch
203 * @tc.desc Function test
204 */
205 HWTEST_F(BranchTest, Telephony_CellularDataService_001, Function | MediumTest | Level3)
206 {
207 CellularDataService service;
208 std::vector<std::u16string> strV;
209 ASSERT_EQ(TELEPHONY_ERR_FAIL, service.Dump(INVALID_FD, strV));
210 service.state_ = ServiceRunningState::STATE_RUNNING;
211 service.OnStart();
212 bool dataEnabled = false;
213 bool dataRoamingEnabled = false;
214 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.IsCellularDataEnabled(dataEnabled));
215 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.EnableCellularData(false));
216 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.GetCellularDataState());
217 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.IsCellularDataRoamingEnabled(INVALID_SLOTID, dataRoamingEnabled));
218 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.EnableCellularDataRoaming(INVALID_SLOTID, false));
219 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.HandleApnChanged(INVALID_SLOTID));
220 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.GetDefaultCellularDataSlotId());
221 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.SetDefaultCellularDataSlotId(INVALID_SLOTID));
222 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.GetCellularDataFlowType());
223 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.HasInternetCapability(INVALID_SLOTID, 0));
224 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.ClearCellularDataConnections(INVALID_SLOTID));
225 DisConnectionReason reason = DisConnectionReason::REASON_NORMAL;
226 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.ClearAllConnections(INVALID_SLOTID, reason));
227 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.StrategySwitch(INVALID_SLOTID, false));
228 NetRequest request;
229 request.ident = "simId12";
230 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.RequestNet(request));
231 request.ident = "simId2";
232 ASSERT_NE(TELEPHONY_ERR_SUCCESS, service.ReleaseNet(request));
233 ASSERT_FALSE(service.CheckParamValid(INVALID_SLOTID));
234 ASSERT_FALSE(service.CheckParamValid(INVALID_SLOTID_TWO));
235 }
236
237 /**
238 * @tc.number CellularDataController_001
239 * @tc.name test error branch
240 * @tc.desc Function test
241 */
242 HWTEST_F(BranchTest, Telephony_CellularDataController_001, Function | MediumTest | Level3)
243 {
244 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("test");
245 CellularDataController controller { runner, 0 };
246 controller.RegisterEvents();
247 controller.Init();
248 controller.cellularDataHandler_ = nullptr;
249 controller.cellularDataRdbObserver_ = nullptr;
250 NetRequest request;
251 ASSERT_FALSE(controller.ReleaseNet(request));
252 ASSERT_FALSE(controller.RequestNet(request));
253 ASSERT_NE(controller.SetCellularDataEnable(true), TELEPHONY_ERR_SUCCESS);
254 bool dataEnabled = false;
255 controller.IsCellularDataEnabled(dataEnabled);
256 ASSERT_FALSE(dataEnabled);
257 ASSERT_NE(controller.SetCellularDataRoamingEnabled(true), TELEPHONY_ERR_SUCCESS);
258 EXPECT_EQ(ApnProfileState::PROFILE_STATE_FAILED, controller.GetCellularDataState());
259 EXPECT_EQ(ApnProfileState::PROFILE_STATE_FAILED, controller.GetCellularDataState(""));
260 bool dataRoamingEnabled = false;
261 controller.IsCellularDataRoamingEnabled(dataRoamingEnabled);
262 ASSERT_FALSE(dataRoamingEnabled);
263 ASSERT_FALSE(controller.HandleApnChanged());
264 auto event = AppExecFwk::InnerEvent::Get(0);
265 controller.ProcessEvent(event);
266 event = nullptr;
267 controller.ProcessEvent(event);
268 controller.RegisterEvents();
269 ASSERT_FALSE(controller.IsRestrictedMode());
270 ASSERT_EQ(DisConnectionReason::REASON_NORMAL, controller.GetDisConnectionReason());
271 controller.HasInternetCapability(0);
272 DisConnectionReason reason = DisConnectionReason::REASON_NORMAL;
273 ASSERT_FALSE(controller.ClearAllConnections(reason));
274 controller.UnRegisterEvents();
275 ASSERT_FALSE(controller.HandleApnChanged());
276 ASSERT_FALSE(controller.GetCellularDataFlowType());
277 }
278
279 /**
280 * @tc.number CellularDataConnectionManager_001
281 * @tc.name test error branch
282 * @tc.desc Function test
283 */
284 HWTEST_F(BranchTest, Telephony_CellularDataConnectionManager_001, Function | MediumTest | Level3)
285 {
286 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("test");
287 DataConnectionManager con { runner, 0 };
288 con.connectionMonitor_ = nullptr;
289 con.ccmDefaultState_ = nullptr;
290 std::shared_ptr<CellularDataStateMachine> stateMachine = nullptr;
291 con.RemoveConnectionStateMachine(stateMachine);
292 ASSERT_TRUE(con.GetActiveConnectionByCid(0) == nullptr);
293 ASSERT_TRUE(con.isNoActiveConnection());
294 auto event = AppExecFwk::InnerEvent::Get(0);
295 event = nullptr;
296 CcmDefaultState ccmDefaultState { con, "CcmDefaultState" };
297 ASSERT_FALSE(ccmDefaultState.StateProcess(event));
298 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_CONNECTED);
299 ASSERT_TRUE(ccmDefaultState.StateProcess(event));
300 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_DATA_CALL_LIST_CHANGED);
301 ASSERT_TRUE(ccmDefaultState.StateProcess(event));
302 event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_LINK_CAPABILITY_CHANGED);
303 ASSERT_TRUE(ccmDefaultState.StateProcess(event));
304 event = AppExecFwk::InnerEvent::Get(0);
305 ASSERT_FALSE(ccmDefaultState.StateProcess(event));
306 ccmDefaultState.RadioDataCallListChanged(event);
307 ccmDefaultState.UpdateNetworkInfo(event);
308 ccmDefaultState.RadioLinkCapabilityChanged(event);
309 con.GetDataFlowType();
310 con.GetDefaultBandWidthsConfig();
311 con.GetDefaultTcpBufferConfig();
312 con.SetDataFlowType(CellDataFlowType::DATA_FLOW_TYPE_NONE);
313 ASSERT_EQ("", con.GetTcpBufferByRadioTech(0));
314 ASSERT_TRUE(con.GetBandwidthsByRadioTech(0).upBandwidth == DEFAULT_BANDWIDTH);
315 }
316
317 /**
318 * @tc.number DataConnectionMonitor_001
319 * @tc.name test error branch
320 * @tc.desc Function test
321 */
322 HWTEST_F(BranchTest, Telephony_DataConnectionMonitor_001, Function | MediumTest | Level3)
323 {
324 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("test");
325 DataConnectionMonitor mon { runner, 0 };
326 mon.trafficManager_ = nullptr;
327 mon.stallDetectionTrafficManager_ = nullptr;
328 mon.UpdateFlowInfo();
329 auto event = AppExecFwk::InnerEvent::Get(0);
330 mon.SetPreferredNetworkPara(event);
331 mon.UpdateDataFlowType();
332 mon.ProcessEvent(event);
333 event = nullptr;
334 mon.ProcessEvent(event);
335 ASSERT_EQ(CellDataFlowType::DATA_FLOW_TYPE_NONE, mon.GetDataFlowType());
336 }
337
338 /**
339 * @tc.number CellularDataUtils_001
340 * @tc.name test error branch
341 * @tc.desc Function test
342 */
343 HWTEST_F(BranchTest, Telephony_CellularDataUtils_001, Function | MediumTest | Level3)
344 {
345 ASSERT_EQ("unknown",
346 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_UNKNOWN)));
347 ASSERT_EQ(
348 "EDGE", CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_GSM)));
349 ASSERT_EQ("1xRTT",
350 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_1XRTT)));
351 ASSERT_EQ("UMTS",
352 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_WCDMA)));
353 ASSERT_EQ(
354 "HSPA", CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_HSPA)));
355 ASSERT_EQ("HSPAP",
356 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_HSPAP)));
357 ASSERT_EQ("TD-SCDMA",
358 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_TD_SCDMA)));
359 ASSERT_EQ(
360 "EVDO", CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_EVDO)));
361 ASSERT_EQ("eHRPD",
362 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_EHRPD)));
363 ASSERT_EQ(
364 "LTE", CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_LTE)));
365 ASSERT_EQ("LTE_CA",
366 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_LTE_CA)));
367 ASSERT_EQ("IWAN",
368 CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_IWLAN)));
369 ASSERT_EQ(
370 "NR", CellularDataUtils::ConvertRadioTechToRadioName(static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_NR)));
371 ASSERT_FALSE(CellularDataUtils::IsDigit(""));
372 ASSERT_TRUE(CellularDataUtils::Split("", "").empty());
373 AddressInfo info;
374 ASSERT_TRUE(CellularDataUtils::ParseDotIpData(
375 "a1.a2.a3.a4.a5.a6.a7.a8.a9.a10.a11.a12.a13.a14.a15.a16.m1.m2.m3.m4.m5.m6.m7.m8.m9.m10.m11.m12.m13.m14.m15.m16",
376 info));
377 ASSERT_TRUE(CellularDataUtils::ParseDotIpData("a1.a2.a3.a4.m1.m2.m3.m4", info));
378 EXPECT_GE(DelayedSingleton<CellularDataClient>::GetInstance()->GetCellularDataFlowType(), 0);
379 auto recipient =
380 std::make_shared<CellularDataClient::CellularDataDeathRecipient>(CellularDataClient::GetInstance());
381 recipient->OnRemoteDied(nullptr);
382 EXPECT_GE(DelayedSingleton<CellularDataClient>::GetInstance()->GetCellularDataFlowType(), 0);
383 }
384
385 } // namespace Telephony
386 } // namespace OHOS
387