2 * Copyright 2017-present 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>
19 #include <folly/init/Init.h>
20 #include <folly/io/async/SSLContext.h>
21 #include <folly/portability/GTest.h>
22 #include <folly/ssl/Init.h>
26 void setupSSLLocks() {
27 folly::ssl::setLockTypes({
28 #ifdef CRYPTO_LOCK_EVP_PKEY
29 {CRYPTO_LOCK_EVP_PKEY, folly::ssl::LockType::NONE},
31 #ifdef CRYPTO_LOCK_SSL_SESSION
32 {CRYPTO_LOCK_SSL_SESSION, folly::ssl::LockType::SPINLOCK},
34 #ifdef CRYPTO_LOCK_SSL_CTX
35 {CRYPTO_LOCK_SSL_CTX, folly::ssl::LockType::NONE}
40 TEST(SSLContextInitializationTest, SSLContextInitializeThenSetLocksAndInit) {
44 folly::ssl::setLockTypesAndInit({});
46 "OpenSSL is already initialized");
49 TEST(SSLContextInitializationTest, SSLContextSetLocksAndInitialize) {
52 folly::ssl::setLockTypesAndInit({});
53 folly::ssl::setLockTypesAndInit({});
55 "OpenSSL is already initialized");
58 TEST(SSLContextInitializationTest, SSLContextLocks) {
63 #ifdef CRYPTO_LOCK_EVP_PKEY
64 EXPECT_TRUE(folly::ssl::isLockDisabled(CRYPTO_LOCK_EVP_PKEY));
66 #ifdef CRYPTO_LOCK_SSL_SESSION
67 EXPECT_FALSE(folly::ssl::isLockDisabled(CRYPTO_LOCK_SSL_SESSION));
69 #ifdef CRYPTO_LOCK_ERR
70 EXPECT_FALSE(folly::ssl::isLockDisabled(CRYPTO_LOCK_ERR));
72 if (::testing::Test::HasFailure()) {
75 LOG(INFO) << "SSLContextLocks passed";
78 ::testing::ExitedWithCode(0),
79 "SSLContextLocks passed");
82 TEST(SSLContextInitializationTest, SSLContextLocksSetAfterInitIgnored) {
87 folly::ssl::setLockTypes({});
88 #ifdef CRYPTO_LOCK_EVP_PKEY
89 EXPECT_TRUE(folly::ssl::isLockDisabled(CRYPTO_LOCK_EVP_PKEY));
91 if (::testing::Test::HasFailure()) {
94 LOG(INFO) << "SSLContextLocksSetAfterInitIgnored passed";
97 ::testing::ExitedWithCode(0),
98 "SSLContextLocksSetAfterInitIgnored passed");
102 int main(int argc, char* argv[]) {
104 signal(SIGPIPE, SIG_IGN);
106 testing::InitGoogleTest(&argc, argv);
107 folly::init(&argc, &argv);
109 return RUN_ALL_TESTS();