proxygen
TestSSLServer.h
Go to the documentation of this file.
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 
18 #include <folly/SocketAddress.h>
30 
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <list>
34 
35 namespace folly {
36 
37 extern const char* kTestCert;
38 extern const char* kTestKey;
39 extern const char* kTestCA;
40 
41 extern const char* kClientTestCert;
42 extern const char* kClientTestKey;
43 extern const char* kClientTestCA;
44 
46 
47 class HandshakeCallback;
48 
50  public:
52  : state(STATE_WAITING), hcb_(hcb) {}
53 
56  }
57 
58  void acceptError(const std::exception& ex) noexcept override {
59  LOG(WARNING) << "SSLServerAcceptCallbackBase::acceptError " << ex.what();
61  }
62 
64  int fd,
65  const SocketAddress& /* clientAddr */) noexcept override {
66  if (socket_) {
67  socket_->detachEventBase();
68  }
69  LOG(INFO) << "Connection accepted";
70  try {
71  // Create a AsyncSSLSocket object with the fd. The socket should be
72  // added to the event base and in the state of accepting SSL connection.
74  } catch (const std::exception& e) {
75  LOG(ERROR) << "Exception %s caught while creating a AsyncSSLSocket "
76  "object with socket "
77  << e.what() << fd;
78  ::close(fd);
79  acceptError(e);
80  return;
81  }
82 
84  }
85 
86  virtual void connAccepted(const std::shared_ptr<AsyncSSLSocket>& s) = 0;
87 
88  void detach() {
89  if (socket_) {
90  socket_->detachEventBase();
91  }
92  }
93 
96  std::shared_ptr<SSLContext> ctx_;
97  std::shared_ptr<AsyncSSLSocket> socket_;
99 };
100 
102  public:
103  // Create a TestSSLServer.
104  // This immediately starts listening on the given port.
105  explicit TestSSLServer(
107  bool enableTFO = false);
108  explicit TestSSLServer(
110  std::shared_ptr<SSLContext> ctx,
111  bool enableTFO = false);
112 
113  // Kills the thread.
114  virtual ~TestSSLServer();
115 
117  return evb_;
118  }
119 
120  void loadTestCerts();
121 
122  const SocketAddress& getAddress() const {
123  return address_;
124  }
125 
126  protected:
128  std::shared_ptr<SSLContext> ctx_;
130  std::shared_ptr<AsyncServerSocket> socket_;
132  std::thread thread_;
133 
134  private:
135  void init(bool);
136 };
137 } // namespace folly
virtual void connAccepted(const std::shared_ptr< AsyncSSLSocket > &s)=0
const char * kTestCert
std::shared_ptr< AsyncSSLSocket > socket_
Definition: TestSSLServer.h:97
const char * kTestKey
SSLServerAcceptCallbackBase(HandshakeCallback *hcb)
Definition: TestSSLServer.h:51
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * kClientTestKey
EventBase & getEventBase()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
SSLServerAcceptCallbackBase * acb_
requires E e noexcept(noexcept(s.error(std::move(e))))
const char * kClientTestCert
void connectionAccepted(int fd, const SocketAddress &) noexceptoverride
Definition: TestSSLServer.h:63
static std::shared_ptr< AsyncSSLSocket > newSocket(const std::shared_ptr< folly::SSLContext > &ctx, EventBase *evb, int fd, bool server=true, bool deferSecurityNegotiation=false)
std::shared_ptr< SSLContext > ctx_
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
void acceptError(const std::exception &ex) noexceptoverride
Definition: TestSSLServer.h:58
EventBase * evb_
const char * kTestCA
std::shared_ptr< AsyncServerSocket > socket_
static set< string > s
SocketAddress address_
const char * kClientTestCA
int close(NetworkSocket s)
Definition: NetOps.cpp:90
const SocketAddress & getAddress() const
std::shared_ptr< SSLContext > ctx_
Definition: TestSSLServer.h:96
state
Definition: http_parser.c:272