• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 NET_WORK_MOCK_H
17 #define NET_WORK_MOCK_H
18 
19 #include "gtest/gtest.h"
20 #include <arpa/inet.h>
21 #include <gmock/gmock.h>
22 #include <linux/netlink.h>
23 #include <linux/rtnetlink.h>
24 #include <mutex>
25 #include <net/if.h>
26 #include <securec.h>
27 #include <stdint.h>
28 #include <sys/ioctl.h>
29 #include <unistd.h>
30 
31 #include "bus_center_event.h"
32 #include "lnn_async_callback_utils.h"
33 #include "lnn_network_manager.h"
34 #include "message_handler.h"
35 #include "softbus_adapter_bt_common.h"
36 #include "softbus_adapter_socket.h"
37 #include "softbus_base_listener.h"
38 #include "softbus_conn_interface.h"
39 #include "softbus_config_type.h"
40 
41 namespace OHOS {
42 class NetworkInterface {
43 public:
NetworkInterface()44     NetworkInterface() {};
~NetworkInterface()45     virtual ~NetworkInterface() {};
46 
47     virtual int32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd) = 0;
48     virtual int32_t SoftBusSocketSetOpt(
49         int32_t socketFd, int32_t level, int32_t optName, const void *optVal, int32_t optLen) = 0;
50     virtual int32_t SoftBusSocketClose(int32_t socketFd) = 0;
51     virtual int32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen) = 0;
52     virtual int32_t LnnGetNetIfTypeByName(const char *ifName, LnnNetIfType *type) = 0;
53     virtual void LnnNotifyAddressChangedEvent(const char *ifName) = 0;
54     virtual int32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags) = 0;
55     virtual int32_t LnnAsyncCallbackHelper(SoftBusLooper *looper, LnnAsyncCallbackFunc callback, void *para) = 0;
56     virtual void LnnNotifyBtAclStateChangeEvent(const char *btMac, SoftBusBtAclState state) = 0;
57     virtual int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen) = 0;
58     virtual int32_t SoftBusAddBtStateListener(const SoftBusBtStateListener *listener, int32_t *listenerId) = 0;
59     virtual int32_t SoftbusGetConfig(ConfigType type, unsigned char *val, uint32_t len) = 0;
60     virtual void LnnNotifyBtStateChangeEvent(void *state) = 0;
61     virtual void LnnNotifyNetlinkStateChangeEvent(NetManagerIfNameState state, const char *ifName) = 0;
62     virtual int32_t LnnAsyncCallbackDelayHelper(SoftBusLooper *looper, LnnAsyncCallbackFunc callback,
63         void *para, uint64_t delayMillis) = 0;
64     virtual int32_t StartBaseClient(ListenerModule module, const SoftbusBaseListener *listener) = 0;
65     virtual int32_t AddTrigger(ListenerModule module, int32_t fd, TriggerType trigger) = 0;
66 };
67 class NetworkInterfaceMock : public NetworkInterface {
68 public:
69     NetworkInterfaceMock();
70     ~NetworkInterfaceMock() override;
71 
72     MOCK_METHOD4(SoftBusSocketCreate, int32_t(int32_t, int32_t, int32_t, int32_t *));
73     MOCK_METHOD5(SoftBusSocketSetOpt, int32_t(int32_t, int32_t, int32_t, const void *, int32_t));
74     MOCK_METHOD1(SoftBusSocketClose, int32_t(int32_t));
75     MOCK_METHOD3(SoftBusSocketBind, int32_t(int32_t, SoftBusSockAddr *, int32_t));
76     MOCK_METHOD2(LnnGetNetIfTypeByName, int32_t(const char *, LnnNetIfType *));
77     MOCK_METHOD1(LnnNotifyAddressChangedEvent, void(const char *));
78     MOCK_METHOD4(SoftBusSocketRecv, int32_t(int32_t, void *, uint32_t, int32_t));
79     MOCK_METHOD3(LnnAsyncCallbackHelper, int32_t(SoftBusLooper *, LnnAsyncCallbackFunc, void *));
80     MOCK_METHOD2(LnnNotifyBtAclStateChangeEvent, void(const char *, SoftBusBtAclState));
81     MOCK_METHOD4(ConvertBtMacToStr, int32_t(char *, uint32_t, const uint8_t *, uint32_t));
82     MOCK_METHOD2(SoftBusAddBtStateListener, int(const SoftBusBtStateListener *, int32_t *));
83     MOCK_METHOD3(SoftbusGetConfig, int(ConfigType, unsigned char *, uint32_t));
84     MOCK_METHOD1(LnnNotifyBtStateChangeEvent, void(void *));
85     MOCK_METHOD2(LnnNotifyNetlinkStateChangeEvent, void(NetManagerIfNameState, const char *));
86     MOCK_METHOD4(LnnAsyncCallbackDelayHelper, int32_t (SoftBusLooper *, LnnAsyncCallbackFunc, void *, uint64_t));
87     MOCK_METHOD2(StartBaseClient, int32_t (ListenerModule module, const SoftbusBaseListener *listener));
88     MOCK_METHOD3(AddTrigger, int32_t (ListenerModule module, int32_t fd, TriggerType trigger));
89 };
90 } // namespace OHOS
91 #endif // NET_WORK_MOCK_H