TestSSLServer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2017-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/SocketAddress.h>
  18. #include <folly/experimental/TestUtil.h>
  19. #include <folly/io/async/AsyncSSLSocket.h>
  20. #include <folly/io/async/AsyncServerSocket.h>
  21. #include <folly/io/async/AsyncSocket.h>
  22. #include <folly/io/async/AsyncTimeout.h>
  23. #include <folly/io/async/AsyncTransport.h>
  24. #include <folly/io/async/EventBase.h>
  25. #include <folly/io/async/ssl/SSLErrors.h>
  26. #include <folly/portability/GTest.h>
  27. #include <folly/portability/Sockets.h>
  28. #include <folly/portability/Unistd.h>
  29. #include <fcntl.h>
  30. #include <sys/types.h>
  31. #include <list>
  32. namespace folly {
  33. extern const char* kTestCert;
  34. extern const char* kTestKey;
  35. extern const char* kTestCA;
  36. extern const char* kClientTestCert;
  37. extern const char* kClientTestKey;
  38. extern const char* kClientTestCA;
  39. enum StateEnum { STATE_WAITING, STATE_SUCCEEDED, STATE_FAILED };
  40. class HandshakeCallback;
  41. class SSLServerAcceptCallbackBase : public AsyncServerSocket::AcceptCallback {
  42. public:
  43. explicit SSLServerAcceptCallbackBase(HandshakeCallback* hcb)
  44. : state(STATE_WAITING), hcb_(hcb) {}
  45. ~SSLServerAcceptCallbackBase() override {
  46. EXPECT_EQ(STATE_SUCCEEDED, state);
  47. }
  48. void acceptError(const std::exception& ex) noexcept override {
  49. LOG(WARNING) << "SSLServerAcceptCallbackBase::acceptError " << ex.what();
  50. state = STATE_FAILED;
  51. }
  52. void connectionAccepted(
  53. int fd,
  54. const SocketAddress& /* clientAddr */) noexcept override {
  55. if (socket_) {
  56. socket_->detachEventBase();
  57. }
  58. LOG(INFO) << "Connection accepted";
  59. try {
  60. // Create a AsyncSSLSocket object with the fd. The socket should be
  61. // added to the event base and in the state of accepting SSL connection.
  62. socket_ = AsyncSSLSocket::newSocket(ctx_, base_, fd);
  63. } catch (const std::exception& e) {
  64. LOG(ERROR) << "Exception %s caught while creating a AsyncSSLSocket "
  65. "object with socket "
  66. << e.what() << fd;
  67. ::close(fd);
  68. acceptError(e);
  69. return;
  70. }
  71. connAccepted(socket_);
  72. }
  73. virtual void connAccepted(const std::shared_ptr<AsyncSSLSocket>& s) = 0;
  74. void detach() {
  75. if (socket_) {
  76. socket_->detachEventBase();
  77. }
  78. }
  79. StateEnum state;
  80. HandshakeCallback* hcb_;
  81. std::shared_ptr<SSLContext> ctx_;
  82. std::shared_ptr<AsyncSSLSocket> socket_;
  83. EventBase* base_;
  84. };
  85. class TestSSLServer {
  86. public:
  87. // Create a TestSSLServer.
  88. // This immediately starts listening on the given port.
  89. explicit TestSSLServer(
  90. SSLServerAcceptCallbackBase* acb,
  91. bool enableTFO = false);
  92. explicit TestSSLServer(
  93. SSLServerAcceptCallbackBase* acb,
  94. std::shared_ptr<SSLContext> ctx,
  95. bool enableTFO = false);
  96. // Kills the thread.
  97. virtual ~TestSSLServer();
  98. EventBase& getEventBase() {
  99. return evb_;
  100. }
  101. void loadTestCerts();
  102. const SocketAddress& getAddress() const {
  103. return address_;
  104. }
  105. protected:
  106. EventBase evb_;
  107. std::shared_ptr<SSLContext> ctx_;
  108. SSLServerAcceptCallbackBase* acb_;
  109. std::shared_ptr<AsyncServerSocket> socket_;
  110. SocketAddress address_;
  111. std::thread thread_;
  112. private:
  113. void init(bool);
  114. };
  115. } // namespace folly