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