SSLContextInitializationTest.cpp 3.0 KB

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