Move OpenSSL locking code out of SSLContext
[folly.git] / folly / io / async / test / SSLContextInitializationTest.cpp
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 #include <folly/io/async/test/AsyncSSLSocketTest.h>
17 #include <functional>
18
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>
23
24 namespace folly {
25
26 void setupSSLLocks() {
27   folly::ssl::setLockTypes({
28 #ifdef CRYPTO_LOCK_EVP_PKEY
29       {CRYPTO_LOCK_EVP_PKEY, folly::ssl::LockType::NONE},
30 #endif
31 #ifdef CRYPTO_LOCK_SSL_SESSION
32       {CRYPTO_LOCK_SSL_SESSION, folly::ssl::LockType::SPINLOCK},
33 #endif
34 #ifdef CRYPTO_LOCK_SSL_CTX
35       {CRYPTO_LOCK_SSL_CTX, folly::ssl::LockType::NONE}
36 #endif
37   });
38 }
39
40 TEST(SSLContextInitializationTest, SSLContextInitializeThenSetLocksAndInit) {
41   EXPECT_DEATH(
42       {
43         folly::ssl::init();
44         folly::ssl::setLockTypesAndInit({});
45       },
46       "OpenSSL is already initialized");
47 }
48
49 TEST(SSLContextInitializationTest, SSLContextSetLocksAndInitialize) {
50   EXPECT_DEATH(
51       {
52         folly::ssl::setLockTypesAndInit({});
53         folly::ssl::setLockTypesAndInit({});
54       },
55       "OpenSSL is already initialized");
56 }
57
58 TEST(SSLContextInitializationTest, SSLContextLocks) {
59   EXPECT_EXIT(
60       {
61         setupSSLLocks();
62         folly::ssl::init();
63 #ifdef CRYPTO_LOCK_EVP_PKEY
64         EXPECT_TRUE(folly::ssl::isLockDisabled(CRYPTO_LOCK_EVP_PKEY));
65 #endif
66 #ifdef CRYPTO_LOCK_SSL_SESSION
67         EXPECT_FALSE(folly::ssl::isLockDisabled(CRYPTO_LOCK_SSL_SESSION));
68 #endif
69 #ifdef CRYPTO_LOCK_ERR
70         EXPECT_FALSE(folly::ssl::isLockDisabled(CRYPTO_LOCK_ERR));
71 #endif
72         if (::testing::Test::HasFailure()) {
73           exit(1);
74         }
75         LOG(INFO) << "SSLContextLocks passed";
76         exit(0);
77       },
78       ::testing::ExitedWithCode(0),
79       "SSLContextLocks passed");
80 }
81
82 TEST(SSLContextInitializationTest, SSLContextLocksSetAfterInitIgnored) {
83   EXPECT_EXIT(
84       {
85         setupSSLLocks();
86         folly::ssl::init();
87         folly::ssl::setLockTypes({});
88 #ifdef CRYPTO_LOCK_EVP_PKEY
89         EXPECT_TRUE(folly::ssl::isLockDisabled(CRYPTO_LOCK_EVP_PKEY));
90 #endif
91         if (::testing::Test::HasFailure()) {
92           exit(1);
93         }
94         LOG(INFO) << "SSLContextLocksSetAfterInitIgnored passed";
95         exit(0);
96       },
97       ::testing::ExitedWithCode(0),
98       "SSLContextLocksSetAfterInitIgnored passed");
99 }
100 } // folly
101
102 int main(int argc, char* argv[]) {
103 #ifdef SIGPIPE
104   signal(SIGPIPE, SIG_IGN);
105 #endif
106   testing::InitGoogleTest(&argc, argv);
107   folly::init(&argc, &argv);
108
109   return RUN_ALL_TESTS();
110 }