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 #include "network_mock.h"
17 #include "softbus_error_code.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 void *g_networkInterface;
24 
NetworkInterfaceMock()25 NetworkInterfaceMock::NetworkInterfaceMock()
26 {
27     g_networkInterface = reinterpret_cast<void *>(this);
28 }
29 
~NetworkInterfaceMock()30 NetworkInterfaceMock::~NetworkInterfaceMock()
31 {
32     g_networkInterface = nullptr;
33 }
34 
GetNetworkInterface()35 static NetworkInterface *GetNetworkInterface()
36 {
37     return reinterpret_cast<NetworkInterfaceMock *>(g_networkInterface);
38 }
39 
40 extern "C" {
SoftBusSocketCreate(int32_t domain,int32_t type,int32_t protocol,int32_t * socketFd)41 int32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd)
42 {
43     return GetNetworkInterface()->SoftBusSocketCreate(domain, type, protocol, socketFd);
44 }
45 
SoftBusSocketSetOpt(int32_t socketFd,int32_t level,int32_t optName,const void * optVal,int32_t optLen)46 int32_t SoftBusSocketSetOpt(int32_t socketFd, int32_t level, int32_t optName, const void *optVal, int32_t optLen)
47 {
48     return GetNetworkInterface()->SoftBusSocketSetOpt(socketFd, level, optName, optVal, optLen);
49 }
50 
SoftBusSocketClose(int32_t socketFd)51 int32_t SoftBusSocketClose(int32_t socketFd)
52 {
53     return GetNetworkInterface()->SoftBusSocketClose(socketFd);
54 }
55 
SoftBusSocketBind(int32_t socketFd,SoftBusSockAddr * addr,int32_t addrLen)56 int32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen)
57 {
58     return GetNetworkInterface()->SoftBusSocketBind(socketFd, addr, addrLen);
59 }
60 
LnnGetNetIfTypeByName(const char * ifName,LnnNetIfType * type)61 int32_t LnnGetNetIfTypeByName(const char *ifName, LnnNetIfType *type)
62 {
63     return GetNetworkInterface()->LnnGetNetIfTypeByName(ifName, type);
64 }
65 
LnnNotifyAddressChangedEvent(const char * ifName)66 void LnnNotifyAddressChangedEvent(const char *ifName)
67 {
68     return GetNetworkInterface()->LnnNotifyAddressChangedEvent(ifName);
69 }
70 
SoftBusSocketRecv(int32_t socketFd,void * buf,uint32_t len,int32_t flags)71 int32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags)
72 {
73     return GetNetworkInterface()->SoftBusSocketRecv(socketFd, buf, len, flags);
74 }
75 
LnnAsyncCallbackHelper(SoftBusLooper * looper,LnnAsyncCallbackFunc callback,void * para)76 int32_t LnnAsyncCallbackHelper(SoftBusLooper *looper, LnnAsyncCallbackFunc callback, void *para)
77 {
78     return GetNetworkInterface()->LnnAsyncCallbackHelper(looper, callback, para);
79 }
80 
LnnNotifyBtAclStateChangeEvent(const char * btMac,SoftBusBtAclState state)81 void LnnNotifyBtAclStateChangeEvent(const char *btMac, SoftBusBtAclState state)
82 {
83     return GetNetworkInterface()->LnnNotifyBtAclStateChangeEvent(btMac, state);
84 }
85 
SoftBusAddBtStateListener(const SoftBusBtStateListener * listener)86 int SoftBusAddBtStateListener(const SoftBusBtStateListener *listener)
87 {
88     return GetNetworkInterface()->SoftBusAddBtStateListener(listener);
89 }
90 
SoftbusGetConfig(ConfigType type,unsigned char * val,uint32_t len)91 int SoftbusGetConfig(ConfigType type, unsigned char *val, uint32_t len)
92 {
93     return GetNetworkInterface()->SoftbusGetConfig(type, val, len);
94 }
95 
LnnNotifyBtStateChangeEvent(void * state)96 void LnnNotifyBtStateChangeEvent(void *state)
97 {
98     return GetNetworkInterface()->LnnNotifyBtStateChangeEvent(state);
99 }
100 }
101 }