• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include <gtest/gtest.h>
17 #include <iostream>
18 #include <string>
19 #include <unistd.h>
20 
21 #include "i_net_monitor_callback.h"
22 #include "net_all_capabilities.h"
23 #include "net_conn_service.h"
24 #include "netmanager_base_test_security.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 using namespace testing::ext;
30 
31 class NetConnHiEventTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp();
36     void TearDown();
37 
38     sptr<Network> GetNetwork();
39     sptr<NetLinkInfo> GetNetLinkInfo() const;
40     void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect);
41     void HandleDetectionResult(uint32_t supplierId, NetDetectionStatus netState);
42 };
43 
SetUpTestCase()44 void NetConnHiEventTest::SetUpTestCase() {}
45 
TearDownTestCase()46 void NetConnHiEventTest::TearDownTestCase() {}
47 
SetUp()48 void NetConnHiEventTest::SetUp() {}
49 
TearDown()50 void NetConnHiEventTest::TearDown() {}
51 
GetNetwork()52 sptr<Network> NetConnHiEventTest::GetNetwork()
53 {
54     int32_t netId = 100;
55     int32_t supplierId = 1001;
56     sptr<Network> network = (std::make_unique<Network>(netId, supplierId,
57                                                        std::bind(&NetConnHiEventTest::HandleDetectionResult, this,
58                                                                  std::placeholders::_1, std::placeholders::_2),
59                                                        BEARER_CELLULAR, nullptr))
60                                 .release();
61     return network;
62 }
63 
GetNetLinkInfo() const64 sptr<NetLinkInfo> NetConnHiEventTest::GetNetLinkInfo() const
65 {
66     sptr<NetLinkInfo> netLinkInfo = (std::make_unique<NetLinkInfo>()).release();
67     netLinkInfo->ifaceName_ = "test";
68     netLinkInfo->domain_ = "test";
69 
70     sptr<INetAddr> netAddr = (std::make_unique<INetAddr>()).release();
71     netAddr->type_ = INetAddr::IPV4;
72     netAddr->family_ = 0x10;
73     netAddr->prefixlen_ = 0x17;
74     netAddr->address_ = "192.168.2.0";
75     netAddr->netMask_ = "192.255.255.255";
76     netAddr->hostName_ = "netAddr";
77     netLinkInfo->netAddrList_.push_back(*netAddr);
78 
79     sptr<Route> route = (std::make_unique<Route>()).release();
80     route->iface_ = "iface0";
81     route->destination_.type_ = INetAddr::IPV4;
82     route->destination_.family_ = 0x10;
83     route->destination_.prefixlen_ = 0x17;
84     route->destination_.address_ = "192.168.2.0";
85     route->destination_.netMask_ = "192.255.255.255";
86     route->destination_.hostName_ = "netAddr";
87     route->gateway_.type_ = INetAddr::IPV4;
88     route->gateway_.family_ = 0x10;
89     route->gateway_.prefixlen_ = 0x17;
90     route->gateway_.address_ = "192.168.2.0";
91     route->gateway_.netMask_ = "192.255.255.255";
92     route->gateway_.hostName_ = "netAddr";
93     netLinkInfo->routeList_.push_back(*route);
94 
95     netLinkInfo->mtu_ = 0x5DC;
96     return netLinkInfo;
97 }
98 
HandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)99 void NetConnHiEventTest::HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) {}
HandleDetectionResult(uint32_t supplierId,NetDetectionStatus netState)100 void NetConnHiEventTest::HandleDetectionResult(uint32_t supplierId, NetDetectionStatus netState) {}
101 
102 /**
103  * @tc.name: NetConnHiEventTest_001
104  * @tc.desc: Test NetConnManager HiSysEvent:UpdateNetSupplierInfo
105  * @tc.type: FUNC
106  */
107 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_001, TestSize.Level1)
108 {
109     int32_t supplierId = 1001;
110     int32_t ret = NetConnService::GetInstance()->UpdateNetSupplierInfo(supplierId, nullptr);
111     EXPECT_NE(ret, NETMANAGER_SUCCESS);
112 }
113 
114 /**
115  * @tc.name: NetConnHiEventTest_002
116  * @tc.desc: Test NetConnManager HiSysEvent:UpdateNetLinkInfo
117  * @tc.type: FUNC
118  */
119 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_002, TestSize.Level1)
120 {
121     int32_t supplierId = 1001;
122     int32_t ret = NetConnService::GetInstance()->UpdateNetLinkInfo(supplierId, nullptr);
123     EXPECT_NE(ret, NETMANAGER_SUCCESS);
124 }
125 
126 /**
127  * @tc.name: NetConnHiEventTest_003
128  * @tc.desc: Test NetConnManager HiSysEvent:UpdateInterfaces
129  * @tc.type: FUNC
130  */
131 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_003, TestSize.Level1)
132 {
133     sptr<Network> network = GetNetwork();
134     ASSERT_NE(network, nullptr);
135     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
136     ASSERT_NE(netLinkInfo, nullptr);
137     network->UpdateInterfaces(*netLinkInfo);
138 }
139 
140 /**
141  * @tc.name: NetConnHiEventTest_004
142  * @tc.desc: Test NetConnManager HiSysEvent:UpdateIpAddrs
143  * @tc.type: FUNC
144  */
145 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_004, TestSize.Level1)
146 {
147     sptr<Network> network = GetNetwork();
148     ASSERT_NE(network, nullptr);
149     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
150     ASSERT_NE(netLinkInfo, nullptr);
151     network->UpdateIpAddrs(*netLinkInfo);
152 }
153 
154 /**
155  * @tc.name: NetConnHiEventTest_005
156  * @tc.desc: Test NetConnManager HiSysEvent:UpdateRoutes
157  * @tc.type: FUNC
158  */
159 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_005, TestSize.Level1)
160 {
161     sptr<Network> network = GetNetwork();
162     ASSERT_NE(network, nullptr);
163     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
164     ASSERT_NE(netLinkInfo, nullptr);
165     network->UpdateRoutes(*netLinkInfo);
166 }
167 
168 /**
169  * @tc.name: NetConnHiEventTest_006
170  * @tc.desc: Test NetConnManager HiSysEvent:UpdateDns
171  * @tc.type: FUNC
172  */
173 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_006, TestSize.Level1)
174 {
175     sptr<Network> network = GetNetwork();
176     ASSERT_NE(network, nullptr);
177     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
178     ASSERT_NE(netLinkInfo, nullptr);
179     network->UpdateDns(*netLinkInfo);
180 }
181 
182 /**
183  * @tc.name: NetConnHiEventTest_007
184  * @tc.desc: Test NetConnManager HiSysEvent:UpdateMtu
185  * @tc.type: FUNC
186  */
187 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_007, TestSize.Level1)
188 {
189     sptr<Network> network = GetNetwork();
190     ASSERT_NE(network, nullptr);
191     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
192     ASSERT_NE(netLinkInfo, nullptr);
193     network->UpdateMtu(*netLinkInfo);
194 }
195 
196 /**
197  * @tc.name: NetConnHiEventTest_009
198  * @tc.desc: Test NetConnManager HiSysEvent:SetDefaultNetWork
199  * @tc.type: FUNC
200  */
201 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_009, TestSize.Level1)
202 {
203     sptr<Network> network = GetNetwork();
204     ASSERT_NE(network, nullptr);
205     network->SetDefaultNetWork();
206 }
207 
208 /**
209  * @tc.name: NetConnHiEventTest_010
210  * @tc.desc: Test NetConnManager HiSysEvent:ClearDefaultNetWorkNetId
211  * @tc.type: FUNC
212  */
213 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_010, TestSize.Level1)
214 {
215     sptr<Network> network = GetNetwork();
216     ASSERT_NE(network, nullptr);
217     network->ClearDefaultNetWorkNetId();
218 }
219 
220 /**
221  * @tc.name: NetConnHiEventTest_011
222  * @tc.desc: Test NetConnManager HiSysEvent:RegisterNetConnCallback
223  * @tc.type: FUNC
224  */
225 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_011, TestSize.Level1)
226 {
227     NetManagerBaseAccessToken token;
228     int32_t ret = NetConnService::GetInstance()->RegisterNetConnCallback(nullptr, nullptr, 0);
229     EXPECT_NE(ret, NETMANAGER_SUCCESS);
230 }
231 
232 /**
233  * @tc.name: NetConnHiEventTest_012
234  * @tc.desc: Test NetConnManager HiSysEvent:UpdateTcpBufferSize
235  * @tc.type: FUNC
236  */
237 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_012, TestSize.Level1)
238 {
239     sptr<Network> network = GetNetwork();
240     ASSERT_NE(network, nullptr);
241     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
242     ASSERT_NE(netLinkInfo, nullptr);
243     network->UpdateTcpBufferSize(*netLinkInfo);
244 }
245 } // namespace NetManagerStandard
246 } // namespace OHOS
247