1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <folly/io/async/AsyncSocket.h>
20 #include <folly/io/async/EventBase.h>
21 #include <folly/portability/GMock.h>
22 
23 namespace folly {
24 
25 namespace test {
26 
27 class MockAsyncSocket : public AsyncSocket {
28  public:
29   typedef std::unique_ptr<MockAsyncSocket, Destructor> UniquePtr;
30 
MockAsyncSocket(EventBase * base)31   explicit MockAsyncSocket(EventBase* base) : AsyncSocket(base) {}
32 
33   MOCK_METHOD6(
34       connect_,
35       void(
36           AsyncSocket::ConnectCallback*,
37           const folly::SocketAddress&,
38           int,
39           const folly::SocketOptionMap&,
40           const folly::SocketAddress&,
41           const std::string&));
connect(AsyncSocket::ConnectCallback * callback,const folly::SocketAddress & address,int timeout,const folly::SocketOptionMap & options,const folly::SocketAddress & bindAddr,const std::string & ifName)42   void connect(
43       AsyncSocket::ConnectCallback* callback,
44       const folly::SocketAddress& address,
45       int timeout,
46       const folly::SocketOptionMap& options,
47       const folly::SocketAddress& bindAddr,
48       const std::string& ifName) noexcept override {
49     connect_(callback, address, timeout, options, bindAddr, ifName);
50   }
51 
52   MOCK_CONST_METHOD1(getPeerAddress, void(folly::SocketAddress*));
53   MOCK_METHOD0(detachNetworkSocket, NetworkSocket());
54   MOCK_CONST_METHOD0(getNetworkSocket, NetworkSocket());
55   MOCK_METHOD0(closeNow, void());
56   MOCK_CONST_METHOD0(good, bool());
57   MOCK_CONST_METHOD0(readable, bool());
58   MOCK_CONST_METHOD0(hangup, bool());
59   MOCK_CONST_METHOD1(getLocalAddress, void(SocketAddress*));
60   MOCK_METHOD1(setReadCB, void(ReadCallback*));
61   MOCK_METHOD1(_setPreReceivedData, void(std::unique_ptr<IOBuf>&));
62   MOCK_CONST_METHOD0(getRawBytesWritten, size_t());
63   MOCK_METHOD4(setSockOptVirtual, int(int, int, void const*, socklen_t));
64   MOCK_METHOD1(setErrMessageCB, void(AsyncSocket::ErrMessageCallback*));
65   MOCK_METHOD1(setSendMsgParamCB, void(AsyncSocket::SendMsgParamsCallback*));
66   MOCK_CONST_METHOD0(getSecurityProtocol, std::string());
setPreReceivedData(std::unique_ptr<IOBuf> data)67   void setPreReceivedData(std::unique_ptr<IOBuf> data) override {
68     return _setPreReceivedData(data);
69   }
70 
71   MOCK_METHOD1(
72       addLifecycleObserver,
73       void(folly::AsyncTransport::LifecycleObserver* observer));
74   MOCK_METHOD1(
75       removeLifecycleObserver,
76       bool(folly::AsyncTransport::LifecycleObserver* observer));
77   MOCK_CONST_METHOD0(
78       getLifecycleObservers, std::vector<AsyncTransport::LifecycleObserver*>());
79 };
80 
81 } // namespace test
82 } // namespace folly
83