blob: 5cd5e48ceeb476f086b8746111149143b5e9dc23 [file] [log] [blame]
Alan Dunnbee7b732014-07-26 13:48:43 -05001/*
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
Jim King9de9b1f2015-04-30 16:03:34 -040023#ifdef _WIN32
24#include <WinSock2.h>
25#endif
Alan Dunnbee7b732014-07-26 13:48:43 -050026
27#include <boost/test/unit_test.hpp>
Alan Dunnbee7b732014-07-26 13:48:43 -050028#include <openssl/evp.h>
Alan Dunnbee7b732014-07-26 13:48:43 -050029#include <thrift/transport/TSSLSocket.h>
30
31using namespace std;
32using namespace apache::thrift::transport;
33
34void make_isolated_sslsocketfactory() {
35 // Here we create an isolated TSSLSocketFactory to ensure the
36 // constructor and destructor of TSSLSocketFactory get run. Thus
37 // without manual initialization normally OpenSSL would be
38 // uninitialized after this function.
39 TSSLSocketFactory factory;
40}
41
42void openssl_init() {
43#if MANUAL_OPENSSL_INIT
44 TSSLSocketFactory::setManualOpenSSLInitialization(true);
45 initializeOpenSSL();
46#endif
47}
48
49void openssl_cleanup() {
50#if MANUAL_OPENSSL_INIT
51 cleanupOpenSSL();
52#endif
53}
54
55void test_openssl_availability() {
56 // Check whether Thrift leaves OpenSSL functionality available after
57 // the last TSSLSocketFactory is destroyed when manual
58 // initialization is set
59 openssl_init();
60 make_isolated_sslsocketfactory();
61
62 // The following function is one that will fail if OpenSSL is
63 // uninitialized. It might also fail on very old versions of
64 // OpenSSL...
65 const EVP_MD* md = EVP_get_digestbyname("SHA256");
66 BOOST_CHECK(md != NULL);
67 openssl_cleanup();
68}
69
Antonio Di Monaco796667b2016-01-04 23:05:19 +010070#ifdef BOOST_TEST_DYN_LINK
71bool init_unit_test_suite() {
72 boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
73 suite->p_name.value = "OpenSSLManualInit";
74
75 suite->add(BOOST_TEST_CASE(test_openssl_availability));
76
77 return true;
78}
79
80int main( int argc, char* argv[] ) {
81 return ::boost::unit_test::unit_test_main(&init_unit_test_suite,argc,argv);
82}
83#else
Alan Dunnbee7b732014-07-26 13:48:43 -050084boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
Konrad Grochowskib3f5ffc2014-11-06 19:32:59 +010085 THRIFT_UNUSED_VARIABLE(argc);
86 THRIFT_UNUSED_VARIABLE(argv);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087 boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
Alan Dunnbee7b732014-07-26 13:48:43 -050088 suite->p_name.value = "OpenSSLManualInit";
89
90 suite->add(BOOST_TEST_CASE(test_openssl_availability));
91
92 return NULL;
93}
Antonio Di Monaco796667b2016-01-04 23:05:19 +010094#endif