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