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