• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MOCK_NETSYS_NATIVE_CLIENT_H
17 #define MOCK_NETSYS_NATIVE_CLIENT_H
18 
19 #include <string>
20 #include <vector>
21 #include <unordered_set>
22 #include <cstring>
23 #include <netdb.h>
24 #include <linux/if.h>
25 
26 #include "netsys_controller_callback.h"
27 #include "netsys_controller_define.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 const std::string MOCK_NETWORKCREATEPHYSICAL_API = "NetworkCreatePhysical";
32 const std::string MOCK_NETWORKDESTROY_API = "NetworkDestroy";
33 const std::string MOCK_NETWORKADDINTERFACE_API = "NetworkAddInterface";
34 const std::string MOCK_NETWORKREMOVEINTERFACE_API = "NetworkRemoveInterface";
35 const std::string MOCK_NETWORKADDROUTE_API = "NetworkAddRoute";
36 const std::string MOCK_NETWORKREMOVEROUTE_API = "NetworkRemoveRoute";
37 const std::string MOCK_SETINTERFACEDOWN_API = "SetInterfaceDown";
38 const std::string MOCK_SETINTERFACEUP_API = "SetInterfaceUp";
39 const std::string MOCK_INTERFACECLEARADDRS_API = "InterfaceClearAddrs";
40 const std::string MOCK_INTERFACEGETMTU_API = "InterfaceGetMtu";
41 const std::string MOCK_INTERFACESETMTU_API = "InterfaceSetMtu";
42 const std::string MOCK_INTERFACEADDADDRESS_API = "InterfaceAddAddress";
43 const std::string MOCK_INTERFACEDELADDRESS_API = "InterfaceDelAddress";
44 const std::string MOCK_SETRESOLVERCONFIG_API = "SetResolverConfig";
45 const std::string MOCK_GETRESOLVERINFO_API = "GetResolverInfo";
46 const std::string MOCK_CREATENETWORKCACHE_API = "CreateNetworkCache";
47 const std::string MOCK_DESTROYNETWORKCACHE_API = "DestroyNetworkCache";
48 const std::string MOCK_FLUSHNETWORKCACHE_API = "FlushNetworkCache";
49 const std::string MOCK_GETADDRINFO_API = "GetAddrInfo";
50 const std::string MOCK_GETCELLULARRXBYTES_API = "GetCellularRxBytes";
51 const std::string MOCK_GETCELLULARTXBYTES_API = "GetCellularTxBytes";
52 const std::string MOCK_GETALLRXBYTES_API = "GetAllRxBytes";
53 const std::string MOCK_GETALLTXBYTES_API = "GetAllTxBytes";
54 const std::string MOCK_GETUIDRXBYTES_API = "GetUidRxBytes";
55 const std::string MOCK_GETUIDTXBYTES_API = "GetUidTxBytes";
56 const std::string MOCK_GETUIDONIFACERXBYTES_API = "GetUidOnIfaceRxBytes";
57 const std::string MOCK_GETUIDONIFACETXBYTES_API = "GetUidOnIfaceTxBytes";
58 const std::string MOCK_GETIFACERXBYTES_API = "GetIfaceRxBytes";
59 const std::string MOCK_GETIFACETXBYTES_API = "GetIfaceTxBytes";
60 const std::string MOCK_INTERFACEGETLIST_API = "InterfaceGetList";
61 const std::string MOCK_UIDGETLIST_API = "UidGetList";
62 const std::string MOCK_GETIFACERXPACKETS_API = "GetIfaceRxPackets";
63 const std::string MOCK_GETIFACETXPACKETS_API = "GetIfaceTxPackets";
64 const std::string MOCK_SETDEFAULTNETWORK_API = "SetDefaultNetWork";
65 const std::string MOCK_CLEARDEFAULTNETWORK_API = "ClearDefaultNetWorkNetId";
66 const std::string MOCK_BINDSOCKET_API = "BindSocket";
67 const std::string MOCK_IPENABLEFORWARDING_API = "IpEnableForwarding";
68 const std::string MOCK_IPDISABLEFORWARDING_API = "IpDisableForwarding";
69 const std::string MOCK_TETHERADDFORWARD_API = "TetherAddForward";
70 const std::string MOCK_TETHERREMOVEFORWARD_API = "TetherRemoveForward";
71 const std::string MOCK_IPFWDADDIFACEFORWARD_API = "IpfwdAddInterfaceForward";
72 const std::string MOCK_IPFWDREMOVEIFACEFORWARD_API = "IpfwdRemoveInterfaceForward";
73 const std::string MOCK_TETHERDNSSET_API = "TetherDnsSet";
74 const std::string MOCK_REGISTERNETSYSNOTIFYCALLBACK_API = "RegisterNetsysNotifyCallback";
75 const std::string MOCK_BINDNETWORKSERVICEVPN_API = "BindNetworkServiceVpn";
76 const std::string MOCK_ENABLEVIRTUALNETIFACECARD_API = "EnableVirtualNetIfaceCard";
77 const std::string MOCK_SETIPADDRESS_API = "SetIpAddress";
78 const std::string MOCK_SETBLOCKING_API = "SetBlocking";
79 const std::string MOCK_STARTDHCPCLIENT_API = "StartDhcpClient";
80 const std::string MOCK_STOPDHCPCLIENT_API = "StopDhcpClient";
81 const std::string MOCK_REGISTERNOTIFYCALLBACK_API = "RegisterNotifyCallback";
82 const std::string MOCK_STARTDHCPSERVICE_API = "StartDhcpService";
83 const std::string MOCK_STOPDHCPSERVICE_API = "StopDhcpService";
84 
85 class MockNetsysNativeClient {
86 public:
87     MockNetsysNativeClient();
88     ~MockNetsysNativeClient();
89     void Init();
90 
91     void RegisterMockApi();
92     bool CheckMockApi(const std::string &api);
93 
94     /**
95      * @brief Create a physical network
96      *
97      * @param netId
98      * @param permission Permission to create a physical network
99      * @return Return the return value of the netsys interface call
100      */
101     int32_t NetworkCreatePhysical(int32_t netId, int32_t permission);
102 
103     /**
104      * @brief Destroy the network
105      *
106      * @param netId
107      * @return Return the return value of the netsys interface call
108      */
109     int32_t NetworkDestroy(int32_t netId);
110 
111     /**
112      * @brief Add network port device
113      *
114      * @param netId
115      * @param iface Network port device name
116      * @return Return the return value of the netsys interface call
117      */
118     int32_t NetworkAddInterface(int32_t netId, const std::string &iface);
119 
120     /**
121      * @brief Delete network port device
122      *
123      * @param netId
124      * @param iface Network port device name
125      * @return Return the return value of the netsys interface call
126      */
127     int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface);
128 
129     /**
130      * @brief Add route
131      *
132      * @param netId
133      * @param ifName Network port device name
134      * @param destination Target host ip
135      * @param nextHop Next hop address
136      * @return Return the return value of the netsys interface call
137      */
138     int32_t NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination,
139         const std::string &nextHop);
140 
141     /**
142      * @brief Remove route
143      *
144      * @param netId
145      * @param ifName Network port device name
146      * @param destination Target host ip
147      * @param nextHop Next hop address
148      * @return Return the return value of the netsys interface call
149      */
150     int32_t NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination,
151         const std::string &nextHop);
152 
153     /**
154      * @brief Turn off the device
155      *
156      * @param iface Network port device name
157      * @return Return the result of this action
158      */
159     int32_t SetInterfaceDown(const std::string &iface);
160 
161     /**
162      * @brief Turn on the device
163      *
164      * @param iface Network port device name
165      * @return Return the result of this action
166      */
167     int32_t SetInterfaceUp(const std::string &iface);
168 
169     /**
170      * @brief Clear the network interface ip address
171      *
172      * @param ifName Network port device name
173      */
174     void InterfaceClearAddrs(const std::string &ifName);
175 
176     /**
177      * @brief Obtain mtu from the network interface device
178      *
179      * @param ifName Network port device name
180      * @return Return the return value of the netsys interface call
181      */
182     int32_t InterfaceGetMtu(const std::string &ifName);
183 
184     /**
185      * @brief Set mtu to network interface device
186      *
187      * @param ifName Network port device name
188      * @param mtu
189      * @return Return the return value of the netsys interface call
190      */
191     int32_t InterfaceSetMtu(const std::string &ifName, int32_t mtu);
192 
193     /**
194      * @brief Add ip address
195      *
196      * @param ifName Network port device name
197      * @param ipAddr    ip address
198      * @param prefixLength  subnet mask
199      * @return Return the return value of the netsys interface call
200      */
201     int32_t InterfaceAddAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength);
202 
203     /**
204      * @brief Delete ip address
205      *
206      * @param ifName Network port device name
207      * @param ipAddr ip address
208      * @param prefixLength subnet mask
209      * @return Return the return value of the netsys interface call
210      */
211     int32_t InterfaceDelAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength);
212 
213     /**
214      * @brief Set dns
215      *
216      * @param netId
217      * @param baseTimeoutMsec
218      * @param retryCount
219      * @param servers
220      * @param domains
221      * @return Return the return value of the netsys interface call
222      */
223     int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
224         const std::vector<std::string> &servers, const std::vector<std::string> &domains);
225     /**
226      * @brief Get dns server param info
227      *
228      * @param netId
229      * @param servers
230      * @param domains
231      * @param baseTimeoutMsec
232      * @param retryCount
233      * @return Return the return value of the netsys interface call
234      */
235     int32_t GetResolverInfo(uint16_t netId, std::vector<std::string> &servers,
236         std::vector<std::string> &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount);
237 
238     /**
239      * @brief Create dns cache before set dns
240      *
241      * @param netId
242      * @return Return the return value for status of call
243      */
244     int32_t CreateNetworkCache(uint16_t netId);
245 
246     /**
247      * @brief Destroy dns cache
248      *
249      * @param netId
250      * @return Return the return value of the netsys interface call
251      */
252     int32_t DestroyNetworkCache(uint16_t netId);
253 
254     /**
255      * @brief Flush dns cache
256      *
257      * @param netId
258      * @return Return the return value of the netsys interface call
259      */
260     int32_t FlushNetworkCache(uint16_t netId);
261 
262     /**
263      * @brief Domain name resolution Obtains the domain name address
264      *
265      * @param hostName
266      * @param serverName
267      * @param hints
268      * @param res
269      * @return Return the return value of the netsys interface call
270      */
271     int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName,
272         const struct addrinfo &hints, std::unique_ptr<addrinfo> &res, uint16_t netId);
273 
274     /**
275      * @brief Obtains the bytes received over the cellular network.
276      *
277      * @return The number of received bytes.
278      */
279     int64_t GetCellularRxBytes();
280 
281     /**
282      * @brief Obtains the bytes sent over the cellular network.
283      *
284      * @return The number of sent bytes.
285      */
286     int64_t GetCellularTxBytes();
287 
288     /**
289      * @brief Obtains the bytes received through all NICs.
290      *
291      * @return The number of received bytes.
292      */
293     int64_t GetAllRxBytes();
294 
295     /**
296      * @brief Obtains the bytes sent through all NICs.
297      *
298      * @return The number of sent bytes.
299      */
300     int64_t GetAllTxBytes();
301 
302     /**
303      * @brief Obtains the bytes received through a specified UID.
304      *
305      * @param uid app id.
306      * @return The number of received bytes.
307      */
308     int64_t GetUidRxBytes(uint32_t uid);
309 
310     /**
311      * @brief Obtains the bytes sent through a specified UID.
312      *
313      * @param uid app id.
314      * @return The number of sent bytes.
315      */
316     int64_t GetUidTxBytes(uint32_t uid);
317 
318     /**
319      * @brief Obtains the bytes received through a specified UID on Iface.
320      *
321      * @param uid app id.
322      * @param iface The name of the interface.
323      * @return The number of received bytes.
324      */
325     int64_t GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName);
326 
327     /**
328      * @brief Obtains the bytes sent through a specified UID on Iface.
329      *
330      * @param uid app id.
331      * @param iface The name of the interface.
332      * @return The number of sent bytes.
333      */
334     int64_t GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName);
335 
336     /**
337      * @brief Obtains the bytes received through a specified NIC.
338      *
339      * @param iface The name of the interface.
340      * @return The number of received bytes.
341      */
342     int64_t GetIfaceRxBytes(const std::string &interfaceName);
343 
344     /**
345      * @brief Obtains the bytes sent through a specified NIC.
346      *
347      * @param iface The name of the interface.
348      * @return The number of sent bytes.
349      */
350     int64_t GetIfaceTxBytes(const std::string &interfaceName);
351 
352     /**
353      * @brief Obtains the NIC list.
354      *
355      * @return The list of interface.
356      */
357     std::vector<std::string> InterfaceGetList();
358 
359     /**
360      * @brief Obtains the uid list.
361      *
362      * @return The list of uid.
363      */
364     std::vector<std::string> UidGetList();
365 
366     /**
367      * @brief Obtains the packets received through a specified NIC.
368      *
369      * @param iface The name of the interface.
370      * @return The number of received packets.
371      */
372     int64_t GetIfaceRxPackets(const std::string &interfaceName);
373 
374     /**
375      * @brief Obtains the packets sent through a specified NIC.
376      *
377      * @param iface The name of the interface.
378      * @return The number of sent packets.
379      */
380     int64_t GetIfaceTxPackets(const std::string &interfaceName);
381 
382     /**
383      * @brief  set default network.
384      *
385      * @return Return the return value of the netsys interface call
386      */
387     int32_t  SetDefaultNetWork(int32_t   netId);
388 
389     /**
390      * @brief clear default network netId.
391      *
392      * @return Return the return value of the netsys interface call
393      */
394     int32_t  ClearDefaultNetWorkNetId();
395 
396     /**
397      * @brief Obtains the NIC list.
398      *
399      * @param socket_fd
400      * @param netId
401      * @return Return the return value of the netsys interface call
402      */
403     int32_t BindSocket(int32_t socket_fd, uint32_t netId);
404 
405     /**
406      * @brief Enable ip forwarding.
407      *
408      * @param requester the requester of forwarding
409      * @return Return the return value of the netsys interface call.
410      */
411     int32_t IpEnableForwarding(const std::string& requester);
412 
413     /**
414      * @brief Disable ip forwarding.
415      *
416      * @param requester the requester of forwarding
417      * @return Return the return value of the netsys interface call.
418      */
419     int32_t IpDisableForwarding(const std::string& requester);
420 
421     /**
422      * @brief Add tether forward.
423      *
424      * @param downstreamIface the name of downstream interface
425      * @param upstreamIface the name of upstream interface
426      * @return Return the return value of the netsys interface call.
427      */
428     int32_t TetherAddForward(const std::string& downstreamIface, const std::string& upstreamIface);
429 
430     /**
431      * @brief Remove tether forward.
432      *
433      * @param downstreamIface the name of downstream interface
434      * @param upstreamIface the name of upstream interface
435      * @return Return the return value of the netsys interface call.
436      */
437     int32_t TetherRemoveForward(const std::string& downstreamIface, const std::string& upstreamIface);
438 
439     /**
440      * @brief Add interface forward.
441      *
442      * @param fromIface the name of incoming interface
443      * @param toIface the name of outcoming interface
444      * @return Return the return value of the netsys interface call.
445      */
446     int32_t IpfwdAddInterfaceForward(const std::string& fromIface, const std::string& toIface);
447 
448     /**
449      * @brief Remove interface forward.
450      *
451      * @param fromIface the name of incoming interface
452      * @param toIface the name of outcoming interface
453      * @return Return the return value of the netsys interface call.
454      */
455     int32_t IpfwdRemoveInterfaceForward(const std::string& fromIface, const std::string& toIface);
456 
457     /**
458      * @brief Set tether dns.
459      *
460      * @param netId network id
461      * @param dnsAddr the list of dns address
462      * @return Return the return value of the netsys interface call.
463      */
464     int32_t TetherDnsSet(uint32_t netId, const std::vector<std::string>& dnsAddrs);
465 
466     /**
467      * @brief Set net callbackfuction.
468      *
469      * @param callback callbackfuction class
470      * @return Return the return value of the netsys interface call.
471      */
472     int32_t RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback);
473 
474     /**
475      * @brief protect tradition network to connect VPN.
476      *
477      * @param socketFd socket file description
478      * @return Return the return value of the netsys interface call.
479      */
480     int32_t BindNetworkServiceVpn(int32_t socketFd);
481 
482     /**
483      * @brief enable virtual network iterface card.
484      *
485      * @param socketFd socket file description
486      * @param ifRequest interface request
487      * @return Return the return value of the netsys interface call.
488      */
489     int32_t EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd);
490 
491     /**
492      * @brief Set ip address.
493      *
494      * @param socketFd socket file description
495      * @param ipAddress ip address
496      * @param prefixLen the mask of ip address
497      * @param ifRequest interface request
498      * @return Return the return value of the netsys interface call.
499      */
500     int32_t SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen, struct ifreq &ifRequest);
501 
502     /**
503      * @brief Set network blocking.
504      *
505      * @param ifaceFd interface file description
506      * @param isBlock network blocking
507      * @return Return the return value of the netsys interface call.
508      */
509     int32_t SetBlocking(int32_t ifaceFd, bool isBlock);
510     /**
511     * @brief Start Dhcp Client.
512     *
513     * @param iface interface file description
514     * @param bIpv6 network blocking
515     * @return.
516     */
517     int32_t StartDhcpClient(const std::string &iface, bool bIpv6);
518     /**
519      * @brief Stop Dhcp Client.
520      *
521      * @param iface interface file description
522      * @param bIpv6 network blocking
523      * @return .
524      */
525     int32_t StopDhcpClient(const std::string &iface, bool bIpv6);
526     /**
527     * @brief Register Notify Callback
528     *
529     * @param callback
530     * @return .
531     */
532     int32_t RegisterCallback(sptr<NetsysControllerCallback> callback);
533     /**
534      * @brief start dhcpservice.
535      *
536      * @param iface interface name
537      * @param ipv4addr ipv4 addr
538      * @return Return the return value of the netsys interface call.
539      */
540     int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr);
541 
542     /**
543      * @brief stop dhcpservice.
544      *
545      * @param iface interface name
546      * @return Return the return value of the netsys interface call.
547      */
548     int32_t StopDhcpService(const std::string &iface);
549 private:
550     int64_t GetIfaceBytes(const std::string &interfaceName, const std::string &filename);
551     int64_t GetAllBytes(const std::string &filename);
552     int32_t AddRoute(const std::string &ip, const std::string &mask, const std::string &gateWay,
553         const std::string &devName);
554 
555 private:
556     std::unordered_set<std::string> mockApi_;
557 };
558 } // namespace NetManagerStandard
559 } // namespace OHOS
560 #endif // MOCK_NETSYS_NATIVE_CLIENT_H
561