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 
16 #include <gtest/gtest.h>
17 
18 #include "core_service_client.h"
19 #include "cellular_data_client.h"
20 
21 #define private public
22 #include "net_connect_callback_impl.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::NWeb;
28 using namespace OHOS::Telephony;
29 
30 namespace OHOS {
31 namespace Telephony {
32 namespace {
33 int32_t g_slotId = 0;
34 }
35 
GetDefaultCellularDataSlotId()36 int32_t CellularDataClient::GetDefaultCellularDataSlotId()
37 {
38     return g_slotId;
39 }
40 }
41 namespace NWeb {
42 using namespace OHOS::NetManagerStandard;
43 class NetConnectCallbackImplTest : public testing::Test {
44 public:
45     static void SetUpTestCase(void);
46     static void TearDownTestCase(void);
47     void SetUp();
48     void TearDown();
49 };
50 
SetUpTestCase(void)51 void NetConnectCallbackImplTest::SetUpTestCase(void)
52 {}
53 
TearDownTestCase(void)54 void NetConnectCallbackImplTest::TearDownTestCase(void)
55 {}
56 
SetUp(void)57 void NetConnectCallbackImplTest::SetUp(void)
58 {}
59 
TearDown(void)60 void NetConnectCallbackImplTest::TearDown(void)
61 {}
62 
63 class NetConnCallbackTest : public NetConnCallback {
64 public:
65     NetConnCallbackTest() = default;
66     virtual ~NetConnCallbackTest() = default;
NetAvailable()67     int32_t NetAvailable() override
68     {
69         return 0;
70     }
NetCapabilitiesChange(const NetConnectType & netConnectType,const NetConnectSubtype & netConnectSubtype)71     int32_t NetCapabilitiesChange(const NetConnectType &netConnectType,
72         const NetConnectSubtype &netConnectSubtype) override
73     {
74         return 0;
75     }
NetConnectionPropertiesChange()76     int32_t NetConnectionPropertiesChange() override
77     {
78         return 0;
79     }
NetUnavailable()80     int32_t NetUnavailable() override
81     {
82         return 0;
83     }
OnNetCapabilitiesChanged(const std::shared_ptr<NetCapabilitiesAdapter> capabilities)84     int32_t OnNetCapabilitiesChanged(
85         const std::shared_ptr<NetCapabilitiesAdapter> capabilities) override
86     {
87         return 0;
88     }
OnNetConnectionPropertiesChanged(const std::shared_ptr<NetConnectionPropertiesAdapter> properties)89     int32_t OnNetConnectionPropertiesChanged(
90         const std::shared_ptr<NetConnectionPropertiesAdapter> properties) override
91     {
92         return 0;
93     }
94 };
95 
96 /**
97  * @tc.name: NetConnectCallbackImplTest_001.
98  * @tc.desc: test lock type.
99  * @tc.type: FUNC
100  * @tc.require:
101  */
102 HWTEST_F(NetConnectCallbackImplTest, NetConnectCallbackImplTest_001, TestSize.Level1)
103 {
104     std::shared_ptr<NetConnCallbackTest> cb = std::make_shared<NetConnCallbackTest>();
105     std::shared_ptr<NetConnectCallbackImpl> netConnectCallbackImpl = std::make_shared<NetConnectCallbackImpl>(cb);
106     EXPECT_NE(netConnectCallbackImpl, nullptr);
107     sptr<NetHandle> netHandle(new NetHandle);
108     sptr<NetHandle> netHandleNull(nullptr);
109     sptr<NetLinkInfo> info(new NetLinkInfo);
110     sptr<NetLinkInfo> infoNull(nullptr);
111     EXPECT_EQ(netConnectCallbackImpl->NetAvailable(netHandle), 0);
112     EXPECT_EQ(netConnectCallbackImpl->NetAvailable(netHandleNull), 0);
113     EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandle, info), 0);
114     EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandleNull, infoNull), 0);
115     EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandle, infoNull), 0);
116     EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandleNull, info), 0);
117     EXPECT_EQ(netConnectCallbackImpl->NetLost(netHandle), 0);
118     EXPECT_EQ(netConnectCallbackImpl->NetUnavailable(), 0);
119 
120     sptr<NetAllCapabilities> netAllCap(new NetAllCapabilities);
121     netAllCap->bearerTypes_.insert(NetBearType::BEARER_WIFI);
122     netAllCap->bearerTypes_.insert(NetBearType::BEARER_BLUETOOTH);
123     netAllCap->bearerTypes_.insert(NetBearType::BEARER_ETHERNET);
124     netAllCap->bearerTypes_.insert(NetBearType::BEARER_CELLULAR);
125     EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, netAllCap), 0);
126     g_slotId = -1;
127     EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, netAllCap), 0);
128     EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, nullptr), 0);
129     EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandleNull, netAllCap), 0);
130     EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandleNull, nullptr), 0);
131 }
132 
133 /**
134  * @tc.name: NetConnectCallbackImplTest_002.
135  * @tc.desc: test lock type.
136  * @tc.type: FUNC
137  * @tc.require:
138  */
139 HWTEST_F(NetConnectCallbackImplTest, NetConnectCallbackImplTest_002, TestSize.Level1)
140 {
141     std::shared_ptr<NetConnectCallbackImpl> netConnectCallbackImpl = std::make_shared<NetConnectCallbackImpl>(nullptr);
142     sptr<NetHandle> netHandle(new NetHandle);
143     sptr<NetLinkInfo> info(new NetLinkInfo);
144     EXPECT_EQ(netConnectCallbackImpl->NetAvailable(netHandle), 0);
145     EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandle, info), 0);
146     EXPECT_EQ(netConnectCallbackImpl->NetLost(netHandle), 0);
147     EXPECT_EQ(netConnectCallbackImpl->NetUnavailable(), 0);
148     EXPECT_EQ(netConnectCallbackImpl->NetBlockStatusChange(netHandle, false), 0);
149     sptr<NetAllCapabilities> netAllCap(new NetAllCapabilities);
150     netAllCap->bearerTypes_.insert(NetBearType::BEARER_WIFI);
151     EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, netAllCap), 0);
152 }
153 }
154 }
155