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 "lnn_socket_mock.h"
17 using namespace testing;
18 using namespace testing::ext;
19 
20 namespace OHOS {
21 void *g_socketInterface;
LnnSocketInterfaceMock()22 LnnSocketInterfaceMock::LnnSocketInterfaceMock()
23 {
24     g_socketInterface = reinterpret_cast<void *>(this);
25 }
26 
~LnnSocketInterfaceMock()27 LnnSocketInterfaceMock::~LnnSocketInterfaceMock()
28 {
29     g_socketInterface = nullptr;
30 }
31 
GetSocketMockInterface()32 static LnnSocketInterface *GetSocketMockInterface()
33 {
34     return reinterpret_cast<LnnSocketInterfaceMock *>(g_socketInterface);
35 }
36 
37 extern "C" {
ConnOpenClientSocket(const ConnectOption * option,const char * bindAddr,bool isNonBlock)38 int32_t ConnOpenClientSocket(const ConnectOption *option, const char *bindAddr, bool isNonBlock)
39 {
40     return GetSocketMockInterface()->ConnOpenClientSocket(option, bindAddr, isNonBlock);
41 }
42 
GetSocketInterface(ProtocolType protocolType)43 const SocketInterface* GetSocketInterface(ProtocolType protocolType)
44 {
45     return GetSocketMockInterface()->GetSocketInterface(protocolType);
46 }
47 
RegistSocketProtocol(const SocketInterface * interface)48 int32_t RegistSocketProtocol(const SocketInterface* interface)
49 {
50     return GetSocketMockInterface()->RegistSocketProtocol(interface);
51 }
52 
ConnSendSocketData(int32_t fd,const char * buf,size_t len,int32_t timeout)53 ssize_t ConnSendSocketData(int32_t fd, const char *buf, size_t len, int32_t timeout)
54 {
55     return GetSocketMockInterface()->ConnSendSocketData(fd, buf, len, timeout);
56 }
57 
ConnShutdownSocket(int32_t fd)58 void ConnShutdownSocket(int32_t fd)
59 {
60     return GetSocketMockInterface()->ConnShutdownSocket(fd);
61 }
62 
ConnSetTcpKeepalive(int32_t fd,int32_t seconds,int32_t keepAliveIntvl,int32_t keepAliveCount)63 int32_t ConnSetTcpKeepalive(int32_t fd, int32_t seconds, int32_t keepAliveIntvl, int32_t keepAliveCount)
64 {
65     return GetSocketMockInterface()->ConnSetTcpKeepalive(fd, seconds, keepAliveIntvl, keepAliveCount);
66 }
67 
ConnSetTcpUserTimeOut(int32_t fd,uint32_t millSec)68 int32_t ConnSetTcpUserTimeOut(int32_t fd, uint32_t millSec)
69 {
70     return GetSocketMockInterface()->ConnSetTcpUserTimeOut(fd, millSec);
71 }
72 
ConnToggleNonBlockMode(int32_t fd,bool isNonBlock)73 int32_t ConnToggleNonBlockMode(int32_t fd, bool isNonBlock)
74 {
75     return GetSocketMockInterface()->ConnToggleNonBlockMode(fd, isNonBlock);
76 }
77 
ConnGetSocketError(int32_t fd)78 int32_t ConnGetSocketError(int32_t fd)
79 {
80     return GetSocketMockInterface()->ConnGetSocketError(fd);
81 }
82 
ConnGetLocalSocketPort(int32_t fd)83 int32_t ConnGetLocalSocketPort(int32_t fd)
84 {
85     return GetSocketMockInterface()->ConnGetLocalSocketPort(fd);
86 }
87 
ConnGetPeerSocketAddr(int32_t fd,SocketAddr * socketAddr)88 int32_t ConnGetPeerSocketAddr(int32_t fd, SocketAddr *socketAddr)
89 {
90     return GetSocketMockInterface()->ConnGetPeerSocketAddr(fd, socketAddr);
91 }
92 }
93 } // namespace OHOS