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> |
| 31 | #include "TestPortFixture.h" |
| 32 | #ifdef __linux__ |
| 33 | #include <signal.h> |
| 34 | #endif |
| 35 | |
| 36 | using apache::thrift::transport::TSSLServerSocket; |
| 37 | using apache::thrift::transport::TSSLSocket; |
| 38 | using apache::thrift::transport::TTransport; |
| 39 | using apache::thrift::transport::TTransportException; |
| 40 | using apache::thrift::transport::TSSLSocketFactory; |
| 41 | |
| 42 | boost::filesystem::path keyDir; |
| 43 | boost::filesystem::path certFile(const std::string& filename) |
| 44 | { |
| 45 | return keyDir / filename; |
| 46 | } |
| 47 | boost::mutex gMutex; |
| 48 | |
| 49 | struct GlobalFixtureSSL |
| 50 | { |
| 51 | GlobalFixtureSSL() |
| 52 | { |
| 53 | using namespace boost::unit_test::framework; |
| 54 | for (int i = 0; i < master_test_suite().argc; ++i) |
| 55 | { |
| 56 | BOOST_TEST_MESSAGE(boost::format("argv[%1%] = \"%2%\"") % i % master_test_suite().argv[i]); |
| 57 | } |
| 58 | |
| 59 | #ifdef __linux__ |
| 60 | // OpenSSL calls send() without MSG_NOSIGPIPE so writing to a socket that has |
| 61 | // disconnected can cause a SIGPIPE signal... |
| 62 | signal(SIGPIPE, SIG_IGN); |
| 63 | #endif |
| 64 | |
| 65 | TSSLSocketFactory::setManualOpenSSLInitialization(true); |
| 66 | apache::thrift::transport::initializeOpenSSL(); |
| 67 | |
| 68 | keyDir = boost::filesystem::current_path().parent_path().parent_path().parent_path() / "test" / "keys"; |
| 69 | if (!boost::filesystem::exists(certFile("server.crt"))) |
| 70 | { |
| 71 | keyDir = boost::filesystem::path(master_test_suite().argv[master_test_suite().argc - 1]); |
| 72 | if (!boost::filesystem::exists(certFile("server.crt"))) |
| 73 | { |
| 74 | throw std::invalid_argument("The last argument to this test must be the directory containing the test certificate(s)."); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | virtual ~GlobalFixtureSSL() |
| 80 | { |
| 81 | apache::thrift::transport::cleanupOpenSSL(); |
| 82 | #ifdef __linux__ |
| 83 | signal(SIGPIPE, SIG_DFL); |
| 84 | #endif |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | #if (BOOST_VERSION >= 105900) |
| 89 | BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL); |
| 90 | #else |
| 91 | BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL) |
| 92 | #endif |
| 93 | |
| 94 | BOOST_FIXTURE_TEST_SUITE(TSSLSocketInterruptTest, TestPortFixture) |
| 95 | |
| 96 | void readerWorker(boost::shared_ptr<TTransport> tt, uint32_t expectedResult) { |
| 97 | uint8_t buf[4]; |
| 98 | try { |
| 99 | tt->read(buf, 1); |
| 100 | BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4)); |
| 101 | } catch (const TTransportException& tx) { |
| 102 | BOOST_CHECK_EQUAL(TTransportException::INTERNAL_ERROR, tx.getType()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void readerWorkerMustThrow(boost::shared_ptr<TTransport> tt) { |
| 107 | try { |
| 108 | uint8_t buf[400]; |
| 109 | tt->read(buf, 1); |
| 110 | tt->read(buf, 400); |
| 111 | BOOST_ERROR("should not have gotten here"); |
| 112 | } catch (const TTransportException& tx) { |
| 113 | BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | boost::shared_ptr<TSSLSocketFactory> createServerSocketFactory() { |
| 118 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory; |
| 119 | |
| 120 | pServerSocketFactory.reset(new TSSLSocketFactory()); |
| 121 | pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
| 122 | pServerSocketFactory->loadCertificate(certFile("server.crt").native().c_str()); |
| 123 | pServerSocketFactory->loadPrivateKey(certFile("server.key").native().c_str()); |
| 124 | pServerSocketFactory->server(true); |
| 125 | return pServerSocketFactory; |
| 126 | } |
| 127 | |
| 128 | boost::shared_ptr<TSSLSocketFactory> createClientSocketFactory() { |
| 129 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory; |
| 130 | |
| 131 | pClientSocketFactory.reset(new TSSLSocketFactory()); |
| 132 | pClientSocketFactory->authenticate(true); |
| 133 | pClientSocketFactory->loadCertificate(certFile("client.crt").native().c_str()); |
| 134 | pClientSocketFactory->loadPrivateKey(certFile("client.key").native().c_str()); |
| 135 | pClientSocketFactory->loadTrustedCertificates(certFile("CA.pem").native().c_str()); |
| 136 | return pClientSocketFactory; |
| 137 | } |
| 138 | |
| 139 | BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_read_while_handshaking) { |
| 140 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
| 141 | TSSLServerSocket sock1("localhost", m_serverPort, pServerSocketFactory); |
| 142 | sock1.listen(); |
| 143 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
| 144 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", m_serverPort); |
| 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(); |
| 160 | TSSLServerSocket sock1("localhost", m_serverPort, pServerSocketFactory); |
| 161 | sock1.listen(); |
| 162 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
| 163 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", m_serverPort); |
| 164 | clientSock->open(); |
| 165 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 166 | boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted)); |
| 167 | clientSock->write((const uint8_t*)"0", 1); |
| 168 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 169 | // readThread is practically guaranteed to be blocking now |
| 170 | sock1.interruptChildren(); |
| 171 | BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(20)), |
| 172 | "server socket interruptChildren did not interrupt child read"); |
| 173 | accepted->close(); |
| 174 | clientSock->close(); |
| 175 | sock1.close(); |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_read) { |
| 179 | std::cout << "An error message from SSL_Shutdown on the console is expected:" << std::endl; |
| 180 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
| 181 | TSSLServerSocket sock1("localhost", m_serverPort, pServerSocketFactory); |
| 182 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 183 | sock1.listen(); |
| 184 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
| 185 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", m_serverPort); |
| 186 | clientSock->open(); |
| 187 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 188 | boost::thread readThread(boost::bind(readerWorker, accepted, 0)); |
| 189 | clientSock->write((const uint8_t*)"0", 1); |
| 190 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 191 | // readThread is practically guaranteed to be blocking here |
| 192 | sock1.interruptChildren(); |
| 193 | BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 194 | "server socket interruptChildren interrupted child read"); |
| 195 | |
| 196 | // only way to proceed is to have the client disconnect |
| 197 | clientSock->close(); |
| 198 | readThread.join(); |
| 199 | accepted->close(); |
| 200 | sock1.close(); |
| 201 | } |
| 202 | |
| 203 | BOOST_AUTO_TEST_CASE(test_ssl_cannot_change_after_listen) { |
| 204 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
| 205 | TSSLServerSocket sock1("localhost", m_serverPort, pServerSocketFactory); |
| 206 | sock1.listen(); |
| 207 | BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error); |
| 208 | sock1.close(); |
| 209 | } |
| 210 | |
| 211 | void peekerWorker(boost::shared_ptr<TTransport> tt, bool expectedResult) { |
| 212 | uint8_t buf[400]; |
| 213 | |
| 214 | tt->read(buf, 1); |
| 215 | BOOST_CHECK_EQUAL(expectedResult, tt->peek()); |
| 216 | } |
| 217 | |
| 218 | void peekerWorkerInterrupt(boost::shared_ptr<TTransport> tt) { |
| 219 | uint8_t buf[400]; |
| 220 | try { |
| 221 | tt->read(buf, 1); |
| 222 | tt->peek(); |
| 223 | } catch (const TTransportException& tx) { |
| 224 | BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType()); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_peek) { |
| 229 | std::cout << "An error message from SSL_Shutdown on the console is expected:" << std::endl; |
| 230 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
| 231 | TSSLServerSocket sock1("localhost", m_serverPort, pServerSocketFactory); |
| 232 | sock1.listen(); |
| 233 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
| 234 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", m_serverPort); |
| 235 | clientSock->open(); |
| 236 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 237 | // peek() will return false if child is interrupted |
| 238 | boost::thread peekThread(boost::bind(peekerWorkerInterrupt, accepted)); |
| 239 | clientSock->write((const uint8_t*)"0", 1); |
| 240 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 241 | // peekThread is practically guaranteed to be blocking now |
| 242 | sock1.interruptChildren(); |
| 243 | BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 244 | "server socket interruptChildren did not interrupt child peek"); |
| 245 | #ifdef __linux__ |
| 246 | signal(SIGPIPE, SIG_IGN); |
| 247 | #endif |
| 248 | clientSock->close(); |
| 249 | accepted->close(); |
| 250 | sock1.close(); |
| 251 | } |
| 252 | |
| 253 | BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_peek) { |
| 254 | std::cout << "An error message from SSL_Shutdown on the console is expected:" << std::endl; |
| 255 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory(); |
| 256 | TSSLServerSocket sock1("localhost", m_serverPort, pServerSocketFactory); |
| 257 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 258 | sock1.listen(); |
| 259 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory(); |
| 260 | boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", m_serverPort); |
| 261 | clientSock->open(); |
| 262 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 263 | // peek() will return false when remote side is closed |
| 264 | boost::thread peekThread(boost::bind(peekerWorker, accepted, false)); |
| 265 | //boost::thread peekThread(boost::bind(peekerWorkerRead, clientSock, false)); |
| 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 | |
| 273 | // only way to proceed is to have the client disconnect |
| 274 | #ifdef __linux__ |
| 275 | signal(SIGPIPE, SIG_IGN); |
| 276 | #endif |
| 277 | clientSock->close(); |
| 278 | peekThread.join(); |
| 279 | accepted->close(); |
| 280 | sock1.close(); |
| 281 | } |
| 282 | |
| 283 | BOOST_AUTO_TEST_SUITE_END() |