/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/ExceptionWrapper.h>
#include <folly/SocketAddress.h>
+#include <folly/experimental/TestUtil.h>
#include <folly/io/async/AsyncSSLSocket.h>
#include <folly/io/async/AsyncServerSocket.h>
#include <folly/io/async/AsyncSocket.h>
#include <folly/io/async/AsyncTransport.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/ssl/SSLErrors.h>
+#include <folly/portability/GTest.h>
#include <folly/portability/Sockets.h>
#include <folly/portability/Unistd.h>
#include <fcntl.h>
-#include <gtest/gtest.h>
#include <sys/types.h>
#include <condition_variable>
#include <iostream>
}
void writeErr(
- size_t bytesWritten,
+ size_t nBytesWritten,
const AsyncSocketException& ex) noexcept override {
- std::cerr << "writeError: bytesWritten " << bytesWritten
+ std::cerr << "writeError: bytesWritten " << nBytesWritten
<< ", exception " << ex.what() << std::endl;
state = STATE_FAILED;
- this->bytesWritten = bytesWritten;
+ this->bytesWritten = nBytesWritten;
exception = ex;
socket_->close();
- socket_->detachEventBase();
}
std::shared_ptr<AsyncSSLSocket> socket_;
std::cerr << "readError " << ex.what() << std::endl;
state = STATE_FAILED;
socket_->close();
- socket_->detachEventBase();
}
void readEOF() noexcept override {
std::cerr << "readEOF" << std::endl;
socket_->close();
- socket_->detachEventBase();
}
std::shared_ptr<AsyncSSLSocket> socket_;
buffer = nullptr;
length = 0;
}
- void allocate(size_t length) {
+ void allocate(size_t len) {
assert(buffer == nullptr);
- this->buffer = static_cast<char*>(malloc(length));
- this->length = length;
+ this->buffer = static_cast<char*>(malloc(len));
+ this->length = len;
}
void free() {
::free(buffer);
wcb_->setSocket(socket_);
// Write back the same data.
- socket_->write(wcb_, currentBuffer.buffer, len);
+ folly::test::msvcSuppressAbortOnInvalidParams([&] {
+ socket_->write(wcb_, currentBuffer.buffer, len);
+ });
if (wcb_->state == STATE_FAILED) {
setState(STATE_SUCCEEDED);
void readErr(const AsyncSocketException& ex) noexcept override {
std::cerr << "readError " << ex.what() << std::endl;
state = STATE_FAILED;
- tcpSocket_->close();
- tcpSocket_->detachEventBase();
+ if (tcpSocket_) {
+ tcpSocket_->close();
+ }
}
void readEOF() noexcept override {
std::cerr << "readEOF" << std::endl;
-
- tcpSocket_->close();
- tcpSocket_->detachEventBase();
+ if (tcpSocket_) {
+ tcpSocket_->close();
+ }
state = STATE_SUCCEEDED;
}
void connectionAccepted(
int fd, const folly::SocketAddress& /* clientAddr */) noexcept override {
+ if (socket_) {
+ socket_->detachEventBase();
+ }
printf("Connection accepted\n");
- std::shared_ptr<AsyncSSLSocket> sslSock;
try {
// Create a AsyncSSLSocket object with the fd. The socket should be
// added to the event base and in the state of accepting SSL connection.
- sslSock = AsyncSSLSocket::newSocket(ctx_, base_, fd);
+ socket_ = AsyncSSLSocket::newSocket(ctx_, base_, fd);
} catch (const std::exception &e) {
LOG(ERROR) << "Exception %s caught while creating a AsyncSSLSocket "
"object with socket " << e.what() << fd;
return;
}
- connAccepted(sslSock);
+ connAccepted(socket_);
}
virtual void connAccepted(
const std::shared_ptr<folly::AsyncSSLSocket> &s) = 0;
+ void detach() {
+ socket_->detachEventBase();
+ }
+
StateEnum state;
HandshakeCallback *hcb_;
std::shared_ptr<folly::SSLContext> ctx_;
+ std::shared_ptr<AsyncSSLSocket> socket_;
folly::EventBase* base_;
};
std::cerr << "SSLServerAcceptCallback::connAccepted" << std::endl;
hcb_->setSocket(sock);
- sock->sslAccept(hcb_, timeout_);
+ sock->sslAccept(hcb_, std::chrono::milliseconds(timeout_));
EXPECT_EQ(sock->getSSLState(),
AsyncSSLSocket::STATE_ACCEPTING);
std::cerr << "SSLServerAcceptCallback::connAccepted" << std::endl;
hcb_->setSocket(sock);
- sock->sslAccept(hcb_, timeout_);
+ sock->sslAccept(hcb_, std::chrono::milliseconds(timeout_));
ASSERT_TRUE((sock->getSSLState() ==
AsyncSSLSocket::STATE_ACCEPTING) ||
(sock->getSSLState() ==
EXPECT_EQ(hcb_->state, STATE_FAILED);
EXPECT_EQ(callback2.state, STATE_FAILED);
- sock->detachEventBase();
-
state = STATE_SUCCEEDED;
hcb_->setState(STATE_SUCCEEDED);
callback2.setState(STATE_SUCCEEDED);
static void *Main(void *ctx) {
TestSSLServer *self = static_cast<TestSSLServer*>(ctx);
self->evb_.loop();
+ self->acb_->detach();
std::cerr << "Server thread exited event loop" << std::endl;
return nullptr;
}
int* copyflag) {
*copyflag = 0;
asyncCallbacks_++;
+ (void)ssl;
#ifdef SSL_ERROR_WANT_SESS_CACHE_LOOKUP
if (!SSL_want_sess_cache_lookup(ssl)) {
// libssl.so mismatch
}
}
- socket_->sslConn(this, 100);
+ socket_->sslConn(this, std::chrono::milliseconds(100));
}
struct iovec* getIovec() const {
bufSize_(2500 * 2000),
bytesRead_(0) {
buf_.reset(new uint8_t[bufSize_]);
- socket_->sslAccept(this, 100);
+ socket_->sslAccept(this, std::chrono::milliseconds(100));
}
void checkBuffer(struct iovec* iov, uint32_t count) const {
bool preverifyResult,
bool verifyResult) :
SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) {
- socket_->sslConn(this, 0);
+ socket_->sslConn(this, std::chrono::milliseconds::zero());
}
};
bool preverifyResult,
bool verifyResult) :
SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) {
- socket_->sslConn(this, 0,
- folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY);
+ socket_->sslConn(
+ this,
+ std::chrono::milliseconds::zero(),
+ folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY);
}
};
bool preverifyResult,
bool verifyResult) :
SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) {
- socket_->sslConn(this, 0,
- folly::SSLContext::SSLVerifyPeerEnum::VERIFY);
+ socket_->sslConn(
+ this,
+ std::chrono::milliseconds::zero(),
+ folly::SSLContext::SSLVerifyPeerEnum::VERIFY);
}
};
bool preverifyResult,
bool verifyResult)
: SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) {
- socket_->sslAccept(this, 0);
+ socket_->sslAccept(this, std::chrono::milliseconds::zero());
}
};
bool verifyResult)
: SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) {
socket_->enableClientHelloParsing();
- socket_->sslAccept(this, 0);
+ socket_->sslAccept(this, std::chrono::milliseconds::zero());
}
std::string clientCiphers_, sharedCiphers_, serverCiphers_, chosenCipher_;
bool preverifyResult,
bool verifyResult)
: SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) {
- socket_->sslAccept(this, 0,
- folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY);
+ socket_->sslAccept(
+ this,
+ std::chrono::milliseconds::zero(),
+ folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY);
}
};
bool preverifyResult,
bool verifyResult)
: SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) {
- socket_->sslAccept(this, 0,
- folly::SSLContext::SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT);
+ socket_->sslAccept(
+ this,
+ std::chrono::milliseconds::zero(),
+ folly::SSLContext::SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT);
}
};