2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/io/async/test/AsyncSSLSocketTest.h>
18 #include <gtest/gtest.h>
21 #include <folly/io/async/AsyncSSLSocket.h>
22 #include <folly/io/async/EventBase.h>
33 class AttachDetachClient : public AsyncSocket::ConnectCallback,
34 public AsyncTransportWrapper::WriteCallback,
35 public AsyncTransportWrapper::ReadCallback {
37 EventBase *eventBase_;
38 std::shared_ptr<AsyncSSLSocket> sslSocket_;
39 std::shared_ptr<SSLContext> ctx_;
40 folly::SocketAddress address_;
45 AttachDetachClient(EventBase *eventBase, const folly::SocketAddress& address)
46 : eventBase_(eventBase), address_(address), bytesRead_(0) {
47 ctx_.reset(new SSLContext());
48 ctx_->setOptions(SSL_OP_NO_TICKET);
49 ctx_->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
53 sslSocket_ = AsyncSSLSocket::newSocket(ctx_, eventBase_);
54 sslSocket_->connect(this, address_);
57 void connectSuccess() noexcept override {
58 cerr << "client SSL socket connected" << endl;
60 for (int i = 0; i < 1000; ++i) {
61 sslSocket_->detachSSLContext();
62 sslSocket_->attachSSLContext(ctx_);
65 EXPECT_EQ(ctx_->getSSLCtx()->references, 2);
67 sslSocket_->write(this, buf_, sizeof(buf_));
68 sslSocket_->setReadCB(this);
69 memset(readbuf_, 'b', sizeof(readbuf_));
73 void connectErr(const AsyncSocketException& ex) noexcept override
75 cerr << "AttachDetachClient::connectError: " << ex.what() << endl;
79 void writeSuccess() noexcept override {
80 cerr << "client write success" << endl;
83 void writeErr(size_t bytesWritten, const AsyncSocketException& ex)
85 cerr << "client writeError: " << ex.what() << endl;
88 void getReadBuffer(void** bufReturn, size_t* lenReturn) override {
89 *bufReturn = readbuf_ + bytesRead_;
90 *lenReturn = sizeof(readbuf_) - bytesRead_;
92 void readEOF() noexcept override {
93 cerr << "client readEOF" << endl;
96 void readErr(const AsyncSocketException& ex) noexcept override {
97 cerr << "client readError: " << ex.what() << endl;
100 void readDataAvailable(size_t len) noexcept override {
101 cerr << "client read data: " << len << endl;
103 if (len == sizeof(buf_)) {
104 EXPECT_EQ(memcmp(buf_, readbuf_, bytesRead_), 0);
105 sslSocket_->closeNow();
111 * Test passing contexts between threads
113 TEST(AsyncSSLSocketTest2, AttachDetachSSLContext) {
114 // Start listening on a local port
115 WriteCallbackBase writeCallback;
116 ReadCallback readCallback(&writeCallback);
117 HandshakeCallback handshakeCallback(&readCallback);
118 SSLServerAcceptCallbackDelay acceptCallback(&handshakeCallback);
119 TestSSLServer server(&acceptCallback);
122 EventBaseAborter eba(&eventBase, 3000);
123 std::shared_ptr<AttachDetachClient> client(
124 new AttachDetachClient(&eventBase, server.getAddress()));
131 ///////////////////////////////////////////////////////////////////////////
132 // init_unit_test_suite
133 ///////////////////////////////////////////////////////////////////////////
136 using folly::SSLContext;
139 signal(SIGPIPE, SIG_IGN);
140 SSLContext::setSSLLockTypes({
141 {CRYPTO_LOCK_EVP_PKEY, SSLContext::LOCK_NONE},
142 {CRYPTO_LOCK_SSL_SESSION, SSLContext::LOCK_SPINLOCK},
143 {CRYPTO_LOCK_SSL_CTX, SSLContext::LOCK_NONE}});
146 Initializer initializer;