Alan Dunn | bee7b73 | 2014-07-26 13:48:43 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | // To show that this test actually tests something, you can change |
| 20 | // MANUAL_OPENSSL_INIT to 0 to cause automatic OpenSSL init/cleanup, |
| 21 | // which will cause the test to fail |
| 22 | #define MANUAL_OPENSSL_INIT 1 |
| 23 | |
| 24 | #include <boost/test/unit_test.hpp> |
| 25 | |
| 26 | #include <openssl/evp.h> |
| 27 | |
| 28 | #include <thrift/transport/TSSLSocket.h> |
| 29 | |
| 30 | using namespace std; |
| 31 | using namespace apache::thrift::transport; |
| 32 | |
| 33 | void make_isolated_sslsocketfactory() { |
| 34 | // Here we create an isolated TSSLSocketFactory to ensure the |
| 35 | // constructor and destructor of TSSLSocketFactory get run. Thus |
| 36 | // without manual initialization normally OpenSSL would be |
| 37 | // uninitialized after this function. |
| 38 | TSSLSocketFactory factory; |
| 39 | } |
| 40 | |
| 41 | void openssl_init() { |
| 42 | #if MANUAL_OPENSSL_INIT |
| 43 | TSSLSocketFactory::setManualOpenSSLInitialization(true); |
| 44 | initializeOpenSSL(); |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | void openssl_cleanup() { |
| 49 | #if MANUAL_OPENSSL_INIT |
| 50 | cleanupOpenSSL(); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | void test_openssl_availability() { |
| 55 | // Check whether Thrift leaves OpenSSL functionality available after |
| 56 | // the last TSSLSocketFactory is destroyed when manual |
| 57 | // initialization is set |
| 58 | openssl_init(); |
| 59 | make_isolated_sslsocketfactory(); |
| 60 | |
| 61 | // The following function is one that will fail if OpenSSL is |
| 62 | // uninitialized. It might also fail on very old versions of |
| 63 | // OpenSSL... |
| 64 | const EVP_MD* md = EVP_get_digestbyname("SHA256"); |
| 65 | BOOST_CHECK(md != NULL); |
| 66 | openssl_cleanup(); |
| 67 | } |
| 68 | |
| 69 | boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { |
| 70 | boost::unit_test::test_suite* suite = |
| 71 | &boost::unit_test::framework::master_test_suite(); |
| 72 | suite->p_name.value = "OpenSSLManualInit"; |
| 73 | |
| 74 | suite->add(BOOST_TEST_CASE(test_openssl_availability)); |
| 75 | |
| 76 | return NULL; |
| 77 | } |