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 "apn_holder.h"
19#include "apn_manager.h"
20#include "cellular_data_state_machine.h"
21#include "cellular_data_client.h"
22#include "data_connection_manager.h"
23#include "gtest/gtest.h"
24#include "tel_event_handler.h"
25#include "pdp_profile_data.h"
26
27namespace OHOS {
28namespace Telephony {
29using namespace testing::ext;
30
31class ApnManagerTest : public testing::Test {
32public:
33    static void SetUpTestCase();
34    static void TearDownTestCase();
35    void SetUp();
36    void TearDown();
37    std::shared_ptr<ApnManager> apnManager;
38};
39void ApnManagerTest::SetUpTestCase() {}
40
41void ApnManagerTest::TearDownTestCase() {}
42
43void ApnManagerTest::SetUp()
44{
45    apnManager = std::make_shared<ApnManager>();
46}
47
48void ApnManagerTest::TearDown()
49{
50    apnManager.reset();
51}
52
53class StateMachineTest : public TelEventHandler {
54public:
55    StateMachineTest() : TelEventHandler("StateMachineTest") {}
56    ~StateMachineTest() = default;
57    std::shared_ptr<CellularDataStateMachine> CreateCellularDataStateMachine(int32_t slotId);
58
59public:
60    std::shared_ptr<CellularDataStateMachine> cellularDataStateMachine_ = nullptr;
61};
62
63std::shared_ptr<CellularDataStateMachine> StateMachineTest::CreateCellularDataStateMachine(int32_t slotId)
64{
65    if (cellularDataStateMachine_ != nullptr) {
66        return cellularDataStateMachine_;
67    }
68    sptr<DataConnectionManager> connectionManager = std::make_unique<DataConnectionManager>(slotId).release();
69    if (connectionManager == nullptr) {
70        return nullptr;
71    }
72    connectionManager->Init();
73    cellularDataStateMachine_ = std::make_shared<CellularDataStateMachine>(
74        connectionManager, std::static_pointer_cast<TelEventHandler>(shared_from_this()));
75    return cellularDataStateMachine_;
76}
77
78/**
79 * @tc.number   FindApnNameByApnId_001
80 * @tc.name     test function branch
81 * @tc.desc     Function test
82 */
83HWTEST_F(ApnManagerTest, FindApnNameByApnId_001, Function | MediumTest | Level1)
84{
85    int32_t id = 1;
86    std::string result = apnManager->FindApnNameByApnId(id);
87    ASSERT_EQ(result, DATA_CONTEXT_ROLE_DEFAULT);
88}
89
90/**
91 * @tc.number   FindApnNameByApnId_002
92 * @tc.name     test function branch
93 * @tc.desc     Function test
94 */
95HWTEST_F(ApnManagerTest, FindApnNameByApnId_002, Function | MediumTest | Level1)
96{
97    int32_t id = 2;
98    std::string result = apnManager->FindApnNameByApnId(id);
99    ASSERT_EQ(result, DATA_CONTEXT_ROLE_MMS);
100}
101
102/**
103 * @tc.number   FindApnIdByCapability_001
104 * @tc.name     test function branch
105 * @tc.desc     Function test
106 */
107HWTEST_F(ApnManagerTest, FindApnIdByCapability_001, Function | MediumTest | Level1)
108{
109    uint64_t capability = NetManagerStandard::NetCap::NET_CAPABILITY_INTERNET;
110    int32_t expected = DATA_CONTEXT_ROLE_DEFAULT_ID;
111    int32_t actual = apnManager->FindApnIdByCapability(capability);
112    ASSERT_EQ(actual, expected);
113}
114
115/**
116 * @tc.number   FindApnIdByCapability_002
117 * @tc.name     test function branch
118 * @tc.desc     Function test
119 */
120HWTEST_F(ApnManagerTest, FindApnIdByCapability_002, Function | MediumTest | Level1)
121{
122    uint64_t capability = NetManagerStandard::NetCap::NET_CAPABILITY_MMS;
123    int32_t expected = DATA_CONTEXT_ROLE_MMS_ID;
124    int32_t actual = apnManager->FindApnIdByCapability(capability);
125    ASSERT_EQ(actual, expected);
126}
127
128/**
129 * @tc.number   FindApnIdByCapability_003
130 * @tc.name     test function branch
131 * @tc.desc     Function test
132 */
133HWTEST_F(ApnManagerTest, FindApnIdByCapability_003, Function | MediumTest | Level1)
134{
135    uint64_t capability = NetManagerStandard::NetCap::NET_CAPABILITY_INTERNAL_DEFAULT;
136    int32_t expected = DATA_CONTEXT_ROLE_INTERNAL_DEFAULT_ID;
137    int32_t actual = apnManager->FindApnIdByCapability(capability);
138    ASSERT_EQ(actual, expected);
139}
140
141/**
142 * @tc.number   FindApnIdByCapability_004
143 * @tc.name     test function branch
144 * @tc.desc     Function test
145 */
146HWTEST_F(ApnManagerTest, FindApnIdByCapability_004, Function | MediumTest | Level1)
147{
148    uint64_t capability = NetManagerStandard::NetCap::NET_CAPABILITY_IA;
149    int32_t expected = DATA_CONTEXT_ROLE_IA_ID;
150    int32_t actual = apnManager->FindApnIdByCapability(capability);
151    ASSERT_EQ(actual, expected);
152}
153
154/**
155 * @tc.number   FindApnIdByCapability_005
156 * @tc.name     test function branch
157 * @tc.desc     Function test
158 */
159HWTEST_F(ApnManagerTest, FindApnIdByCapability_005, Function | MediumTest | Level1)
160{
161    uint64_t capability = NetManagerStandard::NetCap::NET_CAPABILITY_XCAP;
162    int32_t expected = DATA_CONTEXT_ROLE_XCAP_ID;
163    int32_t actual = apnManager->FindApnIdByCapability(capability);
164    ASSERT_EQ(actual, expected);
165}
166
167/**
168 * @tc.number   FindApnIdByCapability_006
169 * @tc.name     test function branch
170 * @tc.desc     Function test
171 */
172HWTEST_F(ApnManagerTest, FindApnIdByCapability_006, Function | MediumTest | Level1)
173{
174    uint64_t capability = NetManagerStandard::NetCap::NET_CAPABILITY_SUPL;
175    int32_t expected = DATA_CONTEXT_ROLE_SUPL_ID;
176    int32_t actual = apnManager->FindApnIdByCapability(capability);
177    ASSERT_EQ(actual, expected);
178}
179
180/**
181 * @tc.number   FindApnIdByCapability_007
182 * @tc.name     test function branch
183 * @tc.desc     Function test
184 */
185HWTEST_F(ApnManagerTest, FindApnIdByCapability_007, Function | MediumTest | Level1)
186{
187    uint64_t capability = NetManagerStandard::NetCap::NET_CAPABILITY_DUN;
188    int32_t expected = DATA_CONTEXT_ROLE_DUN_ID;
189    int32_t actual = apnManager->FindApnIdByCapability(capability);
190    ASSERT_EQ(actual, expected);
191}
192
193/**
194 * @tc.number   FindApnIdByCapability_008
195 * @tc.name     test function branch
196 * @tc.desc     Function test
197 */
198HWTEST_F(ApnManagerTest, FindApnIdByCapability_008, Function | MediumTest | Level1)
199{
200    uint64_t capability = 100;
201    int32_t expected = DATA_CONTEXT_ROLE_INVALID_ID;
202    int32_t actual = apnManager->FindApnIdByCapability(capability);
203    ASSERT_EQ(actual, expected);
204}
205
206/**
207 * @tc.number   FindBestCapability_001
208 * @tc.name     test function branch
209 * @tc.desc     Function test
210 */
211HWTEST_F(ApnManagerTest, FindBestCapability_001, Function | MediumTest | Level1)
212{
213    uint64_t capabilities = 1L << NetManagerStandard::NetCap::NET_CAPABILITY_SUPL;
214    NetManagerStandard::NetCap netCap = apnManager->FindBestCapability(capabilities);
215    ASSERT_EQ(netCap, NetManagerStandard::NetCap::NET_CAPABILITY_SUPL);
216}
217
218/**
219 * @tc.number   FindBestCapability_002
220 * @tc.name     test function branch
221 * @tc.desc     Function test
222 */
223HWTEST_F(ApnManagerTest, FindBestCapability_002, Function | MediumTest | Level1)
224{
225    uint64_t capabilities = 1L << NetManagerStandard::NetCap::NET_CAPABILITY_DUN;
226    NetManagerStandard::NetCap netCap = apnManager->FindBestCapability(capabilities);
227    ASSERT_EQ(netCap, NetManagerStandard::NetCap::NET_CAPABILITY_DUN);
228}
229
230/**
231 * @tc.number   FindBestCapability_003
232 * @tc.name     test function branch
233 * @tc.desc     Function test
234 */
235HWTEST_F(ApnManagerTest, FindBestCapability_003, Function | MediumTest | Level1)
236{
237    uint64_t capabilities = 1L << NetManagerStandard::NetCap::NET_CAPABILITY_XCAP;
238    NetManagerStandard::NetCap netCap = apnManager->FindBestCapability(capabilities);
239    ASSERT_EQ(netCap, NetManagerStandard::NetCap::NET_CAPABILITY_XCAP);
240}
241
242/**
243 * @tc.number   FindBestCapability_004
244 * @tc.name     test function branch
245 * @tc.desc     Function test
246 */
247HWTEST_F(ApnManagerTest, FindBestCapability_004, Function | MediumTest | Level1)
248{
249    uint64_t capabilities = 1L << NetManagerStandard::NetCap::NET_CAPABILITY_IA;
250    NetManagerStandard::NetCap netCap = apnManager->FindBestCapability(capabilities);
251    ASSERT_EQ(netCap, NetManagerStandard::NetCap::NET_CAPABILITY_IA);
252}
253
254/**
255 * @tc.number   CreateMvnoApnItems_001
256 * @tc.name     test function branch
257 * @tc.desc     Function test
258 */
259HWTEST_F(ApnManagerTest, CreateMvnoApnItems_001, Function | MediumTest | Level1)
260{
261    int32_t slotId = 0;
262    std::string mcc = "460";
263    std::string mnc = "00";
264    int32_t result = apnManager->CreateMvnoApnItems(slotId, mcc, mnc);
265    ASSERT_EQ(result, 0);
266}
267
268/**
269 * @tc.number   IsPreferredApnUserEdited_001
270 * @tc.name     test function branch
271 * @tc.desc     Function test
272 */
273HWTEST_F(ApnManagerTest, IsPreferredApnUserEdited_001, Function | MediumTest | Level1)
274{
275    auto preferId = 1;
276    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
277    defaultApnItem->attr_.profileId_ = preferId;
278    defaultApnItem->attr_.isEdited_ = true;
279    apnManager->allApnItem_.push_back(defaultApnItem);
280    apnManager->preferId_ = preferId;
281    ASSERT_TRUE(apnManager->IsPreferredApnUserEdited());
282}
283
284/**
285 * @tc.number   IsPreferredApnUserEdited_002
286 * @tc.name     test function branch
287 * @tc.desc     Function test
288 */
289HWTEST_F(ApnManagerTest, IsPreferredApnUserEdited_002, Function | MediumTest | Level1)
290{
291    auto preferId = 1;
292    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
293    defaultApnItem->attr_.profileId_ = preferId;
294    defaultApnItem->attr_.isEdited_ = false;
295    apnManager->allApnItem_.push_back(defaultApnItem);
296    apnManager->preferId_ = preferId;
297    ASSERT_FALSE(apnManager->IsPreferredApnUserEdited());
298}
299
300/**
301 * @tc.number   IsPreferredApnUserEdited_003
302 * @tc.name     test function branch
303 * @tc.desc     Function test
304 */
305HWTEST_F(ApnManagerTest, IsPreferredApnUserEdited_003, Function | MediumTest | Level1)
306{
307    auto preferId = 2;
308    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
309    defaultApnItem->attr_.profileId_ = 3;
310    defaultApnItem->attr_.isEdited_ = true;
311    apnManager->allApnItem_.push_back(defaultApnItem);
312    apnManager->preferId_ = preferId;
313    ASSERT_FALSE(apnManager->IsPreferredApnUserEdited());
314}
315
316/**
317 * @tc.number   IsDataConnectionNotUsed_001
318 * @tc.name     test function branch
319 * @tc.desc     Function test
320 */
321HWTEST_F(ApnManagerTest, IsDataConnectionNotUsed_001, Function | MediumTest | Level1)
322{
323    std::shared_ptr<CellularDataStateMachine> stateMachine = nullptr;
324    bool result = apnManager->IsDataConnectionNotUsed(stateMachine);
325    ASSERT_FALSE(result);
326}
327
328/**
329 * @tc.number   IsDataConnectionNotUsed_002
330 * @tc.name     test function branch
331 * @tc.desc     Function test
332 */
333HWTEST_F(ApnManagerTest, IsDataConnectionNotUsed_002, Function | MediumTest | Level1)
334{
335    std::shared_ptr<StateMachineTest> machine = std::make_shared<StateMachineTest>();
336    auto stateMachine = machine->CreateCellularDataStateMachine(0);
337    apnManager->apnHolders_.push_back(nullptr);
338    bool result = apnManager->IsDataConnectionNotUsed(stateMachine);
339    ASSERT_TRUE(result);
340}
341
342/**
343 * @tc.number   IsDataConnectionNotUsed_003
344 * @tc.name     test function branch
345 * @tc.desc     Function test
346 */
347HWTEST_F(ApnManagerTest, IsDataConnectionNotUsed_003, Function | MediumTest | Level1)
348{
349    std::shared_ptr<StateMachineTest> machine = std::make_shared<StateMachineTest>();
350    auto stateMachine = machine->CreateCellularDataStateMachine(0);
351    sptr<ApnHolder> apnHolder = std::make_unique<ApnHolder>(DATA_CONTEXT_ROLE_DEFAULT,
352        static_cast<int32_t>(DataContextPriority::PRIORITY_LOW)).release();
353    apnHolder->SetCellularDataStateMachine(stateMachine);
354    apnManager->apnHolders_.push_back(apnHolder);
355    bool result = apnManager->IsDataConnectionNotUsed(stateMachine);
356    ASSERT_FALSE(result);
357}
358
359/**
360 * @tc.number   IsDataConnectionNotUsed_004
361 * @tc.name     test function branch
362 * @tc.desc     Function test
363 */
364HWTEST_F(ApnManagerTest, IsDataConnectionNotUsed_004, Function | MediumTest | Level1)
365{
366    std::shared_ptr<StateMachineTest> machine = std::make_shared<StateMachineTest>();
367    auto stateMachine = machine->CreateCellularDataStateMachine(0);
368    sptr<ApnHolder> apnHolder = std::make_unique<ApnHolder>(DATA_CONTEXT_ROLE_DEFAULT,
369        static_cast<int32_t>(DataContextPriority::PRIORITY_LOW)).release();
370    apnHolder->SetCellularDataStateMachine(nullptr);
371    apnManager->apnHolders_.push_back(apnHolder);
372    bool result = apnManager->IsDataConnectionNotUsed(stateMachine);
373    ASSERT_TRUE(result);
374}
375
376/**
377 * @tc.number   IsDataConnectionNotUsed_005
378 * @tc.name     test function branch
379 * @tc.desc     Function test
380 */
381HWTEST_F(ApnManagerTest, IsDataConnectionNotUsed_005, Function | MediumTest | Level1)
382{
383    std::shared_ptr<StateMachineTest> machine = std::make_shared<StateMachineTest>();
384    auto stateMachine = machine->CreateCellularDataStateMachine(0);
385    sptr<ApnHolder> apnHolder = std::make_unique<ApnHolder>(DATA_CONTEXT_ROLE_DEFAULT,
386        static_cast<int32_t>(DataContextPriority::PRIORITY_LOW)).release();
387    apnHolder->SetCellularDataStateMachine(stateMachine);
388    apnManager->apnHolders_.push_back(apnHolder);
389    machine->cellularDataStateMachine_ = nullptr;
390    auto stateMachine_1 = machine->CreateCellularDataStateMachine(0);
391    bool result = apnManager->IsDataConnectionNotUsed(stateMachine_1);
392    ASSERT_TRUE(result);
393}
394
395/**
396 * @tc.number   MakeSpecificApnItem_001
397 * @tc.name     test function branch
398 * @tc.desc     Function test
399 */
400HWTEST_F(ApnManagerTest, MakeSpecificApnItem_001, Function | MediumTest | Level1)
401{
402    auto preferId = 1;
403    apnManager->preferId_ = preferId;
404    PdpProfile pdpProfile;
405    pdpProfile.profileId = preferId;
406    pdpProfile.apnTypes = "";
407    std::vector<PdpProfile> apnVec;
408    apnVec.push_back(pdpProfile);
409    bool result = apnManager->MakeSpecificApnItem(apnVec);
410    ASSERT_EQ(result, 1);
411}
412
413/**
414 * @tc.number   MakeSpecificApnItem_002
415 * @tc.name     test function branch
416 * @tc.desc     Function test
417 */
418HWTEST_F(ApnManagerTest, MakeSpecificApnItem_002, Function | MediumTest | Level1)
419{
420    auto preferId = 1;
421    apnManager->preferId_ = preferId;
422    PdpProfile pdpProfile;
423    pdpProfile.profileId = preferId;
424    pdpProfile.apnTypes = "default";
425    std::vector<PdpProfile> apnVec;
426    apnVec.push_back(pdpProfile);
427    bool result = apnManager->MakeSpecificApnItem(apnVec);
428    ASSERT_EQ(result, 1);
429}
430
431/**
432 * @tc.number   MakeSpecificApnItem_003
433 * @tc.name     test function branch
434 * @tc.desc     Function test
435 */
436HWTEST_F(ApnManagerTest, MakeSpecificApnItem_003, Function | MediumTest | Level1)
437{
438    auto preferId = 1;
439    apnManager->preferId_ = 2;
440    PdpProfile pdpProfile;
441    pdpProfile.profileId = preferId;
442    pdpProfile.apnTypes = "default";
443    std::vector<PdpProfile> apnVec;
444    apnVec.push_back(pdpProfile);
445    bool result = apnManager->MakeSpecificApnItem(apnVec);
446    ASSERT_EQ(result, 1);
447}
448
449/**
450 * @tc.number   HasAnyConnectedState_001
451 * @tc.name     test function branch
452 * @tc.desc     Function test
453 */
454HWTEST_F(ApnManagerTest, HasAnyConnectedState_001, Function | MediumTest | Level1)
455{
456    std::vector<sptr<ApnHolder>> apnHolders;
457    apnManager->apnHolders_ = apnHolders;
458    bool result = apnManager->HasAnyConnectedState();
459    ASSERT_EQ(result, false);
460}
461
462/**
463 * @tc.number   HasAnyConnectedState_002
464 * @tc.name     test function branch
465 * @tc.desc     Function test
466 */
467HWTEST_F(ApnManagerTest, HasAnyConnectedState_002, Function | MediumTest | Level1)
468{
469    std::vector<sptr<ApnHolder>> apnHolders;
470    sptr<ApnHolder> apnHolder = nullptr;
471    apnHolders.push_back(apnHolder);
472    apnManager->apnHolders_ = apnHolders;
473    bool result = apnManager->HasAnyConnectedState();
474    ASSERT_EQ(result, false);
475}
476
477/**
478 * @tc.number   HasAnyConnectedState_003
479 * @tc.name     test function branch
480 * @tc.desc     Function test
481 */
482HWTEST_F(ApnManagerTest, HasAnyConnectedState_003, Function | MediumTest | Level1)
483{
484    std::vector<sptr<ApnHolder>> apnHolders;
485    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
486    apnHolder->SetApnState(ApnProfileState::PROFILE_STATE_CONNECTED);
487    apnHolders.push_back(apnHolder);
488    apnManager->apnHolders_ = apnHolders;
489    bool result = apnManager->HasAnyConnectedState();
490    ASSERT_EQ(result, true);
491}
492
493/**
494 * @tc.number   HasAnyConnectedState_004
495 * @tc.name     test function branch
496 * @tc.desc     Function test
497 */
498HWTEST_F(ApnManagerTest, HasAnyConnectedState_004, Function | MediumTest | Level1)
499{
500    std::vector<sptr<ApnHolder>> apnHolders;
501    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
502    apnHolder->SetApnState(ApnProfileState::PROFILE_STATE_DISCONNECTING);
503    apnHolders.push_back(apnHolder);
504    apnManager->apnHolders_ = apnHolders;
505    bool result = apnManager->HasAnyConnectedState();
506    ASSERT_EQ(result, true);
507}
508
509/**
510 * @tc.number   GetRilAttachApn_001
511 * @tc.name     test function branch
512 * @tc.desc     Function test
513 */
514HWTEST_F(ApnManagerTest, GetRilAttachApn_001, Function | MediumTest | Level1)
515{
516    std::vector<sptr<ApnItem>> allApnItem;
517    apnManager->allApnItem_ = allApnItem;
518    ASSERT_EQ(apnManager->GetRilAttachApn(), nullptr);
519}
520
521/**
522 * @tc.number   ReleaseDataConnection_001
523 * @tc.name     test function branch
524 * @tc.desc     Function test
525 */
526HWTEST_F(ApnManagerTest, ReleaseDataConnection_001, TestSize.Level0)
527{
528    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
529    apnHolder->cellularDataStateMachine_ = nullptr;
530    apnHolder->ReleaseDataConnection();
531    ASSERT_EQ(apnHolder->cellularDataStateMachine_, nullptr);
532}
533
534/**
535 * @tc.number   ReleaseDataConnection_002
536 * @tc.name     test function branch
537 * @tc.desc     Function test
538 */
539HWTEST_F(ApnManagerTest, ReleaseDataConnection_002, TestSize.Level0)
540{
541    std::shared_ptr<StateMachineTest> machine = std::make_shared<StateMachineTest>();
542    auto stateMachine = machine->CreateCellularDataStateMachine(0);
543    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
544    apnHolder->SetCellularDataStateMachine(stateMachine);
545    apnHolder->ReleaseDataConnection();
546    ASSERT_EQ(apnHolder->cellularDataStateMachine_, nullptr);
547}
548
549/**
550 * @tc.number   RequestCellularData_001
551 * @tc.name     test function branch
552 * @tc.desc     Function test
553 */
554HWTEST_F(ApnManagerTest, RequestCellularData_001, TestSize.Level0)
555{
556    NetRequest netRequest;
557    netRequest.capability = 0;
558    netRequest.ident = "ident";
559    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
560    apnHolder->RequestCellularData(netRequest);
561    ASSERT_EQ(apnHolder->dataCallEnabled_, true);
562}
563
564/**
565 * @tc.number   RequestCellularData_002
566 * @tc.name     test function branch
567 * @tc.desc     Function test
568 */
569HWTEST_F(ApnManagerTest, RequestCellularData_002, TestSize.Level0)
570{
571    NetRequest netRequest;
572    netRequest.capability = 0;
573    netRequest.ident = "ident";
574    NetRequest netRequest1;
575    netRequest1.capability = 0;
576    netRequest1.ident = "abc";
577    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
578    apnHolder->netRequests_.push_back(netRequest1);
579    apnHolder->RequestCellularData(netRequest);
580    ASSERT_EQ(apnHolder->dataCallEnabled_, true);
581}
582
583/**
584 * @tc.number   RequestCellularData_003
585 * @tc.name     test function branch
586 * @tc.desc     Function test
587 */
588HWTEST_F(ApnManagerTest, RequestCellularData_003, TestSize.Level0)
589{
590    NetRequest netRequest;
591    netRequest.capability = 0;
592    netRequest.ident = "ident";
593    NetRequest netRequest1;
594    netRequest1.capability = 1;
595    netRequest1.ident = "ident";
596    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
597    apnHolder->netRequests_.push_back(netRequest1);
598    apnHolder->RequestCellularData(netRequest);
599    ASSERT_EQ(apnHolder->dataCallEnabled_, true);
600}
601
602/**
603 * @tc.number   RequestCellularData_004
604 * @tc.name     test function branch
605 * @tc.desc     Function test
606 */
607HWTEST_F(ApnManagerTest, RequestCellularData_004, TestSize.Level0)
608{
609    NetRequest netRequest;
610    netRequest.capability = 1;
611    netRequest.ident = "ident";
612    sptr<ApnHolder> apnHolder = new ApnHolder("", 0);
613    apnHolder->netRequests_.push_back(netRequest);
614    int size = apnHolder->netRequests_.size();
615    apnHolder->RequestCellularData(netRequest);
616    ASSERT_EQ(size, apnHolder->netRequests_.size());
617}
618
619/**
620 * @tc.number   IsCompatibleApnItem_001
621 * @tc.name     test function branch
622 * @tc.desc     Function test
623 */
624HWTEST_F(ApnManagerTest, IsCompatibleApnItem_001, TestSize.Level0)
625{
626    sptr<ApnItem> newApnItem = nullptr;
627    sptr<ApnItem> oldApnItem = nullptr;
628    bool roamingState = false;
629    bool result = ApnHolder::IsCompatibleApnItem(newApnItem, oldApnItem, roamingState);
630    ASSERT_FALSE(result);
631}
632
633/**
634 * @tc.number   IsCompatibleApnItem_002
635 * @tc.name     test function branch
636 * @tc.desc     Function test
637 */
638HWTEST_F(ApnManagerTest, IsCompatibleApnItem_002, TestSize.Level0)
639{
640    sptr<ApnItem> newApnItem = new ApnItem();
641    sptr<ApnItem> oldApnItem = new ApnItem();
642    std::strcmp(newApnItem->attr_.roamingProtocol_, "test_protocol");
643    std::strcmp(newApnItem->attr_.roamingProtocol_, "test_protocol");
644    bool roamingState = true;
645    bool result = ApnHolder::IsCompatibleApnItem(newApnItem, oldApnItem, roamingState);
646    ASSERT_TRUE(result);
647}
648
649/**
650 * @tc.number   IsCompatibleApnItem_003
651 * @tc.name     test function branch
652 * @tc.desc     Function test
653 */
654HWTEST_F(ApnManagerTest, IsCompatibleApnItem_003, TestSize.Level0)
655{
656    sptr<ApnItem> newApnItem = new ApnItem();
657    sptr<ApnItem> oldApnItem = new ApnItem();
658    std::strcmp(newApnItem->attr_.roamingProtocol_, "test_protocol");
659    std::strcmp(newApnItem->attr_.roamingProtocol_, "test_protocol");
660    bool roamingState = false;
661    bool result = ApnHolder::IsCompatibleApnItem(newApnItem, oldApnItem, roamingState);
662    ASSERT_TRUE(result);
663}
664
665/**
666 * @tc.number   GetNextRetryApnItem001
667 * @tc.name     test function branch
668 * @tc.desc     Function test
669 */
670HWTEST_F(ApnManagerTest, GetNextRetryApnItem001, TestSize.Level0)
671{
672    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
673    connectionRetryPolicy->matchedApns_.clear();
674    EXPECT_EQ(connectionRetryPolicy->GetNextRetryApnItem(), nullptr);
675
676    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
677    defaultApnItem->MarkBadApn(true);
678    connectionRetryPolicy->matchedApns_.push_back(defaultApnItem);
679    connectionRetryPolicy->currentApnIndex_ = 0;
680    EXPECT_EQ(connectionRetryPolicy->GetNextRetryApnItem(), nullptr);
681    defaultApnItem->MarkBadApn(false);
682    EXPECT_NE(connectionRetryPolicy->GetNextRetryApnItem(), nullptr);
683}
684
685/**
686 * @tc.number   GetNextRetryApnItem002
687 * @tc.name     test function branch
688 * @tc.desc     Function test
689 */
690HWTEST_F(ApnManagerTest, GetNextRetryApnItem002, TestSize.Level0)
691{
692    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
693    connectionRetryPolicy->currentApnIndex_ = 10;
694    EXPECT_EQ(connectionRetryPolicy->currentApnIndex_, 10);
695}
696
697/**
698 * @tc.number   GetNextRetryApnItem_003
699 * @tc.name     test function branch
700 * @tc.desc     Function test
701 */
702HWTEST_F(ApnManagerTest, GetNextRetryApnItem_003, TestSize.Level0)
703{
704    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
705    connectionRetryPolicy->currentApnIndex_ = -1;
706    EXPECT_EQ(connectionRetryPolicy->currentApnIndex_, -1);
707}
708
709/**
710 * @tc.number   GetNextRetryApnItem_004
711 * @tc.name     test function branch
712 * @tc.desc     Function test
713 */
714HWTEST_F(ApnManagerTest, GetNextRetryApnItem_004, TestSize.Level0)
715{
716    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
717    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
718    defaultApnItem->MarkBadApn(true);
719    connectionRetryPolicy->matchedApns_.push_back(defaultApnItem);
720    connectionRetryPolicy->currentApnIndex_ = 0;
721    EXPECT_EQ(connectionRetryPolicy->currentApnIndex_, 0);
722}
723
724/**
725 * @tc.number   GetNextRetryApnItem_005
726 * @tc.name     test function branch
727 * @tc.desc     Function test
728 */
729HWTEST_F(ApnManagerTest, GetNextRetryApnItem_005, TestSize.Level0)
730{
731    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
732    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
733    defaultApnItem->MarkBadApn(false);
734    connectionRetryPolicy->matchedApns_.push_back(defaultApnItem);
735    connectionRetryPolicy->tryCount_ = 10;
736    connectionRetryPolicy->maxCount_ = 0;
737    connectionRetryPolicy->currentApnIndex_ = 0;
738    EXPECT_EQ(connectionRetryPolicy->currentApnIndex_, 0);
739}
740
741/**
742 * @tc.number   GetNextRetryApnItem_006
743 * @tc.name     test function branch
744 * @tc.desc     Function test
745 */
746HWTEST_F(ApnManagerTest, GetNextRetryApnItem_006, TestSize.Level0)
747{
748    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
749    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
750    defaultApnItem->MarkBadApn(false);
751    connectionRetryPolicy->matchedApns_.push_back(defaultApnItem);
752    connectionRetryPolicy->tryCount_ = -1;
753    connectionRetryPolicy->maxCount_ = 0;
754    connectionRetryPolicy->currentApnIndex_ = 0;
755    EXPECT_EQ(connectionRetryPolicy->currentApnIndex_, 0);
756}
757
758/**
759 * @tc.number   GetNextRetryApnItem_007
760 * @tc.name     test function branch
761 * @tc.desc     Function test
762 */
763HWTEST_F(ApnManagerTest, GetNextRetryApnItem_007, TestSize.Level0)
764{
765    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
766    connectionRetryPolicy->matchedApns_.clear();
767    connectionRetryPolicy->currentApnIndex_ = 0;
768    EXPECT_EQ(connectionRetryPolicy->GetNextRetryApnItem(), nullptr);
769}
770
771/**
772 * @tc.number   OnPropChanged_001
773 * @tc.name     test function branch
774 * @tc.desc     Function test
775 */
776HWTEST_F(ApnManagerTest, OnPropChanged_001, TestSize.Level0)
777{
778    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
779    connectionRetryPolicy->OnPropChanged(nullptr, nullptr, nullptr);
780    EXPECT_EQ(connectionRetryPolicy->isPropOn_, true);
781    connectionRetryPolicy->OnPropChanged("persist.telephony.retrystrategy.allow", nullptr, nullptr);
782    EXPECT_EQ(connectionRetryPolicy->isPropOn_, true);
783    connectionRetryPolicy->OnPropChanged("persist.telephony.retrystrategy.allow", "true", nullptr);
784    EXPECT_EQ(connectionRetryPolicy->isPropOn_, true);
785    connectionRetryPolicy->OnPropChanged("fakeKey", nullptr, nullptr);
786    EXPECT_EQ(connectionRetryPolicy->isPropOn_, true);
787    connectionRetryPolicy->OnPropChanged("fakeKey", "true", nullptr);
788    EXPECT_EQ(connectionRetryPolicy->isPropOn_, true);
789    connectionRetryPolicy->OnPropChanged("persist.telephony.setupfail.delay", "3000", nullptr);
790    EXPECT_EQ(connectionRetryPolicy->defaultSetupFailDelay_, 3000);
791    connectionRetryPolicy->OnPropChanged("persist.telephony.modemdend.delay", "1000", nullptr);
792    EXPECT_EQ(connectionRetryPolicy->defaultModemDendDelay_, 1000);
793    connectionRetryPolicy->OnPropChanged("persist.telephony.setupfail.delay", "0x3000", nullptr);
794    EXPECT_EQ(connectionRetryPolicy->defaultSetupFailDelay_, 3000);
795    connectionRetryPolicy->OnPropChanged("persist.telephony.modemdend.delay", "0x1000", nullptr);
796    EXPECT_EQ(connectionRetryPolicy->defaultModemDendDelay_, 1000);
797}
798
799/**
800 * @tc.number   IsAllBadApn_001
801 * @tc.name     test function branch
802 * @tc.desc     Function test
803 */
804HWTEST_F(ApnManagerTest, IsAllBadApn_001, TestSize.Level0)
805{
806    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
807    connectionRetryPolicy->matchedApns_.clear();
808    EXPECT_TRUE(connectionRetryPolicy->IsAllBadApn());
809    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
810    defaultApnItem->MarkBadApn(false);
811    connectionRetryPolicy->matchedApns_.push_back(defaultApnItem);
812    EXPECT_FALSE(connectionRetryPolicy->IsAllBadApn());
813    defaultApnItem->MarkBadApn(true);
814    EXPECT_TRUE(connectionRetryPolicy->IsAllBadApn());
815}
816
817/**
818 * @tc.number   GetNextRetryDelay_001
819 * @tc.name     test function branch
820 * @tc.desc     Function test
821 */
822HWTEST_F(ApnManagerTest, GetNextRetryDelay_001, TestSize.Level0)
823{
824    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
825    connectionRetryPolicy->matchedApns_.clear();
826    auto delay = connectionRetryPolicy->GetNextRetryDelay(DATA_CONTEXT_ROLE_DEFAULT, 0, 0,
827        RetryScene::RETRY_SCENE_OTHERS, 0);
828    EXPECT_GE(delay, 0);
829    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
830    defaultApnItem->MarkBadApn(true);
831    connectionRetryPolicy->matchedApns_.push_back(defaultApnItem);
832    delay = connectionRetryPolicy->GetNextRetryDelay(DATA_CONTEXT_ROLE_DEFAULT, 0, 0, RetryScene::RETRY_SCENE_OTHERS,
833        0);
834    EXPECT_GE(delay, 0);
835}
836
837/**
838 * @tc.number   GetNextRetryDelay_002
839 * @tc.name     test function branch
840 * @tc.desc     Function test
841 */
842HWTEST_F(ApnManagerTest, GetNextRetryDelay_002, TestSize.Level0)
843{
844    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
845    connectionRetryPolicy->matchedApns_.clear();
846    sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
847    defaultApnItem->MarkBadApn(false);
848    connectionRetryPolicy->matchedApns_.push_back(defaultApnItem);
849    auto delay = connectionRetryPolicy->GetNextRetryDelay(DATA_CONTEXT_ROLE_DEFAULT, 0, 0,
850        RetryScene::RETRY_SCENE_OTHERS, 0);
851    EXPECT_GE(delay, 0);
852    connectionRetryPolicy->isPropOn_ = false;
853    delay = connectionRetryPolicy->GetNextRetryDelay(DATA_CONTEXT_ROLE_DEFAULT, 0, 0, RetryScene::RETRY_SCENE_OTHERS,
854        0);
855    EXPECT_GE(delay, 0);
856    connectionRetryPolicy->isPropOn_ = true;
857    delay = connectionRetryPolicy->GetNextRetryDelay(DATA_CONTEXT_ROLE_DEFAULT, 0, 0, RetryScene::RETRY_SCENE_OTHERS,
858        0);
859    EXPECT_GE(delay, 0);
860    delay = connectionRetryPolicy->GetNextRetryDelay(DATA_CONTEXT_ROLE_MMS, 0, 0, RetryScene::RETRY_SCENE_OTHERS, 0);
861    EXPECT_GE(delay, 0);
862}
863
864/**
865 * @tc.number   SetMatchedApns_001
866 * @tc.name     test function branch
867 * @tc.desc     Function test
868 */
869HWTEST_F(ApnManagerTest, SetMatchedApns_001, TestSize.Level0)
870{
871    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
872    std::vector<sptr<ApnItem>> matchedApns = {};
873    connectionRetryPolicy->SetMatchedApns(matchedApns);
874    EXPECT_EQ(connectionRetryPolicy->GetMatchedApns().size(), 0);
875}
876
877/**
878 * @tc.number   GetRandomDelay_001
879 * @tc.name     test function branch
880 * @tc.desc     Function test
881 */
882HWTEST_F(ApnManagerTest, GetRandomDelay_001, TestSize.Level0)
883{
884    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
885    EXPECT_GE(connectionRetryPolicy->GetRandomDelay(), 0);
886    EXPECT_LE(connectionRetryPolicy->GetRandomDelay(), 2000);
887}
888
889/**
890 * @tc.number   ConvertPdpErrorToDisconnReason_001
891 * @tc.name     test function branch
892 * @tc.desc     Function test
893 */
894HWTEST_F(ApnManagerTest, ConvertPdpErrorToDisconnReason_001, TestSize.Level0)
895{
896    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
897    auto res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
898        PdpErrorReason::PDP_ERR_OPERATOR_DETERMINED_BARRING);
899    EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT);
900    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
901        PdpErrorReason::PDP_ERR_MISSING_OR_UNKNOWN_APN);
902    EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT);
903    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
904        PdpErrorReason::PDP_ERR_UNKNOWN_PDP_ADDR_OR_TYPE);
905    EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT);
906    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
907        PdpErrorReason::PDP_ERR_USER_VERIFICATION);
908    EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT);
909    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
910        PdpErrorReason::PDP_ERR_ACTIVATION_REJECTED_GGSN);
911    EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT);
912    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
913        PdpErrorReason::PDP_ERR_SERVICE_OPTION_NOT_SUPPORTED);
914    EXPECT_EQ(res, DisConnectionReason::REASON_RETRY_CONNECTION);
915    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
916        PdpErrorReason::PDP_ERR_REQUESTED_SERVICE_OPTION_NOT_SUBSCRIBED);
917    EXPECT_EQ(res, DisConnectionReason::REASON_RETRY_CONNECTION);
918    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
919        PdpErrorReason::PDP_ERR_NSAPI_ALREADY_USED);
920    EXPECT_EQ(res, DisConnectionReason::REASON_RETRY_CONNECTION);
921    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
922        PdpErrorReason::PDP_ERR_IPV4_ONLY_ALLOWED);
923    EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT);
924    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
925        PdpErrorReason::PDP_ERR_IPV6_ONLY_ALLOWED);
926    EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT);
927    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
928        PdpErrorReason::PDP_ERR_PROTOCOL_ERRORS);
929    EXPECT_EQ(res, DisConnectionReason::REASON_RETRY_CONNECTION);
930    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
931        PdpErrorReason::PDP_ERR_UNKNOWN_TO_CLEAR_CONNECTION);
932    EXPECT_EQ(res, DisConnectionReason::REASON_CLEAR_CONNECTION);
933    res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason(
934        PdpErrorReason::PDP_ERR_RETRY);
935    EXPECT_EQ(res, DisConnectionReason::REASON_RETRY_CONNECTION);
936}
937
938/**
939 * @tc.number   InitialRetryCountValue_001
940 * @tc.name     test function branch
941 * @tc.desc     Function test
942 */
943HWTEST_F(ApnManagerTest, InitialRetryCountValue_001, TestSize.Level0)
944{
945    std::shared_ptr<ConnectionRetryPolicy> connectionRetryPolicy = std::make_shared<ConnectionRetryPolicy>();
946    connectionRetryPolicy->InitialRetryCountValue();
947    EXPECT_EQ(connectionRetryPolicy->tryCount_, 0);
948}
949
950/**
951 * @tc.number   EnableCellularDataRoaming_001
952 * @tc.name     test function branch
953 * @tc.desc     Function test
954 */
955HWTEST_F(ApnManagerTest, EnableCellularDataRoaming_001, TestSize.Level0)
956{
957    int32_t result = CellularDataClient::GetInstance().EnableCellularDataRoaming(0, true);
958    EXPECT_NE(result, true);
959}
960
961/**
962 * @tc.number   HasInternetCapability_001
963 * @tc.name     test function branch
964 * @tc.desc     Function test
965 */
966HWTEST_F(ApnManagerTest, HasInternetCapability_001, TestSize.Level0)
967{
968    int32_t result = CellularDataClient::GetInstance().HasInternetCapability(0, 0);
969    EXPECT_EQ(result, false);
970}
971
972/**
973 * @tc.number   HandleApnChanged_001
974 * @tc.name     test function branch
975 * @tc.desc     Function test
976 */
977HWTEST_F(ApnManagerTest, HandleApnChanged_001, TestSize.Level0)
978{
979    int32_t result = CellularDataClient::GetInstance().HandleApnChanged(0);
980    EXPECT_NE(result, true);
981}
982
983/**
984 * @tc.number   UpdateDefaultCellularDataSlotId_001
985 * @tc.name     test function branch
986 * @tc.desc     Function test
987 */
988HWTEST_F(ApnManagerTest, UpdateDefaultCellularDataSlotId_001, TestSize.Level0)
989{
990    int32_t result = CellularDataClient::GetInstance().UpdateDefaultCellularDataSlotId();
991    EXPECT_EQ(result, 0);
992}
993} // namespace Telephony
994} // namespace OHOS