Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [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 | |
| 20 | #include <boost/test/auto_unit_test.hpp> |
| 21 | #include <boost/test/unit_test_suite.hpp> |
| 22 | #include <boost/bind.hpp> |
| 23 | #include <boost/chrono/duration.hpp> |
| 24 | #include <boost/date_time/posix_time/posix_time_duration.hpp> |
| 25 | #include <boost/thread/thread.hpp> |
| 26 | #include <boost/filesystem.hpp> |
| 27 | #include <boost/format.hpp> |
| 28 | #include <boost/shared_ptr.hpp> |
| 29 | #include <thrift/transport/TSSLSocket.h> |
| 30 | #include <thrift/transport/TSSLServerSocket.h> |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 31 | #ifdef __linux__ |
| 32 | #include <signal.h> |
| 33 | #endif |
| 34 | |
| 35 | using apache::thrift::transport::TSSLServerSocket; |
| 36 | using apache::thrift::transport::TSSLSocket; |
| 37 | using apache::thrift::transport::TTransport; |
| 38 | using apache::thrift::transport::TTransportException; |
| 39 | using apache::thrift::transport::TSSLSocketFactory; |
| 40 | |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(TSSLSocketInterruptTest) |
| 42 | |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 43 | boost::filesystem::path keyDir; |
| 44 | boost::filesystem::path certFile(const std::string& filename) |
| 45 | { |
| 46 | return keyDir / filename; |
| 47 | } |
| 48 | boost::mutex gMutex; |
| 49 | |
| 50 | struct GlobalFixtureSSL |
| 51 | { |
| 52 | GlobalFixtureSSL() |
| 53 | { |
| 54 | using namespace boost::unit_test::framework; |
| 55 | for (int i = 0; i < master_test_suite().argc; ++i) |
| 56 | { |
| 57 | BOOST_TEST_MESSAGE(boost::format("argv[%1%] = \"%2%\"") % i % master_test_suite().argv[i]); |
| 58 | } |
| 59 | |
| 60 | #ifdef __linux__ |
| 61 | // OpenSSL calls send() without MSG_NOSIGPIPE so writing to a socket that has |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 62 | // disconnected can cause a SIGPIPE signal... |
| 63 | signal(SIGPIPE, SIG_IGN); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 64 | #endif |
| 65 | |
| 66 | TSSLSocketFactory::setManualOpenSSLInitialization(true); |
| 67 | apache::thrift::transport::initializeOpenSSL(); |
| 68 | |
| 69 | keyDir = boost::filesystem::current_path().parent_path().parent_path().parent_path() / "test" / "keys"; |
| 70 | if (!boost::filesystem::exists(certFile("server.crt"))) |
| 71 | { |
| 72 | keyDir = boost::filesystem::path(master_test_suite().argv[master_test_suite().argc - 1]); |
| 73 | if (!boost::filesystem::exists(certFile("server.crt"))) |
| 74 | { |
| 75 | throw std::invalid_argument("The last argument to this test must be the directory containing the test certificate(s)."); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | virtual ~GlobalFixtureSSL() |
| 81 | { |
| 82 | apache::thrift::transport::cleanupOpenSSL(); |
| 83 | #ifdef __linux__ |
| 84 | signal(SIGPIPE, SIG_DFL); |
| 85 | #endif |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | #if (BOOST_VERSION >= 105900) |
| 90 | BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL); |
| 91 | #else |
| 92 | BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL) |
| 93 | #endif |
| 94 | |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 95 | void readerWorker(boost::shared_ptr<TTransport> tt, uint32_t expectedResult) { |
| 96 | uint8_t buf[4]; |
| 97 | try { |
| 98 | tt->read(buf, 1); |
| 99 | BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4)); |
| 100 | } catch (const TTransportException& tx) { |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(TTransportException::TIMED_OUT, tx.getType()); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | void readerWorkerMustThrow(boost::shared_ptr<TTransport> tt) { |
| 106 | try { |
| 107 | uint8_t buf[400]; |
| 108 | tt->read(buf, 1); |
| 109 | tt->read(buf, 400); |
| 110 | BOOST_ERROR("should not have gotten here"); |
| 111 | } catch (const TTransportException& tx) { |
| 112 | BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | boost::shared_ptr<TSSLSocketFactory> createServerSocketFactory() { |
| 117 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory; |
| 118 | |
| 119 | pServerSocketFactory.reset(new TSSLSocketFactory()); |
| 120 | pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 121 | pServerSocketFactory->loadCertificate(certFile("server.crt").string().c_str()); |
| 122 | pServerSocketFactory->loadPrivateKey(certFile("server.key").string().c_str()); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 123 | pServerSocketFactory->server(true); |
| 124 | return pServerSocketFactory; |
| 125 | } |
| 126 | |
| 127 | boost::shared_ptr<TSSLSocketFactory> createClientSocketFactory() { |
| 128 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory; |
| 129 | |
| 130 | pClientSocketFactory.reset(new TSSLSocketFactory()); |
| 131 | pClientSocketFactory->authenticate(true); |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 132 | pClientSocketFactory->loadCertificate(certFile("client.crt").string().c_str()); |
| 133 | pClientSocketFactory->loadPrivateKey(certFile("client.key").string().c_str()); |
| 134 | pClientSocketFactory->loadTrustedCertificates(certFile("CA.pem").string().c_str()); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 135 | return pClientSocketFactory; |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_read_while_handshaking) { |
| 139 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 140 | TSSLServerSocket sock1("localhost", 0, pServerSocketFactory); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 141 | sock1.listen(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 142 | int port = sock1.getPort(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 143 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 144 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 145 | clientSock->open(); |
| 146 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 147 | boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted)); |
| 148 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 149 | // readThread is practically guaranteed to be blocking now |
| 150 | sock1.interruptChildren(); |
| 151 | BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(20)), |
| 152 | "server socket interruptChildren did not interrupt child read"); |
| 153 | clientSock->close(); |
| 154 | accepted->close(); |
| 155 | sock1.close(); |
| 156 | } |
| 157 | |
| 158 | BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_read) { |
| 159 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 160 | TSSLServerSocket sock1("localhost", 0, pServerSocketFactory); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 161 | sock1.listen(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 162 | int port = sock1.getPort(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 163 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 164 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 165 | clientSock->open(); |
| 166 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 167 | boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted)); |
| 168 | clientSock->write((const uint8_t*)"0", 1); |
| 169 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 170 | // readThread is practically guaranteed to be blocking now |
| 171 | sock1.interruptChildren(); |
| 172 | BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(20)), |
| 173 | "server socket interruptChildren did not interrupt child read"); |
| 174 | accepted->close(); |
| 175 | clientSock->close(); |
| 176 | sock1.close(); |
| 177 | } |
| 178 | |
| 179 | BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_read) { |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 180 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 181 | TSSLServerSocket sock1("localhost", 0, pServerSocketFactory); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 182 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 183 | sock1.listen(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 184 | int port = sock1.getPort(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 185 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 186 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 187 | clientSock->open(); |
| 188 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 189 | boost::static_pointer_cast<TSSLSocket>(accepted)->setRecvTimeout(1000); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 190 | boost::thread readThread(boost::bind(readerWorker, accepted, 0)); |
| 191 | clientSock->write((const uint8_t*)"0", 1); |
| 192 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 193 | // readThread is practically guaranteed to be blocking here |
| 194 | sock1.interruptChildren(); |
| 195 | BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 196 | "server socket interruptChildren interrupted child read"); |
| 197 | |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 198 | // wait for receive timeout to kick in |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 199 | readThread.join(); |
| 200 | accepted->close(); |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 201 | clientSock->close(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 202 | sock1.close(); |
| 203 | } |
| 204 | |
| 205 | BOOST_AUTO_TEST_CASE(test_ssl_cannot_change_after_listen) { |
| 206 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 207 | TSSLServerSocket sock1("localhost", 0, pServerSocketFactory); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 208 | sock1.listen(); |
| 209 | BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error); |
| 210 | sock1.close(); |
| 211 | } |
| 212 | |
| 213 | void peekerWorker(boost::shared_ptr<TTransport> tt, bool expectedResult) { |
| 214 | uint8_t buf[400]; |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 215 | try { |
| 216 | tt->read(buf, 1); |
James E. King, III | 07f5997 | 2017-03-10 06:18:33 -0500 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(expectedResult, tt->peek()); |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 218 | } catch (const TTransportException& tx) { |
| 219 | BOOST_CHECK_EQUAL(TTransportException::TIMED_OUT, tx.getType()); |
| 220 | } |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void peekerWorkerInterrupt(boost::shared_ptr<TTransport> tt) { |
| 224 | uint8_t buf[400]; |
| 225 | try { |
| 226 | tt->read(buf, 1); |
| 227 | tt->peek(); |
| 228 | } catch (const TTransportException& tx) { |
| 229 | BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_peek) { |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 234 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 235 | TSSLServerSocket sock1("localhost", 0, pServerSocketFactory); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 236 | sock1.listen(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 237 | int port = sock1.getPort(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 238 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 239 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 240 | clientSock->open(); |
| 241 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 242 | boost::thread peekThread(boost::bind(peekerWorkerInterrupt, accepted)); |
| 243 | clientSock->write((const uint8_t*)"0", 1); |
| 244 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 245 | // peekThread is practically guaranteed to be blocking now |
| 246 | sock1.interruptChildren(); |
| 247 | BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 248 | "server socket interruptChildren did not interrupt child peek"); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 249 | accepted->close(); |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 250 | clientSock->close(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 251 | sock1.close(); |
| 252 | } |
| 253 | |
| 254 | BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_peek) { |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 255 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 256 | TSSLServerSocket sock1("localhost", 0, pServerSocketFactory); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 257 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 258 | sock1.listen(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 259 | int port = sock1.getPort(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 260 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
John Sirois | b867b27 | 2016-02-12 17:44:01 -0700 | [diff] [blame] | 261 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 262 | clientSock->open(); |
| 263 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 264 | boost::static_pointer_cast<TSSLSocket>(accepted)->setRecvTimeout(1000); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 265 | boost::thread peekThread(boost::bind(peekerWorker, accepted, false)); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 266 | clientSock->write((const uint8_t*)"0", 1); |
| 267 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 268 | // peekThread is practically guaranteed to be blocking now |
| 269 | sock1.interruptChildren(); |
| 270 | BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 271 | "server socket interruptChildren interrupted child peek"); |
| 272 | |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 273 | // wait for the receive timeout to kick in |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 274 | peekThread.join(); |
| 275 | accepted->close(); |
tpcwang | af81cf0 | 2016-10-05 09:48:23 -0700 | [diff] [blame] | 276 | clientSock->close(); |
Martin Haimberger | 0ad6ee9 | 2015-11-13 03:18:50 -0800 | [diff] [blame] | 277 | sock1.close(); |
| 278 | } |
| 279 | |
| 280 | BOOST_AUTO_TEST_SUITE_END() |