1 /*
2 * Copyright (c) 2021-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 "i_netsys_service.h"
17 #include "iservice_registry.h"
18 #include "netmanager_base_common_utils.h"
19 #include "netnative_log_wrapper.h"
20 #include "netsys_native_service_proxy.h"
21 #include "system_ability_definition.h"
22 #include "test.h"
23 #include <iostream>
24 #include <memory>
25
26 using namespace OHOS::nmd;
27 using namespace OHOS;
28 using namespace OHOS::NetsysNative;
29 const int NETID = 12;
30 const int NETID_TEST = 13;
31 const int IPVERSION = 4;
32 const int MASK_MAX = 65535;
33 namespace {
GetProxyR()34 sptr<INetsysService> GetProxyR()
35 {
36 NETNATIVE_LOGE("Get samgr >>>>>>>>>>>>>>>>>>>>>>>>>>");
37 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38 NETNATIVE_LOGE("Get samgr %{public}p", samgr.GetRefPtr());
39 std::cout << "Get samgr " << samgr.GetRefPtr() << std::endl;
40
41 auto remote = samgr->GetSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
42 NETNATIVE_LOGE("Get remote %{public}p", remote.GetRefPtr());
43 std::cout << "Get remote " << remote.GetRefPtr() << std::endl;
44
45 auto proxy = iface_cast<NetsysNative::INetsysService>(remote);
46 if (proxy != nullptr) {
47 NETNATIVE_LOGE("Get proxy %{public}p", proxy.GetRefPtr());
48 std::cout << "Get proxy " << proxy.GetRefPtr() << std::endl;
49 } else {
50 std::cout << "Get proxy nullptr" << std::endl;
51 }
52
53 return proxy;
54 }
55
56 auto netsysServiceR_ = GetProxyR();
57 } // namespace
58
TestNetworkAddRoute()59 void TestNetworkAddRoute()
60 {
61 NETNATIVE_LOGI("Entry TestNetworkAddRoute...");
62 if (netsysServiceR_ == nullptr) {
63 std::cout << "TestNetworkAddRoute netsysServiceR_ is nullptr" << std::endl;
64 return;
65 }
66 netsysServiceR_->NetworkCreatePhysical(NETID, nmd::NetworkPermission::PERMISSION_NONE);
67 NETNATIVE_LOGI("NetworkAddInterface, net");
68 int ret = netsysServiceR_->NetworkAddInterface(NETID, "eth0");
69 NETNATIVE_LOGE("result %{public}d", ret);
70 ret = netsysServiceR_->NetworkAddRoute(NETID, "eth0", "192.168.1.3/24", "192.168.1.1");
71 NETNATIVE_LOGE("ret=%{public}d", ret);
72 }
73
TestNetworkRemoveRoute()74 void TestNetworkRemoveRoute()
75 {
76 int ret = -1;
77 if (netsysServiceR_ == nullptr) {
78 std::cout << "TestNetworkRemoveRoute netsysServiceR_ is nullptr" << std::endl;
79 return;
80 }
81 ret = netsysServiceR_->NetworkRemoveRoute(NETID, "eth0", "192.168.1.3/32", "192.168.1.1");
82 NETNATIVE_LOGE("ret=%{public}d", ret);
83 }
84
TestNetworkAddRouteParcel()85 void TestNetworkAddRouteParcel()
86 {
87 if (netsysServiceR_ == nullptr) {
88 std::cout << "TestNetworkAddRouteParcel netsysServiceR_ is nullptr" << std::endl;
89 return;
90 }
91 nmd::RouteInfoParcel routeInfoParcel = {"", "", "", 1500};
92 netsysServiceR_->NetworkCreatePhysical(NETID, nmd::NetworkPermission::PERMISSION_NONE);
93 netsysServiceR_->NetworkAddInterface(NETID, "eth0");
94 netsysServiceR_->NetworkAddRouteParcel(NETID, routeInfoParcel);
95 nmd::MarkMaskParcel testFwmark;
96 int32_t result = netsysServiceR_->GetFwmarkForNetwork(NETID, testFwmark);
97 std::cout << "TestNetworkAddRouteParcel result " << result << std::endl;
98 if (testFwmark.mark != NETID) {
99 std::cout << "mark is " << testFwmark.mark << std::endl;
100 }
101 if (testFwmark.mask != MASK_MAX) {
102 std::cout << "mark is " << testFwmark.mark << std::endl;
103 }
104 }
105
TestNetWorkRemoveRouteParcel()106 void TestNetWorkRemoveRouteParcel()
107 {
108 if (netsysServiceR_ == nullptr) {
109 std::cout << "TestNetworkRemoveRouteParcel netsysServiceR_ is nullptr" << std::endl;
110 return;
111 }
112 nmd::RouteInfoParcel routeInfoParcel = {"", "", "", 1500};
113 netsysServiceR_->NetworkCreatePhysical(NETID, nmd::NetworkPermission::PERMISSION_NONE);
114 netsysServiceR_->NetworkAddInterface(NETID, "eth0");
115 netsysServiceR_->NetworkRemoveRouteParcel(NETID, routeInfoParcel);
116 }
117
TestNetworkSetDefault()118 void TestNetworkSetDefault()
119 {
120 int netid = NETID;
121 if (netsysServiceR_ == nullptr) {
122 std::cout << " TestNetworkSetDefault netsysServiceR_ is nullptr" << std::endl;
123 return;
124 }
125 int ret = netsysServiceR_->NetworkSetDefault(netid);
126 std::cout << " TestNetworkSetDefault ret =" << ret << std::endl;
127 }
128
TestNetworkSetDefaultWIFI()129 void TestNetworkSetDefaultWIFI()
130 {
131 int netid = NETID_TEST;
132 if (netsysServiceR_ == nullptr) {
133 std::cout << " TestNetworkSetDefaultWIFI netsysServiceR_ is nullptr" << std::endl;
134 return;
135 }
136 int ret = netsysServiceR_->NetworkSetDefault(netid);
137 std::cout << " TestNetworkSetDefaultWIFI ret =" << ret << std::endl;
138 }
139
TestNetworkGetDefault()140 void TestNetworkGetDefault()
141 {
142 if (netsysServiceR_ == nullptr) {
143 std::cout << " TestNetworkGetDefault netsysServiceR_ is nullptr" << std::endl;
144 return;
145 }
146 int ret = netsysServiceR_->NetworkGetDefault();
147 std::cout << " TestNetworkGetDefault ret =" << ret << std::endl;
148 }
149
TestNetworkClearDefault()150 void TestNetworkClearDefault()
151 {
152 if (netsysServiceR_ == nullptr) {
153 std::cout << " TestNetworkClearDefault netsysServiceR_ is nullptr" << std::endl;
154 return;
155 }
156 int ret = netsysServiceR_->NetworkClearDefault();
157 std::cout << " TestNetworkClearDefault ret =" << ret << std::endl;
158 }
159
TestNetworkGetDefaultUnion()160 void TestNetworkGetDefaultUnion()
161 {
162 if (netsysServiceR_ == nullptr) {
163 std::cout << " TestNetworkGetDefaultUnion netsysServiceR_ is nullptr" << std::endl;
164 return;
165 }
166 int32_t ret = netsysServiceR_->NetworkCreatePhysical(NETID, OHOS::nmd::NetworkPermission::PERMISSION_NONE);
167 NETNATIVE_LOGE("NetworkCreatePhysical ret =%{public}d", ret);
168 int32_t id = netsysServiceR_->NetworkGetDefault();
169 NETNATIVE_LOGE("NetworkDefault id =%{public}d", id);
170 netsysServiceR_->NetworkSetDefault(NETID);
171 id = netsysServiceR_->NetworkGetDefault();
172 NETNATIVE_LOGE("NetworkDefault after SET id =%{public}d", id);
173 ret = netsysServiceR_->NetworkCreatePhysical(NETID_TEST, OHOS::nmd::NetworkPermission::PERMISSION_NONE);
174 NETNATIVE_LOGE("NetworkCreatePhysical_A ret =%{public}d", ret);
175 id = netsysServiceR_->NetworkGetDefault();
176 NETNATIVE_LOGE("NetworkDefault id =%{public}d", id);
177 netsysServiceR_->NetworkSetDefault(NETID_TEST);
178 id = netsysServiceR_->NetworkGetDefault();
179 NETNATIVE_LOGE("NetworkDefault after SET id =%{public}d", id);
180 netsysServiceR_->NetworkClearDefault();
181 id = netsysServiceR_->NetworkGetDefault();
182 NETNATIVE_LOGE("NetworkDefault after clear default id =%{public}d", id);
183 }
184
TestNetworkCreatePhysical()185 void TestNetworkCreatePhysical()
186 {
187 if (netsysServiceR_ == nullptr) {
188 std::cout << " TestNetworkCreatePhysical netsysServiceR_ is nullptr" << std::endl;
189 return;
190 }
191 int ret = netsysServiceR_->NetworkCreatePhysical(NETID, OHOS::nmd::NetworkPermission::PERMISSION_NONE);
192 std::cout << " TestNetworkCreatePhysical ret =" << ret << std::endl;
193 }
194
TestInterfaceAddAddress()195 void TestInterfaceAddAddress()
196 {
197 if (netsysServiceR_ == nullptr) {
198 std::cout << " TestAddInterfaceAddress netsysServiceR_ is nullptr" << std::endl;
199 return;
200 }
201 int ret = netsysServiceR_->AddInterfaceAddress("eth0", "172.17.5.245", 23);
202 std::cout << " TestAddInterfaceAddress ret =" << ret << std::endl;
203 }
204
TestInterfaceDelAddress()205 void TestInterfaceDelAddress()
206 {
207 if (netsysServiceR_ == nullptr) {
208 std::cout << " TestDelInterfaceAddress netsysServiceR_ is nullptr" << std::endl;
209 return;
210 }
211 int ret = netsysServiceR_->DelInterfaceAddress("eth0", "172.17.5.245", 23);
212 std::cout << " TestDelInterfaceAddress ret =" << ret << std::endl;
213 }
214
TestNetworkAddInterface()215 void TestNetworkAddInterface()
216 {
217 if (netsysServiceR_ == nullptr) {
218 std::cout << " TestNetworkAddInterface netsysServiceR_ is nullptr" << std::endl;
219 return;
220 }
221 int ret = -1;
222 ret = netsysServiceR_->NetworkCreatePhysical(NETID, OHOS::nmd::NetworkPermission::PERMISSION_NONE);
223 NETNATIVE_LOGE("createPhysical ret = %{public}d", ret);
224 ret = netsysServiceR_->NetworkAddInterface(NETID, "rmnet0");
225 NETNATIVE_LOGE("networkAddInterface ret = %{public}d", ret);
226 }
227
TestNetworkRemoveInterface()228 void TestNetworkRemoveInterface()
229 {
230 if (netsysServiceR_ == nullptr) {
231 std::cout << " TestNetworkRemoveInterface netsysServiceR_ is nullptr" << std::endl;
232 return;
233 }
234 int ret = netsysServiceR_->NetworkRemoveInterface(NETID, "rmnet0");
235 NETNATIVE_LOGE("networkRemoveInterface ret = %{public}d", ret);
236 }
237
TestNetworkAddInterfaceWIFI()238 void TestNetworkAddInterfaceWIFI()
239 {
240 if (netsysServiceR_ == nullptr) {
241 std::cout << " TestNetworkAddInterfaceWIFI netsysServiceR_ is nullptr" << std::endl;
242 return;
243 }
244 int ret = -1;
245 ret = netsysServiceR_->NetworkCreatePhysical(NETID_TEST, OHOS::nmd::NetworkPermission::PERMISSION_NONE);
246 NETNATIVE_LOGE("TestNetworkAddInterfaceWIFI ret = %{public}d", ret);
247 ret = netsysServiceR_->NetworkAddInterface(NETID, "wlan0");
248 NETNATIVE_LOGE("TestNetworkAddInterfaceWIFI ret = %{public}d", ret);
249 }
250
TestNetworkRemoveInterfaceWIFI()251 void TestNetworkRemoveInterfaceWIFI()
252 {
253 if (netsysServiceR_ == nullptr) {
254 std::cout << " TestNetworkRemoveInterfaceWIFI netsysServiceR_ is nullptr" << std::endl;
255 return;
256 }
257 int ret = netsysServiceR_->NetworkRemoveInterface(NETID_TEST, "wlan0");
258 NETNATIVE_LOGE("TestNetworkRemoveInterfaceWIFI ret = %{public}d", ret);
259 }
260
TestGetFwmarkForNetwork()261 void TestGetFwmarkForNetwork()
262 {
263 if (netsysServiceR_ == nullptr) {
264 std::cout << " TestGetFwmakrForNetwork netsysServiceR_ is nullptr" << std::endl;
265 return;
266 }
267 OHOS::nmd::MarkMaskParcel testFwmark = {0, 0};
268 int ret = netsysServiceR_->GetFwmarkForNetwork(NETID, testFwmark);
269 NETNATIVE_LOGE("mark %{public}d,mask %{public}d, ret=%{public}d", testFwmark.mark, testFwmark.mask, ret);
270 }
271
TestInterfaceSetCfg()272 void TestInterfaceSetCfg()
273 {
274 if (netsysServiceR_ == nullptr) {
275 std::cout << " TestInterfaceSetCfg netsysServiceR_ is nullptr" << std::endl;
276 return;
277 }
278 OHOS::nmd::InterfaceConfigurationParcel parcel;
279 parcel.ifName = "eth0";
280 NETNATIVE_LOGE("ZZZZ:TestInterfaceSetCfg");
281 int ret = netsysServiceR_->GetInterfaceConfig(parcel);
282 NETNATIVE_LOGE("before: parcel get ipv4Addr = %{public}s", CommonUtils::ToAnonymousIp(parcel.ipv4Addr).c_str());
283 parcel.ipv4Addr = std::string("192.168.55.121");
284 ret = netsysServiceR_->SetInterfaceConfig(parcel);
285 NETNATIVE_LOGE("SetInterfaceConfig ret %{public}d", ret);
286 ret = netsysServiceR_->GetInterfaceConfig(parcel);
287 NETNATIVE_LOGE("after: parcel get ipv4Addr = %{public}s", CommonUtils::ToAnonymousIp(parcel.ipv4Addr).c_str());
288 }
289
TestNetGetProcSysNet()290 void TestNetGetProcSysNet()
291 {
292 if (netsysServiceR_ == nullptr) {
293 std::cout << " TestNetGetProcSysNet netsysServiceR_ is nullptr" << std::endl;
294 return;
295 }
296 std::string value = "5";
297 int ret = -1;
298 NETNATIVE_LOGE("SetProcSysNet start");
299 ret = netsysServiceR_->SetProcSysNet(IPVERSION, 1, std::string("eth0"), std::string("disable_policy"), value);
300 NETNATIVE_LOGE("SetProcSysNet ret:%{public}d, value:%{public}s \n", ret, value.c_str());
301 std::string readValue;
302 ret = netsysServiceR_->GetProcSysNet(IPVERSION, 1, std::string("eth0"), std::string("disable_policy"), readValue);
303 NETNATIVE_LOGE("GetProcSysNet ret:%{public}d, readValue=%{public}s\n", ret, readValue.c_str());
304 NETNATIVE_LOGE("NetGetProcSysNet OVER");
305 }
306