proxygen
TestServer Class Reference

#include <AsyncSocketTest.h>

Public Member Functions

 TestServer (bool enableTFO=false, int bufSize=-1)
 
 ~TestServer ()
 
const folly::SocketAddressgetAddress () const
 
int acceptFD (int timeout=50)
 
std::shared_ptr< BlockingSocketaccept (int timeout=50)
 
std::shared_ptr< folly::AsyncSocketacceptAsync (folly::EventBase *evb, int timeout=50)
 
void verifyConnection (const char *buf, size_t len)
 

Private Attributes

int fd_
 
folly::SocketAddress address_
 

Detailed Description

Definition at line 267 of file AsyncSocketTest.h.

Constructor & Destructor Documentation

TestServer::TestServer ( bool  enableTFO = false,
int  bufSize = -1 
)
inlineexplicit

Definition at line 271 of file AsyncSocketTest.h.

References folly::netops::bind(), folly::AsyncSocketException::INTERNAL_ERROR, folly::netops::listen(), SCOPE_EXIT, folly::netops::setsockopt(), folly::netops::socket(), and folly::detail::tfo_enable().

271  : fd_(-1) {
272  namespace fsp = folly::portability::sockets;
273  fd_ = fsp::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
274  if (fd_ < 0) {
277  "failed to create test server socket",
278  errno);
279  }
280  if (fcntl(fd_, F_SETFL, O_NONBLOCK) != 0) {
283  "failed to put test server socket in "
284  "non-blocking mode",
285  errno);
286  }
287  if (enableTFO) {
288 #if FOLLY_ALLOW_TFO
290 #endif
291  }
292 
293  struct addrinfo hints, *res;
294  memset(&hints, 0, sizeof(hints));
295  hints.ai_family = AF_INET;
296  hints.ai_socktype = SOCK_STREAM;
297  hints.ai_flags = AI_PASSIVE;
298 
299  if (getaddrinfo(nullptr, "0", &hints, &res)) {
302  "Attempted to bind address to socket with "
303  "bad getaddrinfo",
304  errno);
305  }
306 
307  SCOPE_EXIT {
308  freeaddrinfo(res);
309  };
310 
311  if (bufSize > 0) {
312  setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &bufSize, sizeof(bufSize));
313  setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &bufSize, sizeof(bufSize));
314  }
315 
316  if (bind(fd_, res->ai_addr, res->ai_addrlen)) {
319  "failed to bind to async server socket for port 10",
320  errno);
321  }
322 
323  if (listen(fd_, 10) != 0) {
326  "failed to listen on test server socket",
327  errno);
328  }
329 
331  // The local address will contain 0.0.0.0.
332  // Change it to 127.0.0.1, so it can be used to connect to the server
333  address_.setFromIpPort("127.0.0.1", address_.getPort());
334  }
int setsockopt(NetworkSocket s, int level, int optname, const void *optval, socklen_t optlen)
Definition: NetOps.cpp:384
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
uint16_t getPort() const
folly::SocketAddress address_
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
int listen(NetworkSocket s, int backlog)
Definition: NetOps.cpp:137
int tfo_enable(int, size_t)
void setFromIpPort(const char *ip, uint16_t port)
void setFromLocalAddress(int socket)
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
TestServer::~TestServer ( )
inline

Definition at line 336 of file AsyncSocketTest.h.

References folly::netops::close().

336  {
337  if (fd_ != -1) {
338  close(fd_);
339  }
340  }
int close(NetworkSocket s)
Definition: NetOps.cpp:90

Member Function Documentation

std::shared_ptr<BlockingSocket> TestServer::accept ( int  timeout = 50)
inline

Definition at line 375 of file AsyncSocketTest.h.

Referenced by TEST(), and TEST_P().

375  {
376  int fd = acceptFD(timeout);
377  return std::make_shared<BlockingSocket>(fd);
378  }
int acceptFD(int timeout=50)
std::shared_ptr<folly::AsyncSocket> TestServer::acceptAsync ( folly::EventBase evb,
int  timeout = 50 
)
inline

Definition at line 380 of file AsyncSocketTest.h.

References folly::AsyncSocket::newSocket().

Referenced by TEST(), and testConnectOptWrite().

382  {
383  int fd = acceptFD(timeout);
384  return folly::AsyncSocket::newSocket(evb, fd);
385  }
int acceptFD(int timeout=50)
static std::shared_ptr< AsyncSocket > newSocket(EventBase *evb)
Definition: AsyncSocket.h:281
int TestServer::acceptFD ( int  timeout = 50)
inline

Definition at line 347 of file AsyncSocketTest.h.

References folly::netops::accept(), folly::AsyncSocketException::INTERNAL_ERROR, and folly::netops::poll().

Referenced by TEST().

347  {
348  namespace fsp = folly::portability::sockets;
349  struct pollfd pfd;
350  pfd.fd = fd_;
351  pfd.events = POLLIN;
352  int ret = poll(&pfd, 1, timeout);
353  if (ret == 0) {
356  "test server accept() timed out");
357  } else if (ret < 0) {
360  "test server accept() poll failed",
361  errno);
362  }
363 
364  int acceptedFd = fsp::accept(fd_, nullptr, nullptr);
365  if (acceptedFd < 0) {
368  "test server accept() failed",
369  errno);
370  }
371 
372  return acceptedFd;
373  }
int poll(PollDescriptor fds[], nfds_t nfds, int timeout)
Definition: NetOps.cpp:141
NetworkSocket accept(NetworkSocket s, sockaddr *addr, socklen_t *addrlen)
Definition: NetOps.cpp:71
const folly::SocketAddress& TestServer::getAddress ( ) const
inline

Definition at line 343 of file AsyncSocketTest.h.

Referenced by TEST(), TEST_P(), and testConnectOptWrite().

343  {
344  return address_;
345  }
folly::SocketAddress address_
void TestServer::verifyConnection ( const char *  buf,
size_t  len 
)
inline

Accept a connection, read data from it, and verify that it matches the data in the specified buffer.

Definition at line 391 of file AsyncSocketTest.h.

References folly::netops::accept(), uint32_t, and uint8_t.

Referenced by TEST(), and TEST_P().

391  {
392  // accept a connection
393  std::shared_ptr<BlockingSocket> acceptedSocket = accept();
394  // read the data and compare it to the specified buffer
395  std::unique_ptr<uint8_t[]> readbuf(new uint8_t[len]);
396  acceptedSocket->readAll(readbuf.get(), len);
397  CHECK_EQ(memcmp(buf, readbuf.get(), len), 0);
398  // make sure we get EOF next
399  uint32_t bytesRead = acceptedSocket->read(readbuf.get(), len);
400  CHECK_EQ(bytesRead, 0);
401  }
std::shared_ptr< BlockingSocket > accept(int timeout=50)

Member Data Documentation

folly::SocketAddress TestServer::address_
private

Definition at line 405 of file AsyncSocketTest.h.

int TestServer::fd_
private

Definition at line 404 of file AsyncSocketTest.h.


The documentation for this class was generated from the following file: