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