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 #include <iostream>
18 #include <string>
19 #include <unistd.h>
20
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "net_conn_service.h"
24 #include "net_conn_security.h"
25 #include "net_all_capabilities.h"
26 #include "iservice_registry.h"
27 #include "i_net_monitor_callback.h"
28 #include "system_ability_definition.h"
29 #include "token_setproc.h"
30
31 namespace OHOS {
32 namespace NetManagerStandard {
33 namespace {
34 using namespace testing::ext;
35 using namespace Security::AccessToken;
36 using Security::AccessToken::AccessTokenID;
37
38 HapInfoParams testInfoParms = {
39 .userID = 1,
40 .bundleName = "net_conn_hievent_test",
41 .instIndex = 0,
42 .appIDDesc = "test",
43 };
44
45 PermissionDef testPermDef = {
46 .permissionName = "ohos.permission.GET_NETWORK_INFO",
47 .bundleName = "net_conn_hievent_test",
48 .grantMode = 1,
49 .availableLevel = APL_SYSTEM_BASIC,
50 .label = "label",
51 .labelId = 1,
52 .description = "Test net connect maneger HiSysEvent",
53 .descriptionId = 1,
54 };
55
56 PermissionStateFull testState = {
57 .permissionName = "ohos.permission.GET_NETWORK_INFO",
58 .isGeneral = true,
59 .resDeviceID = {"local"},
60 .grantStatus = {PermissionState::PERMISSION_GRANTED},
61 .grantFlags = {2},
62 };
63
64 HapPolicyParams testPolicyPrams = {
65 .apl = APL_SYSTEM_BASIC,
66 .domain = "test.domain",
67 .permList = {testPermDef},
68 .permStateList = {testState},
69 };
70 } // namespace
71
72 class NetConnHiEventTest : public testing::Test {
73 public:
74 static void SetUpTestCase();
75 static void TearDownTestCase();
76 void SetUp();
77 void TearDown();
78
79 sptr<Network> GetNetwork();
80 sptr<NetLinkInfo> GetNetLinkInfo() const;
81 void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect);
82 void HandleDetectionResult(uint32_t supplierId, bool ifValid);
83 };
84
SetUpTestCase()85 void NetConnHiEventTest::SetUpTestCase() {}
86
TearDownTestCase()87 void NetConnHiEventTest::TearDownTestCase() {}
88
SetUp()89 void NetConnHiEventTest::SetUp() {}
90
TearDown()91 void NetConnHiEventTest::TearDown() {}
92
GetNetwork()93 sptr<Network> NetConnHiEventTest::GetNetwork()
94 {
95 int32_t netId = 100;
96 int32_t supplierId = 1001;
97 sptr<Network> network = (std::make_unique<Network>(netId, supplierId,
98 std::bind(&NetConnHiEventTest::HandleDetectionResult, this,
99 std::placeholders::_1, std::placeholders::_2),
100 BEARER_CELLULAR, nullptr))
101 .release();
102 return network;
103 }
104
GetNetLinkInfo() const105 sptr<NetLinkInfo> NetConnHiEventTest::GetNetLinkInfo() const
106 {
107 sptr<NetLinkInfo> netLinkInfo = (std::make_unique<NetLinkInfo>()).release();
108 netLinkInfo->ifaceName_ = "test";
109 netLinkInfo->domain_ = "test";
110
111 sptr<INetAddr> netAddr = (std::make_unique<INetAddr>()).release();
112 netAddr->type_ = INetAddr::IPV4;
113 netAddr->family_ = 0x10;
114 netAddr->prefixlen_ = 0x17;
115 netAddr->address_ = "192.168.2.0";
116 netAddr->netMask_ = "192.255.255.255";
117 netAddr->hostName_ = "netAddr";
118 netLinkInfo->netAddrList_.push_back(*netAddr);
119
120 sptr<Route> route = (std::make_unique<Route>()).release();
121 route->iface_ = "iface0";
122 route->destination_.type_ = INetAddr::IPV4;
123 route->destination_.family_ = 0x10;
124 route->destination_.prefixlen_ = 0x17;
125 route->destination_.address_ = "192.168.2.0";
126 route->destination_.netMask_ = "192.255.255.255";
127 route->destination_.hostName_ = "netAddr";
128 route->gateway_.type_ = INetAddr::IPV4;
129 route->gateway_.family_ = 0x10;
130 route->gateway_.prefixlen_ = 0x17;
131 route->gateway_.address_ = "192.168.2.0";
132 route->gateway_.netMask_ = "192.255.255.255";
133 route->gateway_.hostName_ = "netAddr";
134 netLinkInfo->routeList_.push_back(*route);
135
136 netLinkInfo->mtu_ = 0x5DC;
137 return netLinkInfo;
138 }
139
HandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)140 void NetConnHiEventTest::HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) {}
HandleDetectionResult(uint32_t supplierId,bool ifValid)141 void NetConnHiEventTest::HandleDetectionResult(uint32_t supplierId, bool ifValid) {}
142
143 /**
144 * @tc.name: NetConnHiEventTest_001
145 * @tc.desc: Test NetConnManager HiSysEvent:UpdateNetSupplierInfo
146 * @tc.type: FUNC
147 */
148 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_001, TestSize.Level1)
149 {
150 int32_t supplierId = 1001;
151 int32_t ret = DelayedSingleton<NetConnService>::GetInstance()->UpdateNetSupplierInfo(supplierId, nullptr);
152 EXPECT_NE(ret, NETMANAGER_SUCCESS);
153 }
154
155 /**
156 * @tc.name: NetConnHiEventTest_002
157 * @tc.desc: Test NetConnManager HiSysEvent:UpdateNetLinkInfo
158 * @tc.type: FUNC
159 */
160 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_002, TestSize.Level1)
161 {
162 int32_t supplierId = 1001;
163 int32_t ret = DelayedSingleton<NetConnService>::GetInstance()->UpdateNetLinkInfo(supplierId, nullptr);
164 EXPECT_NE(ret, NETMANAGER_SUCCESS);
165 }
166
167 /**
168 * @tc.name: NetConnHiEventTest_003
169 * @tc.desc: Test NetConnManager HiSysEvent:UpdateInterfaces
170 * @tc.type: FUNC
171 */
172 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_003, TestSize.Level1)
173 {
174 sptr<Network> network = GetNetwork();
175 ASSERT_NE(network, nullptr);
176 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
177 ASSERT_NE(netLinkInfo, nullptr);
178 network->UpdateInterfaces(*netLinkInfo);
179 }
180
181 /**
182 * @tc.name: NetConnHiEventTest_004
183 * @tc.desc: Test NetConnManager HiSysEvent:UpdateIpAddrs
184 * @tc.type: FUNC
185 */
186 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_004, TestSize.Level1)
187 {
188 sptr<Network> network = GetNetwork();
189 ASSERT_NE(network, nullptr);
190 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
191 ASSERT_NE(netLinkInfo, nullptr);
192 network->UpdateIpAddrs(*netLinkInfo);
193 }
194
195 /**
196 * @tc.name: NetConnHiEventTest_005
197 * @tc.desc: Test NetConnManager HiSysEvent:UpdateRoutes
198 * @tc.type: FUNC
199 */
200 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_005, TestSize.Level1)
201 {
202 sptr<Network> network = GetNetwork();
203 ASSERT_NE(network, nullptr);
204 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
205 ASSERT_NE(netLinkInfo, nullptr);
206 network->UpdateRoutes(*netLinkInfo);
207 }
208
209 /**
210 * @tc.name: NetConnHiEventTest_006
211 * @tc.desc: Test NetConnManager HiSysEvent:UpdateDns
212 * @tc.type: FUNC
213 */
214 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_006, TestSize.Level1)
215 {
216 sptr<Network> network = GetNetwork();
217 ASSERT_NE(network, nullptr);
218 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
219 ASSERT_NE(netLinkInfo, nullptr);
220 network->UpdateDns(*netLinkInfo);
221 }
222
223 /**
224 * @tc.name: NetConnHiEventTest_007
225 * @tc.desc: Test NetConnManager HiSysEvent:UpdateMtu
226 * @tc.type: FUNC
227 */
228 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_007, TestSize.Level1)
229 {
230 sptr<Network> network = GetNetwork();
231 ASSERT_NE(network, nullptr);
232 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
233 ASSERT_NE(netLinkInfo, nullptr);
234 network->UpdateMtu(*netLinkInfo);
235 }
236
237 /**
238 * @tc.name: NetConnHiEventTest_008
239 * @tc.desc: Test NetConnManager HiSysEvent:SetSocketParameter
240 * @tc.type: FUNC
241 */
242 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_008, TestSize.Level1)
243 {
244 int32_t netId = 1000;
245 std::weak_ptr<INetMonitorCallback> callback;
246 callback.reset();
247 sptr<NetMonitor> netMonitor = new (std::nothrow) NetMonitor(netId, callback);
248 int32_t ret = netMonitor->SetSocketParameter(-1);
249 EXPECT_NE(ret, NETMANAGER_SUCCESS);
250 }
251
252 /**
253 * @tc.name: NetConnHiEventTest_009
254 * @tc.desc: Test NetConnManager HiSysEvent:SetDefaultNetWork
255 * @tc.type: FUNC
256 */
257 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_009, TestSize.Level1)
258 {
259 sptr<Network> network = GetNetwork();
260 ASSERT_NE(network, nullptr);
261 network->SetDefaultNetWork();
262 }
263
264 /**
265 * @tc.name: NetConnHiEventTest_010
266 * @tc.desc: Test NetConnManager HiSysEvent:ClearDefaultNetWorkNetId
267 * @tc.type: FUNC
268 */
269 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_010, TestSize.Level1)
270 {
271 sptr<Network> network = GetNetwork();
272 ASSERT_NE(network, nullptr);
273 network->ClearDefaultNetWorkNetId();
274 }
275
276 /**
277 * @tc.name: NetConnHiEventTest_011
278 * @tc.desc: Test NetConnManager HiSysEvent:RegisterNetConnCallback
279 * @tc.type: FUNC
280 */
281 HWTEST_F(NetConnHiEventTest, NetConnHiEventTest_011, TestSize.Level1)
282 {
283 OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams);
284 int32_t ret = DelayedSingleton<NetConnService>::GetInstance()->RegisterNetConnCallback(nullptr, nullptr, 0);
285 EXPECT_NE(ret, NETMANAGER_SUCCESS);
286 }
287 } // namespace NetManagerStandard
288 } // namespace OHOS
289