MockAsyncSSLSocket.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2015-present 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 <folly/io/async/AsyncSSLSocket.h>
  18. #include <folly/portability/GMock.h>
  19. namespace folly {
  20. namespace test {
  21. class MockAsyncSSLSocket : public AsyncSSLSocket {
  22. public:
  23. MockAsyncSSLSocket(
  24. const std::shared_ptr<SSLContext>& ctx,
  25. EventBase* base,
  26. bool deferSecurityNegotiation = false)
  27. : AsyncSocket(base),
  28. AsyncSSLSocket(ctx, base, deferSecurityNegotiation) {}
  29. MOCK_METHOD5(
  30. connect_,
  31. void(
  32. AsyncSocket::ConnectCallback*,
  33. const folly::SocketAddress&,
  34. int,
  35. const OptionMap&,
  36. const folly::SocketAddress&));
  37. void connect(
  38. AsyncSocket::ConnectCallback* callback,
  39. const folly::SocketAddress& address,
  40. int timeout,
  41. const OptionMap& options,
  42. const folly::SocketAddress& bindAddr) noexcept override {
  43. connect_(callback, address, timeout, options, bindAddr);
  44. }
  45. MOCK_CONST_METHOD1(getLocalAddress, void(folly::SocketAddress*));
  46. MOCK_CONST_METHOD1(getPeerAddress, void(folly::SocketAddress*));
  47. MOCK_METHOD0(closeNow, void());
  48. MOCK_CONST_METHOD0(good, bool());
  49. MOCK_CONST_METHOD0(readable, bool());
  50. MOCK_CONST_METHOD0(hangup, bool());
  51. MOCK_CONST_METHOD2(
  52. getSelectedNextProtocol,
  53. void(const unsigned char**, unsigned*));
  54. MOCK_CONST_METHOD2(
  55. getSelectedNextProtocolNoThrow,
  56. bool(const unsigned char**, unsigned*));
  57. MOCK_METHOD1(setReadCB, void(ReadCallback*));
  58. void sslConn(
  59. AsyncSSLSocket::HandshakeCB* cb,
  60. std::chrono::milliseconds timeout,
  61. const SSLContext::SSLVerifyPeerEnum& verify) override {
  62. if (timeout > std::chrono::milliseconds::zero()) {
  63. handshakeTimeout_.scheduleTimeout(timeout);
  64. }
  65. state_ = StateEnum::ESTABLISHED;
  66. sslState_ = STATE_CONNECTING;
  67. handshakeCallback_ = cb;
  68. sslConnectMockable(cb, timeout, verify);
  69. }
  70. void sslAccept(
  71. AsyncSSLSocket::HandshakeCB* cb,
  72. std::chrono::milliseconds timeout,
  73. const SSLContext::SSLVerifyPeerEnum& verify) override {
  74. if (timeout > std::chrono::milliseconds::zero()) {
  75. handshakeTimeout_.scheduleTimeout(timeout);
  76. }
  77. state_ = StateEnum::ESTABLISHED;
  78. sslState_ = STATE_ACCEPTING;
  79. handshakeCallback_ = cb;
  80. sslAcceptMockable(cb, timeout, verify);
  81. }
  82. MOCK_METHOD3(
  83. sslConnectMockable,
  84. void(
  85. AsyncSSLSocket::HandshakeCB*,
  86. std::chrono::milliseconds,
  87. const SSLContext::SSLVerifyPeerEnum&));
  88. MOCK_METHOD3(
  89. sslAcceptMockable,
  90. void(
  91. AsyncSSLSocket::HandshakeCB*,
  92. std::chrono::milliseconds,
  93. const SSLContext::SSLVerifyPeerEnum&));
  94. };
  95. } // namespace test
  96. } // namespace folly