1 /*
2 * Copyright (c) 2023 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 "accesstoken_kit.h"
19 #include "ethernet_client.h"
20 #include "gtest/gtest-message.h"
21 #include "gtest/gtest-test-part.h"
22 #include "gtest/hwext/gtest-ext.h"
23 #include "gtest/hwext/gtest-tag.h"
24 #include "interface_configuration.h"
25 #include "interface_type.h"
26 #include "nativetoken_kit.h"
27 #include "net_manager_constants.h"
28 #include "netmgr_ext_log_wrapper.h"
29 #include "refbase.h"
30 #include "static_configuration.h"
31 #include "token_setproc.h"
32 #define private public
33 #define protected public
34 #include "ethernet_client.h"
35 #include "ethernet_dhcp_controller.h"
36 #include "ethernet_service.h"
37 #include "ethernet_management.h"
38 #include "ethernet_service_proxy.h"
39
40 namespace OHOS {
41 namespace NetManagerStandard {
42 namespace {
43 using namespace testing::ext;
44 namespace {
45 using namespace Security::AccessToken;
46 using Security::AccessToken::AccessTokenID;
47 constexpr const char *DEV_NAME = "eth0";
48
49 HapInfoParams testInfoParms = {.userID = 1,
50 .bundleName = "ethernet_manager_test",
51 .instIndex = 0,
52 .appIDDesc = "test",
53 .isSystemApp = true};
54 PermissionDef testPermDef = {
55 .permissionName = "ohos.permission.GET_NETWORK_INFO",
56 .bundleName = "ethernet_manager_test",
57 .grantMode = 1,
58 .availableLevel = APL_SYSTEM_BASIC,
59 .label = "label",
60 .labelId = 1,
61 .description = "Test network share manager",
62 .descriptionId = 1,
63 };
64 PermissionStateFull testState = {
65 .permissionName = "ohos.permission.GET_NETWORK_INFO",
66 .isGeneral = true,
67 .resDeviceID = {"local"},
68 .grantStatus = {PermissionState::PERMISSION_GRANTED},
69 .grantFlags = {2},
70 };
71 HapPolicyParams testPolicyPrams1 = {
72 .apl = APL_SYSTEM_BASIC,
73 .domain = "test.domain",
74 .permList = {testPermDef},
75 .permStateList = {testState},
76 };
77
78 PermissionDef testPermDef2 = {
79 .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
80 .bundleName = "ethernet_manager_test",
81 .grantMode = 1,
82 .availableLevel = APL_SYSTEM_BASIC,
83 .label = "label",
84 .labelId = 1,
85 .description = "Test network share manager",
86 .descriptionId = 1,
87 };
88 PermissionStateFull testState2 = {
89 .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
90 .isGeneral = true,
91 .resDeviceID = {"local"},
92 .grantStatus = {PermissionState::PERMISSION_GRANTED},
93 .grantFlags = {2},
94 };
95 HapPolicyParams testPolicyPrams2 = {
96 .apl = APL_SYSTEM_BASIC,
97 .domain = "test.domain",
98 .permList = {testPermDef2},
99 .permStateList = {testState2},
100 };
101 } // namespace
102 } // namespace
103
104 class EtherNetServiceTest : public testing::Test {
105 public:
106 static void SetUpTestCase();
107 static void TearDownTestCase();
108 sptr<InterfaceConfiguration> GetIfaceConfig();
109 void SetUp();
110 void TearDown();
111 };
112
GetIfaceConfig()113 sptr<InterfaceConfiguration> EtherNetServiceTest::GetIfaceConfig()
114 {
115 sptr<InterfaceConfiguration> ic = (std::make_unique<InterfaceConfiguration>()).release();
116 if (!ic) {
117 return ic;
118 }
119 INetAddr ipv4Addr;
120 ipv4Addr.type_ = INetAddr::IPV4;
121 ipv4Addr.family_ = 0x01;
122 ipv4Addr.prefixlen_ = 0x01;
123 ipv4Addr.address_ = "172.17.5.234";
124 ipv4Addr.netMask_ = "255.255.254.0";
125 ipv4Addr.hostName_ = "netAddr";
126 ic->ipStatic_.ipAddrList_.push_back(ipv4Addr);
127 INetAddr route;
128 route.type_ = INetAddr::IPV4;
129 route.family_ = 0x01;
130 route.prefixlen_ = 0x01;
131 route.address_ = "0.0.0.0";
132 route.netMask_ = "0.0.0.0";
133 route.hostName_ = "netAddr";
134 ic->ipStatic_.routeList_.push_back(route);
135 INetAddr gateway;
136 gateway.type_ = INetAddr::IPV4;
137 gateway.family_ = 0x01;
138 gateway.prefixlen_ = 0x01;
139 gateway.address_ = "172.17.4.1";
140 gateway.netMask_ = "0.0.0.0";
141 gateway.hostName_ = "netAddr";
142 ic->ipStatic_.gatewayList_.push_back(gateway);
143 INetAddr netMask;
144 netMask.type_ = INetAddr::IPV4;
145 netMask.family_ = 0x01;
146 netMask.address_ = "255.255.255.0";
147 netMask.hostName_ = "netAddr";
148 ic->ipStatic_.netMaskList_.push_back(netMask);
149 INetAddr dns1;
150 dns1.type_ = INetAddr::IPV4;
151 dns1.family_ = 0x01;
152 dns1.address_ = "8.8.8.8";
153 dns1.hostName_ = "netAddr";
154 INetAddr dns2;
155 dns2.type_ = INetAddr::IPV4;
156 dns2.family_ = 0x01;
157 dns2.address_ = "114.114.114.114";
158 dns2.hostName_ = "netAddr";
159 ic->ipStatic_.dnsServers_.push_back(dns1);
160 ic->ipStatic_.dnsServers_.push_back(dns2);
161 return ic;
162 }
163
SetUpTestCase()164 void EtherNetServiceTest::SetUpTestCase() {}
165
TearDownTestCase()166 void EtherNetServiceTest::TearDownTestCase() {}
167
SetUp()168 void EtherNetServiceTest::SetUp() {}
169
TearDown()170 void EtherNetServiceTest::TearDown() {}
171
172 HWTEST_F(EtherNetServiceTest, OnInterfaceAddressUpdatedTest001, TestSize.Level1)
173 {
174 EthernetService ethernetservice;
175 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
176 std::string addr;
177 std::string ifName;
178 int flags = 0;
179 int scope = 0;
180 int ret = globalinterfacestatecallback.OnInterfaceAddressUpdated(addr, ifName, flags, scope);
181 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
182 }
183
184 HWTEST_F(EtherNetServiceTest, OnInterfaceAddressRemovedTest001, TestSize.Level1)
185 {
186 EthernetService ethernetservice;
187 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
188 std::string addr;
189 std::string ifName;
190 int flags = 0;
191 int scope = 0;
192 int ret = globalinterfacestatecallback.OnInterfaceAddressRemoved(addr, ifName, flags, scope);
193 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
194 }
195
196 HWTEST_F(EtherNetServiceTest, OnInterfaceAddedTest001, TestSize.Level1)
197 {
198 EthernetService ethernetservice;
199 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
200 std::string iface;
201 int ret = globalinterfacestatecallback.OnInterfaceAdded(iface);
202 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
203 }
204
205 HWTEST_F(EtherNetServiceTest, OnInterfaceRemovedTest001, TestSize.Level1)
206 {
207 EthernetService ethernetservice;
208 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
209 std::string iface;
210 int ret = globalinterfacestatecallback.OnInterfaceRemoved(iface);
211 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
212 }
213
214 HWTEST_F(EtherNetServiceTest, OnInterfaceChangedTest001, TestSize.Level1)
215 {
216 EthernetService ethernetservice;
217 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
218 std::string iface;
219 int ret = globalinterfacestatecallback.OnInterfaceChanged(iface, true);
220 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
221 }
222
223 HWTEST_F(EtherNetServiceTest, OnInterfaceLinkStateChangedTest001, TestSize.Level1)
224 {
225 EthernetService ethernetservice;
226 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
227 std::string ifName;
228 int ret = globalinterfacestatecallback.OnInterfaceLinkStateChanged(ifName, true);
229 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
230 }
231
232 HWTEST_F(EtherNetServiceTest, OnRouteChangedTest001, TestSize.Level1)
233 {
234 EthernetService ethernetservice;
235 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
236 bool updated = true;
237 std::string route;
238 std::string gateway;
239 std::string ifName;
240 int ret = globalinterfacestatecallback.OnRouteChanged(updated, route, gateway, ifName);
241 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
242 }
243
244 HWTEST_F(EtherNetServiceTest, OnDhcpSuccessTest001, TestSize.Level1)
245 {
246 EthernetService ethernetservice;
247 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
248 NetsysControllerCallback::DhcpResult dhcpResult;
249 int ret = globalinterfacestatecallback.OnDhcpSuccess(dhcpResult);
250 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
251 }
252
253 HWTEST_F(EtherNetServiceTest, OnBandwidthReachedLimitTest001, TestSize.Level1)
254 {
255 EthernetService ethernetservice;
256 EthernetService::GlobalInterfaceStateCallback globalinterfacestatecallback(ethernetservice);
257 std::string limitName;
258 std::string iface;
259 int ret = globalinterfacestatecallback.OnBandwidthReachedLimit(limitName, iface);
260 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
261 }
262
263 HWTEST_F(EtherNetServiceTest, SetIfaceConfigTest001, TestSize.Level1)
264 {
265 EthernetService ethernetService;
266 sptr<InterfaceConfiguration> ic = GetIfaceConfig();
267 int ret = ethernetService.SetIfaceConfig(DEV_NAME, ic);
268 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
269 }
270
271 HWTEST_F(EtherNetServiceTest, GetIfaceConfigTest001, TestSize.Level1)
272 {
273 EthernetService ethernetService;
274 std::string iface;
275 sptr<InterfaceConfiguration> ifaceConfig = GetIfaceConfig();
276 int ret = ethernetService.GetIfaceConfig(iface, ifaceConfig);
277 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
278 }
279
280 HWTEST_F(EtherNetServiceTest, IsIfaceActiveTest001, TestSize.Level1)
281 {
282 EthernetService ethernetService;
283 sptr<InterfaceConfiguration> ic = GetIfaceConfig();
284 int32_t ret = ethernetService.SetIfaceConfig(DEV_NAME, ic);
285 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
286 }
287
288 HWTEST_F(EtherNetServiceTest, GetAllActiveIfacesTest001, TestSize.Level1)
289 {
290 EthernetService ethernetService;
291 std::vector<std::string> result;
292 int32_t ret = ethernetService.GetAllActiveIfaces(result);
293 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
294 }
295
296 HWTEST_F(EtherNetServiceTest, ResetFactoryTest001, TestSize.Level1)
297 {
298 EthernetService ethernetService;
299 int32_t ret = ethernetService.ResetFactory();
300 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
301 }
302
303 HWTEST_F(EtherNetServiceTest, RegisterIfacesStateChangedTest001, TestSize.Level1)
304 {
305 EthernetService ethernetService;
306 sptr<InterfaceStateCallback> callback;
307 int32_t ret = ethernetService.RegisterIfacesStateChanged(callback);
308 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
309 }
310
311 HWTEST_F(EtherNetServiceTest, UnregisterIfacesStateChangedTest001, TestSize.Level1)
312 {
313 EthernetService ethernetService;
314 sptr<InterfaceStateCallback> callback;
315 int32_t ret = ethernetService.UnregisterIfacesStateChanged(callback);
316 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
317 }
318
319 HWTEST_F(EtherNetServiceTest, SetInterfaceUpTest001, TestSize.Level1)
320 {
321 EthernetService ethernetService;
322 int32_t ret = ethernetService.SetInterfaceUp(DEV_NAME);
323 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
324 }
325
326 HWTEST_F(EtherNetServiceTest, SetInterfaceDownTest001, TestSize.Level1)
327 {
328 EthernetService ethernetService;
329 int32_t ret = ethernetService.SetInterfaceDown(DEV_NAME);
330 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
331 }
332
333 HWTEST_F(EtherNetServiceTest, GetInterfaceConfigTest001, TestSize.Level1)
334 {
335 EthernetService ethernetService;
336 OHOS::nmd::InterfaceConfigurationParcel cfg;
337 int32_t ret = ethernetService.GetInterfaceConfig(DEV_NAME, cfg);
338 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
339 }
340
341 HWTEST_F(EtherNetServiceTest, SetInterfaceConfigTest001, TestSize.Level1)
342 {
343 EthernetService ethernetService;
344 std::string deviceId;
345 int32_t systemAbilityId = 0;
346 ethernetService.OnAddSystemAbility(systemAbilityId, deviceId);
347 ethernetService.OnAddSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, deviceId);
348 ethernetService.OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, deviceId);
349
350 OHOS::nmd::InterfaceConfigurationParcel config;
351 config.ifName = "eth0";
352 config.hwAddr = "";
353 config.ipv4Addr = "172.17.5.234";
354 config.prefixLength = 24;
355 config.flags.push_back("up");
356 config.flags.push_back("broadcast");
357 int32_t ret = ethernetService.SetInterfaceConfig(DEV_NAME, config);
358 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
359 }
360 } // namespace NetManagerStandard
361 } // namespace OHOS