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