Add socket mocks
[folly.git] / folly / io / async / test / MockAsyncSSLSocket.h
1 /*
2  * Copyright 2015 Facebook, Inc.
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 #pragma once
17 #include <gmock/gmock.h>
18
19 #include <folly/io/async/AsyncSSLSocket.h>
20
21 namespace folly { namespace test {
22
23 class MockAsyncSSLSocket : public AsyncSSLSocket {
24  public:
25   MockAsyncSSLSocket(
26    const std::shared_ptr<SSLContext>& ctx,
27    EventBase* base) :
28     AsyncSSLSocket(ctx, base) {
29   }
30
31   GMOCK_METHOD5_(, noexcept, ,
32    connect,
33    void(AsyncSocket::ConnectCallback*,
34     const folly::SocketAddress&,
35     int,
36     const OptionMap&,
37     const folly::SocketAddress&));
38   MOCK_CONST_METHOD1(getLocalAddress, void(folly::SocketAddress*));
39   MOCK_CONST_METHOD1(getPeerAddress, void(folly::SocketAddress*));
40   MOCK_METHOD0(closeNow, void());
41   MOCK_CONST_METHOD0(good, bool());
42   MOCK_CONST_METHOD0(readable, bool());
43   MOCK_CONST_METHOD0(hangup, bool());
44   MOCK_CONST_METHOD2(
45    getSelectedNextProtocol,
46    void(const unsigned char**, unsigned*));
47   MOCK_CONST_METHOD2(
48    getSelectedNextProtocolNoThrow,
49    bool(const unsigned char**, unsigned*));
50
51   void sslConn(
52     AsyncSSLSocket::HandshakeCB* cb,
53     uint64_t timeout,
54     const SSLContext::SSLVerifyPeerEnum& verify)
55       override {
56     if (timeout > 0) {
57       handshakeTimeout_.scheduleTimeout((uint32_t)timeout);
58     }
59
60     state_ = StateEnum::ESTABLISHED;
61     sslState_ = STATE_CONNECTING;
62     handshakeCallback_ = cb;
63
64     sslConnectMockable(cb, timeout, verify);
65   }
66   MOCK_METHOD3(
67    sslConnectMockable,
68    void(AsyncSSLSocket::HandshakeCB*, uint64_t,
69      const SSLContext::SSLVerifyPeerEnum&));
70 };
71
72 }}