blob: bf6c15369b5bf7255e16ec1b6fd922da8effa67e [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
Jeremiah50819ce2022-02-08 12:46:45 +010024#include <winsock2.h>
Jim King9de9b1f2015-04-30 16:03:34 -040025#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
Alan Dunnbee7b732014-07-26 13:48:43 -050031using namespace apache::thrift::transport;
32
33void 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
41void openssl_init() {
42#if MANUAL_OPENSSL_INIT
43 TSSLSocketFactory::setManualOpenSSLInitialization(true);
44 initializeOpenSSL();
45#endif
46}
47
48void openssl_cleanup() {
49#if MANUAL_OPENSSL_INIT
50 cleanupOpenSSL();
51#endif
52}
53
54void 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");
Sebastian Zenker042580f2019-01-29 15:48:12 +010065 BOOST_CHECK(md != nullptr);
Alan Dunnbee7b732014-07-26 13:48:43 -050066 openssl_cleanup();
67}
68
Antonio Di Monaco796667b2016-01-04 23:05:19 +010069#ifdef BOOST_TEST_DYN_LINK
70bool init_unit_test_suite() {
71 boost::unit_test::test_suite* suite = &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 true;
77}
78
79int main( int argc, char* argv[] ) {
80 return ::boost::unit_test::unit_test_main(&init_unit_test_suite,argc,argv);
81}
82#else
Alan Dunnbee7b732014-07-26 13:48:43 -050083boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
Konrad Grochowskib3f5ffc2014-11-06 19:32:59 +010084 THRIFT_UNUSED_VARIABLE(argc);
85 THRIFT_UNUSED_VARIABLE(argv);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010086 boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
Alan Dunnbee7b732014-07-26 13:48:43 -050087 suite->p_name.value = "OpenSSLManualInit";
88
89 suite->add(BOOST_TEST_CASE(test_openssl_availability));
90
zeshuai00726681fb2020-06-03 17:24:38 +080091 return nullptr;
Alan Dunnbee7b732014-07-26 13:48:43 -050092}
Antonio Di Monaco796667b2016-01-04 23:05:19 +010093#endif